Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #24 from eko/added-env-file
Added support for an environment variable file on local apps
- Loading branch information
Showing
8 changed files
with
180 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# This is a comment, should be ignored | ||
MY_ENVFILE_VAR_1=this is ok | ||
|
||
# This is a second comment, should be ignored too | ||
MY_ENVFILE_VAR_2=this is really good | ||
MY_ENVFILE_VAR_3=great |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package runner | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
) | ||
|
||
// addEnvVariables adds environment variables given as key/value pair | ||
func (r *Runner) addEnvVariables(cmd *exec.Cmd, envs map[string]string) { | ||
for key, value := range envs { | ||
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", key, value)) | ||
} | ||
} | ||
|
||
// addEnvVariablesFromFile adds environment variables given as a filename | ||
func (r *Runner) addEnvVariablesFromFile(cmd *exec.Cmd, filename string) { | ||
if filename == "" { | ||
return | ||
} | ||
|
||
filename = os.ExpandEnv(filename) | ||
|
||
file, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm) | ||
if err != nil { | ||
r.view.Writef("❌ Unable to open environment file '%s': %v\n", filename, err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
|
||
r, _ := regexp.Compile("([a-zA-Z0-9_]+)=(.*)") | ||
matches := r.FindStringSubmatch(line) | ||
|
||
if len(matches) < 3 { | ||
continue | ||
} | ||
|
||
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", matches[1], matches[2])) | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
r.view.Writef("❌ An error has occured while reading environment file '%s': %v\n", filename, err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package runner | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
mocks "github.com/eko/monday/internal/tests/mocks/proxy" | ||
uimocks "github.com/eko/monday/internal/tests/mocks/ui" | ||
"github.com/eko/monday/pkg/config" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestAddEnvVariables(t *testing.T) { | ||
// Given | ||
view := &uimocks.ViewInterface{} | ||
view.On("Write", mock.Anything) | ||
view.On("Writef", mock.Anything, mock.Anything, mock.Anything) | ||
|
||
proxy := &mocks.ProxyInterface{} | ||
|
||
project := getMockedProjectWithApplicationEnv() | ||
|
||
runner := NewRunner(view, proxy, project) | ||
|
||
// When | ||
runner.Run(project.Applications[0]) | ||
|
||
// Then | ||
assert.IsType(t, new(Runner), runner) | ||
assert.Len(t, runner.cmds, 1) | ||
|
||
cmd := runner.cmds["test-app"] | ||
|
||
assert.Contains(t, cmd.Env, "MY_ENVVAR_1=value") | ||
assert.Contains(t, cmd.Env, "MY_ENVVAR_2=My custom second value") | ||
} | ||
|
||
func TestAddEnvVariablesFromFile(t *testing.T) { | ||
// Given | ||
view := &uimocks.ViewInterface{} | ||
view.On("Write", mock.Anything) | ||
view.On("Writef", mock.Anything, mock.Anything, mock.Anything) | ||
|
||
proxy := &mocks.ProxyInterface{} | ||
|
||
project := getMockedProjectWithApplicationEnv() | ||
|
||
runner := NewRunner(view, proxy, project) | ||
|
||
// When | ||
runner.Run(project.Applications[0]) | ||
|
||
// Then | ||
assert.IsType(t, new(Runner), runner) | ||
assert.Len(t, runner.cmds, 1) | ||
|
||
cmd := runner.cmds["test-app"] | ||
|
||
assert.Contains(t, cmd.Env, "MY_ENVFILE_VAR_1=this is ok") | ||
assert.Contains(t, cmd.Env, "MY_ENVFILE_VAR_2=this is really good") | ||
assert.Contains(t, cmd.Env, "MY_ENVFILE_VAR_3=great") | ||
} | ||
|
||
func getMockedProjectWithApplicationEnv() *config.Project { | ||
dir, _ := os.Getwd() | ||
|
||
return &config.Project{ | ||
Name: "My project name", | ||
Applications: []*config.Application{ | ||
&config.Application{ | ||
Name: "test-app", | ||
Path: "/", | ||
Executable: "echo", | ||
Args: []string{ | ||
"OK", | ||
"Arguments", | ||
"Seems", | ||
"-to=work", | ||
}, | ||
Env: map[string]string{ | ||
"MY_ENVVAR_1": "value", | ||
"MY_ENVVAR_2": "My custom second value", | ||
}, | ||
EnvFile: dir + "/../../internal/tests/runner/test.env", | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Ok this is a test of comment | ||
MY_TEST_1=ok | ||
MY_TEST_2=alwaysok | ||
MY_TEST_3=sureok |