2023-05-01 06:59:34 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
2023-05-19 17:24:18 +03:00
|
|
|
"github.com/neilotoole/sq/cli/run"
|
2023-05-01 06:59:34 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newConfigGetCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "get",
|
2023-05-07 09:29:42 +03:00
|
|
|
Short: "Show config option",
|
|
|
|
Long: `Show config option. By default, only explicitly set options are shown.
|
2023-05-05 20:41:22 +03:00
|
|
|
Use the -v flag to see all options. When flag --src is provided, show config
|
|
|
|
just for that source.`,
|
2023-05-07 09:01:29 +03:00
|
|
|
Args: cobra.ExactArgs(1),
|
2023-05-01 06:59:34 +03:00
|
|
|
ValidArgsFunction: completeOptKey,
|
|
|
|
RunE: execConfigGet,
|
2023-05-07 09:01:29 +03:00
|
|
|
Example: ` # Show individual base option
|
2023-05-01 06:59:34 +03:00
|
|
|
$ sq config get conn.max-open
|
|
|
|
|
2023-05-07 09:01:29 +03:00
|
|
|
# Show more detail, in YAML
|
|
|
|
$ sq config get conn.max-open -yv
|
2023-05-01 06:59:34 +03:00
|
|
|
|
2023-05-07 09:01:29 +03:00
|
|
|
# Show option for src
|
|
|
|
$ sq config get --src @sakila conn.max-open`,
|
2023-05-01 06:59:34 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 00:42:38 +03:00
|
|
|
addTextFormatFlags(cmd)
|
2023-05-01 06:59:34 +03:00
|
|
|
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
|
|
|
|
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
|
2024-02-09 21:54:37 +03:00
|
|
|
addOptionFlag(cmd.Flags(), OptCompact)
|
2023-05-01 06:59:34 +03:00
|
|
|
|
|
|
|
cmd.Flags().String(flag.ConfigSrc, "", flag.ConfigSrcUsage)
|
2024-01-31 22:25:40 +03:00
|
|
|
panicOn(cmd.RegisterFlagCompletionFunc(flag.ConfigSrc, completeHandle(1, true)))
|
2023-05-01 06:59:34 +03:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func execConfigGet(cmd *cobra.Command, args []string) error {
|
2023-05-19 17:24:18 +03:00
|
|
|
ru := run.FromContext(cmd.Context())
|
2023-05-01 06:59:34 +03:00
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
o := ru.Config.Options
|
|
|
|
reg := ru.OptionsRegistry
|
2023-05-01 06:59:34 +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-01 06:59:34 +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.
|
2023-05-03 15:36:10 +03:00
|
|
|
opts := filterOptionsForSrc(src.Type, reg.Opts()...)
|
2023-05-01 06:59:34 +03:00
|
|
|
reg = &options.Registry{}
|
|
|
|
reg.Add(opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print just a single option, e.g.
|
|
|
|
// $ sq config get conn.max-open
|
|
|
|
opt := reg.Get(args[0])
|
|
|
|
if opt == nil {
|
|
|
|
return errz.Errorf("invalid option key: %s", args[0])
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:24:18 +03:00
|
|
|
return ru.Writers.Config.Opt(o, opt)
|
2023-05-01 06:59:34 +03:00
|
|
|
}
|