mirror of
https://github.com/go-task/task.git
synced 2025-07-15 01:35:00 +02:00
Add splitArgs
template function (#1059)
This commit is contained in:
@ -1,5 +1,10 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
- Added new `splitArgs` to the template system
|
||||||
|
(`{{splitArgs "foo bar 'foo bar baz'"}}`) to ensure string is splitted as arguments not whitespaces
|
||||||
|
|
||||||
## v3.22.0 - 2023-03-10
|
## v3.22.0 - 2023-03-10
|
||||||
|
|
||||||
- Add a brand new `--global` (`-g`) flag that will run a Taskfile from your
|
- Add a brand new `--global` (`-g`) flag that will run a Taskfile from your
|
||||||
|
@ -1046,6 +1046,8 @@ Task also adds the following functions:
|
|||||||
- `shellQuote`: Quotes a string to make it safe for use in shell scripts.
|
- `shellQuote`: Quotes a string to make it safe for use in shell scripts.
|
||||||
Task uses [this Go function](https://pkg.go.dev/mvdan.cc/sh/v3@v3.4.0/syntax#Quote)
|
Task uses [this Go function](https://pkg.go.dev/mvdan.cc/sh/v3@v3.4.0/syntax#Quote)
|
||||||
for this. The Bash dialect is assumed.
|
for this. The Bash dialect is assumed.
|
||||||
|
- `splitArgs`: Splits a string as if it were a command's arguments.
|
||||||
|
Task uses [this Go function](https://pkg.go.dev/mvdan.cc/sh/v3@v3.4.0/shell#Fields)
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
sprig "github.com/go-task/slim-sprig"
|
sprig "github.com/go-task/slim-sprig"
|
||||||
|
"mvdan.cc/sh/v3/shell"
|
||||||
"mvdan.cc/sh/v3/syntax"
|
"mvdan.cc/sh/v3/syntax"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -41,6 +42,9 @@ func init() {
|
|||||||
"shellQuote": func(str string) (string, error) {
|
"shellQuote": func(str string) (string, error) {
|
||||||
return syntax.Quote(str, syntax.LangBash)
|
return syntax.Quote(str, syntax.LangBash)
|
||||||
},
|
},
|
||||||
|
"splitArgs": func(s string) ([]string, error) {
|
||||||
|
return shell.Fields(s, nil)
|
||||||
|
},
|
||||||
// IsSH is deprecated.
|
// IsSH is deprecated.
|
||||||
"IsSH": func() bool { return true },
|
"IsSH": func() bool { return true },
|
||||||
}
|
}
|
||||||
|
18
task_test.go
18
task_test.go
@ -1832,3 +1832,21 @@ func TestBashShellOptsCommandLevel(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "globstar\ton\n", buff.String())
|
assert.Equal(t, "globstar\ton\n", buff.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSplitArgs(t *testing.T) {
|
||||||
|
var buff bytes.Buffer
|
||||||
|
e := task.Executor{
|
||||||
|
Dir: "testdata/split_args",
|
||||||
|
Stdout: &buff,
|
||||||
|
Stderr: &buff,
|
||||||
|
Silent: true,
|
||||||
|
}
|
||||||
|
assert.NoError(t, e.Setup())
|
||||||
|
|
||||||
|
vars := &taskfile.Vars{}
|
||||||
|
vars.Set("CLI_ARGS", taskfile.Var{Static: "foo bar 'foo bar baz'"})
|
||||||
|
|
||||||
|
err := e.Run(context.Background(), taskfile.Call{Task: "default", Vars: vars})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "3\n", buff.String())
|
||||||
|
}
|
||||||
|
6
testdata/split_args/Taskfile.yml
vendored
Normal file
6
testdata/split_args/Taskfile.yml
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
default:
|
||||||
|
cmds:
|
||||||
|
- cmd: echo '{{splitArgs .CLI_ARGS | len}}'
|
Reference in New Issue
Block a user