mirror of
https://github.com/go-task/task.git
synced 2025-12-16 19:57:43 +01:00
On v3, treat all CLI variables as global variables
Closes #336 Ref #341 Co-authored-by: Egor Kovetskiy <e.kovetskiy@gmail.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
# v3.0.0 - Unreleased
|
# v3.0.0 - Unreleased
|
||||||
|
|
||||||
|
- On `v3`, all CLI variables will be considered global variables
|
||||||
|
([#336](https://github.com/go-task/task/issues/336), [#341](https://github.com/go-task/task/pull/341))
|
||||||
- Add support to `.env` like files
|
- Add support to `.env` like files
|
||||||
([#324](https://github.com/go-task/task/issues/324), [#356](https://github.com/go-task/task/pull/356)).
|
([#324](https://github.com/go-task/task/issues/324), [#356](https://github.com/go-task/task/pull/356)).
|
||||||
- Add `label:` to task so you can override the task name in the logs
|
- Add `label:` to task so you can override the task name in the logs
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/go-task/task/v3"
|
"github.com/go-task/task/v3"
|
||||||
"github.com/go-task/task/v3/internal/args"
|
"github.com/go-task/task/v3/internal/args"
|
||||||
"github.com/go-task/task/v3/internal/logger"
|
"github.com/go-task/task/v3/internal/logger"
|
||||||
|
"github.com/go-task/task/v3/internal/taskfile"
|
||||||
|
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
@@ -141,13 +142,26 @@ func main() {
|
|||||||
if err := e.Setup(); err != nil {
|
if err := e.Setup(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
v, err := e.Taskfile.ParsedVersion()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if list {
|
if list {
|
||||||
e.PrintTasksHelp()
|
e.PrintTasksHelp()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
calls, globals := args.Parse(pflag.Args()...)
|
var (
|
||||||
|
calls []taskfile.Call
|
||||||
|
globals *taskfile.Vars
|
||||||
|
)
|
||||||
|
if v >= 3.0 {
|
||||||
|
calls, globals = args.ParseV3(pflag.Args()...)
|
||||||
|
} else {
|
||||||
|
calls, globals = args.ParseV2(pflag.Args()...)
|
||||||
|
}
|
||||||
e.Taskfile.Vars.Merge(globals)
|
e.Taskfile.Vars.Merge(globals)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|||||||
@@ -488,19 +488,11 @@ $ TASK_VARIABLE=a-value task do-something
|
|||||||
|
|
||||||
Since some shells don't support above syntax to set environment variables
|
Since some shells don't support above syntax to set environment variables
|
||||||
(Windows) tasks also accepts a similar style when not in the beginning of
|
(Windows) tasks also accepts a similar style when not in the beginning of
|
||||||
the command. Variables given in this form are only visible to the task called
|
the command.
|
||||||
right before.
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ task write-file FILE=file.txt "CONTENT=Hello, World!" print "MESSAGE=All done!"
|
$ task write-file FILE=file.txt "CONTENT=Hello, World!" print "MESSAGE=All done!"
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to set global variables using this syntax, give it before any task:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ task OUTPUT=file.txt generate-file
|
|
||||||
```
|
|
||||||
|
|
||||||
Example of locally declared vars:
|
Example of locally declared vars:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
@@ -6,8 +6,33 @@ import (
|
|||||||
"github.com/go-task/task/v3/internal/taskfile"
|
"github.com/go-task/task/v3/internal/taskfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse parses command line argument: tasks and vars of each task
|
// ParseV3 parses command line argument: tasks and global variables
|
||||||
func Parse(args ...string) ([]taskfile.Call, *taskfile.Vars) {
|
func ParseV3(args ...string) ([]taskfile.Call, *taskfile.Vars) {
|
||||||
|
var calls []taskfile.Call
|
||||||
|
var globals *taskfile.Vars
|
||||||
|
|
||||||
|
for _, arg := range args {
|
||||||
|
if !strings.Contains(arg, "=") {
|
||||||
|
calls = append(calls, taskfile.Call{Task: arg})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if globals == nil {
|
||||||
|
globals = &taskfile.Vars{}
|
||||||
|
}
|
||||||
|
name, value := splitVar(arg)
|
||||||
|
globals.Set(name, taskfile.Var{Static: value})
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(calls) == 0 {
|
||||||
|
calls = append(calls, taskfile.Call{Task: "default"})
|
||||||
|
}
|
||||||
|
|
||||||
|
return calls, globals
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseV2 parses command line argument: tasks and vars of each task
|
||||||
|
func ParseV2(args ...string) ([]taskfile.Call, *taskfile.Vars) {
|
||||||
var calls []taskfile.Call
|
var calls []taskfile.Call
|
||||||
var globals *taskfile.Vars
|
var globals *taskfile.Vars
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,98 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestArgs(t *testing.T) {
|
func TestArgsV3(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
Args []string
|
||||||
|
ExpectedCalls []taskfile.Call
|
||||||
|
ExpectedGlobals *taskfile.Vars
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Args: []string{"task-a", "task-b", "task-c"},
|
||||||
|
ExpectedCalls: []taskfile.Call{
|
||||||
|
{Task: "task-a"},
|
||||||
|
{Task: "task-b"},
|
||||||
|
{Task: "task-c"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Args: []string{"task-a", "FOO=bar", "task-b", "task-c", "BAR=baz", "BAZ=foo"},
|
||||||
|
ExpectedCalls: []taskfile.Call{
|
||||||
|
{Task: "task-a"},
|
||||||
|
{Task: "task-b"},
|
||||||
|
{Task: "task-c"},
|
||||||
|
},
|
||||||
|
ExpectedGlobals: &taskfile.Vars{
|
||||||
|
Keys: []string{"FOO", "BAR", "BAZ"},
|
||||||
|
Mapping: map[string]taskfile.Var{
|
||||||
|
"FOO": taskfile.Var{Static: "bar"},
|
||||||
|
"BAR": taskfile.Var{Static: "baz"},
|
||||||
|
"BAZ": taskfile.Var{Static: "foo"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Args: []string{"task-a", "CONTENT=with some spaces"},
|
||||||
|
ExpectedCalls: []taskfile.Call{
|
||||||
|
{Task: "task-a"},
|
||||||
|
},
|
||||||
|
ExpectedGlobals: &taskfile.Vars{
|
||||||
|
Keys: []string{"CONTENT"},
|
||||||
|
Mapping: map[string]taskfile.Var{
|
||||||
|
"CONTENT": taskfile.Var{Static: "with some spaces"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Args: []string{"FOO=bar", "task-a", "task-b"},
|
||||||
|
ExpectedCalls: []taskfile.Call{
|
||||||
|
{Task: "task-a"},
|
||||||
|
{Task: "task-b"},
|
||||||
|
},
|
||||||
|
ExpectedGlobals: &taskfile.Vars{
|
||||||
|
Keys: []string{"FOO"},
|
||||||
|
Mapping: map[string]taskfile.Var{
|
||||||
|
"FOO": {Static: "bar"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Args: nil,
|
||||||
|
ExpectedCalls: []taskfile.Call{
|
||||||
|
{Task: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Args: []string{},
|
||||||
|
ExpectedCalls: []taskfile.Call{
|
||||||
|
{Task: "default"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Args: []string{"FOO=bar", "BAR=baz"},
|
||||||
|
ExpectedCalls: []taskfile.Call{
|
||||||
|
{Task: "default"},
|
||||||
|
},
|
||||||
|
ExpectedGlobals: &taskfile.Vars{
|
||||||
|
Keys: []string{"FOO", "BAR"},
|
||||||
|
Mapping: map[string]taskfile.Var{
|
||||||
|
"FOO": {Static: "bar"},
|
||||||
|
"BAR": {Static: "baz"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
t.Run(fmt.Sprintf("TestArgs%d", i+1), func(t *testing.T) {
|
||||||
|
calls, globals := args.ParseV3(test.Args...)
|
||||||
|
assert.Equal(t, test.ExpectedCalls, calls)
|
||||||
|
assert.Equal(t, test.ExpectedGlobals, globals)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestArgsV2(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
Args []string
|
Args []string
|
||||||
ExpectedCalls []taskfile.Call
|
ExpectedCalls []taskfile.Call
|
||||||
@@ -105,7 +196,7 @@ func TestArgs(t *testing.T) {
|
|||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
t.Run(fmt.Sprintf("TestArgs%d", i+1), func(t *testing.T) {
|
t.Run(fmt.Sprintf("TestArgs%d", i+1), func(t *testing.T) {
|
||||||
calls, globals := args.Parse(test.Args...)
|
calls, globals := args.ParseV2(test.Args...)
|
||||||
assert.Equal(t, test.ExpectedCalls, calls)
|
assert.Equal(t, test.ExpectedCalls, calls)
|
||||||
assert.Equal(t, test.ExpectedGlobals, globals)
|
assert.Equal(t, test.ExpectedGlobals, globals)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user