mirror of
https://github.com/go-task/task.git
synced 2025-08-10 22:42:19 +02:00
Support for experiment env vars (#1214)
* feat: experiments env var handling * feat: read experiments from dotenv files * docs: removed experimental cli flags * feat: use vars instead of struct with reflection
This commit is contained in:
@@ -21,13 +21,19 @@ are intended to replace.
|
|||||||
|
|
||||||
You can enable an experimental feature by:
|
You can enable an experimental feature by:
|
||||||
|
|
||||||
1. Using the `--x-{feature}` flag. This is intended for one-off invocations of
|
1. Using the relevant environment variable in front of a task command. For
|
||||||
Task to test out experimental features. You can also disable a feature by
|
example, `TASK_X_{FEATURE}=1 task {my-task}`. This is intended for one-off
|
||||||
specifying a falsy value such as `--x-{feature}=false`.
|
invocations of Task to test out experimental features.
|
||||||
1. Using the `TASK_X_{FEATURE}=1` environment variable. This is intended for
|
1. Using the relevant environment variable in your "dotfiles" (e.g. `.bashrc`,
|
||||||
permanently enabling experimental features in your environment.
|
`.zshrc` etc.). This is intended for permanently enabling experimental
|
||||||
|
features in your environment.
|
||||||
|
1. Creating a `.env` file in the same directory as your root Taskfile that
|
||||||
|
contains the relevant environment variables. e.g.
|
||||||
|
|
||||||
Flags will always override environment variables.
|
```shell
|
||||||
|
# .env
|
||||||
|
TASK_X_FEATURE=1
|
||||||
|
```
|
||||||
|
|
||||||
## Current Experimental Features and Deprecations
|
## Current Experimental Features and Deprecations
|
||||||
|
|
||||||
@@ -38,11 +44,11 @@ existing Taskfiles to the new behavior.
|
|||||||
|
|
||||||
<!-- EXPERIMENT TEMPLATE - Include sections as necessary...
|
<!-- EXPERIMENT TEMPLATE - Include sections as necessary...
|
||||||
|
|
||||||
### ![experiment] <Feature> ([#{issue}](https://github.com/go-task/task/issues/{issue})), ...)
|
### ![experiment] {Feature} ([#{issue}](https://github.com/go-task/task/issues/{issue})), ...)
|
||||||
|
|
||||||
- Flag to enable: `--x-{feature}`
|
- Environment variable: `TASK_X_{feature}`
|
||||||
- Env to enable: `TASK_X_{feature}`
|
|
||||||
- Deprecates: {list any existing functionality that will be deprecated by this experiment}
|
- Deprecates: {list any existing functionality that will be deprecated by this experiment}
|
||||||
|
- Breaks: {list any existing functionality that will be broken by this experiment}
|
||||||
|
|
||||||
{Short description of the feature}
|
{Short description of the feature}
|
||||||
|
|
||||||
|
43
internal/experiments/experiments.go
Normal file
43
internal/experiments/experiments.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package experiments
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const envPrefix = "TASK_X_"
|
||||||
|
|
||||||
|
var TestExperiment bool
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if err := readDotEnv(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
TestExperiment = parseEnv("TestExperiment")
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseEnv(xName string) bool {
|
||||||
|
envName := fmt.Sprintf("%s%s", envPrefix, xName)
|
||||||
|
return os.Getenv(envName) == "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
func readDotEnv() error {
|
||||||
|
env, err := godotenv.Read()
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// If the env var is an experiment, set it.
|
||||||
|
for key, value := range env {
|
||||||
|
if strings.HasPrefix(key, envPrefix) {
|
||||||
|
os.Setenv(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
Reference in New Issue
Block a user