Files
kopia/cli/command_repository_create.go
Jarek Kowalski c8fcae93aa logging: refactored logging
This is mostly mechanical and changes how loggers are instantiated.

Logger is now associated with a context, passed around all methods,
(most methods had ctx, but had to add it in a few missing places).

By default Kopia does not produce any logs, but it can be overridden,
either locally for a nested context, by calling

ctx = logging.WithLogger(ctx, newLoggerFunc)

To override logs globally, call logging.SetDefaultLogger(newLoggerFunc)

This refactoring allowed removing dependency from Kopia repo
and go-logging library (the CLI still uses it, though).

It is now also possible to have all test methods emit logs using
t.Logf() so that they show up in failure reports, which should make
debugging of test failures suck less.
2020-02-25 17:24:44 -08:00

106 lines
3.3 KiB
Go

package cli
import (
"context"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/object"
"github.com/kopia/kopia/snapshot/policy"
)
var (
createCommand = repositoryCommands.Command("create", "Create new repository in a specified location.")
createBlockHashFormat = createCommand.Flag("block-hash", "Block hash algorithm.").PlaceHolder("ALGO").Default(content.DefaultHash).Enum(content.SupportedHashAlgorithms()...)
createBlockEncryptionFormat = createCommand.Flag("encryption", "Block encryption algorithm.").PlaceHolder("ALGO").Default(content.DefaultEncryption).Enum(content.SupportedEncryptionAlgorithms()...)
createSplitter = createCommand.Flag("object-splitter", "The splitter to use for new objects in the repository").Default(object.DefaultSplitter).Enum(object.SupportedSplitters...)
createOnly = createCommand.Flag("create-only", "Create repository, but don't connect to it.").Short('c').Bool()
)
func init() {
setupConnectOptions(createCommand)
}
func newRepositoryOptionsFromFlags() *repo.NewRepositoryOptions {
return &repo.NewRepositoryOptions{
BlockFormat: content.FormattingOptions{
Hash: *createBlockHashFormat,
Encryption: *createBlockEncryptionFormat,
},
ObjectFormat: object.Format{
Splitter: *createSplitter,
},
}
}
func ensureEmpty(ctx context.Context, s blob.Storage) error {
hasDataError := errors.New("has data")
err := s.ListBlobs(ctx, "", func(cb blob.Metadata) error {
return hasDataError
})
if err == hasDataError {
return errors.New("found existing data in storage location")
}
return err
}
func runCreateCommandWithStorage(ctx context.Context, st blob.Storage) error {
err := ensureEmpty(ctx, st)
if err != nil {
return errors.Wrap(err, "unable to get repository storage")
}
options := newRepositoryOptionsFromFlags()
password, err := getPasswordFromFlags(ctx, true, false)
if err != nil {
return errors.Wrap(err, "getting password")
}
printStderr("Initializing repository with:\n")
printStderr(" block hash: %v\n", options.BlockFormat.Hash)
printStderr(" encryption: %v\n", options.BlockFormat.Encryption)
printStderr(" splitter: %v\n", options.ObjectFormat.Splitter)
if err := repo.Initialize(ctx, st, options, password); err != nil {
return errors.Wrap(err, "cannot initialize repository")
}
if *createOnly {
return nil
}
if err := runConnectCommandWithStorageAndPassword(ctx, st, password); err != nil {
return errors.Wrap(err, "unable to connect to repository")
}
return populateRepository(ctx, password)
}
func populateRepository(ctx context.Context, password string) error {
rep, err := repo.Open(ctx, repositoryConfigFileName(), password, applyOptionsFromFlags(ctx, nil))
if err != nil {
return errors.Wrap(err, "unable to open repository")
}
defer rep.Close(ctx) //nolint:errcheck
if err := policy.SetPolicy(ctx, rep, policy.GlobalPolicySourceInfo, policy.DefaultPolicy); err != nil {
return errors.Wrap(err, "unable to set global policy")
}
printPolicy(policy.DefaultPolicy, nil)
printStdout("\n")
printStdout("To change the policy use:\n kopia policy set --global <options>\n")
printStdout("or\n kopia policy set <dir> <options>\n")
return nil
}