Skip to content
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

feat: add a new .taskrc.yml to enable experiments #1982

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
47 changes: 40 additions & 7 deletions internal/experiments/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ import (
"slices"
"strings"

"gopkg.in/yaml.v3"

"github.com/Ladicle/tabwriter"
"github.com/joho/godotenv"
"github.com/spf13/pflag"

"github.com/go-task/task/v3/internal/logger"
)

const envPrefix = "TASK_X_"
const (
envPrefix = "TASK_X_"
defaultConfigFilename = ".task-experiments.yml"
vmaerten marked this conversation as resolved.
Show resolved Hide resolved
)

type ExperimentConfigFile struct {
vmaerten marked this conversation as resolved.
Show resolved Hide resolved
Experiments map[string]string `yaml:",inline"`
}

type Experiment struct {
Name string
Expand All @@ -32,8 +41,11 @@ var (
EnvPrecedence Experiment
)

var ExperimentConfig ExperimentConfigFile
vmaerten marked this conversation as resolved.
Show resolved Hide resolved

func init() {
readDotEnv()
ExperimentConfig = readConfig()
GentleForce = New("GENTLE_FORCE")
RemoteTaskfiles = New("REMOTE_TASKFILES")
AnyVariables = New("ANY_VARIABLES", "1", "2")
Expand All @@ -45,7 +57,13 @@ func New(xName string, enabledValues ...string) Experiment {
if len(enabledValues) == 0 {
enabledValues = []string{"1"}
}
value := getEnv(xName)

value := ExperimentConfig.Experiments[xName]

if value == "" {
value = getEnv(xName)
}

return Experiment{
Name: xName,
Enabled: slices.Contains(enabledValues, value),
Expand All @@ -65,7 +83,7 @@ func getEnv(xName string) string {
return os.Getenv(envName)
}

func getEnvFilePath() string {
func getFilePath(filename string) string {
// Parse the CLI flags again to get the directory/taskfile being run
// We use a flagset here so that we can parse a subset of flags without exiting on error.
var dir, taskfile string
Expand All @@ -76,18 +94,18 @@ func getEnvFilePath() string {
_ = fs.Parse(os.Args[1:])
// If the directory is set, find a .env file in that directory.
if dir != "" {
return filepath.Join(dir, ".env")
return filepath.Join(dir, filename)
}
// If the taskfile is set, find a .env file in the directory containing the Taskfile.
if taskfile != "" {
return filepath.Join(filepath.Dir(taskfile), ".env")
return filepath.Join(filepath.Dir(taskfile), filename)
}
// Otherwise just use the current working directory.
return ".env"
return filename
}

func readDotEnv() {
env, _ := godotenv.Read(getEnvFilePath())
env, _ := godotenv.Read(getFilePath(".env"))
// If the env var is an experiment, set it.
for key, value := range env {
if strings.HasPrefix(key, envPrefix) {
Expand All @@ -96,6 +114,21 @@ func readDotEnv() {
}
}

func readConfig() ExperimentConfigFile {
var cfg ExperimentConfigFile
filename := getFilePath(defaultConfigFilename)
content, err := os.ReadFile(filename)
if err != nil {
return ExperimentConfigFile{}
}

if err := yaml.Unmarshal(content, &cfg); err != nil {
return ExperimentConfigFile{}
}

return cfg
}

func printExperiment(w io.Writer, l *logger.Logger, x Experiment) {
l.FOutf(w, logger.Yellow, "* ")
l.FOutf(w, logger.Green, x.Name)
Expand Down
10 changes: 10 additions & 0 deletions website/docs/experiments/experiments.mdx
vmaerten marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ Which method you use depends on how you intend to use the experiment:
TASK_X_FEATURE=1
```

1. Creating a `.task-experiments.yml` file in the same directory as your root Taskfile that
contains the experiment and the value you want to enable. This allows you to enable an
experimental feature at a project level. If you commit the `.task-experiments.yml` file to
source control then other users of your project will also have these
experiments enabled.

```yaml title=".task-experiments.yml"
X_FEATURE: 1
```

## Workflow

Experiments are a way for us to test out new features in Task before committing
Expand Down
Loading