Skip to content

Commit 76030c9

Browse files
feat(remote): add a command to clear the cache (#1639)
* feat(remote): add a command to clear the cache * Update cmd/task/task.go Co-authored-by: Andrey Nering <[email protected]> * rebase --------- Co-authored-by: Andrey Nering <[email protected]>
1 parent a71020e commit 76030c9

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

cmd/task/task.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/go-task/task/v3/internal/logger"
1818
"github.com/go-task/task/v3/internal/sort"
1919
ver "github.com/go-task/task/v3/internal/version"
20+
"github.com/go-task/task/v3/taskfile"
2021
"github.com/go-task/task/v3/taskfile/ast"
2122
)
2223

@@ -125,7 +126,6 @@ func run() error {
125126
OutputStyle: flags.Output,
126127
TaskSorter: taskSorter,
127128
}
128-
129129
listOptions := task.NewListOptions(flags.List, flags.ListAll, flags.ListJson, flags.NoStatus)
130130
if err := listOptions.Validate(); err != nil {
131131
return err
@@ -135,7 +135,6 @@ func run() error {
135135
if err != nil {
136136
return err
137137
}
138-
139138
if experiments.AnyVariables.Enabled {
140139
logger.Warnf("The 'Any Variables' experiment flag is no longer required to use non-map variable types. If you wish to use map variables, please use 'TASK_X_MAP_VARIABLES' instead. See https://github.com/go-task/task/issues/1585\n")
141140
}
@@ -146,6 +145,14 @@ func run() error {
146145
return nil
147146
}
148147

148+
if flags.ClearCache {
149+
cache, err := taskfile.NewCache(e.TempDir.Remote)
150+
if err != nil {
151+
return err
152+
}
153+
return cache.Clear()
154+
}
155+
149156
if (listOptions.ShouldListTasks()) && flags.Silent {
150157
return e.ListTaskNames(flags.ListAll)
151158
}

internal/flags/flags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var (
6565
Experiments bool
6666
Download bool
6767
Offline bool
68+
ClearCache bool
6869
Timeout time.Duration
6970
)
7071

@@ -119,6 +120,7 @@ func init() {
119120
pflag.BoolVar(&Download, "download", false, "Downloads a cached version of a remote Taskfile.")
120121
pflag.BoolVar(&Offline, "offline", false, "Forces Task to only use local or cached Taskfiles.")
121122
pflag.DurationVar(&Timeout, "timeout", time.Second*10, "Timeout for downloading remote Taskfiles.")
123+
pflag.BoolVar(&ClearCache, "clear-cache", false, "Clear the remote cache.")
122124
}
123125

124126
pflag.Parse()
@@ -129,6 +131,10 @@ func Validate() error {
129131
return errors.New("task: You can't set both --download and --offline flags")
130132
}
131133

134+
if Download && ClearCache {
135+
return errors.New("task: You can't set both --download and --clear-cache flags")
136+
}
137+
132138
if Global && Dir != "" {
133139
log.Fatal("task: You can't set both --global and --dir")
134140
return nil

taskfile/cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,7 @@ func (c *Cache) filePath(node Node, suffix string) string {
6666
}
6767
return filepath.Join(c.dir, fmt.Sprintf("%s.%s.%s", prefix, c.key(node), suffix))
6868
}
69+
70+
func (c *Cache) Clear() error {
71+
return os.RemoveAll(c.dir)
72+
}

website/docs/experiments/remote_taskfiles.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ internet, you will still be able to run your tasks by specifying the `--offline`
106106
flag. This will tell Task to use the latest cached version of the file instead
107107
of trying to download it. You are able to use the `--download` flag to update
108108
the cached version of the remote files without running any tasks.
109+
You are able to use the `--clear-cache` flag to clear all cached version of the remote files without running any tasks.
109110

110111
By default, Task will timeout requests to download remote files after 10 seconds
111112
and look for a cached copy instead. This timeout can be configured by setting

0 commit comments

Comments
 (0)