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

44 lines
692 B
Go
Raw Normal View History

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
}