mirror of
https://github.com/go-task/task.git
synced 2024-12-16 10:59:23 +02:00
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
|
||
|
}
|