-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Reorganize global CLI flags #2615
Changes from 4 commits
fcafbc1
854812e
6020ee0
48b1f31
fe9ea42
29224d6
cba497f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
package cli | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
@@ -39,29 +40,29 @@ var configPaths = []string{ | |
"keyring.path", | ||
} | ||
|
||
// configFlags is a mapping of config keys to cli flags to bind to. | ||
// configFlags is a mapping of cli flag names to config keys to bind. | ||
var configFlags = map[string]string{ | ||
"log.level": "log-level", | ||
"log.output": "log-output", | ||
"log.format": "log-format", | ||
"log.stacktrace": "log-stacktrace", | ||
"log.source": "log-source", | ||
"log.overrides": "log-overrides", | ||
"log.nocolor": "log-no-color", | ||
"api.address": "url", | ||
"datastore.maxtxnretries": "max-txn-retries", | ||
"datastore.store": "store", | ||
"datastore.badger.valuelogfilesize": "valuelogfilesize", | ||
"net.peers": "peers", | ||
"net.p2paddresses": "p2paddr", | ||
"net.p2pdisabled": "no-p2p", | ||
"api.allowed-origins": "allowed-origins", | ||
"api.pubkeypath": "pubkeypath", | ||
"api.privkeypath": "privkeypath", | ||
"keyring.namespace": "keyring-namespace", | ||
"keyring.backend": "keyring-backend", | ||
"keyring.path": "keyring-path", | ||
"keyring.disabled": "no-keyring", | ||
"log-level": "log.level", | ||
"log-output": "log.output", | ||
"log-format": "log.format", | ||
"log-stacktrace": "log.stacktrace", | ||
"log-source": "log.source", | ||
"log-overrides": "log.overrides", | ||
"log-no-color": "log.nocolor", | ||
"url": "api.address", | ||
"max-txn-retries": "datastore.maxtxnretries", | ||
"store": "datastore.store", | ||
"valuelogfilesize": "datastore.badger.valuelogfilesize", | ||
"peers": "net.peers", | ||
"p2paddr": "net.p2paddresses", | ||
"no-p2p": "net.p2pdisabled", | ||
"allowed-origins": "api.allowed-origins", | ||
"pubkeypath": "api.pubkeypath", | ||
"privkeypath": "api.privkeypath", | ||
"keyring-namespace": "keyring.namespace", | ||
"keyring-backend": "keyring.backend", | ||
"keyring-path": "keyring.path", | ||
"no-keyring": "keyring.disabled", | ||
} | ||
|
||
// defaultConfig returns a new config with default values. | ||
|
@@ -84,11 +85,11 @@ func defaultConfig() *viper.Viper { | |
} | ||
|
||
// createConfig writes the default config file if one does not exist. | ||
func createConfig(rootdir string, flags *pflag.FlagSet) error { | ||
func createConfig(rootdir string) error { | ||
cfg := defaultConfig() | ||
cfg.AddConfigPath(rootdir) | ||
|
||
if err := bindConfigFlags(cfg, flags); err != nil { | ||
if err := bindConfigFlags(cfg); err != nil { | ||
return err | ||
} | ||
// make sure rootdir exists | ||
|
@@ -106,7 +107,7 @@ func createConfig(rootdir string, flags *pflag.FlagSet) error { | |
} | ||
|
||
// loadConfig returns a new config with values from the config in the given rootdir. | ||
func loadConfig(rootdir string, flags *pflag.FlagSet) (*viper.Viper, error) { | ||
func loadConfig(rootdir string) (*viper.Viper, error) { | ||
cfg := defaultConfig() | ||
cfg.AddConfigPath(rootdir) | ||
|
||
|
@@ -119,7 +120,7 @@ func loadConfig(rootdir string, flags *pflag.FlagSet) (*viper.Viper, error) { | |
return nil, err | ||
} | ||
// bind cli flags to config keys | ||
if err := bindConfigFlags(cfg, flags); err != nil { | ||
if err := bindConfigFlags(cfg); err != nil { | ||
return nil, err | ||
} | ||
|
||
|
@@ -138,6 +139,7 @@ func loadConfig(rootdir string, flags *pflag.FlagSet) (*viper.Viper, error) { | |
Output: cfg.GetString("log.output"), | ||
EnableStackTrace: cfg.GetBool("log.stacktrace"), | ||
EnableSource: cfg.GetBool("log.source"), | ||
DisableColor: cfg.GetBool("log.nocolor"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😁 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The |
||
}) | ||
|
||
// set logging config overrides | ||
|
@@ -147,12 +149,13 @@ func loadConfig(rootdir string, flags *pflag.FlagSet) (*viper.Viper, error) { | |
} | ||
|
||
// bindConfigFlags binds the set of cli flags to config values. | ||
func bindConfigFlags(cfg *viper.Viper, flags *pflag.FlagSet) error { | ||
for key, flag := range configFlags { | ||
err := cfg.BindPFlag(key, flags.Lookup(flag)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
func bindConfigFlags(cfg *viper.Viper) error { | ||
var errs []error | ||
rootFlags.VisitAll(func(f *pflag.Flag) { | ||
errs = append(errs, cfg.BindPFlag(configFlags[f.Name], f)) | ||
}) | ||
startFlags.VisitAll(func(f *pflag.Flag) { | ||
errs = append(errs, cfg.BindPFlag(configFlags[f.Name], f)) | ||
}) | ||
return errors.Join(errs...) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Would be nice to have a consistent usage of
no
anddisable
terminology and theno
prefix to disable a feature.For example: To disable key ring and p2p users use:
no-keyring
andno-p2p
respectively, i.e. leadingno
prefixBut for disabling log, it is
log-no-color
rather thanno-log-color
.Now for config keys we use:
keyring.disabled
net.p2pdisabled
(the disabled term)But for log we have
log.nocolor
instead oflog.colordisabled
IMO would be nice to have at least user facing flags to be predictable / consistent in terminology.