Skip to content

Commit

Permalink
improve code base
Browse files Browse the repository at this point in the history
  • Loading branch information
karimz1 committed Jun 9, 2024
1 parent 656c479 commit a0ea564
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
40 changes: 27 additions & 13 deletions src/AwsServiceAuthenticator.Cli/ServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,16 @@ public static class ServiceConfiguration
{
public static ServiceProvider ConfigureServices(string region, string logFilePath)
{
var logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(logFilePath)
.CreateLogger();

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<ILogger, SerilogLogger>(_ => new SerilogLogger(logger));
serviceCollection.RegisterLogging(logFilePath);
serviceCollection.RegisterCommandsMapping();

serviceCollection.AddSingleton<IAwsAuthenticator, AwsAuthenticator>();
serviceCollection.AddSingleton<ISystemRegion>(_ => new SystemRegion(region));
serviceCollection.AddSingleton<ICommandResolver, CommandResolver>();

// Register commands
serviceCollection.AddTransient<EcrAuthCommand>();
serviceCollection.AddTransient<NuGetAuthCommand>();

return serviceCollection.BuildServiceProvider();
}

public static void ConfigureAwsLogging()
{
AWSConfigs.LoggingConfig.LogTo = LoggingOptions.Console;
Expand All @@ -44,4 +36,26 @@ public static void InitializeTraceListeners()
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
}
}

private static IServiceCollection RegisterLogging(this IServiceCollection services, string logFilePath)
{
var logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(logFilePath)
.CreateLogger();

services.AddSingleton<ILogger, SerilogLogger>(_ => new SerilogLogger(logger));
return services;
}

private static IServiceCollection RegisterCommandsMapping(this IServiceCollection services)
{
services.AddSingleton<ICommandResolver, CommandResolver>();
var commandMappings = CommandConfiguration.GetCommandMappings();

foreach (var command in commandMappings)
services.AddTransient(command.Value);

return services;
}
}
13 changes: 13 additions & 0 deletions src/AwsServiceAuthenticator.Commands/CommandConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace AwsServiceAuthenticator.Commands;

public static class CommandConfiguration
{
public static Dictionary<string, Type> GetCommandMappings()
{
return new Dictionary<string, Type>
{
{ "ecr", typeof(EcrAuthCommand) },
{ "nuget", typeof(NuGetAuthCommand) }
};
}
}
12 changes: 3 additions & 9 deletions src/AwsServiceAuthenticator.Commands/CommandResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ namespace AwsServiceAuthenticator.Commands;

public class CommandResolver(IServiceProvider serviceProvider) : ICommandResolver
{
private readonly Dictionary<string, Type> _commands = new()
{
{ "ecr", typeof(EcrAuthCommand) },
{ "nuget", typeof(NuGetAuthCommand) }
};
private readonly Dictionary<string, Type> _commands = CommandConfiguration.GetCommandMappings();

ICommand? ICommandResolver.ResolveCommand(string commandName)
public ICommand? ResolveCommand(string commandName)
{
if (_commands.TryGetValue(commandName, out var commandType))
{
return serviceProvider.GetService(commandType) as ICommand;
}

throw new ArgumentException("This argument does not exists", nameof(commandName));
throw new ArgumentException("This argument does not exist", nameof(commandName));
}
}
5 changes: 2 additions & 3 deletions src/AwsServiceAuthenticator.Commands/EcrAuthCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ public class EcrAuthCommand(ILogger logger, ISystemRegion systemRegion) : IComma

public async Task ExecuteAsync()
{
var ecrClient = new AmazonECRClient(RegionEndpoint.GetBySystemName(_systemRegion));
var response =
await ecrClient.GetAuthorizationTokenAsync(new Amazon.ECR.Model.GetAuthorizationTokenRequest());
using var client = new AmazonECRClient(RegionEndpoint.GetBySystemName(_systemRegion));
var response = await client.GetAuthorizationTokenAsync(new Amazon.ECR.Model.GetAuthorizationTokenRequest());
var authData = response.AuthorizationData[0];
var token = Convert.FromBase64String(authData.AuthorizationToken);
var credentials = System.Text.Encoding.UTF8.GetString(token).Split(':');
Expand Down
2 changes: 1 addition & 1 deletion src/AwsServiceAuthenticator.Commands/NuGetAuthCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task ExecuteAsync()
var domainName = await awsAuthenticator.GetDomainNameAsync();
var token = await awsAuthenticator.GetAuthorizationTokenAsync(domainName, domainOwner);

var client = new AmazonCodeArtifactClient(RegionEndpoint.GetBySystemName(_region));
using var client = new AmazonCodeArtifactClient(RegionEndpoint.GetBySystemName(_region));
var reposResponse = await client.ListRepositoriesInDomainAsync(new ListRepositoriesInDomainRequest
{
Domain = domainName,
Expand Down

0 comments on commit a0ea564

Please sign in to comment.