-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50c745a
commit 89400c3
Showing
12 changed files
with
367 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using LocalStack.AwsLocal.Contracts; | ||
using LocalStack.AwsLocal.EnvironmentContext; | ||
using LocalStack.AwsLocal.Extensions; | ||
using LocalStack.Client.Contracts; | ||
using LocalStack.Client.Models; | ||
|
||
namespace LocalStack.AwsLocal | ||
{ | ||
public class CommandDispatcher | ||
{ | ||
private const string UsageResource = "LocalStack.AwsLocal.Docs.Usage.txt"; | ||
|
||
private readonly IProcessHelper _processHelper; | ||
private readonly IConfig _config; | ||
private readonly TextWriter _textWriter; | ||
private readonly string[] _args; | ||
|
||
private CommandDispatcher() | ||
{ | ||
} | ||
|
||
public CommandDispatcher(IProcessHelper processHelper, IConfig config, TextWriter textWriter, string[] args) | ||
{ | ||
_processHelper = processHelper; | ||
_config = config; | ||
_textWriter = textWriter; | ||
_args = args; | ||
} | ||
|
||
public void Run() | ||
{ | ||
if (_args.Length == 0 || (_args[0] == "-h")) | ||
{ | ||
string usageInfo = GetUsageInfo(); | ||
_textWriter.WriteLine(usageInfo); | ||
EnvironmentControl.Current.Exit(0); | ||
return; | ||
} | ||
|
||
string serviceName = _args.ExtractServiceName(); | ||
|
||
if (string.IsNullOrEmpty(serviceName)) | ||
{ | ||
_textWriter.WriteLine("ERROR: Invalid argument, please enter a valid aws cli command"); | ||
EnvironmentControl.Current.Exit(1); | ||
return; | ||
} | ||
|
||
AwsServiceEndpoint awsServiceEndpoint = _config.GetServiceEndpoint(serviceName); | ||
|
||
if (awsServiceEndpoint == null) | ||
{ | ||
_textWriter.WriteLine($"ERROR: Unable to find LocalStack endpoint for service {serviceName}"); | ||
EnvironmentControl.Current.Exit(1); | ||
return; | ||
} | ||
|
||
string cliCommand = _args.GetCliCommand(awsServiceEndpoint.ServiceUrl); | ||
|
||
string awsDefaultRegion = Environment.GetEnvironmentVariable("AWS_DEFAULT_REGION") ?? "us-east-1"; | ||
string awsAccessKeyId = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID") ?? "_not_needed_locally_"; | ||
string awsSecretAccessKey = Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY") ?? "_not_needed_locally_"; | ||
|
||
_processHelper.CmdExecute(cliCommand, null, true, true, new Dictionary<string, string> | ||
{ | ||
{"AWS_DEFAULT_REGION", awsDefaultRegion}, | ||
{"AWS_ACCESS_KEY_ID", awsAccessKeyId}, | ||
{"AWS_SECRET_ACCESS_KEY", awsSecretAccessKey} | ||
}); | ||
} | ||
|
||
private static string GetUsageInfo() | ||
{ | ||
using (Stream stream = Assembly.GetCallingAssembly().GetManifestResourceStream(UsageResource)) | ||
{ | ||
using (var reader = new StreamReader(stream)) | ||
{ | ||
string result = reader.ReadToEnd(); | ||
|
||
return result; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/LocalStack.AwsLocal/EnvironmentContext/DefaultEnvironmentControl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
namespace LocalStack.AwsLocal.EnvironmentContext | ||
{ | ||
public class DefaultEnvironmentControl : EnvironmentControl | ||
{ | ||
private static readonly Lazy<DefaultEnvironmentControl> LazyInstance = new Lazy<DefaultEnvironmentControl>(() => new DefaultEnvironmentControl()); | ||
|
||
public override void Exit(int value) | ||
{ | ||
Environment.Exit(value); | ||
} | ||
|
||
public static DefaultEnvironmentControl Instance => LazyInstance.Value; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/LocalStack.AwsLocal/EnvironmentContext/EnvironmentControl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
|
||
namespace LocalStack.AwsLocal.EnvironmentContext | ||
{ | ||
public abstract class EnvironmentControl | ||
{ | ||
private static EnvironmentControl _current = DefaultEnvironmentControl.Instance; | ||
|
||
public static EnvironmentControl Current | ||
{ | ||
get => _current; | ||
|
||
set => _current = value ?? throw new ArgumentNullException(nameof(value)); | ||
} | ||
|
||
public abstract void Exit(int value); | ||
|
||
public static void ResetToDefault() | ||
{ | ||
_current = DefaultEnvironmentControl.Instance; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace LocalStack.AwsLocal.Extensions | ||
{ | ||
public static class ArgumentExtensions | ||
{ | ||
public static string ExtractServiceName(this IEnumerable<string> args) | ||
{ | ||
foreach (string arg in args) | ||
{ | ||
if (arg.StartsWith('-')) | ||
{ | ||
continue; | ||
} | ||
|
||
return arg == "s3api" ? "s3" : arg; | ||
} | ||
|
||
return string.Empty; | ||
} | ||
|
||
public static string GetCliCommand(this IEnumerable<string> args, string serviceUrl) | ||
{ | ||
var arguments = args.ToList(); | ||
arguments.Insert(0, "aws"); | ||
arguments.Insert(1, $"--endpoint-url={serviceUrl}"); | ||
|
||
if (serviceUrl.StartsWith("https")) | ||
{ | ||
arguments.Insert(2, "--no-verify-ssl"); | ||
} | ||
|
||
return string.Join(' ', arguments); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Linq; | ||
using LocalStack.Client.Contracts; | ||
using LocalStack.Client.Models; | ||
|
||
namespace LocalStack.AwsLocal.Extensions | ||
{ | ||
public static class ConfigExtensions | ||
{ | ||
public static AwsServiceEndpoint GetServiceEndpoint(this IConfig config, string serviceName) | ||
{ | ||
var awsServiceEndpoints = config.GetAwsServiceEndpoints(); | ||
|
||
return awsServiceEndpoints.SingleOrDefault(endpoint => endpoint.CliName == serviceName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,21 @@ | ||
using LocalStack.AwsLocal.Contracts; | ||
using LocalStack.Client; | ||
using LocalStack.Client.Contracts; | ||
using LocalStack.Client.Models; | ||
using LocalStack.Client; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace LocalStack.AwsLocal | ||
{ | ||
internal static class Program | ||
{ | ||
private const string UsageResource = "LocalStack.AwsLocal.Docs.Usage.txt"; | ||
|
||
private static readonly IProcessHelper ProcessHelper = new ProcessHelper(); | ||
private static readonly string LocalStackHost = Environment.GetEnvironmentVariable("LOCALSTACK_HOST"); | ||
private static readonly IConfig Config = new Config(LocalStackHost); | ||
|
||
private static IEnumerable<string> Args { get; set; } | ||
|
||
private static void Main(string[] args) | ||
{ | ||
Args = args; | ||
|
||
if (args.Length == 0 || (args[0] == "-h")) | ||
{ | ||
Usage(); | ||
} | ||
|
||
(string service, AwsServiceEndpoint awsServiceEndpoint) = GetServiceEndpoint(); | ||
|
||
if (awsServiceEndpoint == null) | ||
{ | ||
Console.WriteLine($"ERROR: Unable to find LocalStack endpoint for service {service}"); | ||
Environment.Exit(1); | ||
} | ||
|
||
var arguments = args.ToList(); | ||
arguments.Insert(0, "aws"); | ||
arguments.Insert(1, $"--endpoint-url={awsServiceEndpoint.ServiceUrl}"); | ||
|
||
if (awsServiceEndpoint.Host.Contains("https")) | ||
{ | ||
arguments.Insert(2, "--no-verify-ssl"); | ||
} | ||
|
||
string awsDefaultRegion = Environment.GetEnvironmentVariable("AWS_DEFAULT_REGION") ?? "us-east-1"; | ||
string awsAccessKeyId = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID") ?? "_not_needed_locally_"; | ||
string awsSecretAccessKey = Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY") ?? "_not_needed_locally_"; | ||
|
||
ProcessHelper.CmdExecute(string.Join(' ', arguments), null, true, true, new Dictionary<string, string> | ||
{ | ||
{"AWS_DEFAULT_REGION", awsDefaultRegion}, | ||
{"AWS_ACCESS_KEY_ID", awsAccessKeyId}, | ||
{"AWS_SECRET_ACCESS_KEY", awsSecretAccessKey} | ||
}); | ||
} | ||
var processHelper = new ProcessHelper(); | ||
var config = new Config(LocalStackHost); | ||
var textWriter = Console.Out; | ||
|
||
private static string GetService() | ||
{ | ||
foreach (string arg in Args) | ||
{ | ||
if (!arg.StartsWith('-')) | ||
{ | ||
return arg; | ||
} | ||
} | ||
|
||
return string.Empty; | ||
} | ||
|
||
private static (string service, AwsServiceEndpoint awsServiceEndpoint) GetServiceEndpoint() | ||
{ | ||
string service = GetService(); | ||
if (service == "s3api") | ||
{ | ||
service = "s3"; | ||
} | ||
|
||
var awsServiceEndpoints = Config.GetAwsServiceEndpoints(); | ||
return (service, awsServiceEndpoints.SingleOrDefault(endpoint => endpoint.CliName == service)); | ||
} | ||
|
||
private static void Usage() | ||
{ | ||
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(UsageResource)) | ||
{ | ||
using (var reader = new StreamReader(stream)) | ||
{ | ||
string result = reader.ReadToEnd(); | ||
Console.WriteLine(result); | ||
} | ||
} | ||
var commandDispatcher = new CommandDispatcher(processHelper, config, textWriter, args); | ||
|
||
Environment.Exit(0); | ||
commandDispatcher.Run(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.