Skip to content

Commit 50f592c

Browse files
committed
refactor getVariables()
1 parent 7a7f66d commit 50f592c

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,15 @@ They are listed below in order of importance (e.g. most important first):
277277
- Variables given while calling a task from another.
278278
(See [Calling another task](#calling-another-task) above)
279279
- Environment variables
280-
- Variables available in the `Taskvars.yml` file
281280
- Variables declared locally in the task
281+
- Variables available in the `Taskvars.yml` file
282282

283283
Example of overriding with environment variables:
284284

285285
```bash
286286
$ TASK_VARIABLE=a-value task do-something
287287
```
288288

289-
Example of `Taskvars.yml` file:
290-
291-
```yml
292-
PROJECT_NAME: My Project
293-
DEV_MODE: production
294-
GIT_COMMIT: $git log -n 1 --format=%h
295-
```
296-
297289
Example of locally declared vars:
298290

299291
```yml
@@ -304,6 +296,14 @@ print-var:
304296
VAR: Hello!
305297
```
306298

299+
Example of `Taskvars.yml` file:
300+
301+
```yml
302+
PROJECT_NAME: My Project
303+
DEV_MODE: production
304+
GIT_COMMIT: $git log -n 1 --format=%h
305+
```
306+
307307
> NOTE: It's also possible setting a variable globally using `set` attribute
308308
in task, but this is deprecated:
309309

variable_handling.go

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,35 @@ func (e *Executor) handleDynamicVariableContent(value string) (string, error) {
5656
return result, nil
5757
}
5858

59-
func (e *Executor) getVariables(call Call) (map[string]string, error) {
59+
func (e *Executor) getVariables(call Call) (Vars, error) {
6060
t := e.Tasks[call.Task]
6161

62-
localVariables := make(map[string]string)
63-
for key, value := range t.Vars {
64-
val, err := e.handleDynamicVariableContent(value)
65-
if err != nil {
66-
return nil, err
67-
}
68-
localVariables[key] = val
69-
}
70-
if e.taskvars != nil {
71-
for key, value := range e.taskvars {
72-
val, err := e.handleDynamicVariableContent(value)
62+
result := make(Vars, len(t.Vars)+len(e.taskvars)+len(call.Vars))
63+
merge := func(vars Vars) error {
64+
for k, v := range vars {
65+
v, err := e.handleDynamicVariableContent(v)
7366
if err != nil {
74-
return nil, err
67+
return err
7568
}
76-
localVariables[key] = val
69+
result[k] = v
7770
}
71+
return nil
7872
}
79-
for key, value := range getEnvironmentVariables() {
80-
localVariables[key] = value
73+
74+
if err := merge(e.taskvars); err != nil {
75+
return nil, err
8176
}
82-
if call.Vars != nil {
83-
for k, v := range call.Vars {
84-
val, err := e.handleDynamicVariableContent(v)
85-
if err != nil {
86-
return nil, err
87-
}
88-
localVariables[k] = val
89-
}
77+
if err := merge(t.Vars); err != nil {
78+
return nil, err
79+
}
80+
if err := merge(getEnvironmentVariables()); err != nil {
81+
return nil, err
9082
}
91-
return localVariables, nil
83+
if err := merge(call.Vars); err != nil {
84+
return nil, err
85+
}
86+
87+
return result, nil
9288
}
9389

9490
var templateFuncs template.FuncMap
@@ -152,10 +148,10 @@ func (e *Executor) ReplaceVariables(initial string, call Call) (string, error) {
152148
}
153149

154150
// GetEnvironmentVariables returns environment variables as map
155-
func getEnvironmentVariables() map[string]string {
151+
func getEnvironmentVariables() Vars {
156152
var (
157153
env = os.Environ()
158-
m = make(map[string]string, len(env))
154+
m = make(Vars, len(env))
159155
)
160156

161157
for _, e := range env {

0 commit comments

Comments
 (0)