From 20ac7ad3f76ea538cfa51618cbf2e38c665254fe Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Fri, 12 Dec 2025 22:58:44 +0100 Subject: [PATCH] feat(taskrc): add remote.cache-dir configuration option --- executor.go | 14 ++++++++++++++ internal/flags/flags.go | 4 ++++ setup.go | 12 ++++++------ taskrc/ast/taskrc.go | 2 ++ website/src/docs/experiments/remote-taskfiles.md | 14 ++++++++++++++ 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/executor.go b/executor.go index 3ca5fbcf..03b951a1 100644 --- a/executor.go +++ b/executor.go @@ -37,6 +37,7 @@ type ( TrustedHosts []string Timeout time.Duration CacheExpiryDuration time.Duration + RemoteCacheDir string Watch bool Verbose bool Silent bool @@ -271,6 +272,19 @@ func (o *cacheExpiryDurationOption) ApplyToExecutor(r *Executor) { r.CacheExpiryDuration = o.duration } +// WithRemoteCacheDir sets the directory where remote taskfiles are cached. +func WithRemoteCacheDir(dir string) ExecutorOption { + return &remoteCacheDirOption{dir: dir} +} + +type remoteCacheDirOption struct { + dir string +} + +func (o *remoteCacheDirOption) ApplyToExecutor(e *Executor) { + e.RemoteCacheDir = o.dir +} + // WithWatch tells the [Executor] to keep running in the background and watch // for changes to the fingerprint of the tasks that are run. When changes are // detected, a new task run is triggered. diff --git a/internal/flags/flags.go b/internal/flags/flags.go index eb5930dc..22358938 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -12,6 +12,7 @@ import ( "github.com/go-task/task/v3" "github.com/go-task/task/v3/errors" "github.com/go-task/task/v3/experiments" + "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" @@ -79,6 +80,7 @@ var ( ClearCache bool Timeout time.Duration CacheExpiryDuration time.Duration + RemoteCacheDir string ) func init() { @@ -161,6 +163,7 @@ func init() { pflag.DurationVar(&Timeout, "timeout", getConfig(config, func() *time.Duration { return config.Remote.Timeout }, time.Second*10), "Timeout for downloading remote Taskfiles.") pflag.BoolVar(&ClearCache, "clear-cache", false, "Clear the remote cache.") pflag.DurationVar(&CacheExpiryDuration, "expiry", getConfig(config, func() *time.Duration { return config.Remote.CacheExpiry }, 0), "Expiry duration for cached remote Taskfiles.") + RemoteCacheDir = getConfig(config, func() *string { return config.Remote.CacheDir }, env.GetTaskEnv("REMOTE_DIR")) } pflag.Parse() } @@ -247,6 +250,7 @@ func (o *flagsOption) ApplyToExecutor(e *task.Executor) { task.WithTrustedHosts(TrustedHosts), task.WithTimeout(Timeout), task.WithCacheExpiryDuration(CacheExpiryDuration), + task.WithRemoteCacheDir(RemoteCacheDir), task.WithWatch(Watch), task.WithVerbose(Verbose), task.WithSilent(Silent), diff --git a/setup.go b/setup.go index 2fc3a6bf..47c3c7b7 100644 --- a/setup.go +++ b/setup.go @@ -153,16 +153,16 @@ func (e *Executor) setupTempDir() error { } } - remoteDir := env.GetTaskEnv("REMOTE_DIR") - if remoteDir != "" { - if filepath.IsAbs(remoteDir) || strings.HasPrefix(remoteDir, "~") { - remoteTempDir, err := execext.ExpandLiteral(remoteDir) + // RemoteCacheDir from taskrc/env can override the remote cache directory + if e.RemoteCacheDir != "" { + if filepath.IsAbs(e.RemoteCacheDir) || strings.HasPrefix(e.RemoteCacheDir, "~") { + remoteCacheDir, err := execext.ExpandLiteral(e.RemoteCacheDir) if err != nil { return err } - e.TempDir.Remote = remoteTempDir + e.TempDir.Remote = remoteCacheDir } else { - e.TempDir.Remote = filepathext.SmartJoin(e.Dir, ".task") + e.TempDir.Remote = filepathext.SmartJoin(e.Dir, e.RemoteCacheDir) } } diff --git a/taskrc/ast/taskrc.go b/taskrc/ast/taskrc.go index 8952315d..ffa3abcd 100644 --- a/taskrc/ast/taskrc.go +++ b/taskrc/ast/taskrc.go @@ -24,6 +24,7 @@ type Remote struct { Offline *bool `yaml:"offline"` Timeout *time.Duration `yaml:"timeout"` CacheExpiry *time.Duration `yaml:"cache-expiry"` + CacheDir *string `yaml:"cache-dir"` TrustedHosts []string `yaml:"trusted-hosts"` } @@ -46,6 +47,7 @@ func (t *TaskRC) Merge(other *TaskRC) { t.Remote.Offline = cmp.Or(other.Remote.Offline, t.Remote.Offline) t.Remote.Timeout = cmp.Or(other.Remote.Timeout, t.Remote.Timeout) t.Remote.CacheExpiry = cmp.Or(other.Remote.CacheExpiry, t.Remote.CacheExpiry) + t.Remote.CacheDir = cmp.Or(other.Remote.CacheDir, t.Remote.CacheDir) if len(other.Remote.TrustedHosts) > 0 { merged := slices.Concat(other.Remote.TrustedHosts, t.Remote.TrustedHosts) diff --git a/website/src/docs/experiments/remote-taskfiles.md b/website/src/docs/experiments/remote-taskfiles.md index 066cc855..5f1e28f1 100644 --- a/website/src/docs/experiments/remote-taskfiles.md +++ b/website/src/docs/experiments/remote-taskfiles.md @@ -308,6 +308,7 @@ remote: offline: false timeout: "30s" cache-expiry: "24h" + cache-dir: ~/.task trusted-hosts: - github.com - gitlab.com @@ -360,6 +361,19 @@ remote: cache-expiry: "6h" ``` +#### `cache-dir` + +- **Type**: `string` +- **Default**: `.task` +- **Description**: Directory where remote Taskfiles are cached. Can be an + absolute path (e.g., `/var/cache/task`) or relative to the Taskfile directory. +- **Environment variable**: `TASK_REMOTE_DIR` (lower priority than config file) + +```yaml +remote: + cache-dir: ~/.task +``` + #### `trusted-hosts` - **Type**: `array of strings`