2023-06-18 02:04:05 +01:00
|
|
|
package experiments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-03-21 01:00:35 +09:00
|
|
|
"path/filepath"
|
2023-06-18 02:04:05 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
2024-01-26 00:15:02 +00:00
|
|
|
"github.com/spf13/pflag"
|
2023-06-18 02:04:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const envPrefix = "TASK_X_"
|
|
|
|
|
2025-02-08 23:02:51 +00:00
|
|
|
// A set of experiments that can be enabled or disabled.
|
2023-09-12 16:42:54 -05:00
|
|
|
var (
|
2023-12-23 02:33:12 +00:00
|
|
|
GentleForce Experiment
|
|
|
|
RemoteTaskfiles Experiment
|
2024-04-24 21:40:52 +01:00
|
|
|
AnyVariables Experiment
|
2024-04-09 12:14:14 +01:00
|
|
|
MapVariables Experiment
|
2024-07-17 00:44:34 +02:00
|
|
|
EnvPrecedence Experiment
|
2023-09-12 16:42:54 -05:00
|
|
|
)
|
2023-06-18 02:04:05 +01:00
|
|
|
|
2025-02-08 23:02:51 +00:00
|
|
|
// An internal list of all the initialized experiments used for iterating.
|
|
|
|
var xList []Experiment
|
|
|
|
|
2023-06-18 02:04:05 +01:00
|
|
|
func init() {
|
2023-06-30 13:33:37 +01:00
|
|
|
readDotEnv()
|
2025-02-08 23:02:51 +00:00
|
|
|
GentleForce = New("GENTLE_FORCE", "1")
|
|
|
|
RemoteTaskfiles = New("REMOTE_TASKFILES", "1")
|
|
|
|
AnyVariables = New("ANY_VARIABLES")
|
2024-04-09 12:14:14 +01:00
|
|
|
MapVariables = New("MAP_VARIABLES", "1", "2")
|
2025-02-08 23:02:51 +00:00
|
|
|
EnvPrecedence = New("ENV_PRECEDENCE", "1")
|
2023-12-23 02:33:12 +00:00
|
|
|
}
|
|
|
|
|
2025-02-08 23:02:51 +00:00
|
|
|
// Validate checks if any experiments have been enabled while being inactive.
|
|
|
|
// If one is found, the function returns an error.
|
|
|
|
func Validate() error {
|
|
|
|
for _, x := range List() {
|
|
|
|
if err := x.Valid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-23 02:33:12 +00:00
|
|
|
}
|
2025-02-08 23:02:51 +00:00
|
|
|
return nil
|
2023-12-23 02:33:12 +00:00
|
|
|
}
|
|
|
|
|
2025-02-08 23:02:51 +00:00
|
|
|
func List() []Experiment {
|
|
|
|
return xList
|
2023-06-18 02:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-12-23 02:33:12 +00:00
|
|
|
func getEnv(xName string) string {
|
2023-06-18 02:04:05 +01:00
|
|
|
envName := fmt.Sprintf("%s%s", envPrefix, xName)
|
2023-12-23 02:33:12 +00:00
|
|
|
return os.Getenv(envName)
|
2023-06-18 02:04:05 +01:00
|
|
|
}
|
|
|
|
|
2024-01-26 00:15:02 +00:00
|
|
|
func getEnvFilePath() string {
|
|
|
|
// Parse the CLI flags again to get the directory/taskfile being run
|
|
|
|
// We use a flagset here so that we can parse a subset of flags without exiting on error.
|
|
|
|
var dir, taskfile string
|
|
|
|
fs := pflag.NewFlagSet("experiments", pflag.ContinueOnError)
|
|
|
|
fs.StringVarP(&dir, "dir", "d", "", "Sets directory of execution.")
|
|
|
|
fs.StringVarP(&taskfile, "taskfile", "t", "", `Choose which Taskfile to run. Defaults to "Taskfile.yml".`)
|
2024-09-14 11:16:47 +02:00
|
|
|
fs.Usage = func() {}
|
2024-01-26 00:15:02 +00:00
|
|
|
_ = fs.Parse(os.Args[1:])
|
|
|
|
// If the directory is set, find a .env file in that directory.
|
|
|
|
if dir != "" {
|
2024-03-21 01:00:35 +09:00
|
|
|
return filepath.Join(dir, ".env")
|
2024-01-26 00:15:02 +00:00
|
|
|
}
|
|
|
|
// If the taskfile is set, find a .env file in the directory containing the Taskfile.
|
|
|
|
if taskfile != "" {
|
2024-03-21 01:00:35 +09:00
|
|
|
return filepath.Join(filepath.Dir(taskfile), ".env")
|
2024-01-26 00:15:02 +00:00
|
|
|
}
|
|
|
|
// Otherwise just use the current working directory.
|
|
|
|
return ".env"
|
|
|
|
}
|
|
|
|
|
2023-06-30 13:33:37 +01:00
|
|
|
func readDotEnv() {
|
2024-01-26 00:15:02 +00:00
|
|
|
env, _ := godotenv.Read(getEnvFilePath())
|
2023-06-18 02:04:05 +01:00
|
|
|
// If the env var is an experiment, set it.
|
|
|
|
for key, value := range env {
|
|
|
|
if strings.HasPrefix(key, envPrefix) {
|
|
|
|
os.Setenv(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|