Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(remote): add a command to clear the cache #1639

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading