Skip to content

feat: read cache expiry from .taskrc and env #2210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions experiments/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_"
Expand All @@ -31,16 +32,20 @@ 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)

// Read the Task config file
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)
Expand Down
26 changes: 24 additions & 2 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
11 changes: 10 additions & 1 deletion taskrc/ast/taskrc.go
Original file line number Diff line number Diff line change
@@ -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"`
}
15 changes: 13 additions & 2 deletions website/docs/experiments/remote_taskfiles.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
10 changes: 10 additions & 0 deletions website/static/schema-taskrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading