Files
kopia/cli/command_cache_clear.go
Jarek Kowalski 1f1465f4ba Improvements and cleanups for connecting to kopia server (#870)
* repo: refactored connect code set up cache for server repositories

- improved logic to close the cache on last connection
- preemptively add all contents with a prefix to the cache
- refactored how config is loaded and saved

Now cache dir will be stored as relative and resolved to absolute as
part of loading and saving the file, in all other places cache dir
is expected to be absolute.

* server: removed cache directory from the API and UI

This won't be easily available and does not seem useful to expose
anyway.

* cli: enabled cache commands for server repositories

* cli: added KOPIA_CACHE_DIRECTORY environment variable

This is used on two occassions - when setting up connection (it gets
persisted in the config) and later when opening (to override the
cache location from config). It makes setting up docker container with
mounted cache somewhat easier with one environment variable.

* cli: show cache size for the server cache

* tls: present more helpful error message that includes SHA256 fingerprint of the TLS server on mismatch

* server: return the name of user who attempted to login when authentication fails
2021-03-07 11:25:21 -08:00

62 lines
1.5 KiB
Go

package cli
import (
"context"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/retry"
"github.com/kopia/kopia/repo"
)
var (
cacheClearCommand = cacheCommands.Command("clear", "Clears the cache")
cacheClearCommandPartial = cacheClearCommand.Flag("partial", "Specifies the cache to clear").Enum("contents", "indexes", "metadata", "own-writes", "blob-list")
)
func runCacheClearCommand(ctx context.Context, rep repo.Repository) error {
opts, err := repo.GetCachingOptions(ctx, repositoryConfigFileName())
if err != nil {
return errors.Wrap(err, "error getting caching options")
}
d := opts.CacheDirectory
if d == "" {
return errors.New("caching not enabled")
}
// close repository before removing cache
if err := rep.Close(ctx); err != nil {
return errors.Wrap(err, "unable to close repository")
}
if *cacheClearCommandPartial == "" {
return clearCacheDirectory(ctx, d)
}
return clearCacheDirectory(ctx, filepath.Join(d, *cacheClearCommandPartial))
}
func clearCacheDirectory(ctx context.Context, d string) error {
log(ctx).Infof("Clearing cache directory: %v.", d)
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")
}
return nil
}
func init() {
cacheClearCommand.Action(repositoryReaderAction(runCacheClearCommand))
}