Files
kopia/cli/command_cache_clear.go
Jarek Kowalski e03971fc59 Upgraded linter to v1.33.0 (#734)
* linter: upgraded to 1.33, disabled some linters

* lint: fixed 'errorlint' errors

This ensures that all error comparisons use errors.Is() or errors.As().
We will be wrapping more errors going forward so it's important that
error checks are not strict everywhere.

Verified that there are no exceptions for errorlint linter which
guarantees that.

* lint: fixed or suppressed wrapcheck errors

* lint: nolintlint and misc cleanups

Co-authored-by: Julio López <julio+gh@kasten.io>
2020-12-21 22:39:22 -08:00

46 lines
1.0 KiB
Go

package cli
import (
"context"
"os"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/retry"
"github.com/kopia/kopia/repo"
)
var cacheClearCommand = cacheCommands.Command("clear", "Clears the cache")
func runCacheClearCommand(ctx context.Context, rep *repo.DirectRepository) error {
if d := rep.Content.CachingOptions.CacheDirectory; d != "" {
log(ctx).Infof("Clearing cache directory: %v.", d)
// close repository before removing cache
if err := rep.Close(ctx); err != nil {
return errors.Wrap(err, "unable to close repository")
}
err := retry.WithExponentialBackoffNoValue(ctx, "delete cache", func() error {
return os.RemoveAll(d)
}, retry.Always)
if err != nil {
return errors.Wrap(err, "error removing cache directory")
}
if err := os.MkdirAll(d, 0o700); err != nil {
return errors.Wrap(err, "error creating cache directory")
}
log(ctx).Infof("Cache cleared.")
return nil
}
return errors.New("caching not enabled")
}
func init() {
cacheClearCommand.Action(directRepositoryAction(runCacheClearCommand))
}