sq/cli/cmd_config.go
Neil O'Toole 11525eae10
#199: Last spit n' polish for config overhaul (#219)
* Split 'sq config ls' into its own command

* Tidy up 'sq config' help

* Fixed help text
2023-05-07 00:01:29 -06:00

91 lines
2.1 KiB
Go

package cli
import (
"github.com/neilotoole/sq/cli/config/yamlstore"
"github.com/neilotoole/sq/cli/flag"
"github.com/spf13/cobra"
)
func newConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Args: cobra.NoArgs,
Short: "Manage config",
Long: `View and edit config.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
Example: ` # Print config location
$ sq config location
# Show base config
$ sq config ls
# Show base config including unset and default values.
$ sq config ls -v
# 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
$ sq config set format json
# Set source-specific option value
$ sq config set --src @sakila conn.max-open 50
# Help for an option
$ sq config set format --help
# Edit base config in $EDITOR
$ sq config edit
# Edit config for source in $EDITOR
$ sq config edit @sakila
# Delete option (reset to default value)
$ sq config set -D log.level`,
}
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
$ sq config location
/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
if store, ok := rc.ConfigStore.(*yamlstore.Store); ok {
origin = store.PathOrigin
}
return rc.writers.configw.Location(path, origin)
}