Skip to content

Commit

Permalink
feat(remote): add a command to clear the cache (#1639)
Browse files Browse the repository at this point in the history
* feat(remote): add a command to clear the cache

* Update cmd/task/task.go

Co-authored-by: Andrey Nering <andrey@nering.com.br>

* rebase

---------

Co-authored-by: Andrey Nering <andrey@nering.com.br>
  • Loading branch information
vmaerten and andreynering authored Jun 28, 2024
1 parent a71020e commit 76030c9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
11 changes: 9 additions & 2 deletions cmd/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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")
}
Expand All @@ -146,6 +145,14 @@ func run() error {
return nil
}

if flags.ClearCache {
cache, err := taskfile.NewCache(e.TempDir.Remote)
if err != nil {
return err
}
return cache.Clear()
}

if (listOptions.ShouldListTasks()) && flags.Silent {
return e.ListTaskNames(flags.ListAll)
}
Expand Down
6 changes: 6 additions & 0 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var (
Experiments bool
Download bool
Offline bool
ClearCache bool
Timeout time.Duration
)

Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions taskfile/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions website/docs/experiments/remote_taskfiles.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76030c9

Please sign in to comment.