Skip to content
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

[Feature] Add support for multiple env files #137

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
21 changes: 13 additions & 8 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 All @@ -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 @@ -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 Expand Up @@ -274,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
21 changes: 21 additions & 0 deletions v2/testdata/env-multiple.ahoy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ahoyapi: v2
env:
- .env
- .env.cmd
commands:
test-global:
cmd: echo $GLOBAL
test-command:
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
12 changes: 8 additions & 4 deletions v2/testdata/env.ahoy.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
ahoyapi: v2
env: ./.env
env:
- .env
commands:
test-global:
cmd: echo $GLOBAL

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
22 changes: 22 additions & 0 deletions v2/tests/environment-variables.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,25 @@
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" ]]
}

@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" ]]
}

Loading