Skip to content

Commit f0768b3

Browse files
committed
Allow setting global variables through the CLI
Closes #192
1 parent 0233ce5 commit f0768b3

File tree

5 files changed

+55
-31
lines changed

5 files changed

+55
-31
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Allow setting global variables through the CLI
6+
([#192](https://github.com/go-task/task/issues/192)).
7+
38
## 2.5.1 - 2019-04-27
49

510
- Fixed some issues with interactive command line tools, where sometimes

cmd/task/task.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ func main() {
121121
arguments = []string{"default"}
122122
}
123123

124-
calls, err := args.Parse(arguments...)
125-
if err != nil {
126-
log.Fatal(err)
124+
calls, globals := args.Parse(arguments...)
125+
for name, value := range globals {
126+
e.Taskfile.Vars[name] = value
127127
}
128128

129129
ctx := context.Background()
@@ -132,7 +132,7 @@ func main() {
132132
}
133133

134134
if status {
135-
if err = e.Status(ctx, calls...); err != nil {
135+
if err := e.Status(ctx, calls...); err != nil {
136136
log.Fatal(err)
137137
}
138138
return

docs/usage.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ right before.
371371
$ task write-file FILE=file.txt "CONTENT=Hello, World!" print "MESSAGE=All done!"
372372
```
373373

374+
If you want to set global variables using this syntax, give it before any task:
375+
376+
```bash
377+
$ task OUTPUT=file.txt generate-file
378+
```
379+
374380
Example of locally declared vars:
375381

376382
```yaml
@@ -582,7 +588,7 @@ dependencies:
582588
commands:
583589
- your-release-tool
584590
```
585-
If a summary is missing, the description will be printed.
591+
If a summary is missing, the description will be printed.
586592
If the task does not have a summary or a description, a warning is printed.
587593

588594
Please note: *showing the summary will not execute the command*.

internal/args/args.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
package args
22

33
import (
4-
"errors"
54
"strings"
65

76
"github.com/go-task/task/v2/internal/taskfile"
87
)
98

10-
var (
11-
// ErrVariableWithoutTask is returned when variables are given before any task
12-
ErrVariableWithoutTask = errors.New("task: variable given before any task")
13-
)
14-
159
// Parse parses command line argument: tasks and vars of each task
16-
func Parse(args ...string) ([]taskfile.Call, error) {
10+
func Parse(args ...string) ([]taskfile.Call, taskfile.Vars) {
1711
var calls []taskfile.Call
12+
var globals taskfile.Vars
1813

1914
for _, arg := range args {
2015
if !strings.Contains(arg, "=") {
2116
calls = append(calls, taskfile.Call{Task: arg})
2217
continue
2318
}
19+
2420
if len(calls) < 1 {
25-
return nil, ErrVariableWithoutTask
26-
}
21+
if globals == nil {
22+
globals = taskfile.Vars{}
23+
}
2724

28-
if calls[len(calls)-1].Vars == nil {
29-
calls[len(calls)-1].Vars = make(taskfile.Vars)
30-
}
25+
name, value := splitVar(arg)
26+
globals[name] = taskfile.Var{Static: value}
27+
} else {
28+
if calls[len(calls)-1].Vars == nil {
29+
calls[len(calls)-1].Vars = make(taskfile.Vars)
30+
}
3131

32-
pair := strings.SplitN(arg, "=", 2)
33-
calls[len(calls)-1].Vars[pair[0]] = taskfile.Var{Static: pair[1]}
32+
name, value := splitVar((arg))
33+
calls[len(calls)-1].Vars[name] = taskfile.Var{Static: value}
34+
}
3435
}
35-
return calls, nil
36+
37+
return calls, globals
38+
}
39+
40+
func splitVar(s string) (string, string) {
41+
pair := strings.SplitN(s, "=", 2)
42+
return pair[0], pair[1]
3643
}

internal/args/args_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ import (
1212

1313
func TestArgs(t *testing.T) {
1414
tests := []struct {
15-
Args []string
16-
Expected []taskfile.Call
17-
Err error
15+
Args []string
16+
ExpectedCalls []taskfile.Call
17+
ExpectedGlobals taskfile.Vars
1818
}{
1919
{
2020
Args: []string{"task-a", "task-b", "task-c"},
21-
Expected: []taskfile.Call{
21+
ExpectedCalls: []taskfile.Call{
2222
{Task: "task-a"},
2323
{Task: "task-b"},
2424
{Task: "task-c"},
2525
},
2626
},
2727
{
2828
Args: []string{"task-a", "FOO=bar", "task-b", "task-c", "BAR=baz", "BAZ=foo"},
29-
Expected: []taskfile.Call{
29+
ExpectedCalls: []taskfile.Call{
3030
{
3131
Task: "task-a",
3232
Vars: taskfile.Vars{
@@ -45,7 +45,7 @@ func TestArgs(t *testing.T) {
4545
},
4646
{
4747
Args: []string{"task-a", "CONTENT=with some spaces"},
48-
Expected: []taskfile.Call{
48+
ExpectedCalls: []taskfile.Call{
4949
{
5050
Task: "task-a",
5151
Vars: taskfile.Vars{
@@ -55,16 +55,22 @@ func TestArgs(t *testing.T) {
5555
},
5656
},
5757
{
58-
Args: []string{"FOO=bar", "task-a"},
59-
Err: args.ErrVariableWithoutTask,
58+
Args: []string{"FOO=bar", "task-a", "task-b"},
59+
ExpectedCalls: []taskfile.Call{
60+
{Task: "task-a"},
61+
{Task: "task-b"},
62+
},
63+
ExpectedGlobals: taskfile.Vars{
64+
"FOO": {Static: "bar"},
65+
},
6066
},
6167
}
6268

6369
for i, test := range tests {
6470
t.Run(fmt.Sprintf("TestArgs%d", i+1), func(t *testing.T) {
65-
calls, err := args.Parse(test.Args...)
66-
assert.Equal(t, test.Err, err)
67-
assert.Equal(t, test.Expected, calls)
71+
calls, globals := args.Parse(test.Args...)
72+
assert.Equal(t, test.ExpectedCalls, calls)
73+
assert.Equal(t, test.ExpectedGlobals, globals)
6874
})
6975
}
7076
}

0 commit comments

Comments
 (0)