2023-04-19 08:28:09 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-04-26 18:16:42 +03:00
|
|
|
"github.com/neilotoole/sq/cli/config/yamlstore"
|
2023-04-19 08:28:09 +03:00
|
|
|
"github.com/neilotoole/sq/cli/flag"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newConfigCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "config",
|
2023-05-01 06:59:34 +03:00
|
|
|
Args: cobra.NoArgs,
|
2023-04-19 08:28:09 +03:00
|
|
|
Short: "Manage config",
|
2023-05-05 20:41:22 +03:00
|
|
|
Long: `View and edit config.`,
|
2023-04-19 08:28:09 +03:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return cmd.Help()
|
|
|
|
},
|
|
|
|
Example: ` # Print config location
|
2023-04-23 01:31:28 +03:00
|
|
|
$ sq config location
|
|
|
|
|
2023-05-03 15:36:10 +03:00
|
|
|
# Show base config
|
2023-05-07 09:01:29 +03:00
|
|
|
$ sq config ls
|
2023-04-23 01:31:28 +03:00
|
|
|
|
2023-05-03 15:36:10 +03:00
|
|
|
# Show base config including unset and default values.
|
2023-05-07 09:01:29 +03:00
|
|
|
$ sq config ls -v
|
2023-05-03 15:36:10 +03:00
|
|
|
|
2023-05-07 09:01:29 +03:00
|
|
|
# Show base config in maximum detail (YAML format)
|
|
|
|
$ sq config ls -yv
|
|
|
|
|
|
|
|
# Get base value of an option
|
|
|
|
$ sq config get format
|
|
|
|
|
|
|
|
# Get source-specific value of an option
|
|
|
|
$ sq config get --src @sakila conn.max-open
|
|
|
|
|
|
|
|
# Set base option value
|
2023-05-03 15:36:10 +03:00
|
|
|
$ sq config set format json
|
|
|
|
|
2023-05-07 09:01:29 +03:00
|
|
|
# Set source-specific option value
|
|
|
|
$ sq config set --src @sakila conn.max-open 50
|
|
|
|
|
2023-05-07 05:36:34 +03:00
|
|
|
# Help for an option
|
|
|
|
$ sq config set format --help
|
|
|
|
|
2023-05-07 09:01:29 +03:00
|
|
|
# Edit base config in $EDITOR
|
2023-04-23 01:31:28 +03:00
|
|
|
$ sq config edit
|
|
|
|
|
2023-05-07 09:01:29 +03:00
|
|
|
# Edit config for source in $EDITOR
|
2023-05-05 20:41:22 +03:00
|
|
|
$ sq config edit @sakila
|
|
|
|
|
|
|
|
# Delete option (reset to default value)
|
|
|
|
$ sq config set -D log.level`,
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConfigLocationCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "location",
|
|
|
|
Aliases: []string{"loc"},
|
|
|
|
Short: "Print config location",
|
|
|
|
Long: "Print config location. Use --verbose for more detail.",
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
RunE: execConfigLocation,
|
|
|
|
Example: ` # Print config location
|
2023-04-23 01:31:28 +03:00
|
|
|
$ sq config location
|
2023-04-19 08:28:09 +03:00
|
|
|
/Users/neilotoole/.config/sq
|
|
|
|
|
|
|
|
# Print location, also show origin (flag, env, default)
|
|
|
|
$ sq config location -v
|
|
|
|
/Users/neilotoole/.config/sq
|
|
|
|
Origin: env`,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().BoolP(flag.JSON, flag.JSONShort, false, flag.JSONUsage)
|
|
|
|
cmd.Flags().BoolP(flag.YAML, flag.YAMLShort, false, flag.YAMLUsage)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func execConfigLocation(cmd *cobra.Command, _ []string) error {
|
|
|
|
rc := RunContextFrom(cmd.Context())
|
|
|
|
path := rc.ConfigStore.Location()
|
|
|
|
var origin string
|
2023-04-26 18:16:42 +03:00
|
|
|
if store, ok := rc.ConfigStore.(*yamlstore.Store); ok {
|
2023-04-19 08:28:09 +03:00
|
|
|
origin = store.PathOrigin
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc.writers.configw.Location(path, origin)
|
|
|
|
}
|