Skip to content

Commit

Permalink
Add ability to specify env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwhitaker committed Jul 21, 2024
1 parent 27edea0 commit 0c50042
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .runny.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
shell: /bin/bash
env:
- FOO=fooster
- BAR=barbara
commands:
clean:
run: |
Expand All @@ -16,3 +19,7 @@ commands:
goreleaser
test:
run: go test ./...
test-env:
env:
- FOO=baz
run: echo $FOO $BAR
8 changes: 6 additions & 2 deletions runny/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions runny/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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, " ")
}
Expand All @@ -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
Expand Down

0 comments on commit 0c50042

Please sign in to comment.