mirror of
https://github.com/go-task/task.git
synced 2025-11-25 22:32:55 +02:00
Merge pull request #9 from go-task/feature/env
Allow specifying additional environment settings
This commit is contained in:
12
README.md
12
README.md
@@ -42,6 +42,18 @@ task assets build
|
|||||||
If Bash is available (Linux and Windows while on Git Bash), the commands will
|
If Bash is available (Linux and Windows while on Git Bash), the commands will
|
||||||
run in Bash, otherwise will fallback to `cmd` (on Windows).
|
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
|
### Running task in another dir
|
||||||
|
|
||||||
By default, tasks will be executed in the directory where the Taskfile is
|
By default, tasks will be executed in the directory where the Taskfile is
|
||||||
|
|||||||
21
task.go
21
task.go
@@ -2,6 +2,7 @@ package task
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@@ -48,6 +49,7 @@ type Task struct {
|
|||||||
Dir string
|
Dir string
|
||||||
Vars map[string]string
|
Vars map[string]string
|
||||||
Set string
|
Set string
|
||||||
|
Env map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run runs Task
|
// Run runs Task
|
||||||
@@ -105,7 +107,7 @@ func RunTask(name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := range t.Cmds {
|
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}
|
return &taskRunError{name, err}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,7 +132,7 @@ func (t *Task) isUpToDate() bool {
|
|||||||
return generatesMinTime.After(sourcesMaxTime)
|
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()
|
vars, err := t.handleVariables()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -152,6 +154,21 @@ func (t *Task) runCommand(i int) error {
|
|||||||
if dir != "" {
|
if dir != "" {
|
||||||
cmd.Dir = dir
|
cmd.Dir = dir
|
||||||
}
|
}
|
||||||
|
if nil != envVariables {
|
||||||
|
env := os.Environ()
|
||||||
|
for key, value := range envVariables {
|
||||||
|
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
|
||||||
|
}
|
||||||
cmd.Stdin = os.Stdin
|
cmd.Stdin = os.Stdin
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
if t.Set != "" {
|
if t.Set != "" {
|
||||||
|
|||||||
Reference in New Issue
Block a user