diff --git a/experiments/experiments.go b/experiments/experiments.go index 9e646c57b7..59722e1feb 100644 --- a/experiments/experiments.go +++ b/experiments/experiments.go @@ -9,6 +9,7 @@ import ( "github.com/joho/godotenv" "github.com/go-task/task/v3/taskrc" + "github.com/go-task/task/v3/taskrc/ast" ) const envPrefix = "TASK_X_" @@ -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 dab9fdf8f4..59890268e0 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -16,6 +16,7 @@ import ( "github.com/go-task/task/v3/internal/env" "github.com/go-task/task/v3/internal/sort" "github.com/go-task/task/v3/taskfile/ast" + "github.com/go-task/task/v3/taskrc" ) const usage = `Usage: task [flags...] [task...] @@ -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.CacheExpiry, "REMOTE_CACHE_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,17 @@ func (o *flagsOption) ApplyToExecutor(e *task.Executor) { task.WithVersionCheck(true), ) } + +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 f82452a94d..fdd0b2b305 100644 --- a/taskrc/ast/taskrc.go +++ b/taskrc/ast/taskrc.go @@ -1,8 +1,17 @@ package ast -import "github.com/Masterminds/semver/v3" +import ( + "time" + + "github.com/Masterminds/semver/v3" +) type TaskRC struct { Version *semver.Version `yaml:"version"` Experiments map[string]int `yaml:"experiments"` + Remote remote `yaml:"remote"` +} + +type remote struct { + CacheExpiry *time.Duration `yaml:"cache-expiry"` } diff --git a/website/docs/experiments/remote_taskfiles.mdx b/website/docs/experiments/remote_taskfiles.mdx index b9c2d1f2cb..6246de4398 100644 --- a/website/docs/experiments/remote_taskfiles.mdx +++ b/website/docs/experiments/remote_taskfiles.mdx @@ -224,8 +224,19 @@ internet and cached locally. This cached file will be used for all future invocations of the Taskfile until the cache expires. Once it expires, Task will download the latest copy of the file and update the cache. By default, the cache is set to expire immediately. This means that Task will always fetch the latest -version. However, the cache expiry duration can be modified by setting the -`--expiry` flag. +version. However, the cache expiry duration can be modified by: +- Setting the `--expiry` flag +- Configuring it in your taskrc file: +```yaml +remote: + cache-expiry: 24h +``` +- Setting the `TASK_REMOTE_CACHE_EXPIRY` environment variable + +The expiry duration should be specified in a format like "24h", "30m", "1h30m", etc. + + + If for any reason you lose access to the internet or you are running Task in offline mode (via the `--offline` flag or `TASK_OFFLINE` environment variable), diff --git a/website/static/schema-taskrc.json b/website/static/schema-taskrc.json index 4ed35be313..6ac80142f7 100644 --- a/website/static/schema-taskrc.json +++ b/website/static/schema-taskrc.json @@ -9,6 +9,16 @@ "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