From 2663aa625ececa3f8a7095608ede57ba905a0bf3 Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Sun, 5 May 2024 20:09:33 +0200 Subject: [PATCH 1/3] feat(remote): add a command to clear the cache --- cmd/task/task.go | 12 ++++++++++-- internal/flags/flags.go | 6 ++++++ taskfile/cache.go | 4 ++++ website/docs/experiments/remote_taskfiles.mdx | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index 5969c840d8..fae23ec006 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -17,6 +17,7 @@ import ( "github.com/go-task/task/v3/internal/logger" "github.com/go-task/task/v3/internal/sort" ver "github.com/go-task/task/v3/internal/version" + "github.com/go-task/task/v3/taskfile" "github.com/go-task/task/v3/taskfile/ast" ) @@ -125,7 +126,6 @@ func run() error { OutputStyle: flags.Output, TaskSorter: taskSorter, } - listOptions := task.NewListOptions(flags.List, flags.ListAll, flags.ListJson, flags.NoStatus) if err := listOptions.Validate(); err != nil { return err @@ -135,7 +135,6 @@ func run() error { if err != nil { return err } - if experiments.AnyVariables.Enabled { 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") } @@ -146,6 +145,15 @@ func run() error { return nil } + if flags.ClearCache { + cache, err := taskfile.NewCache(e.TempDir) + if err != nil { + return err + } + err = cache.Clear() + return err + } + if (listOptions.ShouldListTasks()) && flags.Silent { return e.ListTaskNames(flags.ListAll) } diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 6c67443c55..2a1f9476fc 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -65,6 +65,7 @@ var ( Experiments bool Download bool Offline bool + ClearCache bool Timeout time.Duration ) @@ -119,6 +120,7 @@ func init() { 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.BoolVar(&ClearCache, "clear-cache", false, "Clear the remote cache.") } pflag.Parse() @@ -129,6 +131,10 @@ func Validate() error { return errors.New("task: You can't set both --download and --offline flags") } + if Download && ClearCache { + return errors.New("task: You can't set both --download and --clear-cache flags") + } + if Global && Dir != "" { log.Fatal("task: You can't set both --global and --dir") return nil diff --git a/taskfile/cache.go b/taskfile/cache.go index 2736051974..2b57c17dd8 100644 --- a/taskfile/cache.go +++ b/taskfile/cache.go @@ -66,3 +66,7 @@ func (c *Cache) filePath(node Node, suffix string) string { } return filepath.Join(c.dir, fmt.Sprintf("%s.%s.%s", prefix, c.key(node), suffix)) } + +func (c *Cache) Clear() error { + return os.RemoveAll(c.dir) +} diff --git a/website/docs/experiments/remote_taskfiles.mdx b/website/docs/experiments/remote_taskfiles.mdx index 8df5ac8b5b..74055acbd0 100644 --- a/website/docs/experiments/remote_taskfiles.mdx +++ b/website/docs/experiments/remote_taskfiles.mdx @@ -106,6 +106,7 @@ internet, you will still be able to run your tasks by specifying the `--offline` flag. This will tell Task to use the latest cached version of the file instead of trying to download it. You are able to use the `--download` flag to update the cached version of the remote files without running any tasks. +You are able to use the `--clear-cache` flag to clear all cached version of the remote files without running any tasks. By default, Task will timeout requests to download remote files after 10 seconds and look for a cached copy instead. This timeout can be configured by setting From 762ecb4648e4aabf61532849fcc0301af4d61542 Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Fri, 14 Jun 2024 04:32:38 -0400 Subject: [PATCH 2/3] Update cmd/task/task.go Co-authored-by: Andrey Nering --- cmd/task/task.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index fae23ec006..357564afb0 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -150,8 +150,7 @@ func run() error { if err != nil { return err } - err = cache.Clear() - return err + return cache.Clear() } if (listOptions.ShouldListTasks()) && flags.Silent { From 0d1450c758dbe9a957ec19b93918e5a277a7f549 Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Fri, 28 Jun 2024 18:33:06 +0200 Subject: [PATCH 3/3] rebase --- cmd/task/task.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index 357564afb0..0de9075054 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -146,7 +146,7 @@ func run() error { } if flags.ClearCache { - cache, err := taskfile.NewCache(e.TempDir) + cache, err := taskfile.NewCache(e.TempDir.Remote) if err != nil { return err }