mirror of
https://github.com/neilotoole/sq.git
synced 2024-11-28 21:11:48 +03:00
f0aa65791b
* CHANGELOG text clarification * Dialing in config/options * Yet more dialing in of config/options * Refactor output writers * YAML output for more commands
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
)
|
|
|
|
// cmdFlagChanged returns true if cmd is non-nil and
|
|
// has the named flag and that flag been changed.
|
|
func cmdFlagChanged(cmd *cobra.Command, name string) bool {
|
|
if cmd == nil {
|
|
return false
|
|
}
|
|
|
|
f := cmd.Flag(name)
|
|
if f == nil {
|
|
return false
|
|
}
|
|
|
|
return f.Changed
|
|
}
|
|
|
|
// cmdFlagTrue returns true if flag name has been changed
|
|
// and the flag value is true.
|
|
func cmdFlagTrue(cmd *cobra.Command, name string) bool {
|
|
if !cmdFlagChanged(cmd, name) {
|
|
return false
|
|
}
|
|
|
|
b, err := cmd.Flags().GetBool(name)
|
|
if err != nil {
|
|
panic(err) // Should never happen
|
|
}
|
|
|
|
return b
|
|
}
|
|
|
|
// getBootstrapFlagValue parses osArgs looking for flg. The flag is always
|
|
// treated as string. This function exists because some components such
|
|
// as logging and config interrogate flags before cobra has loaded.
|
|
func getBootstrapFlagValue(flg, flgShort, flgUsage string, osArgs []string) (val string, ok bool, err error) {
|
|
fs := pflag.NewFlagSet("bootstrap", pflag.ContinueOnError)
|
|
fs.ParseErrorsWhitelist.UnknownFlags = true
|
|
fs.SetOutput(io.Discard)
|
|
|
|
_ = fs.StringP(flg, flgShort, "", flgUsage)
|
|
if err = fs.Parse(osArgs); err != nil {
|
|
return "", false, errz.Err(err)
|
|
}
|
|
|
|
if !fs.Changed(flg) {
|
|
return "", false, nil
|
|
}
|
|
|
|
if val, err = fs.GetString(flg); err != nil {
|
|
return "", false, errz.Err(err)
|
|
}
|
|
|
|
return val, true, nil
|
|
}
|