2023-06-18 03:04:05 +02:00
|
|
|
package experiments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-09-12 23:42:54 +02:00
|
|
|
"io"
|
2023-06-18 03:04:05 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
2023-06-30 03:29:28 +02:00
|
|
|
"text/tabwriter"
|
2023-06-18 03:04:05 +02:00
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
2023-12-23 04:33:12 +02:00
|
|
|
"golang.org/x/exp/slices"
|
2023-06-30 03:29:28 +02:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
2023-06-18 03:04:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const envPrefix = "TASK_X_"
|
|
|
|
|
2023-12-23 04:33:12 +02:00
|
|
|
type Experiment struct {
|
|
|
|
Name string
|
|
|
|
Enabled bool
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2023-09-12 23:42:54 +02:00
|
|
|
// A list of experiments.
|
|
|
|
var (
|
2023-12-23 04:33:12 +02:00
|
|
|
GentleForce Experiment
|
|
|
|
RemoteTaskfiles Experiment
|
|
|
|
AnyVariables Experiment
|
2023-09-12 23:42:54 +02:00
|
|
|
)
|
2023-06-18 03:04:05 +02:00
|
|
|
|
|
|
|
func init() {
|
2023-06-30 14:33:37 +02:00
|
|
|
readDotEnv()
|
2023-12-23 04:33:12 +02:00
|
|
|
GentleForce = New("GENTLE_FORCE")
|
|
|
|
RemoteTaskfiles = New("REMOTE_TASKFILES")
|
2023-12-23 06:59:10 +02:00
|
|
|
AnyVariables = New("ANY_VARIABLES", "1", "2")
|
2023-12-23 04:33:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(xName string, enabledValues ...string) Experiment {
|
|
|
|
if len(enabledValues) == 0 {
|
|
|
|
enabledValues = []string{"1"}
|
|
|
|
}
|
|
|
|
value := getEnv(xName)
|
|
|
|
return Experiment{
|
|
|
|
Name: xName,
|
|
|
|
Enabled: slices.Contains(enabledValues, value),
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x Experiment) String() string {
|
|
|
|
if x.Enabled {
|
|
|
|
return fmt.Sprintf("on (%s)", x.Value)
|
|
|
|
}
|
|
|
|
return "off"
|
2023-06-18 03:04:05 +02:00
|
|
|
}
|
|
|
|
|
2023-12-23 04:33:12 +02:00
|
|
|
func getEnv(xName string) string {
|
2023-06-18 03:04:05 +02:00
|
|
|
envName := fmt.Sprintf("%s%s", envPrefix, xName)
|
2023-12-23 04:33:12 +02:00
|
|
|
return os.Getenv(envName)
|
2023-06-18 03:04:05 +02:00
|
|
|
}
|
|
|
|
|
2023-06-30 14:33:37 +02:00
|
|
|
func readDotEnv() {
|
|
|
|
env, _ := godotenv.Read()
|
2023-06-18 03:04:05 +02:00
|
|
|
// If the env var is an experiment, set it.
|
|
|
|
for key, value := range env {
|
|
|
|
if strings.HasPrefix(key, envPrefix) {
|
|
|
|
os.Setenv(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 03:29:28 +02:00
|
|
|
|
2023-12-23 04:33:12 +02:00
|
|
|
func printExperiment(w io.Writer, l *logger.Logger, x Experiment) {
|
2023-06-30 03:29:28 +02:00
|
|
|
l.FOutf(w, logger.Yellow, "* ")
|
2023-12-23 04:33:12 +02:00
|
|
|
l.FOutf(w, logger.Green, x.Name)
|
|
|
|
l.FOutf(w, logger.Default, ": \t%s\n", x.String())
|
2023-09-12 23:42:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func List(l *logger.Logger) error {
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, ' ', 0)
|
2023-12-23 04:33:12 +02:00
|
|
|
printExperiment(w, l, GentleForce)
|
|
|
|
printExperiment(w, l, RemoteTaskfiles)
|
|
|
|
printExperiment(w, l, AnyVariables)
|
2023-06-30 03:29:28 +02:00
|
|
|
return w.Flush()
|
|
|
|
}
|