2023-04-26 18:16:42 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-05-05 17:32:50 +03:00
|
|
|
"io"
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
"github.com/spf13/cobra"
|
2023-05-05 17:32:50 +03:00
|
|
|
"github.com/spf13/pflag"
|
2023-11-20 04:06:36 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2023-04-26 18:16:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
// cmdFlagAnyChanges returns true (with the name of the flag)
|
|
|
|
// if cmd is non-nil and cmd has at least one of the named flags,
|
|
|
|
// and that flag been changed.
|
|
|
|
func cmdFlagAnyChanged(cmd *cobra.Command, names ...string) (name string, changed bool) {
|
|
|
|
if cmd == nil {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name = range names {
|
|
|
|
if cmdFlagChanged(cmd, name) {
|
|
|
|
return name, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
// cmdFlagIsSetTrue returns true if flag name has been changed
|
2023-04-26 18:16:42 +03:00
|
|
|
// and the flag value is true.
|
2023-05-22 18:08:14 +03:00
|
|
|
// Contrast with cmdFlagIsSetTrue.
|
|
|
|
func cmdFlagIsSetTrue(cmd *cobra.Command, name string) bool {
|
2023-04-26 18:16:42 +03:00
|
|
|
if !cmdFlagChanged(cmd, name) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := cmd.Flags().GetBool(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // Should never happen
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
2023-05-05 17:32:50 +03:00
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
// cmdFlagBool returns the bool value of flag name. If the flag
|
2023-05-22 18:08:14 +03:00
|
|
|
// has not been set, its default value is returned.
|
|
|
|
// Contrast with cmdFlagIsSetTrue.
|
2024-01-15 04:45:34 +03:00
|
|
|
func cmdFlagBool(cmd *cobra.Command, name string) bool {
|
2023-05-22 18:08:14 +03:00
|
|
|
b, err := cmd.Flags().GetBool(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // Should never happen
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-05-05 17:32:50 +03:00
|
|
|
// getBootstrapFlagValue parses osArgs looking for flg. The flag is always
|
2023-11-19 03:05:48 +03:00
|
|
|
// treated as string. This function exists because some components, such
|
|
|
|
// as logging and config, interrogate flags before cobra has loaded.
|
2024-01-15 04:45:34 +03:00
|
|
|
//
|
|
|
|
//nolint:unparam
|
2023-05-05 17:32:50 +03:00
|
|
|
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
|
|
|
|
}
|