Skip to content

Commit

Permalink
Feat: Get Attribute Command (#8)
Browse files Browse the repository at this point in the history
* feat: get attribute command

allows for getting an attribute from configuration (avoid having to do any kind of mapping inside workflow files)

* feat: run build on PRs
  • Loading branch information
the-avid-engineer authored Aug 28, 2024
1 parent e0e6cf8 commit 7a934bc
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
on:
pull_request:
push:
branches:
- main
Expand Down
19 changes: 14 additions & 5 deletions src/InfrastructureCli/Commands/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,28 @@ protected static void AttachConfigurationsFileNameOption(Command parentCommand)
parentCommand.AddGlobalOption(configurationsFileName);
}

protected static async Task<(IRootRewriter, ICloudProvisioningService)> GetProvisioningTools
protected static ICloudProviderService GetCloudProviderService
(
ConfigurationsFile configurationsFile,
Configuration configuration,
IConsole console,
string currentPath
IConsole console
)
{
ICloudProviderService cloudProviderService = configuration.TemplateType switch
return configuration.TemplateType switch
{
TemplateType.AwsCloudFormation => new AwsService(console),
_ => throw new NotSupportedException()
};
}

protected static async Task<(IRootRewriter, ICloudProvisioningService)> GetProvisioningTools
(
ConfigurationsFile configurationsFile,
Configuration configuration,
IConsole console,
string currentPath
)
{
var cloudProviderService = GetCloudProviderService(configuration, console);

var region = cloudProviderService.GetRegionName();

Expand Down
88 changes: 88 additions & 0 deletions src/InfrastructureCli/Commands/GetAttributeCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.NamingConventionBinder;
using System.IO;
using System.Threading.Tasks;
using InfrastructureCli.Models;
using InfrastructureCli.Services;

namespace InfrastructureCli.Commands;

internal record GetAttributeCommand : CommandBase
{
private record Arguments
(
FileInfo ConfigurationsFileName,
string ConfigurationKey,
string AttributeName,
IConsole Console
);

private static async Task<int> Execute(Arguments arguments)
{
var configurationsFile = await FileService.DeserializeFromFile<ConfigurationsFile>(arguments.ConfigurationsFileName);

var configuration = configurationsFile.Configurations.GetValueOrDefault(arguments.ConfigurationKey);

if (configuration == default)
{
return 1;
}

var cloudProviderService = GetCloudProviderService(configuration, arguments.Console);

var region = cloudProviderService.GetRegionName();

if (configurationsFile.GlobalAttributes.TryGetValue(arguments.AttributeName, out var globalAttribute))
{
arguments.Console.Out.Write(globalAttribute.ToString());
return 0;
}

if (configurationsFile.GlobalRegionAttributes.TryGetValue(region, out var globalRegionAttributes) && globalRegionAttributes.TryGetValue(arguments.AttributeName,
out var globalRegionAttribute))
{
arguments.Console.Out.Write(globalRegionAttribute.ToString());
return 0;
}

if (configuration.Attributes.TryGetValue(arguments.AttributeName, out var attribute))
{
arguments.Console.Out.Write(attribute.ToString());
return 0;
}

if (configuration.RegionAttributes.TryGetValue(region, out var regionAttributes) && regionAttributes.TryGetValue(arguments.AttributeName, out var regionAttribute))
{
arguments.Console.Out.Write(regionAttribute.ToString());
return 0;
}

return 2;
}

private static void AttachAttributeNameArgument(Command parentCommand)
{
var propertyName = new Argument<string>("attribute-name")
{
Description = "The name of the attribute that contains the desired information."
};

parentCommand.AddArgument(propertyName);
}

public static void Attach(RootCommand rootCommand)
{
var getCommand = new Command("get-attribute")
{
Handler = CommandHandler.Create<Arguments>(Execute),
Description = "Retrieves an attribute value for a deployment."
};

AttachConfigurationsFileNameOption(getCommand);
AttachConfigurationKeyArgument(getCommand);
AttachAttributeNameArgument(getCommand);

rootCommand.AddCommand(getCommand);
}
}
1 change: 1 addition & 0 deletions src/InfrastructureCli/Commands/ProgramCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public ProgramCommand(ProgramCommandOptions options)
CanDeployCommand.Attach(rootCommand);
DeployCommand.Attach(rootCommand, options.ValidateConfigurationsFile);
GetCommand.Attach(rootCommand);
GetAttributeCommand.Attach(rootCommand);

if (options.GenerateCommands is { Length: > 0 })
{
Expand Down

0 comments on commit 7a934bc

Please sign in to comment.