Skip to content

feat(test): Allow users to override variables defined in documents via --var when running ie test. #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion cmd/ie/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"
"os"
"strings"

"github.com/Azure/InnovationEngine/internal/engine"
"github.com/Azure/InnovationEngine/internal/engine/common"
Expand All @@ -19,6 +20,9 @@ func init() {
String("subscription", "", "Sets the subscription ID used by a scenarios azure-cli commands. Will rely on the default subscription if not set.")
testCommand.PersistentFlags().
String("working-directory", ".", "Sets the working directory for innovation engine to operate out of. Restores the current working directory when finished.")

testCommand.PersistentFlags().
StringArray("var", []string{}, "Sets an environment variable for the scenario. Format: --var <key>=<value>")
}

var testCommand = &cobra.Command{
Expand All @@ -37,6 +41,25 @@ var testCommand = &cobra.Command{
workingDirectory, _ := cmd.Flags().GetString("working-directory")
environment, _ := cmd.Flags().GetString("environment")

environmentVariables, _ := cmd.Flags().GetStringArray("var")

// Parse the environment variables from the command line into a map
cliEnvironmentVariables := make(map[string]string)
for _, environmentVariable := range environmentVariables {
keyValuePair := strings.SplitN(environmentVariable, "=", 2)
if len(keyValuePair) != 2 {
logging.GlobalLogger.Errorf(
"Error: Invalid environment variable format: %s",
environmentVariable,
)
fmt.Printf("Error: Invalid environment variable format: %s", environmentVariable)
cmd.Help()
os.Exit(1)
}

cliEnvironmentVariables[keyValuePair[0]] = keyValuePair[1]
}

innovationEngine, err := engine.NewEngine(engine.EngineConfiguration{
Verbose: verbose,
DoNotDelete: false,
Expand All @@ -54,7 +77,7 @@ var testCommand = &cobra.Command{
scenario, err := common.CreateScenarioFromMarkdown(
markdownFile,
[]string{"bash", "azurecli", "azurecli-interactive", "terraform"},
nil,
cliEnvironmentVariables,
)
if err != nil {
logging.GlobalLogger.Errorf("Error creating scenario %s", err)
Expand Down
Loading