From c8ff2489997c3071cf8f4fcf1c863257c12b07cd Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Tue, 22 Apr 2025 08:48:34 +0200 Subject: [PATCH] feat: read cache expiry from taskrc and env --- experiments/experiments.go | 11 +++-- internal/flags/flags.go | 71 ++++++++++++++++++++++++++++++- taskrc/ast/taskrc.go | 10 ++++- website/static/schema-taskrc.json | 9 ++++ 4 files changed, 95 insertions(+), 6 deletions(-) diff --git a/experiments/experiments.go b/experiments/experiments.go index 9e646c57..00d2cac7 100644 --- a/experiments/experiments.go +++ b/experiments/experiments.go @@ -2,6 +2,7 @@ package experiments import ( "fmt" + "github.com/go-task/task/v3/taskrc/ast" "os" "path/filepath" "strings" @@ -31,9 +32,6 @@ var ( var xList []Experiment func Parse(dir string) { - // Read any .env files - readDotEnv(dir) - // Create a node for the Task config reader node, _ := taskrc.NewNode("", dir) @@ -41,6 +39,13 @@ func Parse(dir string) { reader := taskrc.NewReader() config, _ := reader.Read(node) + ParseWithConfig(dir, config) +} + +func ParseWithConfig(dir string, config *ast.TaskRC) { + // Read any .env files + readDotEnv(dir) + // Initialize the experiments GentleForce = New("GENTLE_FORCE", config, 1) RemoteTaskfiles = New("REMOTE_TASKFILES", config, 1) diff --git a/internal/flags/flags.go b/internal/flags/flags.go index dab9fdf8..4911408c 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -2,6 +2,7 @@ package flags import ( "cmp" + "github.com/go-task/task/v3/taskrc" "log" "os" "path/filepath" @@ -95,7 +96,14 @@ func init() { // Parse the experiments dir = cmp.Or(dir, filepath.Dir(entrypoint)) - experiments.Parse(dir) + + node, _ := taskrc.NewNode("", dir) + + reader := taskrc.NewReader() + config, _ := reader.Read(node) + + expiry := getDurationValue(config.Remote.Expiry, "REMOTE_EXPIRY", 0) + experiments.ParseWithConfig(dir, config) // Parse the rest of the flags log.SetFlags(0) @@ -153,7 +161,7 @@ func init() { pflag.BoolVar(&Offline, "offline", offline, "Forces Task to only use local or cached Taskfiles.") pflag.DurationVar(&Timeout, "timeout", time.Second*10, "Timeout for downloading remote Taskfiles.") pflag.BoolVar(&ClearCache, "clear-cache", false, "Clear the remote cache.") - pflag.DurationVar(&CacheExpiryDuration, "expiry", 0, "Expiry duration for cached remote Taskfiles.") + pflag.DurationVar(&CacheExpiryDuration, "expiry", expiry, "Expiry duration for cached remote Taskfiles.") } pflag.Parse() @@ -251,3 +259,62 @@ func (o *flagsOption) ApplyToExecutor(e *task.Executor) { task.WithVersionCheck(true), ) } + +//func getEffectiveValueFromConfig[T any](config any, fieldName string, envVar string, defaultValue T) T { +// v := reflect.ValueOf(config) +// if v.Kind() == reflect.Ptr { +// v = v.Elem() +// } +// +// if v.Kind() != reflect.Struct { +// return defaultValue +// } +// +// field := v.FieldByName(fieldName) +// if field.IsValid() && !field.IsZero() && field.Kind() == reflect.Ptr { +// // Return value from struct +// actual := field.Elem() +// if actual.IsValid() { +// return actual.Interface().(T) +// } +// } +// +// envVal := os.Getenv(envVar) +// if envVal != "" { +// var parsed any +// var err error +// +// switch any(defaultValue).(type) { +// case int: +// parsedInt, e := strconv.Atoi(envVal) +// parsed, err = parsedInt, e +// case string: +// parsed = envVal +// case bool: +// parsedBool, e := strconv.ParseBool(envVal) +// parsed, err = parsedBool, e +// default: +// return defaultValue // unsupported type +// } +// +// if err == nil { +// return parsed.(T) +// } +// } +// +// return defaultValue +//} + +func getDurationValue(configValue *time.Duration, envVarName string, defaultValue time.Duration) time.Duration { + if configValue != nil { + return *configValue + } + + if envVal := env.GetTaskEnv(envVarName); envVal != "" { + if intVal, err := time.ParseDuration(envVal); err == nil { + return intVal + } + } + + return defaultValue +} diff --git a/taskrc/ast/taskrc.go b/taskrc/ast/taskrc.go index f82452a9..9ff8c095 100644 --- a/taskrc/ast/taskrc.go +++ b/taskrc/ast/taskrc.go @@ -1,8 +1,16 @@ package ast -import "github.com/Masterminds/semver/v3" +import ( + "github.com/Masterminds/semver/v3" + "time" +) type TaskRC struct { Version *semver.Version `yaml:"version"` Experiments map[string]int `yaml:"experiments"` + Remote remote `yaml:"remote"` +} + +type remote struct { + Expiry *time.Duration `yaml:"expiry"` } diff --git a/website/static/schema-taskrc.json b/website/static/schema-taskrc.json index 4ed35be3..594d5212 100644 --- a/website/static/schema-taskrc.json +++ b/website/static/schema-taskrc.json @@ -9,6 +9,15 @@ "additionalProperties": { "type": "integer" } + }, "remote": { + "type": "object", + "properties": { + "cache-expiry": { + "type": "string", + "description": "Duration format (e.g. '5s', '10m', '1h30m') compatible with Go time.Duration" + } + }, + "additionalProperties": false } }, "additionalProperties": false