From 0c500420cf25bbf5aeace7de2219660dcdbf6b9a Mon Sep 17 00:00:00 2001 From: Simon Whitaker Date: Sun, 21 Jul 2024 22:37:54 +0100 Subject: [PATCH] Add ability to specify env variables --- .runny.yaml | 7 +++++++ runny/model.go | 8 ++++++-- runny/shell.go | 5 +++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.runny.yaml b/.runny.yaml index 8aff24b..5133248 100644 --- a/.runny.yaml +++ b/.runny.yaml @@ -1,4 +1,7 @@ shell: /bin/bash +env: + - FOO=fooster + - BAR=barbara commands: clean: run: | @@ -16,3 +19,7 @@ commands: goreleaser test: run: go test ./... + test-env: + env: + - FOO=baz + run: echo $FOO $BAR diff --git a/runny/model.go b/runny/model.go index 60aca01..14a2838 100644 --- a/runny/model.go +++ b/runny/model.go @@ -16,11 +16,13 @@ type CommandDef struct { Run string Needs []CommandName If string + Env []string } type Config struct { Commands map[CommandName]CommandDef Shell string + Env []string verbose bool } @@ -88,10 +90,12 @@ func (c *Config) Execute(name CommandName, args ...string) error { return fmt.Errorf(errorMsg) } + env := append(c.Env, command.Env...) + // Check the If condition cond := strings.TrimSpace(command.If) if len(cond) > 0 { - err := shell.Run(cond, []string{}, false, c.verbose) + err := shell.Run(cond, []string{}, false, c.verbose, env) if err != nil { // Run returns an error if the exit status is not zero. So in this case, this means the test failed. if c.verbose { @@ -112,7 +116,7 @@ func (c *Config) Execute(name CommandName, args ...string) error { // Handle the Run run := strings.TrimSpace(command.Run) if len(run) > 0 { - err := shell.Run(run, args, true, c.verbose) + err := shell.Run(run, args, true, c.verbose, env) if err != nil { fmt.Printf("%s %s\n", color.RedString(string(run)), secondaryColor.Sprint(err)) return err diff --git a/runny/shell.go b/runny/shell.go index 0b20653..d6d49d9 100644 --- a/runny/shell.go +++ b/runny/shell.go @@ -9,7 +9,7 @@ import ( ) type Shell interface { - Run(command string, extraArgs []string, echoStdout, verbose bool) error + Run(command string, extraArgs []string, echoStdout, verbose bool, env []string) error } type BashShell struct { @@ -24,7 +24,7 @@ func NewShell(command string) (Shell, error) { return nil, fmt.Errorf("unsupported shell: %s", command) } -func (b BashShell) Run(command string, extraArgs []string, echoStdout, verbose bool) error { +func (b BashShell) Run(command string, extraArgs []string, echoStdout, verbose bool, env []string) error { if len(extraArgs) > 0 { command = command + " " + strings.Join(extraArgs, " ") } @@ -35,6 +35,7 @@ func (b BashShell) Run(command string, extraArgs []string, echoStdout, verbose b args := []string{"-c", command} cmd := exec.Command(b.command, args...) + cmd.Env = env if echoStdout { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr