Skip to content

feat: recursive config search #2166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .taskrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
experiments:
GENTLE_FORCE: 0
REMOTE_TASKFILES: 0
ENV_PRECEDENCE: 0
26 changes: 26 additions & 0 deletions args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,36 @@ package args
import (
"strings"

"github.com/spf13/pflag"
"mvdan.cc/sh/v3/syntax"

"github.com/go-task/task/v3"
"github.com/go-task/task/v3/taskfile/ast"
)

// Get fetches the remaining arguments after CLI parsing and splits them into
// two groups: the arguments before the double dash (--) and the arguments after
// the double dash.
func Get() ([]string, []string, error) {
args := pflag.Args()
doubleDashPos := pflag.CommandLine.ArgsLenAtDash()

if doubleDashPos == -1 {
return args, nil, nil
}

var quotedCliArgs []string
for _, arg := range args[doubleDashPos:] {
quotedCliArg, err := syntax.Quote(arg, syntax.LangBash)
if err != nil {
return nil, nil, err
}
quotedCliArgs = append(quotedCliArgs, quotedCliArg)
}

return args[:doubleDashPos], quotedCliArgs, nil
}

// Parse parses command line argument: tasks and global variables
func Parse(args ...string) ([]*task.Call, *ast.Vars) {
calls := []*task.Call{}
Expand Down
36 changes: 4 additions & 32 deletions cmd/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/pflag"
"mvdan.cc/sh/v3/syntax"

"github.com/go-task/task/v3"
"github.com/go-task/task/v3/args"
Expand Down Expand Up @@ -75,7 +73,7 @@ func run() error {
if err != nil {
return err
}
args, _, err := getArgs()
_, args, err := args.Get()
if err != nil {
return err
}
Expand Down Expand Up @@ -155,17 +153,12 @@ func run() error {
return nil
}

var (
calls []*task.Call
globals *ast.Vars
)

tasksAndVars, cliArgs, err := getArgs()
// Parse the remaining arguments
argv, cliArgs, err := args.Get()
if err != nil {
return err
}

calls, globals = args.Parse(tasksAndVars...)
calls, globals := args.Parse(argv...)

// If there are no calls, run the default task instead
if len(calls) == 0 {
Expand All @@ -191,24 +184,3 @@ func run() error {

return e.Run(ctx, calls...)
}

func getArgs() ([]string, string, error) {
var (
args = pflag.Args()
doubleDashPos = pflag.CommandLine.ArgsLenAtDash()
)

if doubleDashPos == -1 {
return args, "", nil
}

var quotedCliArgs []string
for _, arg := range args[doubleDashPos:] {
quotedCliArg, err := syntax.Quote(arg, syntax.LangBash)
if err != nil {
return nil, "", err
}
quotedCliArgs = append(quotedCliArgs, quotedCliArg)
}
return args[:doubleDashPos], strings.Join(quotedCliArgs, " "), nil
}
5 changes: 5 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const (
CodeUnknown // Used when no other exit code is appropriate
)

// TaskRC related exit codes
const (
CodeTaskRCNotFoundError int = iota + 50
)

// Taskfile related exit codes
const (
CodeTaskfileNotFound int = iota + 100
Expand Down
20 changes: 20 additions & 0 deletions errors/errors_taskrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package errors

import "fmt"

type TaskRCNotFoundError struct {
URI string
Walk bool
}

func (err TaskRCNotFoundError) Error() string {
var walkText string
if err.Walk {
walkText = " (or any of the parent directories)"
}
return fmt.Sprintf(`task: No Task config file found at %q%s`, err.URI, walkText)
}

func (err TaskRCNotFoundError) Code() int {
return CodeTaskRCNotFoundError
}
9 changes: 7 additions & 2 deletions internal/experiments/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"slices"
"strconv"

"github.com/go-task/task/v3/taskrc/ast"
)

type Experiment struct {
Expand All @@ -14,8 +16,11 @@ type Experiment struct {

// New creates a new experiment with the given name and sets the values that can
// enable it.
func New(xName string, allowedValues ...int) Experiment {
value := experimentConfig.Experiments[xName]
func New(xName string, config *ast.TaskRC, allowedValues ...int) Experiment {
var value int
if config != nil {
value = config.Experiments[xName]
}

if value == 0 {
value, _ = strconv.Atoi(getEnv(xName))
Expand Down
87 changes: 76 additions & 11 deletions internal/experiments/experiment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/go-task/task/v3/internal/experiments"
"github.com/go-task/task/v3/taskrc/ast"
)

func TestNew(t *testing.T) {
Expand All @@ -16,60 +17,124 @@ func TestNew(t *testing.T) {
)
tests := []struct {
name string
config *ast.TaskRC
allowedValues []int
value int
env int
wantEnabled bool
wantActive bool
wantValid error
wantValue int
}{
{
name: `[] allowed, value=""`,
name: `[] allowed, env=""`,
wantEnabled: false,
wantActive: false,
},
{
name: `[] allowed, value="1"`,
value: 1,
name: `[] allowed, env="1"`,
env: 1,
wantEnabled: false,
wantActive: false,
wantValid: &experiments.InactiveError{
Name: exampleExperiment,
},
wantValue: 1,
},
{
name: `[1] allowed, value=""`,
name: `[1] allowed, env=""`,
allowedValues: []int{1},
wantEnabled: false,
wantActive: true,
},
{
name: `[1] allowed, value="1"`,
name: `[1] allowed, env="1"`,
allowedValues: []int{1},
value: 1,
env: 1,
wantEnabled: true,
wantActive: true,
wantValue: 1,
},
{
name: `[1] allowed, value="2"`,
name: `[1] allowed, env="2"`,
allowedValues: []int{1},
value: 2,
env: 2,
wantEnabled: false,
wantActive: true,
wantValid: &experiments.InvalidValueError{
Name: exampleExperiment,
AllowedValues: []int{1},
Value: 2,
},
wantValue: 2,
},
{
name: `[1, 2] allowed, env="1"`,
allowedValues: []int{1, 2},
env: 1,
wantEnabled: true,
wantActive: true,
wantValue: 1,
},
{
name: `[1, 2] allowed, env="1"`,
allowedValues: []int{1, 2},
env: 2,
wantEnabled: true,
wantActive: true,
wantValue: 2,
},
{
name: `[1] allowed, config="1"`,
config: &ast.TaskRC{
Experiments: map[string]int{
exampleExperiment: 1,
},
},
allowedValues: []int{1},
wantEnabled: true,
wantActive: true,
wantValue: 1,
},
{
name: `[1] allowed, config="2"`,
config: &ast.TaskRC{
Experiments: map[string]int{
exampleExperiment: 2,
},
},
allowedValues: []int{1},
wantEnabled: false,
wantActive: true,
wantValid: &experiments.InvalidValueError{
Name: exampleExperiment,
AllowedValues: []int{1},
Value: 2,
},
wantValue: 2,
},
{
name: `[1, 2] allowed, env="1", config="2"`,
config: &ast.TaskRC{
Experiments: map[string]int{
exampleExperiment: 2,
},
},
allowedValues: []int{1, 2},
env: 1,
wantEnabled: true,
wantActive: true,
wantValue: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv(exampleExperimentEnv, strconv.Itoa(tt.value))
x := experiments.New(exampleExperiment, tt.allowedValues...)
t.Setenv(exampleExperimentEnv, strconv.Itoa(tt.env))
x := experiments.New(exampleExperiment, tt.config, tt.allowedValues...)
assert.Equal(t, exampleExperiment, x.Name)
assert.Equal(t, tt.wantEnabled, x.Enabled())
assert.Equal(t, tt.wantActive, x.Active())
assert.Equal(t, tt.wantValid, x.Valid())
assert.Equal(t, tt.wantValue, x.Value)
})
}
}
Loading
Loading