1
0
mirror of https://github.com/go-task/task.git synced 2025-01-08 04:04:08 +02:00

Allow setting global variables through the CLI

Closes #192
This commit is contained in:
Andrey Nering 2019-05-11 11:06:47 -03:00
parent 0233ce52ed
commit f0768b3af1
5 changed files with 55 additions and 31 deletions

View File

@ -1,5 +1,10 @@
# Changelog # Changelog
## Unreleased
- Allow setting global variables through the CLI
([#192](https://github.com/go-task/task/issues/192)).
## 2.5.1 - 2019-04-27 ## 2.5.1 - 2019-04-27
- Fixed some issues with interactive command line tools, where sometimes - Fixed some issues with interactive command line tools, where sometimes

View File

@ -121,9 +121,9 @@ func main() {
arguments = []string{"default"} arguments = []string{"default"}
} }
calls, err := args.Parse(arguments...) calls, globals := args.Parse(arguments...)
if err != nil { for name, value := range globals {
log.Fatal(err) e.Taskfile.Vars[name] = value
} }
ctx := context.Background() ctx := context.Background()
@ -132,7 +132,7 @@ func main() {
} }
if status { if status {
if err = e.Status(ctx, calls...); err != nil { if err := e.Status(ctx, calls...); err != nil {
log.Fatal(err) log.Fatal(err)
} }
return return

View File

@ -371,6 +371,12 @@ right before.
$ 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

View File

@ -1,36 +1,43 @@
package args package args
import ( import (
"errors"
"strings" "strings"
"github.com/go-task/task/v2/internal/taskfile" "github.com/go-task/task/v2/internal/taskfile"
) )
var (
// ErrVariableWithoutTask is returned when variables are given before any task
ErrVariableWithoutTask = errors.New("task: variable given before any task")
)
// Parse parses command line argument: tasks and vars of each task // Parse parses command line argument: tasks and vars of each task
func Parse(args ...string) ([]taskfile.Call, error) { func Parse(args ...string) ([]taskfile.Call, taskfile.Vars) {
var calls []taskfile.Call var calls []taskfile.Call
var globals taskfile.Vars
for _, arg := range args { for _, arg := range args {
if !strings.Contains(arg, "=") { if !strings.Contains(arg, "=") {
calls = append(calls, taskfile.Call{Task: arg}) calls = append(calls, taskfile.Call{Task: arg})
continue continue
} }
if len(calls) < 1 { if len(calls) < 1 {
return nil, ErrVariableWithoutTask if globals == nil {
} globals = taskfile.Vars{}
}
if calls[len(calls)-1].Vars == nil { name, value := splitVar(arg)
calls[len(calls)-1].Vars = make(taskfile.Vars) globals[name] = taskfile.Var{Static: value}
} } else {
if calls[len(calls)-1].Vars == nil {
calls[len(calls)-1].Vars = make(taskfile.Vars)
}
pair := strings.SplitN(arg, "=", 2) name, value := splitVar((arg))
calls[len(calls)-1].Vars[pair[0]] = taskfile.Var{Static: pair[1]} calls[len(calls)-1].Vars[name] = taskfile.Var{Static: value}
}
} }
return calls, nil
return calls, globals
}
func splitVar(s string) (string, string) {
pair := strings.SplitN(s, "=", 2)
return pair[0], pair[1]
} }

View File

@ -12,13 +12,13 @@ import (
func TestArgs(t *testing.T) { func TestArgs(t *testing.T) {
tests := []struct { tests := []struct {
Args []string Args []string
Expected []taskfile.Call ExpectedCalls []taskfile.Call
Err error ExpectedGlobals taskfile.Vars
}{ }{
{ {
Args: []string{"task-a", "task-b", "task-c"}, Args: []string{"task-a", "task-b", "task-c"},
Expected: []taskfile.Call{ ExpectedCalls: []taskfile.Call{
{Task: "task-a"}, {Task: "task-a"},
{Task: "task-b"}, {Task: "task-b"},
{Task: "task-c"}, {Task: "task-c"},
@ -26,7 +26,7 @@ func TestArgs(t *testing.T) {
}, },
{ {
Args: []string{"task-a", "FOO=bar", "task-b", "task-c", "BAR=baz", "BAZ=foo"}, Args: []string{"task-a", "FOO=bar", "task-b", "task-c", "BAR=baz", "BAZ=foo"},
Expected: []taskfile.Call{ ExpectedCalls: []taskfile.Call{
{ {
Task: "task-a", Task: "task-a",
Vars: taskfile.Vars{ Vars: taskfile.Vars{
@ -45,7 +45,7 @@ func TestArgs(t *testing.T) {
}, },
{ {
Args: []string{"task-a", "CONTENT=with some spaces"}, Args: []string{"task-a", "CONTENT=with some spaces"},
Expected: []taskfile.Call{ ExpectedCalls: []taskfile.Call{
{ {
Task: "task-a", Task: "task-a",
Vars: taskfile.Vars{ Vars: taskfile.Vars{
@ -55,16 +55,22 @@ func TestArgs(t *testing.T) {
}, },
}, },
{ {
Args: []string{"FOO=bar", "task-a"}, Args: []string{"FOO=bar", "task-a", "task-b"},
Err: args.ErrVariableWithoutTask, ExpectedCalls: []taskfile.Call{
{Task: "task-a"},
{Task: "task-b"},
},
ExpectedGlobals: taskfile.Vars{
"FOO": {Static: "bar"},
},
}, },
} }
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, err := args.Parse(test.Args...) calls, globals := args.Parse(test.Args...)
assert.Equal(t, test.Err, err) assert.Equal(t, test.ExpectedCalls, calls)
assert.Equal(t, test.Expected, calls) assert.Equal(t, test.ExpectedGlobals, globals)
}) })
} }
} }