using InnovEnergy.Lib.SysTools.Utils; namespace InnovEnergy.Lib.SysTools.Remote; public readonly struct RemoteCommand { public SshHost Host { get; } public SysPath Path { get; } public String Args { get; } public RemoteCommand(RemotePath remotePath, String args = "") { var (host, path) = remotePath; Args = args; Host = host; Path = path; } public RemoteCommand(SshHost host, SysPath path, String args = "") { Args = args; Host = host; Path = path; } public static RemoteCommand FromRemotePath(RemotePath remotePath) => new RemoteCommand(remotePath); // TODO public RemoteCommand Opt1(String option) { return new RemoteCommand(Host, Path, $"{Args} -{option}"); } public RemoteCommand Opt1(String option, Object value, String separator = " ") { return new RemoteCommand(Host, Path, $"{Args} -{option}{separator}{Quote(value)}"); } public RemoteCommand Opt2(String option) { return new RemoteCommand(Host, Path, $"{Args} --{option}"); } public RemoteCommand Opt2(String option, Object value, String separator = "=") { return new RemoteCommand(Host, Path, $"{Args} --{option}{separator}{Quote(value)}"); } public RemoteCommand Opt(String option) { return option.Length == 1 ? Opt1(option) : Opt2(option); } public RemoteCommand Opt(String option, Object value) { return option.Length == 1 ? Opt1(option, value) : Opt2(option, value); } public RemoteCommand Arg(Object argument) { return new RemoteCommand(Host, Path, Args + " " + Quote(argument)); } private static String Quote(Object argument) { var arg = argument.ToString(); if (arg.Contains(" ")) arg = arg.Quote(); // TODO: single quote? return arg; } #region equality public Boolean Equals(RemoteCommand other) { return Host.Equals(other.Host) && Path.Equals(other.Path) && Args == other.Args; } public override Boolean Equals(Object obj) { return obj is RemoteCommand other && Equals(other); } public override Int32 GetHashCode() { unchecked { var hashCode = Host.GetHashCode(); hashCode = (hashCode * 397) ^ Path.GetHashCode(); hashCode = (hashCode * 397) ^ (Args != null ? Args.GetHashCode() : 0); return hashCode; } } #endregion equality public static implicit operator RemoteCommand(RemotePath remotePath) => new RemoteCommand(remotePath); public void Deconstruct(out SshHost host, out SysPath path, out String args) { host = Host; path = Path; args = Args; } public override String ToString() => $"{Host}:{Path}{Args}"; }