-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: support loading config from yaml file
- Loading branch information
1 parent
c657ea6
commit 5ebac35
Showing
7 changed files
with
143 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package config | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func Load(path string, conf interface{}) error { | ||
buf, err := os.ReadFile(path) | ||
if err != nil { | ||
return fmt.Errorf("read file: %s: %w", path, err) | ||
} | ||
|
||
dec := yaml.NewDecoder(bytes.NewReader(buf)) | ||
dec.KnownFields(true) | ||
|
||
if err := dec.Decode(conf); err != nil { | ||
return fmt.Errorf("parse config: %s: %w", path, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type fakeConfig struct { | ||
Foo string `yaml:"foo"` | ||
Bar string `yaml:"bar"` | ||
Sub fakeSubConfig `yaml:"sub"` | ||
} | ||
|
||
type fakeSubConfig struct { | ||
Car int `yaml:"car"` | ||
} | ||
|
||
func TestLoad(t *testing.T) { | ||
t.Run("ok", func(t *testing.T) { | ||
f, err := os.CreateTemp("", "pico") | ||
assert.NoError(t, err) | ||
|
||
_, err = f.WriteString(`foo: val1 | ||
bar: val2 | ||
sub: | ||
car: 5`) | ||
assert.NoError(t, err) | ||
|
||
var conf fakeConfig | ||
assert.NoError(t, Load(f.Name(), &conf)) | ||
|
||
assert.Equal(t, "val1", conf.Foo) | ||
assert.Equal(t, "val2", conf.Bar) | ||
assert.Equal(t, 5, conf.Sub.Car) | ||
}) | ||
|
||
t.Run("invalid yaml", func(t *testing.T) { | ||
f, err := os.CreateTemp("", "pico") | ||
assert.NoError(t, err) | ||
|
||
_, err = f.WriteString(`invalid yaml...`) | ||
assert.NoError(t, err) | ||
|
||
var conf fakeConfig | ||
assert.Error(t, Load(f.Name(), &conf)) | ||
}) | ||
|
||
t.Run("not found", func(t *testing.T) { | ||
var conf fakeConfig | ||
assert.Error(t, Load("notfound", &conf)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters