Skip to content

Commit

Permalink
Allow executor to append environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Jul 25, 2020
1 parent 1890eea commit 32fed2f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func main() {
log.Printf("[INFO] CLI args: %#v", os.Args)

e := tfexec.NewExecutor("tmp/test", os.Environ())
e.AppendEnv("TF_LOG", "TRACE")
e.AppendEnv("DIRENV_LOG_FORMAT", "")
terraformCLI := tfexec.NewTerraformCLI(e)
terraformCLI.SetExecPath("direnv exec . terraform")
v, err := terraformCLI.Version(context.Background())
Expand Down
7 changes: 7 additions & 0 deletions tfexec/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Executor interface {
NewCommandContext(ctx context.Context, name string, args ...string) (Command, error)
// Run executes a command.
Run(cmd Command) error
// AppendEnv appends an environment variable.
AppendEnv(key string, value string)
}

// executor impolements the Executor interface.
Expand Down Expand Up @@ -77,3 +79,8 @@ func (e *executor) Run(cmd Command) error {
}
return nil
}

// AppendEnv appends an environment variable.
func (e *executor) AppendEnv(key string, value string) {
e.env = append(e.env, key+"="+value)
}
26 changes: 20 additions & 6 deletions tfexec/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,38 @@ func TestExecutorRun(t *testing.T) {

func TestExecutorEnv(t *testing.T) {
cases := []struct {
desc string
args []string
env []string
want string
ok bool
desc string
args []string
env []string
appendKey string
appendValue string
want string
ok bool
}{
{
desc: "test set env",
desc: "test set env with NewExecutor",
args: []string{"/bin/sh", "-c", "echo $FOO"},
env: []string{"FOO=foo"},
want: "foo\n",
ok: true,
},
{
desc: "test set env with AppendEnv",
args: []string{"/bin/sh", "-c", "echo $FOO $BAR"},
env: []string{"FOO=foo"},
appendKey: "BAR",
appendValue: "bar",
want: "foo bar\n",
ok: true,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
e := NewExecutor(".", tc.env)
if len(tc.appendKey) > 0 {
e.AppendEnv(tc.appendKey, tc.appendValue)
}
cmd, err := e.NewCommandContext(context.Background(), tc.args[0], tc.args[1:]...)
if err != nil {
t.Fatalf("failed to NewCommandContext: %s", err)
Expand Down
5 changes: 5 additions & 0 deletions tfexec/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func (e *mockExecutor) Run(cmd Command) error {
return cmd.Run()
}

// AppendEnv appends an environment variable.
func (e *mockExecutor) AppendEnv(key string, value string) {
// no op.
}

// mockRunFunc is a type for callback of mockCommand.Run() to allow us to cause side effects.
type mockRunFunc func(args ...string) error

Expand Down

0 comments on commit 32fed2f

Please sign in to comment.