1
0
mirror of https://github.com/go-task/task.git synced 2024-12-16 10:59:23 +02:00
task/internal/experiments/experiments.go
Pete Davison d8a12fe56d
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
2023-06-18 01:04:05 +00:00

44 lines
692 B
Go

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
}