You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
A very basic implementation on #151. Environment variable names are generated by capitalising parameter names and secret management is disabled while using the new flag.
PR Type
enhancement, documentation
Description
Added a new action PopulateInputsWithEnvVariablesAction to generate environment variable placeholders for inputs.
Introduced a new command option UseEnvVariablesAsParameterValues to replace parameter references with environment variable names.
Updated the command handler to conditionally execute actions based on the new option.
Enhanced documentation to include the new CLI option for generating Docker Compose files with environment variables.
Changes walkthrough 📝
Relevant files
Enhancement
11 files
PopulateInputsWithEnvVariablesAction.cs
Add PopulateInputsWithEnvVariablesAction for environment variables
Conditional Logic The new implementation introduces conditional logic based on the useEnvVariablesAsParameterValues flag. This might lead to different behavior paths that need to be thoroughly tested.
Regex Change The regex pattern for placeholder detection has been modified. This change could potentially affect how placeholders are identified and processed throughout the application.
State Management A new property UseEnvVariablesAsParameterValues has been added to the AspirateState class. Ensure that this new state is properly managed and doesn't introduce any inconsistencies in the application's state handling.
Why: Using named capture groups significantly improves code readability and maintainability by making the regex pattern more understandable and reducing reliance on magic numbers.
9
Enhancement
Refactor nested if statements to use pattern matching for improved readability
Consider using pattern matching with 'when' clause instead of nested if statements to improve readability and reduce nesting.
var matches = PlaceholderPatternRegex().Matches(input);
-for (var i = 0; i < matches.Count; i++)+foreach (Match match in matches)
{
- var match = matches[i];- if (!string.IsNullOrEmpty(match.Groups[1].Value))+ var prefix = match.Groups["prefix"].Value;+ var content = match.Groups["content"].Value;++ if (string.IsNullOrEmpty(prefix))
{
- continue;+ var pathParts = content.Split('.');+ input = pathParts.Length switch+ {+ 1 => input.Replace($"{{{content}}}", rootNode[pathParts[0]].ToString(), StringComparison.OrdinalIgnoreCase),+ _ => input // Handle multi-part paths here+ };
}
+}- var jsonPath = match.Groups[2].Value;- var pathParts = jsonPath.Split('.');- if (pathParts.Length == 1)- {- input = input.Replace($"{{{jsonPath}}}", rootNode[pathParts[0]].ToString(), StringComparison.OrdinalIgnoreCase);-
Apply this suggestion
Suggestion importance[1-10]: 8
Why: The refactoring suggestion enhances readability by reducing nesting and using pattern matching, which is a modern and cleaner approach in C#. It improves the code structure without altering functionality.
8
Refactor the manifest generation logic to improve extensibility and maintainability
Consider using a strategy pattern or factory method to create the appropriate action sequence based on the output format and whether environment variables are used for parameter values. This would simplify the HandleAsync method and make it easier to add new output formats in the future.
-return outputFormat.Name switch+return GenerateManifests(outputFormat.Name, options.UseEnvVariablesAsParameterValues ?? false);++// Add this method:+private Task<int> GenerateManifests(string outputFormat, bool useEnvVariablesAsParameterValues)
{
- nameof(OutputFormat.Kustomize) => GenerateKustomizeManifests(),- nameof(OutputFormat.DockerCompose) => GenerateDockerComposeManifests(options.UseEnvVariablesAsParameterValues ?? false),- nameof(OutputFormat.Helm) => GenerateHelmManifests(),- _ => throw new ArgumentOutOfRangeException(nameof(options.OutputFormat), $"The output format '{options.OutputFormat}' is not supported."),-};+ return outputFormat switch+ {+ nameof(OutputFormat.Kustomize) => GenerateKustomizeManifests(),+ nameof(OutputFormat.DockerCompose) => GenerateDockerComposeManifests(useEnvVariablesAsParameterValues),+ nameof(OutputFormat.Helm) => GenerateHelmManifests(),+ _ => throw new ArgumentOutOfRangeException(nameof(outputFormat), $"The output format '{outputFormat}' is not supported."),+ };+}
Apply this suggestion
Suggestion importance[1-10]: 7
Why: The suggestion offers a refactoring that could improve the extensibility and maintainability of the code by centralizing the logic for generating manifests. However, it is not a critical change and mainly enhances code organization.
7
Maintainability
Extract common action sequence to reduce duplication in the BaseGenerateActionSequence method
Consider extracting the common parts of the BaseGenerateActionSequence method into a separate method to reduce code duplication and improve readability.
private ActionExecutor BaseGenerateActionSequence(bool useEnvVariablesAsParameterValues = false)
{
- var result = ActionExecutor+ var result = CommonActionSequence();++ if (useEnvVariablesAsParameterValues)+ {+ result.QueueAction(nameof(PopulateInputsWithEnvVariablesAction));+ }+ else+ {+ result.QueueAction(nameof(PopulateInputsAction))+ .QueueAction(nameof(SaveSecretsAction));+ }++ return result;+}++private ActionExecutor CommonActionSequence()+{+ return ActionExecutor
.QueueAction(nameof(LoadConfigurationAction))
.QueueAction(nameof(GenerateAspireManifestAction))
.QueueAction(nameof(LoadAspireManifestAction))
- .QueueAction(nameof(IncludeAspireDashboardAction));- if (!useEnvVariablesAsParameterValues)- {- result.QueueAction(nameof(PopulateInputsAction));- }- else- {- result.QueueAction(nameof(PopulateInputsWithEnvVariablesAction));- }- result+ .QueueAction(nameof(IncludeAspireDashboardAction))
.QueueAction(nameof(SubstituteValuesAspireManifestAction))
.QueueAction(nameof(ApplyDaprAnnotationsAction))
.QueueAction(nameof(PopulateContainerDetailsForProjectsAction))
.QueueAction(nameof(BuildAndPushContainersFromProjectsAction))
.QueueAction(nameof(BuildAndPushContainersFromDockerfilesAction));
- if (!useEnvVariablesAsParameterValues)- {- result.QueueAction(nameof(SaveSecretsAction));- }-- return result;
}
Apply this suggestion
Suggestion importance[1-10]: 8
Why: This suggestion effectively reduces code duplication and enhances readability, which is beneficial for maintainability. It is a good practice to extract common logic into separate methods.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
A very basic implementation on #151. Environment variable names are generated by capitalising parameter names and secret management is disabled while using the new flag.
PR Type
enhancement, documentation
Description
PopulateInputsWithEnvVariablesAction
to generate environment variable placeholders for inputs.UseEnvVariablesAsParameterValues
to replace parameter references with environment variable names.Changes walkthrough 📝
11 files
PopulateInputsWithEnvVariablesAction.cs
Add PopulateInputsWithEnvVariablesAction for environment variables
src/Aspirate.Commands/Actions/Secrets/PopulateInputsWithEnvVariablesAction.cs
PopulateInputsWithEnvVariablesAction
.BaseCommand.cs
Conditional secret loading based on command options
src/Aspirate.Commands/Commands/BaseCommand.cs
RequiresSecrets
flag.BaseCommandOptions.cs
Add RequiresSecrets property to command options
src/Aspirate.Commands/Commands/BaseCommandOptions.cs
RequiresSecrets
property.GenerateCommand.cs
Add UseEnvVariablesAsParameterValues option to GenerateCommand
src/Aspirate.Commands/Commands/Generate/GenerateCommand.cs
UseEnvVariablesAsParameterValues
.GenerateCommandHandler.cs
Modify action sequence for environment variable support
src/Aspirate.Commands/Commands/Generate/GenerateCommandHandler.cs
PopulateInputsWithEnvVariablesAction
.GenerateOptions.cs
Add UseEnvVariablesAsParameterValues property to GenerateOptions
src/Aspirate.Commands/Commands/Generate/GenerateOptions.cs
UseEnvVariablesAsParameterValues
property.RequiresSecrets
logic.UseEnvVariablesAsParameterValuesOption.cs
Create UseEnvVariablesAsParameterValuesOption class
src/Aspirate.Commands/Options/UseEnvVariablesAsParameterValuesOption.cs
ServiceCollectionExtensions.cs
Register PopulateInputsWithEnvVariablesAction in service collection
src/Aspirate.Commands/ServiceCollectionExtensions.cs
PopulateInputsWithEnvVariablesAction
.JsonExpressionProcessor.cs
Update regex for environment variable placeholders
src/Aspirate.Processors/Transformation/Json/JsonExpressionProcessor.cs
IGenerateOptions.cs
Extend IGenerateOptions with environment variable option
src/Aspirate.Shared/Interfaces/Commands/Contracts/IGenerateOptions.cs
UseEnvVariablesAsParameterValues
to interface.AspirateState.cs
Add UseEnvVariablesAsParameterValues to AspirateState
src/Aspirate.Shared/Models/Aspirate/AspirateState.cs
UseEnvVariablesAsParameterValues
property.1 files
Generate-Command.md
Document UseEnvVariablesAsParameterValues CLI option
docs/Writerside/topics/Generate-Command.md