Skip to content

Commit

Permalink
fix: ask user to create config file if nonexistent
Browse files Browse the repository at this point in the history
  • Loading branch information
synackd committed Dec 9, 2024
1 parent 7a417b2 commit be8263c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
12 changes: 12 additions & 0 deletions cmd/config-cluster-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cmd

import (
"errors"
"os"

"github.com/OpenCHAMI/ochami/internal/config"
Expand Down Expand Up @@ -62,6 +63,17 @@ with a different base URL will change the base URL for the 'foobar' cluster.`,
fileToModify = config.UserConfigFile
}

// Ask user to create file if it does not exist
if err := AskToCreate(fileToModify); err != nil {
if errors.Is(err, UserDeclinedError) {
log.Logger.Info().Msgf("user declined creating config file %s, exiting")
os.Exit(0)
} else {
log.Logger.Error().Err(err).Msgf("failed to create %s")
os.Exit(1)
}
}

// Read in config from file
cfg, err := config.ReadConfig(fileToModify)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions cmd/config-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cmd

import (
"errors"
"os"
"strings"

Expand Down Expand Up @@ -54,6 +55,17 @@ This command does not handle cluster configs. For that, use the
fileToModify = config.UserConfigFile
}

// Ask user to create file if it does not exist
if err := AskToCreate(fileToModify); err != nil {
if errors.Is(err, UserDeclinedError) {
log.Logger.Info().Msgf("user declined creating config file %s, exiting")
os.Exit(0)
} else {
log.Logger.Error().Err(err).Msgf("failed to create %s")
os.Exit(1)
}
}

// Refuse to modify config if user tries to modify cluster config
if strings.HasPrefix(args[0], "clusters") {
log.Logger.Error().Msg("`ochami config set` is meant for modifying general config, use `ochami config cluster set` for modifying cluster config")
Expand Down
60 changes: 38 additions & 22 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd

import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -24,6 +25,9 @@ const (
)

var (
// Errors
UserDeclinedError = fmt.Errorf("user declined")

configFile string
logLevel string
logFormat string
Expand Down Expand Up @@ -112,6 +116,34 @@ func InitLogging() {
log.Logger.Debug().Msg("logging has been initialized")
}

// AskToCreate prompts the user to, if path does not exist, to create a blank
// file at path. If it exists, nil is returned. If the user declines, a
// UserDeclinedError is returned. If an error occurs during creation, an error
// is returned.
func AskToCreate(path string) error {
if path == "" {
return fmt.Errorf("path cannot be empty")
}
if _, err := os.Stat(path); os.IsNotExist(err) {
respConfigCreate := loopYesNo(fmt.Sprintf("%s does not exist. Create it?", path))
if respConfigCreate {
parentDir := filepath.Dir(path)
if err := os.MkdirAll(parentDir, 0755); err != nil {
return fmt.Errorf("could not create parent dir %s: %w", parentDir, err)
}
f, err := os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("creating %s failed: %w", path, err)
}
f.Close()
} else {
return UserDeclinedError
}
}

return nil
}

func InitConfig() {
// Do not read or write config file if --ignore-config passed
if rootCmd.Flag("ignore-config").Changed {
Expand All @@ -120,29 +152,13 @@ func InitConfig() {

if configFile != "" {
// Try to create config file with default values if it doesn't exist
if _, err := os.Stat(configFile); os.IsNotExist(err) {
respConfigCreate := loopYesNo(fmt.Sprintf("Config file %s does not exist. Create it?", configFile))
if respConfigCreate {
configDir := filepath.Dir(configFile)
err := os.MkdirAll(configDir, 0755)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: could not create config dir %s: %v\n", config.ProgName, configDir, err)
os.Exit(1)
}
f, err := os.OpenFile(configFile, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: creating %s failed: %v\n", config.ProgName, configFile, err)
os.Exit(1)
}
f.Close()
err = config.WriteConfig(configFile, config.GlobalConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: writing %s failed: %v\n", config.ProgName, configFile, err)
os.Exit(1)
}
} else {
fmt.Fprintf(os.Stderr, "%s: not creating config file. Exiting...\n", config.ProgName)
if err := AskToCreate(configFile); err != nil {
if errors.Is(err, UserDeclinedError) {
fmt.Fprintf(os.Stderr, "%s: user declined to create file; exiting...\n", config.ProgName)
os.Exit(0)
} else {
fmt.Fprintf(os.Stderr, "%s: failed to create %s: %v\n", config.ProgName, configFile, err)
os.Exit(1)
}
}
}
Expand Down

0 comments on commit be8263c

Please sign in to comment.