1
0
mirror of https://github.com/go-task/task.git synced 2025-07-15 01:35:00 +02:00
Add ability to globally set environment variables
This commit is contained in:
Andrey Nering
2019-01-02 13:29:14 -02:00
committed by GitHub
10 changed files with 91 additions and 26 deletions

View File

@ -5,7 +5,12 @@
- On Windows, Task can now be installed using [Scoop](https://scoop.sh/) - On Windows, Task can now be installed using [Scoop](https://scoop.sh/)
([#152](https://github.com/go-task/task/pull/152)); ([#152](https://github.com/go-task/task/pull/152));
- Fixes issue with file/directory globing - Fixes issue with file/directory globing
([#153](https://github.com/go-task/task/issues/153)). ([#153](https://github.com/go-task/task/issues/153));
- Add ability to globally set environment variables
(
[#138](https://github.com/go-task/task/pull/138),
[#159](https://github.com/go-task/task/pull/159)
).
## v2.2.1 - 2018-12-09 ## v2.2.1 - 2018-12-09

View File

@ -72,7 +72,7 @@ Both methods requires having the [Go][go] environment properly setup locally.
## Install script ## Install script
We also have a [install script][installscript], which is very useful on We also have a [install script][installscript], which is very useful on
scanarios like CIs. Many thanks to [godownloader][godownloader] for allowing scenarios like CIs. Many thanks to [godownloader][godownloader] for allowing
easily generating this script. easily generating this script.
```bash ```bash

View File

@ -31,23 +31,41 @@ interpreter. So you can write sh/bash commands and it will work even on
Windows, where `sh` or `bash` are usually not available. Just remember any Windows, where `sh` or `bash` are usually not available. Just remember any
executable called must be available by the OS or in PATH. executable called must be available by the OS or in PATH.
If you ommit a task name, "default" will be assumed. If you omit a task name, "default" will be assumed.
## Environment ## Environment
You can specify environment variables that are added when running a command: You can use `env` to set custom environment variables for a specific task:
```yaml ```yaml
version: '2' version: '2'
tasks: tasks:
build: greet:
cmds: cmds:
- echo $hallo - echo $GREETING
env: env:
hallo: welt GREETING: Hey, there!
``` ```
Additionally, you can set globally environment variables, that'll be available
to all tasks:
```yaml
version: '2'
env:
GREETING: Hey, there!
tasks:
greet:
cmds:
- echo $GREETING
```
> NOTE: `env` supports expansion and and retrieving output from a shell command
> just like variables, as you can see on the [Variables](#variables) section.
## Operating System specific tasks ## Operating System specific tasks
If you add a `Taskfile_{{GOOS}}.yml` you can override or amend your Taskfile If you add a `Taskfile_{{GOOS}}.yml` you can override or amend your Taskfile
@ -455,7 +473,7 @@ Task also adds the following functions:
- `catLines`: Replaces Unix (\n) and Windows (\r\n) styled newlines with a space. - `catLines`: Replaces Unix (\n) and Windows (\r\n) styled newlines with a space.
- `toSlash`: Does nothing on Unix, but on Windows converts a string from `\` - `toSlash`: Does nothing on Unix, but on Windows converts a string from `\`
path format to `/`. path format to `/`.
- `fromSlash`: Oposite of `toSlash`. Does nothing on Unix, but on Windows - `fromSlash`: Opposite of `toSlash`. Does nothing on Unix, but on Windows
converts a string from `\` path format to `/`. converts a string from `\` path format to `/`.
- `exeExt`: Returns the right executable extension for the current OS - `exeExt`: Returns the right executable extension for the current OS
(`".exe"` for Windows, `""` for others). (`".exe"` for Windows, `""` for others).
@ -488,7 +506,7 @@ tasks:
## Help ## Help
Running `task --list` (or `task -l`) lists all tasks with a description. Running `task --list` (or `task -l`) lists all tasks with a description.
The following taskfile: The following Taskfile:
```yaml ```yaml
version: '2' version: '2'
@ -575,7 +593,7 @@ tasks:
* Or globally with `--silent` or `-s` flag * Or globally with `--silent` or `-s` flag
If you want to suppress stdout instead, just redirect a command to `/dev/null`: If you want to suppress STDOUT instead, just redirect a command to `/dev/null`:
```yaml ```yaml
version: '2' version: '2'

View File

@ -35,6 +35,13 @@ func Merge(t1, t2 *Taskfile, namespaces ...string) error {
t1.Vars[k] = v t1.Vars[k] = v
} }
if t1.Env == nil {
t1.Env = make(Vars)
}
for k, v := range t2.Vars {
t1.Env[k] = v
}
if t1.Tasks == nil { if t1.Tasks == nil {
t1.Tasks = make(Tasks) t1.Tasks = make(Tasks)
} }

View File

@ -7,6 +7,7 @@ type Taskfile struct {
Output string Output string
Includes map[string]string Includes map[string]string
Vars Vars Vars Vars
Env Vars
Tasks Tasks Tasks Tasks
} }
@ -23,6 +24,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
Output string Output string
Includes map[string]string Includes map[string]string
Vars Vars Vars Vars
Env Vars
Tasks Tasks Tasks Tasks
} }
if err := unmarshal(&taskfile); err != nil { if err := unmarshal(&taskfile); err != nil {
@ -33,6 +35,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
tf.Output = taskfile.Output tf.Output = taskfile.Output
tf.Includes = taskfile.Includes tf.Includes = taskfile.Includes
tf.Vars = taskfile.Vars tf.Vars = taskfile.Vars
tf.Env = taskfile.Env
tf.Tasks = taskfile.Tasks tf.Tasks = taskfile.Tasks
if tf.Expansions <= 0 { if tf.Expansions <= 0 {
tf.Expansions = 2 tf.Expansions = 2

View File

@ -268,9 +268,9 @@ func getEnviron(t *taskfile.Task) []string {
return nil return nil
} }
envs := os.Environ() environ := os.Environ()
for k, v := range t.Env.ToStringMap() { for k, v := range t.Env.ToStringMap() {
envs = append(envs, fmt.Sprintf("%s=%s", k, v)) environ = append(environ, fmt.Sprintf("%s=%s", k, v))
} }
return envs return environ
} }

View File

@ -61,7 +61,8 @@ func TestEnv(t *testing.T) {
Target: "default", Target: "default",
TrimSpace: false, TrimSpace: false,
Files: map[string]string{ Files: map[string]string{
"env.txt": "GOOS='linux' GOARCH='amd64' CGO_ENABLED='0'\n", "local.txt": "GOOS='linux' GOARCH='amd64' CGO_ENABLED='0'\n",
"global.txt": "FOO='foo' BAR='overriden' BAZ='baz'\n",
}, },
} }
tt.Run(t) tt.Run(t)

View File

@ -1 +1 @@
env.txt *.txt

View File

@ -1,4 +1,21 @@
version: '2'
vars:
BAZ:
sh: echo baz
env:
FOO: foo
BAR: bar
BAZ: "{{.BAZ}}"
tasks:
default: default:
cmds:
- task: local
- task: global
local:
vars: vars:
AMD64: amd64 AMD64: amd64
env: env:
@ -7,4 +24,10 @@ default:
CGO_ENABLED: CGO_ENABLED:
sh: echo '0' sh: echo '0'
cmds: cmds:
- echo "GOOS='$GOOS' GOARCH='$GOARCH' CGO_ENABLED='$CGO_ENABLED'" > env.txt - echo "GOOS='$GOOS' GOARCH='$GOARCH' CGO_ENABLED='$CGO_ENABLED'" > local.txt
global:
env:
BAR: overriden
cmds:
- echo "FOO='$FOO' BAR='$BAR' BAZ='$BAZ'" > global.txt

View File

@ -30,7 +30,7 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
Status: r.ReplaceSlice(origTask.Status), Status: r.ReplaceSlice(origTask.Status),
Dir: r.Replace(origTask.Dir), Dir: r.Replace(origTask.Dir),
Vars: nil, Vars: nil,
Env: r.ReplaceVars(origTask.Env), Env: nil,
Silent: origTask.Silent, Silent: origTask.Silent,
Method: r.Replace(origTask.Method), Method: r.Replace(origTask.Method),
Prefix: r.Replace(origTask.Prefix), Prefix: r.Replace(origTask.Prefix),
@ -46,6 +46,14 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
if new.Prefix == "" { if new.Prefix == "" {
new.Prefix = new.Task new.Prefix = new.Task
} }
new.Env = make(taskfile.Vars, len(e.Taskfile.Env)+len(origTask.Env))
for k, v := range r.ReplaceVars(e.Taskfile.Env) {
new.Env[k] = v
}
for k, v := range r.ReplaceVars(origTask.Env) {
new.Env[k] = v
}
for k, v := range new.Env { for k, v := range new.Env {
static, err := e.Compiler.HandleDynamicVar(v) static, err := e.Compiler.HandleDynamicVar(v)
if err != nil { if err != nil {