From 9eb9ccd201b224b7578ddfb8e01111e5d2d284e6 Mon Sep 17 00:00:00 2001 From: wizeguyy Date: Wed, 9 Oct 2024 14:48:13 -0500 Subject: [PATCH 1/2] Do not require config file --- cmd/go-quai/root.go | 14 +++++++++----- cmd/utils/flags.go | 7 +++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/go-quai/root.go b/cmd/go-quai/root.go index df51a6753c..05aafb3585 100644 --- a/cmd/go-quai/root.go +++ b/cmd/go-quai/root.go @@ -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 diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 8d62ce6577..7abcab481f 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -63,6 +63,7 @@ var Flags = [][]Flag{ var GlobalFlags = []Flag{ ConfigDirFlag, DataDirFlag, + InitConfigFlag, AncientDirFlag, LogLevelFlag, LogSizeFlag, @@ -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: "", From 58667d3f68019af275d121979bb89da427f45b78 Mon Sep 17 00:00:00 2001 From: wizeguyy Date: Wed, 9 Oct 2024 15:15:58 -0500 Subject: [PATCH 2/2] Implement the config command --- cmd/go-quai/config.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/cmd/go-quai/config.go b/cmd/go-quai/config.go index 4f6d63bc7d..454d2d07a9 100644 --- a/cmd/go-quai/config.go +++ b/cmd/go-quai/config.go @@ -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" ) @@ -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 }