Skip to content

Commit

Permalink
add support for multiple global env files
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwrfuller committed Jan 13, 2025
1 parent 0f4a78e commit 4140335
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
10 changes: 6 additions & 4 deletions v2/ahoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Config struct {
AhoyAPI string
Commands map[string]Command
Entrypoint []string
Env string
Env []string
}

// Command is an ahoy command detailed in ahoy.yml files. Multiple
Expand Down Expand Up @@ -202,9 +202,11 @@ func getCommands(config Config) []cli.Command {
envVars := []string{}

// Get environment variables from the 'global' environment variable file, if it is defined.
if config.Env != "" {
globalEnvFile := filepath.Join(AhoyConf.srcDir, config.Env)
envVars = append(envVars, getEnvironmentVars(globalEnvFile)...)
if config.Env != nil {
for _, file := range config.Env {
globalEnvFile := filepath.Join(AhoyConf.srcDir, file)
envVars = append(envVars, getEnvironmentVars(globalEnvFile)...)
}
}

var keys []string
Expand Down
11 changes: 11 additions & 0 deletions v2/testdata/env-multiple.ahoy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ahoyapi: v2
env:
- .env
- .env.cmd
commands:
test-global:
cmd: echo $GLOBAL
test-command:
cmd: echo $COMMAND
test-overridden:
cmd: echo $TO_BE_OVERRIDDEN
3 changes: 2 additions & 1 deletion v2/testdata/env.ahoy.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ahoyapi: v2
env: ./.env
env:
- .env
commands:
test-global:
cmd: echo $GLOBAL
Expand Down
12 changes: 12 additions & 0 deletions v2/tests/environment-variables.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

@test "Command-level variables can be defined and used" {
run ./ahoy -f testdata/env.ahoy.yml test-cmd
echo "$output"
[[ "$output" == "123456789" ]]
}

Expand All @@ -19,3 +20,14 @@
run ./ahoy -f testdata/env.ahoy.yml test-invalid-env
[ $status -eq 1 ]
}

@test "Multiple global env files can be defined" {
run ./ahoy -f testdata/env-multiple.ahoy.yml test-global
[[ "$output" = "global" ]]

run ./ahoy -f testdata/env-multiple.ahoy.yml test-command
[[ "$output" = "123456789" ]]

run ./ahoy -f testdata/env-multiple.ahoy.yml test-overridden
[[ "$output" = "after" ]]
}

0 comments on commit 4140335

Please sign in to comment.