1
0
mirror of https://github.com/go-task/task.git synced 2025-05-13 22:16:31 +02:00

feat: read cache expiry from taskrc and env

This commit is contained in:
Valentin Maerten 2025-04-22 08:48:34 +02:00
parent 6cb0a5a2f2
commit c8ff248999
No known key found for this signature in database
GPG Key ID: 2F8E54DDF815C341
4 changed files with 95 additions and 6 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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"`
}

View File

@ -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