2023-05-01 06:59:34 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-05-05 17:32:50 +03:00
|
|
|
"fmt"
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lga"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newConfigSetCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "set",
|
|
|
|
RunE: execConfigSet,
|
2023-05-05 17:32:50 +03:00
|
|
|
Args: cobra.RangeArgs(1, 2),
|
2023-05-01 06:59:34 +03:00
|
|
|
ValidArgsFunction: completeConfigSet,
|
|
|
|
Short: "Set config value",
|
2023-05-05 17:32:50 +03:00
|
|
|
Long: `Set base config value, or set value for a specific source.
|
2023-05-03 15:36:10 +03:00
|
|
|
Use "sq config get -v" to see available options.`,
|
2023-05-05 17:32:50 +03:00
|
|
|
Example: ` # Set base output format
|
2023-05-01 06:59:34 +03:00
|
|
|
$ sq config set format json
|
|
|
|
|
2023-05-05 17:32:50 +03:00
|
|
|
# Set base max DB connections
|
2023-05-01 06:59:34 +03:00
|
|
|
$ sq config set conn.max-open 10
|
|
|
|
|
|
|
|
# Set max DB connections for source @sakila
|
2023-05-05 17:32:50 +03:00
|
|
|
$ sq config set --src @sakila conn.max-open 50
|
|
|
|
|
|
|
|
# Delete an option (resets to default value)
|
|
|
|
$ sq config set -D conn.max-open`,
|
2023-05-01 06:59:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
|
2023-05-05 20:41:22 +03:00
|
|
|
cmd.Flags().BoolP(flag.Compact, flag.CompactShort, false, flag.CompactUsage)
|
2023-05-01 06:59:34 +03:00
|
|
|
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
|
|
|
|
|
|
|
|
cmd.Flags().String(flag.ConfigSrc, "", flag.ConfigSrcUsage)
|
|
|
|
panicOn(cmd.RegisterFlagCompletionFunc(flag.ConfigSrc, completeHandle(1)))
|
2023-05-05 17:32:50 +03:00
|
|
|
|
|
|
|
cmd.Flags().BoolP(flag.ConfigDelete, flag.ConfigDeleteShort, false, flag.ConfigDeleteUsage)
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func execConfigSet(cmd *cobra.Command, args []string) error {
|
2023-05-05 17:32:50 +03:00
|
|
|
log := logFrom(cmd)
|
2023-05-01 06:59:34 +03:00
|
|
|
rc, ctx := RunContextFrom(cmd.Context()), cmd.Context()
|
|
|
|
|
|
|
|
o := rc.Config.Options
|
|
|
|
|
|
|
|
opt := rc.OptionsRegistry.Get(args[0])
|
|
|
|
if opt == nil {
|
|
|
|
return errz.Errorf("invalid config key: %s", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
var src *source.Source
|
|
|
|
if cmdFlagChanged(cmd, flag.ConfigSrc) {
|
|
|
|
handle, err := cmd.Flags().GetString(flag.ConfigSrc)
|
|
|
|
if err != nil {
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err = rc.Config.Collection.Get(handle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if src.Options == nil {
|
|
|
|
src.Options = options.Options{}
|
|
|
|
}
|
|
|
|
|
|
|
|
o = src.Options
|
|
|
|
}
|
|
|
|
|
2023-05-05 17:32:50 +03:00
|
|
|
if cmdFlagChanged(cmd, flag.ConfigDelete) {
|
|
|
|
if len(args) > 1 {
|
|
|
|
return errz.Errorf("accepts 1 arg when used with --%s flag", flag.ConfigDelete)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(o, opt.Key())
|
|
|
|
if src == nil {
|
|
|
|
log.Info("Unset base config value", lga.Key, opt.Key())
|
|
|
|
} else {
|
|
|
|
log.Info("Unset source config value", lga.Src, src, lga.Key, opt.Key())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rc.ConfigStore.Save(ctx, rc.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc.writers.configw.UnsetOption(opt)
|
|
|
|
}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
o2 := options.Options{}
|
|
|
|
o2[opt.Key()] = args[1]
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if o2, err = opt.Process(o2); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
o[opt.Key()] = o2[opt.Key()]
|
|
|
|
if err = rc.ConfigStore.Save(ctx, rc.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 17:32:50 +03:00
|
|
|
if src == nil {
|
|
|
|
log.Info(
|
|
|
|
"Set base config value",
|
|
|
|
lga.Key, opt.Key(),
|
|
|
|
lga.Val, o[opt.Key()],
|
|
|
|
)
|
2023-05-01 06:59:34 +03:00
|
|
|
} else {
|
2023-05-05 17:32:50 +03:00
|
|
|
log.Info(
|
|
|
|
"Set source config value",
|
|
|
|
lga.Key, opt.Key(),
|
|
|
|
lga.Src, src,
|
|
|
|
lga.Val, o,
|
|
|
|
)
|
2023-05-01 06:59:34 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 17:32:50 +03:00
|
|
|
return rc.writers.configw.SetOption(o, opt)
|
2023-05-01 06:59:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func completeConfigSet(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
return completeOptKey(cmd, args, toComplete)
|
2023-05-05 17:32:50 +03:00
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
case 1:
|
2023-05-05 17:32:50 +03:00
|
|
|
if cmdFlagChanged(cmd, flag.ConfigDelete) {
|
|
|
|
logFrom(cmd).Warn(fmt.Sprintf("No 2nd arg when using --%s flag", flag.ConfigDelete))
|
|
|
|
return nil, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
return completeOptValue(cmd, args, toComplete)
|
|
|
|
default:
|
|
|
|
// Maximum of two args
|
|
|
|
return nil, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
}
|