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:
parent
6cb0a5a2f2
commit
c8ff248999
@ -2,6 +2,7 @@ package experiments
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/go-task/task/v3/taskrc/ast"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -31,9 +32,6 @@ var (
|
|||||||
var xList []Experiment
|
var xList []Experiment
|
||||||
|
|
||||||
func Parse(dir string) {
|
func Parse(dir string) {
|
||||||
// Read any .env files
|
|
||||||
readDotEnv(dir)
|
|
||||||
|
|
||||||
// Create a node for the Task config reader
|
// Create a node for the Task config reader
|
||||||
node, _ := taskrc.NewNode("", dir)
|
node, _ := taskrc.NewNode("", dir)
|
||||||
|
|
||||||
@ -41,6 +39,13 @@ func Parse(dir string) {
|
|||||||
reader := taskrc.NewReader()
|
reader := taskrc.NewReader()
|
||||||
config, _ := reader.Read(node)
|
config, _ := reader.Read(node)
|
||||||
|
|
||||||
|
ParseWithConfig(dir, config)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseWithConfig(dir string, config *ast.TaskRC) {
|
||||||
|
// Read any .env files
|
||||||
|
readDotEnv(dir)
|
||||||
|
|
||||||
// Initialize the experiments
|
// Initialize the experiments
|
||||||
GentleForce = New("GENTLE_FORCE", config, 1)
|
GentleForce = New("GENTLE_FORCE", config, 1)
|
||||||
RemoteTaskfiles = New("REMOTE_TASKFILES", config, 1)
|
RemoteTaskfiles = New("REMOTE_TASKFILES", config, 1)
|
||||||
|
@ -2,6 +2,7 @@ package flags
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"cmp"
|
"cmp"
|
||||||
|
"github.com/go-task/task/v3/taskrc"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -95,7 +96,14 @@ func init() {
|
|||||||
|
|
||||||
// Parse the experiments
|
// Parse the experiments
|
||||||
dir = cmp.Or(dir, filepath.Dir(entrypoint))
|
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
|
// Parse the rest of the flags
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
@ -153,7 +161,7 @@ func init() {
|
|||||||
pflag.BoolVar(&Offline, "offline", offline, "Forces Task to only use local or cached Taskfiles.")
|
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.DurationVar(&Timeout, "timeout", time.Second*10, "Timeout for downloading remote Taskfiles.")
|
||||||
pflag.BoolVar(&ClearCache, "clear-cache", false, "Clear the remote cache.")
|
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()
|
pflag.Parse()
|
||||||
@ -251,3 +259,62 @@ func (o *flagsOption) ApplyToExecutor(e *task.Executor) {
|
|||||||
task.WithVersionCheck(true),
|
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
|
||||||
|
}
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
package ast
|
package ast
|
||||||
|
|
||||||
import "github.com/Masterminds/semver/v3"
|
import (
|
||||||
|
"github.com/Masterminds/semver/v3"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type TaskRC struct {
|
type TaskRC struct {
|
||||||
Version *semver.Version `yaml:"version"`
|
Version *semver.Version `yaml:"version"`
|
||||||
Experiments map[string]int `yaml:"experiments"`
|
Experiments map[string]int `yaml:"experiments"`
|
||||||
|
Remote remote `yaml:"remote"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type remote struct {
|
||||||
|
Expiry *time.Duration `yaml:"expiry"`
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,15 @@
|
|||||||
"additionalProperties": {
|
"additionalProperties": {
|
||||||
"type": "integer"
|
"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
|
"additionalProperties": false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user