From c6ab37a59f3975165f98d25bb29d7a9b3d7481a7 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 1 Jul 2024 18:09:58 +0100 Subject: [PATCH] flags: factor AddFlagsFromOptions from cmd This is in preparation for generalising the backend config --- cmd/cmd.go | 35 ++--------------------- fs/config/flags/flags.go | 62 ++++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 51 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index b6f4bc4ba..203253601 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -522,41 +522,12 @@ var backendFlags map[string]struct{} func AddBackendFlags() { backendFlags = map[string]struct{}{} for _, fsInfo := range fs.Registry { - done := map[string]struct{}{} + flags.AddFlagsFromOptions(pflag.CommandLine, fsInfo.Prefix, fsInfo.Options) + // Store the backend flag names for the help generator for i := range fsInfo.Options { opt := &fsInfo.Options[i] - // Skip if done already (e.g. with Provider options) - if _, doneAlready := done[opt.Name]; doneAlready { - continue - } - done[opt.Name] = struct{}{} - // Make a flag from each option name := opt.FlagName(fsInfo.Prefix) - found := pflag.CommandLine.Lookup(name) != nil - if !found { - // Take first line of help only - help := strings.TrimSpace(opt.Help) - if nl := strings.IndexRune(help, '\n'); nl >= 0 { - help = help[:nl] - } - help = strings.TrimRight(strings.TrimSpace(help), ".!?") - if opt.IsPassword { - help += " (obscured)" - } - flag := pflag.CommandLine.VarPF(opt, name, opt.ShortOpt, help) - flags.SetDefaultFromEnv(pflag.CommandLine, name) - if _, isBool := opt.Default.(bool); isBool { - flag.NoOptDefVal = "true" - } - // Hide on the command line if requested - if opt.Hide&fs.OptionHideCommandLine != 0 { - flag.Hidden = true - } - backendFlags[name] = struct{}{} - } else { - fs.Errorf(nil, "Not adding duplicate flag --%s", name) - } - // flag.Hidden = true + backendFlags[name] = struct{}{} } } } diff --git a/fs/config/flags/flags.go b/fs/config/flags/flags.go index ca20402d2..25183fa0f 100644 --- a/fs/config/flags/flags.go +++ b/fs/config/flags/flags.go @@ -151,7 +151,7 @@ func installFlag(flags *pflag.FlagSet, name string, groupsString string) { } // Add flag to Group if it is a global flag - if flags == pflag.CommandLine { + if groupsString != "" && flags == pflag.CommandLine { for _, groupName := range strings.Split(groupsString, ",") { if groupName == "rc-" { groupName = "RC" @@ -165,24 +165,6 @@ func installFlag(flags *pflag.FlagSet, name string, groupsString string) { } } -// SetDefaultFromEnv constructs a name from the flag passed in and -// sets the default from the environment if possible -// -// Used to create backend flags like --skip-links -func SetDefaultFromEnv(flags *pflag.FlagSet, name string) { - envKey := fs.OptionToEnv(name) - envValue, found := os.LookupEnv(envKey) - if found { - flag := flags.Lookup(name) - if flag == nil { - log.Fatalf("Couldn't find flag --%q", name) - } - fs.Debugf(nil, "Setting default for %s=%q from environment variable %s", name, envValue, envKey) - //err = tempValue.Set() - flag.DefValue = envValue - } -} - // StringP defines a flag which can be set by an environment variable // // It is a thin wrapper around pflag.StringP @@ -350,3 +332,45 @@ func CountVarP(flags *pflag.FlagSet, p *int, name, shorthand string, usage strin flags.CountVarP(p, name, shorthand, usage) installFlag(flags, name, groups) } + +// AddFlagsFromOptions takes a slice of fs.Option and adds flags for all of them +func AddFlagsFromOptions(flags *pflag.FlagSet, prefix string, options fs.Options) { + done := map[string]struct{}{} + for i := range options { + opt := &options[i] + // Skip if done already (e.g. with Provider options) + if _, doneAlready := done[opt.Name]; doneAlready { + continue + } + done[opt.Name] = struct{}{} + // Make a flag from each option + if prefix == "" { + opt.NoPrefix = true + } + name := opt.FlagName(prefix) + found := flags.Lookup(name) != nil + if !found { + // Take first line of help only + help := strings.TrimSpace(opt.Help) + if nl := strings.IndexRune(help, '\n'); nl >= 0 { + help = help[:nl] + } + help = strings.TrimRight(strings.TrimSpace(help), ".!?") + if opt.IsPassword { + help += " (obscured)" + } + flag := flags.VarPF(opt, name, opt.ShortOpt, help) + installFlag(flags, name, opt.Groups) + if _, isBool := opt.Default.(bool); isBool { + flag.NoOptDefVal = "true" + } + // Hide on the command line if requested + if opt.Hide&fs.OptionHideCommandLine != 0 { + flag.Hidden = true + } + } else { + fs.Errorf(nil, "Not adding duplicate flag --%s", name) + } + // flag.Hidden = true + } +}