1
0
mirror of https://github.com/go-task/task.git synced 2025-06-25 00:47:04 +02:00

refactor getVariables()

This commit is contained in:
Andrey Nering
2017-07-08 14:46:49 -03:00
parent 7a7f66dfdc
commit 50f592c540
2 changed files with 32 additions and 36 deletions

View File

@ -277,8 +277,8 @@ They are listed below in order of importance (e.g. most important first):
- Variables given while calling a task from another. - Variables given while calling a task from another.
(See [Calling another task](#calling-another-task) above) (See [Calling another task](#calling-another-task) above)
- Environment variables - Environment variables
- Variables available in the `Taskvars.yml` file
- Variables declared locally in the task - Variables declared locally in the task
- Variables available in the `Taskvars.yml` file
Example of overriding with environment variables: Example of overriding with environment variables:
@ -286,14 +286,6 @@ Example of overriding with environment variables:
$ TASK_VARIABLE=a-value task do-something $ TASK_VARIABLE=a-value task do-something
``` ```
Example of `Taskvars.yml` file:
```yml
PROJECT_NAME: My Project
DEV_MODE: production
GIT_COMMIT: $git log -n 1 --format=%h
```
Example of locally declared vars: Example of locally declared vars:
```yml ```yml
@ -304,6 +296,14 @@ print-var:
VAR: Hello! VAR: Hello!
``` ```
Example of `Taskvars.yml` file:
```yml
PROJECT_NAME: My Project
DEV_MODE: production
GIT_COMMIT: $git log -n 1 --format=%h
```
> NOTE: It's also possible setting a variable globally using `set` attribute > NOTE: It's also possible setting a variable globally using `set` attribute
in task, but this is deprecated: in task, but this is deprecated:

View File

@ -56,39 +56,35 @@ func (e *Executor) handleDynamicVariableContent(value string) (string, error) {
return result, nil return result, nil
} }
func (e *Executor) getVariables(call Call) (map[string]string, error) { func (e *Executor) getVariables(call Call) (Vars, error) {
t := e.Tasks[call.Task] t := e.Tasks[call.Task]
localVariables := make(map[string]string) result := make(Vars, len(t.Vars)+len(e.taskvars)+len(call.Vars))
for key, value := range t.Vars { merge := func(vars Vars) error {
val, err := e.handleDynamicVariableContent(value) for k, v := range vars {
v, err := e.handleDynamicVariableContent(v)
if err != nil { if err != nil {
return err
}
result[k] = v
}
return nil
}
if err := merge(e.taskvars); err != nil {
return nil, err return nil, err
} }
localVariables[key] = val if err := merge(t.Vars); err != nil {
}
if e.taskvars != nil {
for key, value := range e.taskvars {
val, err := e.handleDynamicVariableContent(value)
if err != nil {
return nil, err return nil, err
} }
localVariables[key] = val if err := merge(getEnvironmentVariables()); err != nil {
}
}
for key, value := range getEnvironmentVariables() {
localVariables[key] = value
}
if call.Vars != nil {
for k, v := range call.Vars {
val, err := e.handleDynamicVariableContent(v)
if err != nil {
return nil, err return nil, err
} }
localVariables[k] = val if err := merge(call.Vars); err != nil {
return nil, err
} }
}
return localVariables, nil return result, nil
} }
var templateFuncs template.FuncMap var templateFuncs template.FuncMap
@ -152,10 +148,10 @@ func (e *Executor) ReplaceVariables(initial string, call Call) (string, error) {
} }
// GetEnvironmentVariables returns environment variables as map // GetEnvironmentVariables returns environment variables as map
func getEnvironmentVariables() map[string]string { func getEnvironmentVariables() Vars {
var ( var (
env = os.Environ() env = os.Environ()
m = make(map[string]string, len(env)) m = make(Vars, len(env))
) )
for _, e := range env { for _, e := range env {