Skip to content

Commit

Permalink
add support for multiple command env files
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwrfuller committed Jan 13, 2025
1 parent 3f8b0cb commit 09895f6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
11 changes: 7 additions & 4 deletions v2/ahoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Command struct {
Description string
Usage string
Cmd string
Env string
Env []string
Hide bool
Optional bool
Imports []string
Expand Down Expand Up @@ -276,9 +276,12 @@ func getCommands(config Config) []cli.Command {
// If defined, included specified command-level environment variables.
// Note that this will intentionally override any conflicting variables
// defined in the 'global' env file.
if cmd.Env != "" {
cmdEnvFile := filepath.Join(AhoyConf.srcDir, cmd.Env)
envVars = append(envVars, getEnvironmentVars(cmdEnvFile)...)
if cmd.Env != nil {
for _, file := range cmd.Env {
cmdEnvFile := filepath.Join(AhoyConf.srcDir, file)
envVars = append(envVars, getEnvironmentVars(cmdEnvFile)...)

}
}

if verbose {
Expand Down
2 changes: 2 additions & 0 deletions v2/testdata/.env.local1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
UNIQUE=unique
TO_BE_OVERRIDDEN=local1
1 change: 1 addition & 0 deletions v2/testdata/.env.local2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TO_BE_OVERRIDDEN=local2
10 changes: 10 additions & 0 deletions v2/testdata/env-multiple.ahoy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ commands:
cmd: echo $COMMAND
test-overridden:
cmd: echo $TO_BE_OVERRIDDEN
test-cmd-multiple-1:
cmd: echo $UNIQUE
env:
- .env.local1
- .env.local2
test-cmd-multiple-2:
cmd: echo $TO_BE_OVERRIDDEN
env:
- .env.local1
- .env.local2
9 changes: 6 additions & 3 deletions v2/testdata/env.ahoy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ commands:

test-cmd:
cmd: echo $COMMAND
env: .env.cmd
env:
- .env.cmd

test-override:
cmd: echo $TO_BE_OVERRIDDEN
env: .env.cmd
env:
- .env.cmd

test-invalid-env:
cmd: echo "This should not print!"
env: .env.thisfiledoesntexist
env:
- .env.thisfiledoesntexist
12 changes: 11 additions & 1 deletion v2/tests/environment-variables.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

Expand Down Expand Up @@ -31,3 +30,14 @@
run ./ahoy -f testdata/env-multiple.ahoy.yml test-overridden
[[ "$output" = "after" ]]
}

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

run ./ahoy -f testdata/env-multiple.ahoy.yml test-cmd-multiple-2
echo $output
[[ "$output" = "local2" ]]
}

0 comments on commit 09895f6

Please sign in to comment.