Skip to content

Commit

Permalink
Adds various check for config files presence and validity
Browse files Browse the repository at this point in the history
Add checks for empty or non existent config file
Add check for existing contexts folder
Add viper config file consistency check

Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
  • Loading branch information
eddbbt committed Sep 16, 2024
1 parent 0f48d39 commit 0fadbfc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 59 deletions.
13 changes: 1 addition & 12 deletions cmd/cluster/instance_get_cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,7 @@ func doGetClusterConfig(resourceName, resourceNamespace string) error {
fmt.Printf("Created new Context %s\n", contextName)

// Now set the current context to the new one
baseConfig, err := config.LoadBaseConfiguration(configDir)
if err != nil {
// Shoot out a warning, but don't fail
baseConfig = config.BaseConfigFile{}
fmt.Fprintf(os.Stderr, "warn: Could not load configuration file: %s. Will proceed creating a new one\n", err.Error())
}

baseConfig.CurrentContext = contextName
if err := config.SaveBaseConfiguration(configDir, baseConfig); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
config.UpdateBaseConfigWithContext(configDir, contextName)
fmt.Printf("Context switched to %s\n", contextName)

return nil
Expand Down
6 changes: 1 addition & 5 deletions cmd/config/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ func contextsListF(command *cobra.Command, args []string) error {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
baseConfig, err := config.LoadBaseConfiguration(config.GetConfigDir())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
baseConfig := config.GetBaseConfig(configDir)

w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', tabwriter.DiscardEmptyColumns)
// header
Expand Down
28 changes: 4 additions & 24 deletions cmd/config/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,13 @@ func init() {
}

func currentContextF(command *cobra.Command, args []string) error {
baseConfig, err := config.LoadBaseConfiguration(config.GetConfigDir())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

baseConfig := config.GetBaseConfig(config.GetConfigDir())
fmt.Println(baseConfig.CurrentContext)
return nil
}

func currentClusterF(command *cobra.Command, args []string) error {
baseConfig, err := config.LoadBaseConfiguration(config.GetConfigDir())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

baseConfig := config.GetBaseConfig(config.GetConfigDir())
currentContext, err := config.LoadContextConfiguration(config.GetConfigDir(), baseConfig.CurrentContext)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -94,16 +84,6 @@ func setCurrentContextF(command *cobra.Command, args []string) error {
}

func updateCurrentContext(newCurrentContext string) error {
configDir := config.GetConfigDir()
baseConfig, err := config.LoadBaseConfiguration(configDir)
if err != nil {
return err
}
if _, err := config.LoadContextConfiguration(configDir, newCurrentContext); err != nil {
return err
}

baseConfig.CurrentContext = newCurrentContext

return config.SaveBaseConfiguration(configDir, baseConfig)
config.UpdateBaseConfigWithContext(config.GetConfigDir(), newCurrentContext)
return nil
}
15 changes: 1 addition & 14 deletions cmd/housekeeping/realms.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,7 @@ func realmsCreateF(command *cobra.Command, args []string) error {
fmt.Printf("Context %s created successfully\n", contextName)

// Now set the current context to the new one
baseConfig, err := config.LoadBaseConfiguration(configDir)
if err != nil {
// Shoot out a warning, but don't fail
baseConfig = config.BaseConfigFile{}
fmt.Fprintf(os.Stderr, "warn: Could not load configuration file: %s. Will proceed creating a new one\n", err.Error())
}

baseConfig.CurrentContext = contextName
if err := config.SaveBaseConfiguration(configDir, baseConfig); err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "warn: Context not switched")
} else {
fmt.Printf("Context switched to %s\n", contextName)
}
config.UpdateBaseConfigWithContext(configDir, contextName)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
package config

import (
"gopkg.in/yaml.v2"
"os"
"path"

"gopkg.in/yaml.v2"
)

const baseConfigName = "astartectl"
Expand Down Expand Up @@ -58,5 +57,6 @@ func SaveBaseConfiguration(configDir string, configuration BaseConfigFile) error
if err != nil {
return err
}

return os.WriteFile(path.Join(configDir, baseConfigName+".yaml"), contents, 0644)
}
55 changes: 53 additions & 2 deletions config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package config

import (
"errors"
"fmt"
"os"
"path"
"strings"
Expand Down Expand Up @@ -43,7 +44,13 @@ func ConfigureViper(contextOverride string) error {
}

// Now, get the current context
currentContext := viper.Get("context").(string)
currentContextInterface := viper.Get("context")
currentContext := ""
//in case of empty file, the following check used to fail, now it defaults to empty context and enable file rewrite
if currentContextInterface != nil {
currentContext = currentContextInterface.(string)
}

// Check overrides
if contextOverride != "" {
currentContext = contextOverride
Expand Down Expand Up @@ -118,7 +125,16 @@ func contextsDirFromConfigDir(configDir string) string {

func listYamlNames(dirName string) ([]string, error) {
file, err := os.Open(dirName)
if err != nil {

if os.IsNotExist(err) {
//if we cannot open the directory, create it and open again
_ = os.MkdirAll(dirName, 0600)
file, err = os.Open(dirName)
if err != nil {
return nil, err
}

} else if err != nil {
return nil, err
}
defer file.Close()
Expand Down Expand Up @@ -178,3 +194,38 @@ func ensureConfigDirectoryStructure(configDir string) error {
}
return nil
}

func GetBaseConfig(configDir string) BaseConfigFile {

baseConfig, err := LoadBaseConfiguration(configDir)
if err != nil {
// Shoot out a warning, but don't fail
baseConfig = BaseConfigFile{}
fmt.Fprintf(os.Stderr, "warn: Could not load configuration file: %s. Will proceed creating a new one\n", err.Error())
// Now set the current context to the new one
baseConfig.CurrentContext = ""

if err := SaveBaseConfiguration(configDir, baseConfig); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
return baseConfig
}
func UpdateBaseConfigWithContext(configDir, context string) BaseConfigFile {
baseConfig, err := LoadBaseConfiguration(configDir)
if err != nil {
// Shoot out a warning, but don't fail
baseConfig = BaseConfigFile{}
fmt.Fprintf(os.Stderr, "warn: Could not load configuration file: %s. Will proceed creating a new one\n", err.Error())
// Now set the current context to the new one
}

baseConfig.CurrentContext = context

if err := SaveBaseConfiguration(configDir, baseConfig); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return baseConfig
}

0 comments on commit 0fadbfc

Please sign in to comment.