Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename SshConfigOptions to SshConfigSettings. #228

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ namespace Tmds.Ssh;

class SshClient : IDisposable
{
SshClient(string destination, ILoggerFactory? loggerFactory = null); // uses SshConfigOptions.DefaultConfig.
SshClient(string destination, SshConfigOptions configOptions, ILoggerFactory? loggerFactory = null);
SshClient(string destination, ILoggerFactory? loggerFactory = null); // uses SshConfigSettings.DefaultConfig.
SshClient(string destination, SshConfigSettings configSettings, ILoggerFactory? loggerFactory = null);
SshClient(SshClientSettings settings, ILoggerFactory? loggerFactory = null);

// Calling ConnectAsync is optional when SshClientSettings.AutoConnect is set (default).
Expand Down Expand Up @@ -119,7 +119,7 @@ class SftpClient : IDisposable
const UnixFilePermissions DefaultCreateFilePermissions; // = '-rwxrwxrwx'.

// The SftpClient owns the connection.
SftpClient(string destination, SshConfigOptions configOptions, ILoggerFactory? loggerFactory = null, SftpClientOptions? options = null);
SftpClient(string destination, SshConfigSettings configSettings, ILoggerFactory? loggerFactory = null, SftpClientOptions? options = null);
SftpClient(SshClientSettings settings, ILoggerFactory? loggerFactory = null, SftpClientOptions? options = null);

// The SshClient owns the connection.
Expand Down Expand Up @@ -230,10 +230,10 @@ class SshClientSettings

IReadOnlyDictionary<string, string>? EnvironmentVariables { get; set; }
}
class SshConfigOptions
class SshConfigSettings
{
static SshConfigOptions DefaultConfig { get; } // use DefaultConfigFilePaths.
static SshConfigOptions NoConfig { get; } // use [ ]
static SshConfigSettings DefaultConfig { get; } // use DefaultConfigFilePaths.
static SshConfigSettings NoConfig { get; } // use [ ]
static IReadOnlyList<string> DefaultConfigFilePaths { get; } // [ '~/.ssh/config', '/etc/ssh/ssh_config' ]

IReadOnlyList<string> ConfigFilePaths { get; set; }
Expand Down
12 changes: 6 additions & 6 deletions examples/ssh/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ static async Task ExecuteAsync(string destination, string command, string[] opti
}
});

SshConfigOptions configOptions = CreateSshConfigOptions(options);
SshConfigSettings configSettings = CreateSshConfigSettings(options);

using SshClient client = new SshClient(destination, configOptions, loggerFactory);
using SshClient client = new SshClient(destination, configSettings, loggerFactory);

using var process = await client.ExecuteAsync(command);
Task[] tasks = new[]
Expand Down Expand Up @@ -94,9 +94,9 @@ static void PrintExceptions(Task[] tasks)
}
}

private static SshConfigOptions CreateSshConfigOptions(string[] options)
private static SshConfigSettings CreateSshConfigSettings(string[] options)
{
SshConfigOptions configOptions = new SshConfigOptions(SshConfigOptions.DefaultConfigFilePaths);
SshConfigSettings configSettings = new SshConfigSettings(SshConfigSettings.DefaultConfigFilePaths);

Dictionary<SshConfigOption, SshConfigOptionValue> optionsDict = new();
foreach (var option in options)
Expand All @@ -115,9 +115,9 @@ private static SshConfigOptions CreateSshConfigOptions(string[] options)
throw new ArgumentException($"Unsupported option: {option}.");
}
}
configOptions.Options = optionsDict;
configSettings.Options = optionsDict;

return configOptions;
return configSettings;
}

static bool IsEnvvarTrue(string variableName)
Expand Down
4 changes: 2 additions & 2 deletions src/Tmds.Ssh/SftpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ enum State
internal SshClient SshClient => _client;
internal bool IsDisposed => _state == State.Disposed;

public SftpClient(string destination, SshConfigOptions configOptions, ILoggerFactory? loggerFactory = null, SftpClientOptions? options = null) :
this(new SshClient(destination, configOptions, loggerFactory), options, ownsClient: true)
public SftpClient(string destination, SshConfigSettings configSettings, ILoggerFactory? loggerFactory = null, SftpClientOptions? options = null) :
this(new SshClient(destination, configSettings, loggerFactory), options, ownsClient: true)
{ }

public SftpClient(SshClientSettings settings, ILoggerFactory? loggerFactory = null, SftpClientOptions? options = null) :
Expand Down
12 changes: 6 additions & 6 deletions src/Tmds.Ssh/SshClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed partial class SshClient : IDisposable
private readonly TimeSpan _connectTimeout;
private readonly SshClientSettings? _settings;
private readonly string? _destination;
private readonly SshConfigOptions? _sshConfigOptions;
private readonly SshConfigSettings? _sshConfigOptions;
private readonly SshLoggers _loggers;
private State _state = State.Initial;

Expand All @@ -36,18 +36,18 @@ enum State
internal bool IsDisposed => _state == State.Disposed;

public SshClient(string destination, ILoggerFactory? loggerFactory = null) :
this(destination, SshConfigOptions.DefaultConfig, loggerFactory)
this(destination, SshConfigSettings.DefaultConfig, loggerFactory)
{ }

public SshClient(SshClientSettings settings, ILoggerFactory? loggerFactory = null) :
this(settings, destination: null, configOptions: null,
this(settings, destination: null, configSettings: null,
settings?.AutoConnect ?? default, settings?.AutoReconnect ?? default, settings?.ConnectTimeout ?? default,
loggerFactory)
{
ArgumentNullException.ThrowIfNull(settings);
}

public SshClient(string destination, SshConfigOptions sshConfigOptions, ILoggerFactory? loggerFactory = null) :
public SshClient(string destination, SshConfigSettings sshConfigOptions, ILoggerFactory? loggerFactory = null) :
this(settings: null, destination, sshConfigOptions,
sshConfigOptions?.AutoConnect ?? default, sshConfigOptions?.AutoReconnect ?? default, sshConfigOptions?.ConnectTimeout ?? default,
loggerFactory)
Expand All @@ -59,15 +59,15 @@ public SshClient(string destination, SshConfigOptions sshConfigOptions, ILoggerF
private SshClient(
SshClientSettings? settings,
string? destination,
SshConfigOptions? configOptions,
SshConfigSettings? configSettings,
bool autoConnect,
bool autoReconnect,
TimeSpan connectTimeout,
ILoggerFactory? loggerFactory)
{
_settings = settings;
_destination = destination;
_sshConfigOptions = configOptions;
_sshConfigOptions = configSettings;
_autoConnect = autoConnect;
_autoReconnect = autoReconnect;
_connectTimeout = connectTimeout;
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.Ssh/SshClientSettings.SshConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ partial class SshClientSettings
AlgorithmNames.Password
];

internal static async ValueTask<SshClientSettings> LoadFromConfigAsync(string? userName, string host, int? port, SshConfigOptions options, CancellationToken cancellationToken = default)
internal static async ValueTask<SshClientSettings> LoadFromConfigAsync(string? userName, string host, int? port, SshConfigSettings options, CancellationToken cancellationToken = default)
{
SshConfig sshConfig = await SshConfig.DetermineConfigForHost(userName, host, port, options.Options, options.ConfigFilePaths, cancellationToken);

Expand Down
18 changes: 9 additions & 9 deletions src/Tmds.Ssh/SshConfigOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace Tmds.Ssh;
// Because we're a library the default of these options differs from the default 'ssh' command:
// - BatchMode=yes: not user interactive
// - ClearAllForwardings=yes: don't do any forwardings automatically
public sealed class SshConfigOptions
public sealed class SshConfigSettings
{
public static IReadOnlyList<string> DefaultConfigFilePaths { get; } = CreateDefaultConfigFilePaths();
public static SshConfigOptions DefaultConfig { get; }= CreateDefault();
public static SshConfigSettings DefaultConfig { get; }= CreateDefault();

public static SshConfigOptions NoConfig { get; }= CreateNoConfig();
public static SshConfigSettings NoConfig { get; }= CreateNoConfig();

private bool _locked;

Expand All @@ -25,7 +25,7 @@ public sealed class SshConfigOptions
private TimeSpan _connectTimeout = SshClientSettings.DefaultConnectTimeout;
private HostAuthentication? _hostAuthentication;

public SshConfigOptions(IReadOnlyList<string> configFilePaths)
public SshConfigSettings(IReadOnlyList<string> configFilePaths)
{
_configFilePaths = ValidateConfigFilePaths(configFilePaths);
_options = new Dictionary<SshConfigOption, SshConfigOptionValue>();
Expand Down Expand Up @@ -130,11 +130,11 @@ private void ThrowIfLocked()
{
if (_locked)
{
throw new InvalidOperationException($"{nameof(SshConfigOptions)} can not be changed.");
throw new InvalidOperationException($"{nameof(SshConfigSettings)} can not be changed.");
}
}

private static SshConfigOptions CreateDefault()
private static SshConfigSettings CreateDefault()
{
string userConfigFilePath = Path.Combine(SshClientSettings.Home, ".ssh", "config");
string systemConfigFilePath;
Expand All @@ -146,16 +146,16 @@ private static SshConfigOptions CreateDefault()
{
systemConfigFilePath = "/etc/ssh/ssh_config";
}
var config = new SshConfigOptions([userConfigFilePath, systemConfigFilePath]);
var config = new SshConfigSettings([userConfigFilePath, systemConfigFilePath]);

config.Lock();

return config;
}

private static SshConfigOptions CreateNoConfig()
private static SshConfigSettings CreateNoConfig()
{
var config = new SshConfigOptions(DefaultConfigFilePaths);
var config = new SshConfigSettings(DefaultConfigFilePaths);

config.Lock();

Expand Down
6 changes: 3 additions & 3 deletions src/Tmds.Ssh/SshSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sealed partial class SshSession

private readonly SshClient _client;
private readonly string? _destination;
private readonly SshConfigOptions? _sshConfigOptions;
private readonly SshConfigSettings? _sshConfigOptions;
private readonly object _gate = new object();
private readonly CancellationTokenSource _abortCts; // Used to stop all operations
private SshClientSettings? _settings;
Expand All @@ -38,13 +38,13 @@ sealed partial class SshSession
internal SshSession(
SshClientSettings? settings,
string? destination,
SshConfigOptions? configOptions, SshClient client, SshLoggers loggers)
SshConfigSettings? configSettings, SshClient client, SshLoggers loggers)
{
_abortCts = new CancellationTokenSource();

_settings = settings;
_destination = destination;
_sshConfigOptions = configOptions;
_sshConfigOptions = configSettings;

ConnectionInfo = new SshConnectionInfo();

Expand Down
14 changes: 7 additions & 7 deletions test/Tmds.Ssh.Tests/ConnectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public async Task CancelConnect(int msTimeout)
s.Listen();
int port = (s.LocalEndPoint as IPEndPoint)!.Port;

using var client = new SshClient($"user@{address}:{port}", SshConfigOptions.NoConfig);
using var client = new SshClient($"user@{address}:{port}", SshConfigSettings.NoConfig);

CancellationTokenSource cts = new();
cts.CancelAfter(msTimeout);
Expand Down Expand Up @@ -344,7 +344,7 @@ public async Task AutoReconnect(bool autoReconnect)
public async Task SshConfig_AutoConnect(bool autoConnect)
{
using var client = await _sshServer.CreateClientAsync(
new SshConfigOptions([_sshServer.SshConfigFilePath])
new SshConfigSettings([_sshServer.SshConfigFilePath])
{
AutoConnect = autoConnect
},
Expand All @@ -367,7 +367,7 @@ public async Task SshConfig_AutoConnect(bool autoConnect)
public async Task SshConfig_AutoReconnect(bool autoReconnect)
{
using var client = await _sshServer.CreateClientAsync(
new SshConfigOptions([_sshServer.SshConfigFilePath])
new SshConfigSettings([_sshServer.SshConfigFilePath])
{
AutoReconnect = autoReconnect
}
Expand Down Expand Up @@ -399,7 +399,7 @@ public async Task SshConfig_Timeout(int msTimeout)
int port = (s.LocalEndPoint as IPEndPoint)!.Port;

using var client = new SshClient($"user@{address}:{port}",
new SshConfigOptions([_sshServer.SshConfigFilePath])
new SshConfigSettings([_sshServer.SshConfigFilePath])
{
ConnectTimeout = TimeSpan.FromMilliseconds(msTimeout)
});
Expand All @@ -412,7 +412,7 @@ public async Task SshConfig_Timeout(int msTimeout)
public async Task SshConfig_ConnectFailure()
{
await Assert.ThrowsAnyAsync<SshConnectionException>(() =>
_sshServer.CreateClientAsync(SshConfigOptions.NoConfig));
_sshServer.CreateClientAsync(SshConfigSettings.NoConfig));
}

[Fact]
Expand All @@ -425,7 +425,7 @@ public async Task SshConfig_HostAuthentication()
UserKnownHostsFile {Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())}
""");
using var _ = await _sshServer.CreateClientAsync(
new SshConfigOptions([configFile.Path])
new SshConfigSettings([configFile.Path])
{
HostAuthentication =
(KnownHostResult knownHostResult, SshConnectionInfo connectionInfo, CancellationToken cancellationToken) =>
Expand All @@ -449,7 +449,7 @@ public async Task SshConfig_HostAuthentication()
[Fact]
public async Task SshConfig_Options()
{
var options = new SshConfigOptions(configFilePaths: [])
var options = new SshConfigSettings(configFilePaths: [])
{
Options = new Dictionary<SshConfigOption, SshConfigOptionValue>()
{
Expand Down
4 changes: 2 additions & 2 deletions test/Tmds.Ssh.Tests/SshServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ private void VerifyServerWorks()
Assert.Contains(HelloWorld, output);
}

public async Task<SshClient> CreateClientAsync(SshConfigOptions configOptions, CancellationToken cancellationToken = default, bool connect = true)
public async Task<SshClient> CreateClientAsync(SshConfigSettings configSettings, CancellationToken cancellationToken = default, bool connect = true)
{
var client = new SshClient(Destination, configOptions);
var client = new SshClient(Destination, configSettings);

if (connect)
{
Expand Down
Loading