diff --git a/domain/app.go b/domain/app.go index 649fc4c..1296d64 100644 --- a/domain/app.go +++ b/domain/app.go @@ -2,12 +2,13 @@ package domain import ( "fmt" - "gopkg.in/yaml.v2" "io/ioutil" "os" "os/exec" "path/filepath" + "gopkg.in/yaml.v2" + "github.com/spf13/viper" ) @@ -30,6 +31,7 @@ type Mode struct { StopCommand []string `yaml:"stop-command"` WorkingDir string `yaml:"working-dir"` Dependencies []Dependency + Env EnvVars } type Dependency struct { @@ -37,6 +39,13 @@ type Dependency struct { ModeName string `yaml:"mode"` } +type EnvVar struct { + Name string `yaml:"name"` + Value string `yaml:"value"` +} + +type EnvVars []EnvVar + func NewApp(name string, modeName string) (App, error) { var err error @@ -79,7 +88,7 @@ func (a *App) Start() error { return fmt.Errorf("Don't know how to start %s in '%s' mode, because start-command is unset.", a.Name, a.Mode.Name) } - return runCommand(a.Mode.StartCommand, a.Mode.WorkingDir) + return runCommand(a.Mode.StartCommand, a.Mode.WorkingDir, a.Mode.Env) } func (a *App) Stop() error { @@ -90,10 +99,20 @@ func (a *App) Stop() error { return fmt.Errorf("Don't know how to stop %s in '%s' mode, because stop-command is unset.", a.Name, a.Mode.Name) } - return runCommand(a.Mode.StopCommand, a.Mode.WorkingDir) + return runCommand(a.Mode.StopCommand, a.Mode.WorkingDir, a.Mode.Env) +} + +func (env EnvVars) ToStrings() []string { + strings := []string{} + + for _, variable := range env { + strings = append(strings, variable.Name+"="+variable.Value) + } + + return strings } -func runCommand(command []string, workingDir string) error { +func runCommand(command []string, workingDir string, env EnvVars) error { // In the case of tools like `make`, the executable will be on the PATH // and LookPath will find it. @@ -117,6 +136,7 @@ func runCommand(command []string, workingDir string) error { Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr, + Env: env.ToStrings(), } if viper.IsSet("verbose") { @@ -173,6 +193,7 @@ func readConfig(appName string, sourceDir string) (Config, error) { StopCommand: mode.StopCommand, Dependencies: mode.Dependencies, WorkingDir: workingDir, + Env: mode.Env, } config.Modes[name] = newMode diff --git a/domain/app_test.go b/domain/app_test.go index 53fe7ea..949baba 100644 --- a/domain/app_test.go +++ b/domain/app_test.go @@ -74,4 +74,14 @@ func TestNewApp(t *testing.T) { t.Errorf("Expected working dir for '%s' mode to be '%s', but it was '%s'.", modeName, expectedDir, actualDir) } } + + // Modes' environment variables are read correctly + + envVars := EnvVars{EnvVar{Name: "FOO", Value: "bar"}} + if expected, actual := envVars[0].Name, a.Mode.Env[0].Name; expected != actual { + t.Errorf("Expected env var Name to be %s, got %s", expected, actual) + } + if expected, actual := envVars[0].Value, a.Mode.Env[0].Value; expected != actual { + t.Errorf("Expected env var Value to be %s, got %s", expected, actual) + } } diff --git a/domain/test-fixtures/src/foo/devon.conf.yaml b/domain/test-fixtures/src/foo/devon.conf.yaml index 465c5d8..855d546 100644 --- a/domain/test-fixtures/src/foo/devon.conf.yaml +++ b/domain/test-fixtures/src/foo/devon.conf.yaml @@ -1,6 +1,9 @@ modes: development: start-command: ["bar"] + env: + - name: FOO + value: bar unguessable: start-command: ["baz"] working-dir: "fergus" diff --git a/example.yaml b/example.yaml index 013c0f8..1546524 100644 --- a/example.yaml +++ b/example.yaml @@ -27,8 +27,11 @@ dependencies: &default_dependencies # modes: development: - start-command: ["bash", "-c", "sleep 5; echo Starting devon!"] + start-command: ["bash", "-c", "sleep 5; echo Starting devon with FOO=${FOO}!"] working-dir: . + env: + - name: FOO + value: bar dependencies: *default_dependencies dependency: start-command: ["./dev/up.sh"]