Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ type Compiler struct {
}

func (c *Compiler) GetTaskfileVariables() (*ast.Vars, error) {
return c.getVariables(nil, nil, true)
return c.getVariables(nil, nil, true, map[string]string{})
}

func (c *Compiler) GetVariables(t *ast.Task, call *Call) (*ast.Vars, error) {
return c.getVariables(t, call, true)
return c.getVariables(t, call, true, map[string]string{})
}

func (c *Compiler) FastGetVariables(t *ast.Task, call *Call) (*ast.Vars, error) {
return c.getVariables(t, call, false)
func (c *Compiler) FastGetVariables(t *ast.Task, call *Call, overrideVars map[string]string) (*ast.Vars, error) {
return c.getVariables(t, call, false, overrideVars)
}

func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*ast.Vars, error) {
func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool, overrideVars map[string]string) (*ast.Vars, error) {
result := env.GetEnviron()
specialVars, err := c.getSpecialVars(t, call)
if err != nil {
Expand All @@ -56,6 +56,12 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*

getRangeFunc := func(dir string) func(k string, v ast.Var) error {
return func(k string, v ast.Var) error {
val, ok := overrideVars[k]

if ok {
v = ast.Var{Value: val}
}

cache := &templater.Cache{Vars: result}
// Replace values
newVar := templater.ReplaceVar(v, cache)
Expand Down
13 changes: 13 additions & 0 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type (
CacheExpiryDuration time.Duration
Watch bool
Verbose bool
Vars map[string]string
Silent bool
AssumeYes bool
AssumeTerm bool // Used for testing
Expand Down Expand Up @@ -274,14 +275,26 @@ func WithVerbose(verbose bool) ExecutorOption {
return &verboseOption{verbose}
}

func WithVars(vars map[string]string) ExecutorOption {
return &varsOption{vars}
}

type verboseOption struct {
verbose bool
}

type varsOption struct {
vars map[string]string
}

func (o *verboseOption) ApplyToExecutor(e *Executor) {
e.Verbose = o.verbose
}

func (o *varsOption) ApplyToExecutor(e *Executor) {
e.Vars = o.vars
}

// WithSilent tells the [Executor] to suppress all output except for the output
// of the tasks that are run.
func WithSilent(silent bool) ExecutorOption {
Expand Down
11 changes: 11 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ func TestVars(t *testing.T) {
)
}

func TestVarOverride(t *testing.T) {
t.Parallel()
NewExecutorTest(t,
WithExecutorOptions(
task.WithDir("testdata/var_override"),
task.WithSilent(true),
task.WithVars(map[string]string{"MYVAR": "bar"}),
),
)
}

func TestRequires(t *testing.T) {
t.Parallel()
NewExecutorTest(t,
Expand Down
3 changes: 3 additions & 0 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var (
ForceAll bool
Watch bool
Verbose bool
Vars map[string]string
Silent bool
AssumeYes bool
Dry bool
Expand Down Expand Up @@ -121,6 +122,7 @@ func init() {
pflag.BoolVar(&Insecure, "insecure", false, "Forces Task to download Taskfiles over insecure connections.")
pflag.BoolVarP(&Watch, "watch", "w", false, "Enables watch of the given task.")
pflag.BoolVarP(&Verbose, "verbose", "v", false, "Enables verbose mode.")
pflag.StringToStringVarP(&Vars, "var", "V", map[string]string{}, "Provide variable overrides")
pflag.BoolVarP(&Silent, "silent", "s", false, "Disables echoing.")
pflag.BoolVarP(&AssumeYes, "yes", "y", false, "Assume \"yes\" as answer to all prompts.")
pflag.BoolVarP(&Parallel, "parallel", "p", false, "Executes tasks provided on command line in parallel.")
Expand Down Expand Up @@ -238,6 +240,7 @@ func (o *flagsOption) ApplyToExecutor(e *task.Executor) {
task.WithCacheExpiryDuration(CacheExpiryDuration),
task.WithWatch(Watch),
task.WithVerbose(Verbose),
task.WithVars(Vars),
task.WithSilent(Silent),
task.WithAssumeYes(AssumeYes),
task.WithDry(Dry || Status),
Expand Down
2 changes: 1 addition & 1 deletion task.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (e *Executor) runCommand(ctx context.Context, t *ast.Task, call *Call, i in
if t.Interactive {
outputWrapper = output.Interleaved{}
}
vars, err := e.Compiler.FastGetVariables(t, call)
vars, err := e.Compiler.FastGetVariables(t, call, map[string]string{})
outputTemplater := &templater.Cache{Vars: vars}
if err != nil {
return fmt.Errorf("task: failed to get variables: %w", err)
Expand Down
9 changes: 9 additions & 0 deletions testdata/var_override/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3'

vars:
MYVAR: foo

tasks:
default:
cmds:
- echo "{{.MYVAR}}"
1 change: 1 addition & 0 deletions testdata/var_override/testdata/TestVarOverride.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
4 changes: 2 additions & 2 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (e *Executor) compiledTask(call *Call, evaluateShVars bool) (*ast.Task, err

var vars *ast.Vars
if evaluateShVars {
vars, err = e.Compiler.GetVariables(origTask, call)
vars, err = e.Compiler.getVariables(origTask, call, true, e.Vars)
} else {
vars, err = e.Compiler.FastGetVariables(origTask, call)
vars, err = e.Compiler.FastGetVariables(origTask, call, e.Vars)
}
if err != nil {
return nil, err
Expand Down
8 changes: 8 additions & 0 deletions website/src/docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ Disable command echoing.
task deploy --silent
```

#### `-V, --var <key>=<value>`

Provide variable overrides to override variables defined in the Taskfile.

```bash
task build --var FOO=bar
```

### Execution Control

#### `-f, --force`
Expand Down