Files
kopia/cli/command_repository_set_client.go
Jarek Kowalski d2288c443f cli: major refactoring (#1046)
cli: major refactoring of how CLI commands are registered

The goal is to eliminate flags as global variables to allow for better
testing. Each command and subcommand and most sets of flags are now
their own struct with 'setup()' methods that attached the flags or
subcommand to the provided parent.

This change is 94.3% mechanical, but is fully organic and hand-made.

* introduced cli.appServices interface which provides the environment in which commands run
* remove auto-maintenance global flag
* removed globals in memory_tracking.go
* removed globals from cli_progress.go
* removed globals from the update_check.go
* moved configPath into TheApp
* removed remaining globals from config.go
* refactored logfile to get rid of global variables
* removed 'app' global variable
* linter fixes
* fixed password_*.go build
* fixed BSD build
2021-05-03 10:28:00 -07:00

88 lines
2.2 KiB
Go

package cli
import (
"context"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
)
type commandRepositorySetClient struct {
repoClientOptionsReadOnly bool
repoClientOptionsReadWrite bool
repoClientOptionsDescription []string
repoClientOptionsUsername []string
repoClientOptionsHostname []string
svc appServices
}
func (c *commandRepositorySetClient) setup(svc appServices, parent commandParent) {
cmd := parent.Command("set-client", "Set repository client options.")
cmd.Flag("read-only", "Set repository to read-only").BoolVar(&c.repoClientOptionsReadOnly)
cmd.Flag("read-write", "Set repository to read-write").BoolVar(&c.repoClientOptionsReadWrite)
cmd.Flag("description", "Change description").StringsVar(&c.repoClientOptionsDescription)
cmd.Flag("username", "Change username").StringsVar(&c.repoClientOptionsUsername)
cmd.Flag("hostname", "Change hostname").StringsVar(&c.repoClientOptionsHostname)
cmd.Action(svc.repositoryReaderAction(c.run))
c.svc = svc
}
func (c *commandRepositorySetClient) run(ctx context.Context, rep repo.Repository) error {
var anyChange bool
opt := rep.ClientOptions()
if c.repoClientOptionsReadOnly {
if opt.ReadOnly {
log(ctx).Infof("Repository is already in read-only mode.")
} else {
opt.ReadOnly = true
anyChange = true
log(ctx).Infof("Setting repository to read-only mode.")
}
}
if c.repoClientOptionsReadWrite {
if !opt.ReadOnly {
log(ctx).Infof("Repository is already in read-write mode.")
} else {
opt.ReadOnly = false
anyChange = true
log(ctx).Infof("Setting repository to read-write mode.")
}
}
if v := c.repoClientOptionsDescription; len(v) > 0 {
opt.Description = v[0]
anyChange = true
log(ctx).Infof("Setting description to %v", opt.Description)
}
if v := c.repoClientOptionsUsername; len(v) > 0 {
opt.Username = v[0]
anyChange = true
log(ctx).Infof("Setting local username to %v", opt.Username)
}
if v := c.repoClientOptionsHostname; len(v) > 0 {
opt.Hostname = v[0]
anyChange = true
log(ctx).Infof("Setting local hostname to %v", opt.Hostname)
}
if !anyChange {
return errors.Errorf("no changes")
}
return repo.SetClientOptions(ctx, c.svc.repositoryConfigFileName(), opt)
}