Skip to content

Commit

Permalink
Use --alias | -R flag instead of --repository | -r for aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
penguwin committed Sep 7, 2020
1 parent 36f1ba3 commit 67be08f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
18 changes: 18 additions & 0 deletions cmd/knoxite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
// GlobalOptions holds all those options that can be set for every command.
type GlobalOptions struct {
Repo string
Alias string
Password string
ConfigURL string
}
Expand Down Expand Up @@ -63,6 +64,7 @@ func main() {
// shutdown.SetTimeout(0)

RootCmd.PersistentFlags().StringVarP(&globalOpts.Repo, "repo", "r", "", "Repository directory to backup to/restore from (default: current working dir)")
RootCmd.PersistentFlags().StringVarP(&globalOpts.Alias, "alias", "R", "", "Repository alias to backup to/restore from")
RootCmd.PersistentFlags().StringVarP(&globalOpts.Password, "password", "p", "", "Password to use for data encryption")
RootCmd.PersistentFlags().StringVarP(&globalOpts.ConfigURL, "configURL", "C", config.DefaultPath(), "Path to the configuration file")

Expand Down Expand Up @@ -92,6 +94,12 @@ func init() {
// It'll use the the default config url unless specified otherwise via the
// ConfigURL flag.
func initConfig() {
// We dont allow both flags to be set as this can lead to unclear instructions.
if RootCmd.PersistentFlags().Changed("repo") && RootCmd.PersistentFlags().Changed("alias") {
log.Fatalf("Specify either repository directory '-r' or an alias '-R'")
return
}

var err error
cfg, err = config.New(globalOpts.ConfigURL)
if err != nil {
Expand All @@ -109,4 +117,14 @@ func initConfig() {
if cfg.Repositories == nil {
cfg.Repositories = make(map[string]config.RepoConfig)
}

if globalOpts.Alias != "" {
rep, ok := cfg.Repositories[globalOpts.Alias]
if !ok {
log.Fatalf("error loading the specified alias\n")
return
}

globalOpts.Repo = rep.Url
}
}
3 changes: 0 additions & 3 deletions cmd/knoxite/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ func openRepository(path, password string) (knoxite.Repository, error) {
}
}

if rep, ok := cfg.Repositories[path]; ok {
return knoxite.OpenRepository(rep.Url, password)
}
return knoxite.OpenRepository(path, password)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/knoxite/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var (
// Values set via the command line flags will overwrite settings stored in the
// configuration file.
func configureRestoreOpts(cmd *cobra.Command, opts *RestoreOptions) {
if rep, ok := cfg.Repositories[globalOpts.Repo]; ok {
if rep, ok := cfg.Repositories[globalOpts.Alias]; ok {
if !cmd.Flags().Changed("excludes") {
opts.Excludes = rep.RestoreExcludes
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/knoxite/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var (
// Values set via the command line flags will overwrite settings stored in the
// configuration file.
func configureStoreOpts(cmd *cobra.Command, opts *StoreOptions) {
if rep, ok := cfg.Repositories[globalOpts.Repo]; ok {
if rep, ok := cfg.Repositories[globalOpts.Alias]; ok {
if !cmd.Flags().Changed("compression") {
opts.Compression = rep.Compression
}
Expand Down

0 comments on commit 67be08f

Please sign in to comment.