Skip to content
This repository has been archived by the owner on Apr 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from redbubble/add-env-to-config
Browse files Browse the repository at this point in the history
Allow start/stop command to read env variables
  • Loading branch information
Dito Hartoto authored Nov 18, 2020
2 parents df58962 + a738642 commit c4bfb88
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
29 changes: 25 additions & 4 deletions domain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -30,13 +31,21 @@ type Mode struct {
StopCommand []string `yaml:"stop-command"`
WorkingDir string `yaml:"working-dir"`
Dependencies []Dependency
Env EnvVars
}

type Dependency struct {
AppName string `yaml:"name"`
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

Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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") {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions domain/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions domain/test-fixtures/src/foo/devon.conf.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
modes:
development:
start-command: ["bar"]
env:
- name: FOO
value: bar
unguessable:
start-command: ["baz"]
working-dir: "fergus"
5 changes: 4 additions & 1 deletion example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit c4bfb88

Please sign in to comment.