refactor(*): Use a strongly-typed options object for reading configuration for the SftpRepository

This commit is contained in:
Josh Creek
2021-09-02 21:17:36 +01:00
parent 73be5a52f9
commit 2f152f2a3d
4 changed files with 32 additions and 10 deletions
@@ -25,6 +25,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Creek.HelpfulExtensions" Version="1.2.0" /> <PackageReference Include="Creek.HelpfulExtensions" Version="1.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0"> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -1,5 +1,6 @@
using Creek.FileRepository.Helpers; using Creek.FileRepository.Helpers;
using Creek.FileRepository.Models; using Creek.FileRepository.Models;
using Creek.FileRepository.RepositoryOptions;
using Creek.HelpfulExtensions; using Creek.HelpfulExtensions;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Renci.SshNet; using Renci.SshNet;
@@ -28,11 +29,14 @@ namespace Creek.FileRepository.Repositories
/// <param name="config">The configuration to initialise the repository with.</param> /// <param name="config">The configuration to initialise the repository with.</param>
internal SftpRepository(IConfiguration config) internal SftpRepository(IConfiguration config)
{ {
this.host = config["SftpRepository:host"]; SftpRepositoryOptions sftpRepositoryOptions = config.GetSection(SftpRepositoryOptions.SftpRepository)
this.port = int.Parse(config["SftpRepository:port"]); .Get<SftpRepositoryOptions>();
this.username = config["SftpRepository:username"].ToString();
this.password = config["SftpRepository:password"].ToString(); this.host = sftpRepositoryOptions.Host;
this.remoteDirectoryPath = config["SftpRepository:remoteDirectoryPath"].ToString(); this.port = sftpRepositoryOptions.Port;
this.username = sftpRepositoryOptions.Username;
this.password = sftpRepositoryOptions.Password;
this.remoteDirectoryPath = sftpRepositoryOptions.RemoteDirectoryPath;
} }
/// <summary> /// <summary>
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Creek.FileRepository.RepositoryOptions
{
public class SftpRepositoryOptions
{
public const string SftpRepository = "SftpRepository";
public string Host { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string RemoteDirectoryPath { get; set; }
}
}
+5 -5
View File
@@ -1,9 +1,9 @@
{ {
"SftpRepository": { "SftpRepository": {
"host": "", "Host": "",
"port": 22, "Port": 22,
"username": "", "Username": "",
"password": "", "Password": "",
"remoteDirectoryPath": "/" "RemoteDirectoryPath": "/"
} }
} }