Skip to content

Commit 6ff3c90

Browse files
On v3, treat all CLI variables as global variables
Closes #336 Ref #341 Co-authored-by: Egor Kovetskiy <[email protected]>
1 parent e28b82b commit 6ff3c90

File tree

5 files changed

+138
-14
lines changed

5 files changed

+138
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# v3.0.0 - Unreleased
44

5+
- On `v3`, all CLI variables will be considered global variables
6+
([#336](https://github.com/go-task/task/issues/336), [#341](https://github.com/go-task/task/pull/341))
57
- Add support to `.env` like files
68
([#324](https://github.com/go-task/task/issues/324), [#356](https://github.com/go-task/task/pull/356)).
79
- Add `label:` to task so you can override the task name in the logs

cmd/task/task.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/go-task/task/v3"
1313
"github.com/go-task/task/v3/internal/args"
1414
"github.com/go-task/task/v3/internal/logger"
15+
"github.com/go-task/task/v3/internal/taskfile"
1516

1617
"github.com/spf13/pflag"
1718
)
@@ -141,13 +142,26 @@ func main() {
141142
if err := e.Setup(); err != nil {
142143
log.Fatal(err)
143144
}
145+
v, err := e.Taskfile.ParsedVersion()
146+
if err != nil {
147+
log.Fatal(err)
148+
return
149+
}
144150

145151
if list {
146152
e.PrintTasksHelp()
147153
return
148154
}
149155

150-
calls, globals := args.Parse(pflag.Args()...)
156+
var (
157+
calls []taskfile.Call
158+
globals *taskfile.Vars
159+
)
160+
if v >= 3.0 {
161+
calls, globals = args.ParseV3(pflag.Args()...)
162+
} else {
163+
calls, globals = args.ParseV2(pflag.Args()...)
164+
}
151165
e.Taskfile.Vars.Merge(globals)
152166

153167
ctx := context.Background()

docs/usage.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -488,19 +488,11 @@ $ TASK_VARIABLE=a-value task do-something
488488

489489
Since some shells don't support above syntax to set environment variables
490490
(Windows) tasks also accepts a similar style when not in the beginning of
491-
the command. Variables given in this form are only visible to the task called
492-
right before.
493-
491+
the command.
494492
```bash
495493
$ task write-file FILE=file.txt "CONTENT=Hello, World!" print "MESSAGE=All done!"
496494
```
497495

498-
If you want to set global variables using this syntax, give it before any task:
499-
500-
```bash
501-
$ task OUTPUT=file.txt generate-file
502-
```
503-
504496
Example of locally declared vars:
505497

506498
```yaml

internal/args/args.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,33 @@ import (
66
"github.com/go-task/task/v3/internal/taskfile"
77
)
88

9-
// Parse parses command line argument: tasks and vars of each task
10-
func Parse(args ...string) ([]taskfile.Call, *taskfile.Vars) {
9+
// ParseV3 parses command line argument: tasks and global variables
10+
func ParseV3(args ...string) ([]taskfile.Call, *taskfile.Vars) {
11+
var calls []taskfile.Call
12+
var globals *taskfile.Vars
13+
14+
for _, arg := range args {
15+
if !strings.Contains(arg, "=") {
16+
calls = append(calls, taskfile.Call{Task: arg})
17+
continue
18+
}
19+
20+
if globals == nil {
21+
globals = &taskfile.Vars{}
22+
}
23+
name, value := splitVar(arg)
24+
globals.Set(name, taskfile.Var{Static: value})
25+
}
26+
27+
if len(calls) == 0 {
28+
calls = append(calls, taskfile.Call{Task: "default"})
29+
}
30+
31+
return calls, globals
32+
}
33+
34+
// ParseV2 parses command line argument: tasks and vars of each task
35+
func ParseV2(args ...string) ([]taskfile.Call, *taskfile.Vars) {
1136
var calls []taskfile.Call
1237
var globals *taskfile.Vars
1338

internal/args/args_test.go

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,98 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13-
func TestArgs(t *testing.T) {
13+
func TestArgsV3(t *testing.T) {
14+
tests := []struct {
15+
Args []string
16+
ExpectedCalls []taskfile.Call
17+
ExpectedGlobals *taskfile.Vars
18+
}{
19+
{
20+
Args: []string{"task-a", "task-b", "task-c"},
21+
ExpectedCalls: []taskfile.Call{
22+
{Task: "task-a"},
23+
{Task: "task-b"},
24+
{Task: "task-c"},
25+
},
26+
},
27+
{
28+
Args: []string{"task-a", "FOO=bar", "task-b", "task-c", "BAR=baz", "BAZ=foo"},
29+
ExpectedCalls: []taskfile.Call{
30+
{Task: "task-a"},
31+
{Task: "task-b"},
32+
{Task: "task-c"},
33+
},
34+
ExpectedGlobals: &taskfile.Vars{
35+
Keys: []string{"FOO", "BAR", "BAZ"},
36+
Mapping: map[string]taskfile.Var{
37+
"FOO": taskfile.Var{Static: "bar"},
38+
"BAR": taskfile.Var{Static: "baz"},
39+
"BAZ": taskfile.Var{Static: "foo"},
40+
},
41+
},
42+
},
43+
{
44+
Args: []string{"task-a", "CONTENT=with some spaces"},
45+
ExpectedCalls: []taskfile.Call{
46+
{Task: "task-a"},
47+
},
48+
ExpectedGlobals: &taskfile.Vars{
49+
Keys: []string{"CONTENT"},
50+
Mapping: map[string]taskfile.Var{
51+
"CONTENT": taskfile.Var{Static: "with some spaces"},
52+
},
53+
},
54+
},
55+
{
56+
Args: []string{"FOO=bar", "task-a", "task-b"},
57+
ExpectedCalls: []taskfile.Call{
58+
{Task: "task-a"},
59+
{Task: "task-b"},
60+
},
61+
ExpectedGlobals: &taskfile.Vars{
62+
Keys: []string{"FOO"},
63+
Mapping: map[string]taskfile.Var{
64+
"FOO": {Static: "bar"},
65+
},
66+
},
67+
},
68+
{
69+
Args: nil,
70+
ExpectedCalls: []taskfile.Call{
71+
{Task: "default"},
72+
},
73+
},
74+
{
75+
Args: []string{},
76+
ExpectedCalls: []taskfile.Call{
77+
{Task: "default"},
78+
},
79+
},
80+
{
81+
Args: []string{"FOO=bar", "BAR=baz"},
82+
ExpectedCalls: []taskfile.Call{
83+
{Task: "default"},
84+
},
85+
ExpectedGlobals: &taskfile.Vars{
86+
Keys: []string{"FOO", "BAR"},
87+
Mapping: map[string]taskfile.Var{
88+
"FOO": {Static: "bar"},
89+
"BAR": {Static: "baz"},
90+
},
91+
},
92+
},
93+
}
94+
95+
for i, test := range tests {
96+
t.Run(fmt.Sprintf("TestArgs%d", i+1), func(t *testing.T) {
97+
calls, globals := args.ParseV3(test.Args...)
98+
assert.Equal(t, test.ExpectedCalls, calls)
99+
assert.Equal(t, test.ExpectedGlobals, globals)
100+
})
101+
}
102+
}
103+
104+
func TestArgsV2(t *testing.T) {
14105
tests := []struct {
15106
Args []string
16107
ExpectedCalls []taskfile.Call
@@ -105,7 +196,7 @@ func TestArgs(t *testing.T) {
105196

106197
for i, test := range tests {
107198
t.Run(fmt.Sprintf("TestArgs%d", i+1), func(t *testing.T) {
108-
calls, globals := args.Parse(test.Args...)
199+
calls, globals := args.ParseV2(test.Args...)
109200
assert.Equal(t, test.ExpectedCalls, calls)
110201
assert.Equal(t, test.ExpectedGlobals, globals)
111202
})

0 commit comments

Comments
 (0)