From 6f7b26908f502a60fe8fc002629727ed41b1df68 Mon Sep 17 00:00:00 2001 From: Sascha Andres Date: Mon, 6 Mar 2017 13:48:56 +0100 Subject: [PATCH 1/4] Added ability to add environment variables per task Add an anv section to the task --- cmd/task/Taskfile.yml | 5 +++++ task.go | 13 +++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 cmd/task/Taskfile.yml diff --git a/cmd/task/Taskfile.yml b/cmd/task/Taskfile.yml new file mode 100644 index 00000000..59d951ae --- /dev/null +++ b/cmd/task/Taskfile.yml @@ -0,0 +1,5 @@ +build: + cmds: + - echo $hallo + env: + hallo: welt diff --git a/task.go b/task.go index 1c232a32..044a4dcb 100644 --- a/task.go +++ b/task.go @@ -2,6 +2,7 @@ package task import ( "encoding/json" + "fmt" "io/ioutil" "log" "os" @@ -47,6 +48,7 @@ type Task struct { Dir string Vars map[string]string Set string + Env map[string]string } // Run runs Task @@ -104,7 +106,7 @@ func RunTask(name string) error { } for i := range t.Cmds { - if err = t.runCommand(i); err != nil { + if err = t.runCommand(i, t.Env); err != nil { return &taskRunError{name, err} } } @@ -129,7 +131,7 @@ func (t *Task) isUpToDate() bool { return generatesMinTime.After(sourcesMaxTime) } -func (t *Task) runCommand(i int) error { +func (t *Task) runCommand(i int, envVariables map[string]string) error { vars, err := t.handleVariables() if err != nil { return err @@ -151,6 +153,13 @@ func (t *Task) runCommand(i int) error { if dir != "" { cmd.Dir = dir } + if nil != envVariables { + env := os.Environ() + for key, value := range envVariables { + env = append(env, fmt.Sprintf("%s=%s", key, value)) + } + cmd.Env = env + } cmd.Stdin = os.Stdin cmd.Stderr = os.Stderr if t.Set != "" { From 72528af18aacb821414e2dafa98563ade29c10a8 Mon Sep 17 00:00:00 2001 From: Sascha Andres Date: Mon, 6 Mar 2017 13:52:46 +0100 Subject: [PATCH 2/4] Applying variables to env section --- task.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/task.go b/task.go index 044a4dcb..49c66580 100644 --- a/task.go +++ b/task.go @@ -156,7 +156,15 @@ func (t *Task) runCommand(i int, envVariables map[string]string) error { if nil != envVariables { env := os.Environ() for key, value := range envVariables { - env = append(env, fmt.Sprintf("%s=%s", key, value)) + replacedValue, err := ReplaceVariables(value, vars) + if err != nil { + return err + } + replacedKey, err := ReplaceVariables(key, vars) + if err != nil { + return err + } + env = append(env, fmt.Sprintf("%s=%s", replacedKey, replacedValue)) } cmd.Env = env } From 2e52e101bebf7e9d568e234c9f4ed963912f8e23 Mon Sep 17 00:00:00 2001 From: Sascha Andres Date: Mon, 6 Mar 2017 13:53:51 +0100 Subject: [PATCH 3/4] Removed Taskfile.yml --- cmd/task/Taskfile.yml | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 cmd/task/Taskfile.yml diff --git a/cmd/task/Taskfile.yml b/cmd/task/Taskfile.yml deleted file mode 100644 index 59d951ae..00000000 --- a/cmd/task/Taskfile.yml +++ /dev/null @@ -1,5 +0,0 @@ -build: - cmds: - - echo $hallo - env: - hallo: welt From 3291f60fe70f3dfa722be0498828e3d6a77f540c Mon Sep 17 00:00:00 2001 From: Sascha Andres Date: Mon, 6 Mar 2017 13:55:03 +0100 Subject: [PATCH 4/4] Added environment to README --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index e49afd49..e3b04c05 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,18 @@ task assets build If Bash is available (Linux and Windows while on Git Bash), the commands will run in Bash, otherwise will fallback to `cmd` (on Windows). +### Environment + +You can specify environment variables that are added when running a command: + +```yml +build: + cmds: + - echo $hallo + env: + hallo: welt +``` + ### Running task in another dir By default, tasks will be executed in the directory where the Taskfile is