Skip to content

Commit

Permalink
config: Initialize default config struct directly.
Browse files Browse the repository at this point in the history
There is no need to declare a dozen different "default..." vars only to
immediately insert them into a default config struct when the struct
itself could just be initialized with the default values instead.
  • Loading branch information
jholdstock committed May 23, 2024
1 parent 8b6b2e4 commit 17f993a
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions cmd/vspd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,9 @@ import (
flags "github.com/jessevdk/go-flags"
)

const appName = "vspd"

var (
defaultListen = ":8800"
defaultLogLevel = "debug"
defaultMaxLogSize = int64(10)
defaultLogsToKeep = 20
defaultVSPFee = 3.0
defaultNetworkName = "testnet"
defaultHomeDir = dcrutil.AppDataDir(appName, false)
defaultConfigFilename = fmt.Sprintf("%s.conf", appName)
defaultDBFilename = fmt.Sprintf("%s.db", appName)
defaultDcrdHost = "127.0.0.1"
defaultWalletHost = "127.0.0.1"
defaultWebServerDebug = false
defaultBackupInterval = time.Minute * 3
defaultVspClosed = false
defaultDesignation = "Voting Service Provider"
const (
configFilename = "vspd.conf"
dbFilename = "vspd.db"
)

// vspdConfig defines the configuration options for the vspd process.
Expand Down Expand Up @@ -85,6 +70,22 @@ type vspdConfig struct {
walletCerts [][]byte
}

var defaultConfig = vspdConfig{
Listen: ":8800",
LogLevel: "debug",
MaxLogSize: int64(10),
LogsToKeep: 20,
NetworkName: "testnet",
VSPFee: 3.0,
HomeDir: dcrutil.AppDataDir("vspd", false),
DcrdHost: "127.0.0.1",
WalletHosts: "127.0.0.1",
WebServerDebug: false,
BackupInterval: time.Minute * 3,
VspClosed: false,
Designation: "Voting Service Provider",
}

// fileExists reports whether the named file or directory exists.
func fileExists(name string) bool {
if _, err := os.Stat(name); os.IsNotExist(err) {
Expand Down Expand Up @@ -170,23 +171,7 @@ func normalizeAddress(addr, defaultPort string) string {
// while still allowing the user to override settings with config files and
// command line options. Command line options always take precedence.
func loadConfig() (*vspdConfig, error) {

// Default config.
cfg := vspdConfig{
Listen: defaultListen,
LogLevel: defaultLogLevel,
MaxLogSize: defaultMaxLogSize,
LogsToKeep: defaultLogsToKeep,
NetworkName: defaultNetworkName,
VSPFee: defaultVSPFee,
HomeDir: defaultHomeDir,
DcrdHost: defaultDcrdHost,
WalletHosts: defaultWalletHost,
WebServerDebug: defaultWebServerDebug,
BackupInterval: defaultBackupInterval,
VspClosed: defaultVspClosed,
Designation: defaultDesignation,
}
cfg := defaultConfig

// If command line options are requesting help, write it to stdout and exit.
if config.WriteHelp(&cfg) {
Expand Down Expand Up @@ -230,7 +215,7 @@ func loadConfig() (*vspdConfig, error) {

// Create a default config file when one does not exist and the user did
// not specify an override.
configFile := filepath.Join(cfg.HomeDir, defaultConfigFilename)
configFile := filepath.Join(cfg.HomeDir, configFilename)
if !fileExists(configFile) {
preIni := flags.NewIniParser(preParser)
err = preIni.WriteFile(configFile,
Expand Down Expand Up @@ -407,8 +392,8 @@ func loadConfig() (*vspdConfig, error) {
return nil, fmt.Errorf("unknown log level: %s", cfg.LogLevel)
}

// Set the database path
cfg.dbPath = filepath.Join(dataDir, defaultDBFilename)
// Set the database path.
cfg.dbPath = filepath.Join(dataDir, dbFilename)

// If xpub has been provided, create a new database and exit.
if cfg.FeeXPub != "" {
Expand Down

0 comments on commit 17f993a

Please sign in to comment.