diff --git a/cmd/root.go b/cmd/root.go index 96e58bd..e25a0ec 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,7 +29,7 @@ import ( ) const ( - version = "v0.0.5" + version = "v0.0.6" ) var ( diff --git a/internal/config/read.go b/internal/config/read.go index 53ab5f9..466e63d 100644 --- a/internal/config/read.go +++ b/internal/config/read.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "os" "github.com/mitchellh/mapstructure" @@ -20,12 +21,18 @@ import ( // - error // An error if reading or unmarshalling the configuration fails. func (c *Config) Read(configPath string) error { + + if _, err := os.Stat(configPath); os.IsNotExist(err) { + // No configuration file found + return nil + } + // Set the Viper config file path viper.SetConfigFile(configPath) // Read the config file - if err := viper.ReadInConfig(); err == nil { - return nil + if err := viper.ReadInConfig(); err != nil { + return fmt.Errorf("Failed to read config file: %v", err) } if err := viper.Unmarshal(c, func(d *mapstructure.DecoderConfig) { diff --git a/internal/config/read_test.go b/internal/config/read_test.go index 202c7e9..329a267 100644 --- a/internal/config/read_test.go +++ b/internal/config/read_test.go @@ -31,7 +31,7 @@ func TestReadConfig(t *testing.T) { content := ` server: api: - skipInsecureVerify: "true" + skipInsecureVerify: true ` filePath, err := createTempFile(content) if err != nil { @@ -43,13 +43,32 @@ server: err = config.Read(filePath.Name()) assert.NoError(t, err) assert.Equal(t, true, config.Server.Api.SkipInsecureVerify) + assert.Equal(t, "", config.Server.Api.Token.Get()) + }) + + t.Run("Validate token", func(t *testing.T) { + content := ` +server: + api: + token: token +` + filePath, err := createTempFile(content) + if err != nil { + t.Fatalf("Failed to create temp file: %v", err) + } + defer os.Remove(filePath.Name()) + + config := &Config{} + err = config.Read(filePath.Name()) + assert.NoError(t, err) + assert.Equal(t, "token", config.Server.Api.Token.Get()) }) // Test reading a non-existent file t.Run("Invalid file path", func(t *testing.T) { config := &Config{} err := config.Read("path/to/non_existent.yaml") - assert.Error(t, err) + assert.Nil(t, err) }) t.Run("Invalid YAML", func(t *testing.T) { diff --git a/internal/config/utils.go b/internal/config/utils.go index 25dcd9f..4357d9c 100644 --- a/internal/config/utils.go +++ b/internal/config/utils.go @@ -91,5 +91,6 @@ func customTokenDecodeHook(from reflect.Type, to reflect.Type, data interface{}) // Initialize a Token with the decoded string var token Token token.Set(tokenValue) + return token, nil }