1
0
mirror of https://github.com/go-task/task.git synced 2025-01-20 04:59:37 +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.
(See [Calling another task](#calling-another-task) above)
- Environment variables
- Variables available in the `Taskvars.yml` file
- Variables declared locally in the task
- Variables available in the `Taskvars.yml` file
Example of overriding with environment variables:
@ -286,14 +286,6 @@ Example of overriding with environment variables:
$ 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:
```yml
@ -304,6 +296,14 @@ print-var:
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
in task, but this is deprecated:

View File

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