Skip to content
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

Do not require config file #2210

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion cmd/go-quai/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/dominant-strategies/go-quai/cmd/utils"
"github.com/dominant-strategies/go-quai/common/constants"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/params"
)
Expand Down Expand Up @@ -46,6 +48,31 @@ func configCmdPreRun(cmd *cobra.Command, args []string) error {
}

func runConfig(cmd *cobra.Command, args []string) error {
log.Global.Info("Creating default config file")
// set config path to read config file
configDir := cmd.Flag(utils.ConfigDirFlag.Name).Value.String()

// Make sure configDir is a valid directory using path/filepath
// filepath.Clean returns the shortest path name equivalent to path by purely lexical processing
configDir = filepath.Clean(configDir)

_, err := os.Stat(configDir)
if err != nil && os.IsNotExist(err) {
// If the directory does not exist, create it
if err := os.MkdirAll(configDir, 0755); err != nil {
log.Global.Fatalf("Failed to create config directory: %s, Error: %v", configDir, err)
}
log.Global.Debugf("Config directory created: %s", configDir)
} else if err != nil {
log.Global.Fatalf("Error accessing config directory: %s, Error: %v", configDir, err)
}
if _, err := os.Stat(filepath.Join(configDir, constants.CONFIG_FILE_NAME)); os.IsNotExist(err) {
err := utils.WriteDefaultConfigFile(configDir, constants.CONFIG_FILE_NAME, constants.CONFIG_FILE_TYPE)
if err != nil {
return err
}
log.Global.WithField("path", filepath.Join(configDir, constants.CONFIG_FILE_NAME)).Info("Initialized new config file.")
} else {
log.Global.WithField("path", filepath.Join(configDir, constants.CONFIG_FILE_NAME)).Fatal("Cannot init config file. File already exists. Either remove this option to run with the existing config file, or delete the existing config file to re-initialize a new one.")
}
return nil
}
14 changes: 9 additions & 5 deletions cmd/go-quai/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ func rootCmdPreRun(cmd *cobra.Command, args []string) error {
viper.SetConfigType(constants.CONFIG_FILE_TYPE)

// Write default config file if it does not exist
if _, err := os.Stat(filepath.Join(configDir, constants.CONFIG_FILE_NAME)); os.IsNotExist(err) {
err := utils.WriteDefaultConfigFile(configDir, constants.CONFIG_FILE_NAME, constants.CONFIG_FILE_TYPE)
if err != nil {
return err
if viper.GetBool(utils.InitConfigFlag.Name) {
if _, err := os.Stat(filepath.Join(configDir, constants.CONFIG_FILE_NAME)); os.IsNotExist(err) {
err := utils.WriteDefaultConfigFile(configDir, constants.CONFIG_FILE_NAME, constants.CONFIG_FILE_TYPE)
if err != nil {
return err
}
log.Global.WithField("path", filepath.Join(configDir, constants.CONFIG_FILE_NAME)).Info("Initialized new config file.")
} else {
log.Global.WithField("path", filepath.Join(configDir, constants.CONFIG_FILE_NAME)).Fatal("Cannot init config file. File already exists. Either remove this option to run with the existing config file, or delete the existing config file to re-initialize a new one.")
}
log.Global.WithField("path", filepath.Join(configDir, constants.CONFIG_FILE_NAME)).Info("Default config file created")
}

// load config from file and environment variables
Expand Down
7 changes: 7 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var Flags = [][]Flag{
var GlobalFlags = []Flag{
ConfigDirFlag,
DataDirFlag,
InitConfigFlag,
AncientDirFlag,
LogLevelFlag,
LogSizeFlag,
Expand Down Expand Up @@ -185,6 +186,12 @@ var (
Usage: "data directory" + generateEnvDoc(c_GlobalFlagPrefix+"data-dir"),
}

InitConfigFlag = Flag{
Name: c_GlobalFlagPrefix + "init-cfg",
Value: false,
Usage: "initialize a new config file, if one does not already exist",
}

AncientDirFlag = Flag{
Name: c_GlobalFlagPrefix + "datadir-ancient",
Value: "",
Expand Down
Loading