Skip to content

Commit

Permalink
cli: reorder config priority (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow authored Dec 9, 2024
1 parent 5b859ac commit 79a23f4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
3 changes: 2 additions & 1 deletion cmd/kwil-cli/cmds/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func NewCmdConfigure() *cobra.Command {
return display.PrintErr(cmd, errors.New("cannot configure run in silence mode"))
}

conf, err := config.LoadPersistedConfig()
// config.LoadPersistedConfig() => just the config file
conf, err := config.ActiveConfig() // considering merged config including flags and env
if err != nil {
return display.PrintErr(cmd, err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/kwil-cli/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func NewRootCmd() *cobra.Command {
SilenceUsage: true,
DisableAutoGenTag: true,
PersistentPreRunE: bind.ChainPreRuns(bind.MaybeEnableCLIDebug,
config.PreRunBindFlags, config.PreRunBindConfigFile,
// Config priority, highest to lowest: env, flags, config.json
config.PreRunBindConfigFile, config.PreRunBindFlags, config.PreRunBindEnv,
config.PreRunPrintEffectiveConfig),
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
Expand All @@ -53,7 +54,7 @@ func NewRootCmd() *cobra.Command {
config.BindConfigPath(rootCmd)

// Automatically define flags for all of the fields of the config struct.
config.SetFlags(rootCmd.Flags())
config.SetFlags(rootCmd.PersistentFlags()) // share configs with all subcommands

helpers.BindAssumeYesFlag(rootCmd) // --assume-yes/-Y

Expand Down
16 changes: 9 additions & 7 deletions cmd/kwil-cli/cmds/utils/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ func pingCmd() *cobra.Command {
Long: "Ping the kwil provider endpoint. If successful, returns 'pong'.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return client.DialClient(cmd.Context(), cmd, client.WithoutPrivateKey, func(ctx context.Context, client clientType.Client, cfg *config.KwilCliConfig) error {
res, err := client.Ping(ctx)
if err != nil {
return display.PrintErr(cmd, err)
}
return client.DialClient(cmd.Context(), cmd, client.WithoutPrivateKey,
func(ctx context.Context, client clientType.Client, cfg *config.KwilCliConfig) error {
res, err := client.Ping(ctx)
if err != nil {
return display.PrintErr(cmd, err)
}

return display.PrintCmd(cmd, display.RespString(res))
})
return display.PrintCmd(cmd, display.RespString(res))
},
)
},
}

Expand Down
38 changes: 29 additions & 9 deletions cmd/kwil-cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,32 @@ func BindConfigPath(cmd *cobra.Command) {
configFile, desc)
}

func ConfigFilePath(cmd *cobra.Command) string {
// path, err := cmd.Flags().GetString(configFileFlag)
// if err != nil {
// fmt.Println("Unable to retrieve config file flag value:", err)
// return configFile
// }
// ConfigFilePFlag returns the Flag for the --config flag. If the flag was not
// correctly bound with [BindConfigPath] first, it returns nil.
func ConfigFilePFlag(cmd *cobra.Command) *pflag.Flag {
return cmd.Flags().Lookup(configFileFlag)
}

// ConfigFilePath returns the value bound to the --config flag. If you need to
// know if it was changed from the default or correctly bound, use [ConfigFilePFlag].
func ConfigFilePath() string {
return configFile // bound to configFileFlag by pointer
}

// ConfigDir is the equivalent of filepath.Dir(ConfigFilePath()).
func ConfigDir() string {
return filepath.Dir(configFile)
}

// PreRunBindConfigFile loads and merges settings from the JSON config file.
func PreRunBindConfigFile(cmd *cobra.Command, args []string) error {
cfgPath := ConfigFilePath(cmd)
confFlag := ConfigFilePFlag(cmd)
if confFlag == nil {
return fmt.Errorf("--%s flag is not bound (missing BindConfigPath)", configFileFlag)
}
cfgPath := confFlag.Value.String()
cfgPathSet := confFlag.Changed // if true, error if file not found

cfgPath, err := helpers.ExpandPath(cfgPath)
if err != nil {
return err
Expand All @@ -105,14 +115,24 @@ func PreRunBindConfigFile(cmd *cobra.Command, args []string) error {
if !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("error loading config from %v: %w", confPath, err)
}
// Not an error, just no config file present.
if cfgPathSet {
return fmt.Errorf("specified config file at %v not found", confPath)
}
// Not an error, just no config file present at default location.
bind.Debugf("No config file present at %v", confPath)
}
return nil
}

// PreRunBindFlags binds the current command's flags to the merged config. Use
// this with PersistentPreRunE in the root command to have it run for every
// command, or use with PreRunE for just the current command.
func PreRunBindFlags(cmd *cobra.Command, args []string) error {
flagSet := cmd.Flags()
return PreRunBindFlagset(cmd.Flags(), args)
}

// PreRunBindFlagset is like [PreRunBindFlags] be used for a specific flag set.
func PreRunBindFlagset(flagSet *pflag.FlagSet, args []string) error {
err := k.Load(posflag.ProviderWithFlag(flagSet, ".", nil, /* <- k if we want defaults from the flags' defaults*/
func(f *pflag.Flag) (string, interface{}) {
// if !f.Changed { Debugf("not changed %v", f.Name) }
Expand Down
4 changes: 2 additions & 2 deletions cmd/kwil-cli/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os"

// NOTE: if extensions are used to build a kwild with new transaction
Expand All @@ -12,12 +11,13 @@ import (
// introduced by the consensus extensions.

root "github.com/kwilteam/kwil-db/cmd/kwil-cli/cmds"
"github.com/kwilteam/kwil-db/cmd/kwil-cli/config"
)

func main() {
root := root.NewRootCmd()
if err := root.Execute(); err != nil {
fmt.Println(err)
config.PreRunPrintEffectiveConfig(root, nil) // only when --debug is set
os.Exit(-1)
}
os.Exit(0)
Expand Down

0 comments on commit 79a23f4

Please sign in to comment.