2023-05-07 09:01:29 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
2023-05-19 17:24:18 +03:00
|
|
|
"github.com/neilotoole/sq/cli/run"
|
2023-05-07 09:01:29 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newConfigListCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls [--src @HANDLE]",
|
2023-05-07 09:29:42 +03:00
|
|
|
Short: "List config options",
|
2023-05-07 09:01:29 +03:00
|
|
|
Long: `Show config. By default, only explicitly set options are shown.
|
|
|
|
Use the -v flag to see all options. When flag --src is provided, show config
|
|
|
|
just for that source.`,
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: execConfigList,
|
|
|
|
Example: ` # Show base config
|
|
|
|
$ sq config ls
|
|
|
|
|
|
|
|
# Also show defaults and unset options
|
|
|
|
$ sq config ls -v
|
|
|
|
|
|
|
|
# Show all config in YAML
|
|
|
|
$ sq config ls -yv
|
|
|
|
|
|
|
|
# Show config for source
|
|
|
|
$ sq config ls --src @sakila
|
|
|
|
|
|
|
|
# Show config for source, including defaults and unset options
|
|
|
|
$ sq config ls --src @sakila -v
|
|
|
|
|
|
|
|
# Show config for active source
|
|
|
|
$ sq config ls --src @active`,
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
addTextFlags(cmd)
|
2023-05-07 09:01:29 +03:00
|
|
|
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
|
|
|
|
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
|
|
|
|
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
|
|
|
|
|
|
|
|
cmd.Flags().String(flag.ConfigSrc, "", flag.ConfigSrcUsage)
|
|
|
|
panicOn(cmd.RegisterFlagCompletionFunc(flag.ConfigSrc, completeHandle(1)))
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func execConfigList(cmd *cobra.Command, _ []string) error {
|
2023-05-19 17:24:18 +03:00
|
|
|
ru := run.FromContext(cmd.Context())
|
2023-05-07 09:01:29 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
o := ru.Config.Options
|
|
|
|
reg := ru.OptionsRegistry
|
2023-05-07 09:01:29 +03:00
|
|
|
|
|
|
|
if cmdFlagChanged(cmd, flag.ConfigSrc) {
|
|
|
|
handle, err := cmd.Flags().GetString(flag.ConfigSrc)
|
|
|
|
if err != nil {
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
src, err := ru.Config.Collection.Get(handle)
|
2023-05-07 09:01:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
o = src.Options
|
|
|
|
if o == nil {
|
|
|
|
o = options.Options{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new registry that only contains Opts applicable
|
|
|
|
// to this source.
|
|
|
|
opts := filterOptionsForSrc(src.Type, reg.Opts()...)
|
|
|
|
reg = &options.Registry{}
|
|
|
|
reg.Add(opts...)
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
return ru.Writers.Config.Options(reg, o)
|
2023-05-07 09:01:29 +03:00
|
|
|
}
|