-
Notifications
You must be signed in to change notification settings - Fork 51
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: Make config internal to CLI #2310
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
daf61f6
refactor config
nasdf da779cd
update cli with refactored config
nasdf 42296e4
Merge branch 'develop' into nasdf/refactor/config
nasdf 19359da
Merge branch 'develop' into nasdf/refactor/config
nasdf 9739882
move config into cli package
nasdf 6bd738a
fix private key loading logic
nasdf 4ee8d50
move rootdir to cli context
nasdf de99a36
review suggestions
nasdf 19e428b
add config doc
nasdf 7b049a8
Merge branch 'develop' into nasdf/refactor/config
nasdf 9dfb1b6
review fixes
nasdf 25c427b
Merge branch 'develop' into nasdf/refactor/config
nasdf 225f02e
Merge branch 'develop' into nasdf/refactor/config
nasdf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/sourcenetwork/defradb/logging" | ||
) | ||
|
||
const ( | ||
configStoreBadger = "badger" | ||
configStoreMemory = "memory" | ||
configLogFormatJSON = "json" | ||
configLogFormatCSV = "csv" | ||
configLogLevelInfo = "info" | ||
configLogLevelDebug = "debug" | ||
configLogLevelError = "error" | ||
configLogLevelFatal = "fatal" | ||
) | ||
|
||
// configPaths are config keys that will be made relative to the rootdir | ||
var configPaths = []string{ | ||
"datastore.badger.path", | ||
"api.pubkeypath", | ||
"api.privkeypath", | ||
} | ||
|
||
// configFlags is a mapping of config keys to cli flags to bind to. | ||
var configFlags = map[string]string{ | ||
"log.level": "loglevel", | ||
"log.output": "logoutput", | ||
"log.format": "logformat", | ||
"log.stacktrace": "logtrace", | ||
"log.nocolor": "lognocolor", | ||
"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", | ||
} | ||
|
||
// defaultConfig returns a new config with default values. | ||
func defaultConfig() *viper.Viper { | ||
cfg := viper.New() | ||
|
||
cfg.AutomaticEnv() | ||
cfg.SetEnvPrefix("DEFRA") | ||
cfg.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) | ||
|
||
cfg.SetConfigName("config") | ||
cfg.SetConfigType("yaml") | ||
|
||
cfg.SetDefault("datastore.badger.path", "data") | ||
cfg.SetDefault("net.pubSubEnabled", true) | ||
cfg.SetDefault("net.relay", false) | ||
cfg.SetDefault("log.caller", false) | ||
|
||
return cfg | ||
} | ||
|
||
// createConfig writes the default config file if one does not exist. | ||
func createConfig(rootdir string, flags *pflag.FlagSet) error { | ||
cfg := defaultConfig() | ||
fredcarle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cfg.AddConfigPath(rootdir) | ||
|
||
if err := bindConfigFlags(cfg, flags); err != nil { | ||
return err | ||
} | ||
// make sure rootdir exists | ||
if err := os.MkdirAll(rootdir, 0755); err != nil { | ||
return err | ||
} | ||
err := cfg.SafeWriteConfig() | ||
// error type is known and shouldn't be wrapped | ||
// | ||
//nolint:errorlint | ||
if _, ok := err.(viper.ConfigFileAlreadyExistsError); ok { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
// loadConfig returns a new config with values from the config in the given rootdir. | ||
func loadConfig(rootdir string, flags *pflag.FlagSet) (*viper.Viper, error) { | ||
cfg := defaultConfig() | ||
cfg.AddConfigPath(rootdir) | ||
|
||
// attempt to read the existing config | ||
err := cfg.ReadInConfig() | ||
// error type is known and shouldn't be wrapped | ||
// | ||
//nolint:errorlint | ||
if _, ok := err.(viper.ConfigFileNotFoundError); err != nil && !ok { | ||
return nil, err | ||
} | ||
// bind cli flags to config keys | ||
if err := bindConfigFlags(cfg, flags); err != nil { | ||
return nil, err | ||
} | ||
|
||
// make paths relative to the rootdir | ||
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. praise: Thank you for comments like this, it helps a lot when the code itself is non-obvious. |
||
for _, key := range configPaths { | ||
path := cfg.GetString(key) | ||
if path != "" && !filepath.IsAbs(path) { | ||
cfg.Set(key, filepath.Join(rootdir, path)) | ||
} | ||
} | ||
|
||
logCfg := loggingConfig(cfg.Sub("log")) | ||
logCfg.OverridesByLoggerName = make(map[string]logging.Config) | ||
|
||
// apply named logging overrides | ||
for key := range cfg.GetStringMap("log.overrides") { | ||
logCfg.OverridesByLoggerName[key] = loggingConfig(cfg.Sub("log.overrides." + key)) | ||
} | ||
logging.SetConfig(logCfg) | ||
|
||
return cfg, nil | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
// loggingConfig returns a new logging config from the given config. | ||
func loggingConfig(cfg *viper.Viper) logging.Config { | ||
var level int8 | ||
switch value := cfg.GetString("level"); value { | ||
case configLogLevelDebug: | ||
level = logging.Debug | ||
case configLogLevelInfo: | ||
level = logging.Info | ||
case configLogLevelError: | ||
level = logging.Error | ||
case configLogLevelFatal: | ||
level = logging.Fatal | ||
default: | ||
level = logging.Info | ||
} | ||
|
||
var format logging.EncoderFormat | ||
switch value := cfg.GetString("format"); value { | ||
case configLogFormatJSON: | ||
format = logging.JSON | ||
case configLogFormatCSV: | ||
format = logging.CSV | ||
default: | ||
format = logging.CSV | ||
} | ||
|
||
return logging.Config{ | ||
Level: logging.NewLogLevelOption(level), | ||
EnableStackTrace: logging.NewEnableStackTraceOption(cfg.GetBool("stacktrace")), | ||
DisableColor: logging.NewDisableColorOption(cfg.GetBool("nocolor")), | ||
EncoderFormat: logging.NewEncoderFormatOption(format), | ||
OutputPaths: []string{cfg.GetString("output")}, | ||
EnableCaller: logging.NewEnableCallerOption(cfg.GetBool("caller")), | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
praise: This is nice, and keeps both
.md
focused.