Files
kopia/cli/storage_providers.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

60 lines
1.5 KiB
Go

package cli
import (
"context"
"github.com/pkg/errors"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/kopia/kopia/repo/blob"
)
// RegisterStorageConnectFlags registers repository subcommand to connect to a storage
// or create new repository in a given storage.
func RegisterStorageConnectFlags(
name, description string,
flags func(*kingpin.CmdClause),
connect func(ctx context.Context, isNew bool) (blob.Storage, error),
) {
if name != "from-config" {
// Set up 'create' subcommand
cc := createCommand.Command(name, "Create repository in "+description)
flags(cc)
cc.Action(func(_ *kingpin.ParseContext) error {
ctx := rootContext()
st, err := connect(ctx, true)
if err != nil {
return errors.Wrap(err, "can't connect to storage")
}
return runCreateCommandWithStorage(ctx, st)
})
}
// Set up 'connect' subcommand
cc := connectCommand.Command(name, "Connect to repository in "+description)
flags(cc)
cc.Action(func(_ *kingpin.ParseContext) error {
ctx := rootContext()
st, err := connect(ctx, false)
if err != nil {
return errors.Wrap(err, "can't connect to storage")
}
return runConnectCommandWithStorage(ctx, st)
})
// Set up 'repair' subcommand
cc = repairCommand.Command(name, "Repair repository in "+description)
flags(cc)
cc.Action(func(_ *kingpin.ParseContext) error {
ctx := rootContext()
st, err := connect(ctx, false)
if err != nil {
return errors.Wrap(err, "can't connect to storage")
}
return runRepairCommandWithStorage(ctx, st)
})
}