mirror of
https://github.com/go-task/task.git
synced 2024-12-16 10:59:23 +02:00
d8a12fe56d
* feat: experiments env var handling * feat: read experiments from dotenv files * docs: removed experimental cli flags * feat: use vars instead of struct with reflection
44 lines
692 B
Go
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
|
|
}
|