Skip to content

Commit 5377601

Browse files
authoredJul 30, 2024··
feat(test): Allow users to override variables defined in documents via --var when running ie test. (#210)
This allows users to specify/override environment variables inside a markdown document by providing `--var KEY=VALUE` when running `ie test`. Both `execute` and `interactive` already support this feature and the implementation was ported over to `ie test`.
1 parent 1ee5d3d commit 5377601

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed
 

‎cmd/ie/commands/test.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/Azure/InnovationEngine/internal/engine"
89
"github.com/Azure/InnovationEngine/internal/engine/common"
@@ -19,6 +20,9 @@ func init() {
1920
String("subscription", "", "Sets the subscription ID used by a scenarios azure-cli commands. Will rely on the default subscription if not set.")
2021
testCommand.PersistentFlags().
2122
String("working-directory", ".", "Sets the working directory for innovation engine to operate out of. Restores the current working directory when finished.")
23+
24+
testCommand.PersistentFlags().
25+
StringArray("var", []string{}, "Sets an environment variable for the scenario. Format: --var <key>=<value>")
2226
}
2327

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

44+
environmentVariables, _ := cmd.Flags().GetStringArray("var")
45+
46+
// Parse the environment variables from the command line into a map
47+
cliEnvironmentVariables := make(map[string]string)
48+
for _, environmentVariable := range environmentVariables {
49+
keyValuePair := strings.SplitN(environmentVariable, "=", 2)
50+
if len(keyValuePair) != 2 {
51+
logging.GlobalLogger.Errorf(
52+
"Error: Invalid environment variable format: %s",
53+
environmentVariable,
54+
)
55+
fmt.Printf("Error: Invalid environment variable format: %s", environmentVariable)
56+
cmd.Help()
57+
os.Exit(1)
58+
}
59+
60+
cliEnvironmentVariables[keyValuePair[0]] = keyValuePair[1]
61+
}
62+
4063
innovationEngine, err := engine.NewEngine(engine.EngineConfiguration{
4164
Verbose: verbose,
4265
DoNotDelete: false,
@@ -54,7 +77,7 @@ var testCommand = &cobra.Command{
5477
scenario, err := common.CreateScenarioFromMarkdown(
5578
markdownFile,
5679
[]string{"bash", "azurecli", "azurecli-interactive", "terraform"},
57-
nil,
80+
cliEnvironmentVariables,
5881
)
5982
if err != nil {
6083
logging.GlobalLogger.Errorf("Error creating scenario %s", err)

0 commit comments

Comments
 (0)
Please sign in to comment.