Skip to content

Commit d6acec6

Browse files
committed
fix: config read
1 parent 9e98ee6 commit d6acec6

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
const (
32-
version = "v0.0.5"
32+
version = "v0.0.6"
3333
)
3434

3535
var (

internal/config/read.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/mitchellh/mapstructure"
78

@@ -20,12 +21,18 @@ import (
2021
// - error
2122
// An error if reading or unmarshalling the configuration fails.
2223
func (c *Config) Read(configPath string) error {
24+
25+
if _, err := os.Stat(configPath); os.IsNotExist(err) {
26+
// No configuration file found
27+
return nil
28+
}
29+
2330
// Set the Viper config file path
2431
viper.SetConfigFile(configPath)
2532

2633
// Read the config file
27-
if err := viper.ReadInConfig(); err == nil {
28-
return nil
34+
if err := viper.ReadInConfig(); err != nil {
35+
return fmt.Errorf("Failed to read config file: %v", err)
2936
}
3037

3138
if err := viper.Unmarshal(c, func(d *mapstructure.DecoderConfig) {

internal/config/read_test.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestReadConfig(t *testing.T) {
3131
content := `
3232
server:
3333
api:
34-
skipInsecureVerify: "true"
34+
skipInsecureVerify: true
3535
`
3636
filePath, err := createTempFile(content)
3737
if err != nil {
@@ -43,13 +43,32 @@ server:
4343
err = config.Read(filePath.Name())
4444
assert.NoError(t, err)
4545
assert.Equal(t, true, config.Server.Api.SkipInsecureVerify)
46+
assert.Equal(t, "", config.Server.Api.Token.Get())
47+
})
48+
49+
t.Run("Validate token", func(t *testing.T) {
50+
content := `
51+
server:
52+
api:
53+
token: token
54+
`
55+
filePath, err := createTempFile(content)
56+
if err != nil {
57+
t.Fatalf("Failed to create temp file: %v", err)
58+
}
59+
defer os.Remove(filePath.Name())
60+
61+
config := &Config{}
62+
err = config.Read(filePath.Name())
63+
assert.NoError(t, err)
64+
assert.Equal(t, "token", config.Server.Api.Token.Get())
4665
})
4766

4867
// Test reading a non-existent file
4968
t.Run("Invalid file path", func(t *testing.T) {
5069
config := &Config{}
5170
err := config.Read("path/to/non_existent.yaml")
52-
assert.Error(t, err)
71+
assert.Nil(t, err)
5372
})
5473

5574
t.Run("Invalid YAML", func(t *testing.T) {

internal/config/utils.go

+1
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,6 @@ func customTokenDecodeHook(from reflect.Type, to reflect.Type, data interface{})
9191
// Initialize a Token with the decoded string
9292
var token Token
9393
token.Set(tokenValue)
94+
9495
return token, nil
9596
}

0 commit comments

Comments
 (0)