Skip to content

Commit

Permalink
Implement the config command
Browse files Browse the repository at this point in the history
  • Loading branch information
wizeguyy committed Oct 9, 2024
1 parent 9eb9ccd commit 58667d3
Showing 1 changed file with 28 additions and 1 deletion.
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
}

0 comments on commit 58667d3

Please sign in to comment.