Skip to content

Commit

Permalink
feat: wire up new flag for cache TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucktay committed Jul 3, 2024
1 parent 775fb2e commit 69629d0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func run() error {
Download: flags.Download,
Offline: flags.Offline,
Timeout: flags.Timeout,
CacheTTL: flags.CacheTTL,
Watch: flags.Watch,
Verbose: flags.Verbose,
Silent: flags.Silent,
Expand Down
6 changes: 5 additions & 1 deletion internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/pflag"

"github.com/go-task/task/v3/internal/experiments"
"github.com/go-task/task/v3/taskfile"
"github.com/go-task/task/v3/taskfile/ast"
)

Expand Down Expand Up @@ -66,6 +67,7 @@ var (
Download bool
Offline bool
Timeout time.Duration
CacheTTL time.Duration
)

func init() {
Expand Down Expand Up @@ -114,11 +116,13 @@ func init() {
pflag.BoolVarP(&ForceAll, "force", "f", false, "Forces execution even when the task is up-to-date.")
}

// Remote Taskfiles experiment will adds the "download" and "offline" flags
// Remote Taskfiles experiment will add the following flags.
if experiments.RemoteTaskfiles.Enabled {
pflag.BoolVar(&Download, "download", false, "Downloads a cached version of a remote Taskfile.")
pflag.BoolVar(&Offline, "offline", false, "Forces Task to only use local or cached Taskfiles.")
pflag.DurationVar(&Timeout, "timeout", time.Second*10, "Timeout for downloading remote Taskfiles.")
pflag.DurationVar(&CacheTTL, "remote-cache-ttl", taskfile.DefaultCacheTTL,
"TTL of remote Taskfiles downloaded into the local cache.")
}

pflag.Parse()
Expand Down
1 change: 1 addition & 0 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (e *Executor) readTaskfile(node taskfile.Node) error {
e.Download,
e.Offline,
e.Timeout,
e.CacheTTL,
e.TempDir,
e.Logger,
)
Expand Down
1 change: 1 addition & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Executor struct {
Download bool
Offline bool
Timeout time.Duration
CacheTTL time.Duration
Watch bool
Verbose bool
Silent bool
Expand Down
6 changes: 3 additions & 3 deletions taskfile/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"
)

// defaultCacheTTL is one day (24 hours).
const defaultCacheTTL = time.Duration(time.Hour * 24)
// DefaultCacheTTL is one day (24 hours).
const DefaultCacheTTL = time.Duration(time.Hour * 24)

type Cache struct {
dir string
Expand All @@ -31,7 +31,7 @@ func NewCache(dir string, opts ...CacheOption) (*Cache, error) {

cache := &Cache{
dir: dir,
ttl: defaultCacheTTL,
ttl: DefaultCacheTTL,
}

// Apply options.
Expand Down
5 changes: 4 additions & 1 deletion taskfile/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Reader struct {
download bool
offline bool
timeout time.Duration
cacheTTL time.Duration
tempDir string
logger *logger.Logger
}
Expand All @@ -46,6 +47,7 @@ func NewReader(
download bool,
offline bool,
timeout time.Duration,
cacheTTL time.Duration,
tempDir string,
logger *logger.Logger,
) *Reader {
Expand All @@ -56,6 +58,7 @@ func NewReader(
download: download,
offline: offline,
timeout: timeout,
cacheTTL: cacheTTL,
tempDir: tempDir,
logger: logger,
}
Expand Down Expand Up @@ -185,7 +188,7 @@ func (r *Reader) readNode(node Node) (*ast.Taskfile, error) {
var cache *Cache

if node.Remote() {
cache, err = NewCache(r.tempDir)
cache, err = NewCache(r.tempDir, WithTTL(r.cacheTTL))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 69629d0

Please sign in to comment.