Skip to content

Commit

Permalink
Merge pull request #69 from am3o/main
Browse files Browse the repository at this point in the history
refactor: change the default configuration into a explicit structed
  • Loading branch information
MichaelEischer authored Sep 11, 2023
2 parents 8c8cd94 + 8d3793f commit 99404dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
30 changes: 14 additions & 16 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

type File struct {
Expand All @@ -18,29 +18,27 @@ type File struct {
UpdateConcurrency int `yaml:"updateConcurrency,omitempty"`
}

func NewFromFile(file string) (cfg *File, err error) {
yamlFile, err := os.ReadFile(file)
func NewFromFile(path string) (*File, error) {
yamlFile, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("cannot read config file: %w", err)
}

cfg = &File{
Path: file,
}
err = yaml.Unmarshal(yamlFile, cfg)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal config file: %w", err)
config := File{
Path: path,
Auth: AuthStorage{
StorageMode: "yaml",
Config: CustomMap{
"path": "./auth-storage.yaml",
},
},
}

if cfg.Auth.StorageMode == "" {
cfg.Auth.StorageMode = "yaml"
}
if cfg.Auth.Config == nil {
cfg.Auth.Config = make(CustomMap)
cfg.Auth.Config["path"] = "./auth-storage.yaml"
if err := yaml.Unmarshal(yamlFile, &config); err != nil {
return nil, fmt.Errorf("cannot unmarshal config file: %w", err)
}

return cfg, nil
return &config, nil
}

type AuthStorage struct {
Expand Down
10 changes: 10 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ func (suite *ConfigTestSuite) TestAuthStorageDefaultsFromFile() {
assert.Equal(suite.T(), "./auth-storage.yaml", sut.Auth.Config["path"])
}

func (suite *ConfigTestSuite) TestAuthStorageMixesFromFile() {
sut, err := config.NewFromFile("../../testdata/mixed_auth_storage.yaml")

assert.Nil(suite.T(), err)
assert.NotNil(suite.T(), sut)

assert.Equal(suite.T(), "yaml", sut.Auth.StorageMode)
assert.Equal(suite.T(), "./foo.mixed", sut.Auth.Config["path"])
}

func (suite *ConfigTestSuite) TestCustomAuthStorageFromFile() {
sut, err := config.NewFromFile("../../testdata/custom_auth_storage.yaml")

Expand Down
3 changes: 3 additions & 0 deletions testdata/mixed_auth_storage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
auth:
config:
path: "./foo.mixed"

0 comments on commit 99404dd

Please sign in to comment.