Compare commits

..

No commits in common. "e72c317bbb1b93d5418e2c6fce335362f032a5e0" and "c4ee92e94f433f5e8c8f62c4a8a6c4263c776ff5" have entirely different histories.

22 changed files with 291 additions and 458 deletions

View file

@ -577,7 +577,6 @@ linters-settings:
# List of allowed modules.
# Default: []
modules: # List of allowed modules
- github.com/pelletier/go-toml/v2
- github.com/stretchr/testify
# - gopkg.in/yaml.v2
# List of allowed module domains.

View file

@ -2,10 +2,6 @@
## Unreleased
- Add support for TOML configuration files
- Use stdlib for tests instead of convey
- Update golangci-lint config
## v0.2.0 (2021-12-19)
- Added License
@ -13,5 +9,5 @@
- Added a Loadfiles function to load at once several config files
## v0.1.0 (2020-03-17)
- Initial varsion
- Initial varsion with support for JSON files

View file

@ -1,86 +0,0 @@
package conf
import (
"encoding/json"
"fmt"
"strings"
"github.com/pelletier/go-toml/v2"
)
type filetype int
const (
typeInvalid filetype = iota
typeJSON
typeTOML
)
func getType(filename string) filetype {
switch {
case strings.HasSuffix(filename, ".json"):
return typeJSON
case strings.HasSuffix(filename, ".toml"):
return typeTOML
default:
return typeInvalid
}
}
func unmarshal(ft filetype, data []byte, v interface{}) error {
switch ft {
case typeJSON:
return unmarshalJSON(data, v)
case typeTOML:
return unmarshalTOML(data, v)
default:
return ErrUnsupportedFileType
}
}
func marshal(ft filetype, v interface{}) ([]byte, error) {
switch ft {
case typeJSON:
return marshalJSON(v)
case typeTOML:
return marshalTOML(v)
default:
return nil, ErrUnsupportedFileType
}
}
func unmarshalJSON(data []byte, v interface{}) error {
err := json.Unmarshal(data, v)
if err != nil {
return fmt.Errorf("cannot parse config file: %w", err)
}
return nil
}
func marshalJSON(v interface{}) ([]byte, error) {
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
return nil, fmt.Errorf("cannot generate config content: %w", err)
}
return data, nil
}
func unmarshalTOML(data []byte, v interface{}) error {
err := toml.Unmarshal(data, v)
if err != nil {
return fmt.Errorf("cannot parse config file: %w", err)
}
return nil
}
func marshalTOML(v interface{}) ([]byte, error) {
data, err := toml.Marshal(v)
if err != nil {
return nil, fmt.Errorf("cannot generate config content: %w", err)
}
return data, nil
}

View file

@ -1,25 +1,15 @@
// Package conf defines utils to simplify configuration management.
//
// It provides functions to load and save config files.
//
// Several formats are supported. The encoders/decoders are selected according
// to the extension of the paths passed to functions:
//
// - JSON: ".json"
// - TOML: ".toml"
// Package conf defines utils to simplify configuration
// management.
package conf
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
)
// ErrUnsupportedFileType is returned when the type of the config file is not
// supported.
var ErrUnsupportedFileType = errors.New("unsupported config type")
// LoadFile reads the file at path, parses its json content and fills the struct
// with the content of the file.
func LoadFile(path string, data interface{}) error {
@ -31,7 +21,7 @@ func LoadFile(path string, data interface{}) error {
// If a path does not exist, it is ignored.
//
// It returns an error only if the content of a file is invalid, i.e. it
// cannot be unmarshaled to the struct.
// cannot be unmarshaled by json.
func LoadFiles(data interface{}, paths ...string) error {
for _, p := range paths {
err := read(p, data)
@ -88,13 +78,18 @@ func read(path string, data interface{}) error {
return fmt.Errorf("cannot read config file: %w", err)
}
return unmarshal(getType(path), content, data)
err = json.Unmarshal(content, &data)
if err != nil {
return fmt.Errorf("cannot parse config file: %w", err)
}
return nil
}
func write(path string, data interface{}) error {
content, err := marshal(getType(path), data)
content, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
return fmt.Errorf("cannot generate config content: %w", err)
}
err = os.WriteFile(path, content, 0o600)

View file

@ -11,68 +11,6 @@ import (
"code.bcarlin.xyz/go/conf"
)
func TestJSONFiles(t *testing.T) {
t.Parallel()
runTestSuite(t, "json")
}
func TestTOMLFiles(t *testing.T) {
t.Parallel()
runTestSuite(t, "toml")
}
func TestUnknownFiles(t *testing.T) {
t.Parallel()
t.Run("LoadFile", func(t *testing.T) {
t.Parallel()
s := struct{}{}
err := conf.LoadFile("test_data/valid.unknown", &s)
if assert.Error(t, err) {
assert.ErrorIs(t, err, conf.ErrUnsupportedFileType)
}
})
t.Run("SaveFile", func(t *testing.T) {
t.Parallel()
s := struct{}{}
err := conf.SaveFile("test.unknown", &s)
if assert.Error(t, err) {
assert.ErrorIs(t, err, conf.ErrUnsupportedFileType)
}
assert.NoFileExists(t, "test.unknown")
})
t.Run("LoadAndUpdateFile", func(t *testing.T) {
t.Parallel()
s := struct{}{}
err := conf.LoadAndUpdateFile("test.unknown", &s)
if assert.Error(t, err) {
assert.ErrorIs(t, err, conf.ErrUnsupportedFileType)
}
assert.NoFileExists(t, "test.unknown")
})
}
func runTestSuite(t *testing.T, ext string) {
t.Helper()
testLoadFile(t, ext)
testLoadFiles(t, ext)
testSaveFile(t, ext)
testLoadAndUpdateFile(t, ext)
}
type testconf struct {
inUpdate func()
String string
@ -86,10 +24,7 @@ func (t testconf) Update() {
}
}
func testLoadFile(t *testing.T, ext string) {
t.Helper()
t.Run("LoadFile", func(t *testing.T) {
func TestLoadFile(t *testing.T) {
t.Parallel()
t.Run("with a valid file", func(t *testing.T) {
@ -101,7 +36,7 @@ func testLoadFile(t *testing.T, ext string) {
Invariant: "should not change",
}
file := "test_data/valid." + ext
file := "test_data/valid.json"
err := conf.LoadFile(file, &c)
require.NoError(t, err)
@ -120,7 +55,7 @@ func testLoadFile(t *testing.T, ext string) {
Invariant: "should not change",
}
file := "test_data/invalid." + ext
file := "test_data/invalid.json"
err := conf.LoadFile(file, &c)
require.Error(t, err)
@ -139,7 +74,7 @@ func testLoadFile(t *testing.T, ext string) {
Invariant: "should not change",
}
file := "does-not-exist." + ext
file := "does-not-exist.conf"
err := conf.LoadFile(file, &c)
require.Error(t, err)
@ -148,20 +83,30 @@ func testLoadFile(t *testing.T, ext string) {
assert.Equal(t, 1, c.Int)
assert.Equal(t, "should not change", c.Invariant)
})
})
}
func testLoadFiles(t *testing.T, ext string) {
t.Helper()
t.Run("LoadFiles", func(t *testing.T) {
func TestLoadFiles(t *testing.T) {
t.Parallel()
t.Run("with two valid files with different options", func(t *testing.T) {
t.Parallel()
c := testconf{}
err := conf.LoadFiles(&c, "test_data/part1."+ext, "test_data/part2."+ext)
tmpDir := t.TempDir()
content1 := []byte(`{"String": "foo"}`)
content2 := []byte(`{"Int": 42}`)
paths := []string{
filepath.Join(tmpDir, "file1.json"),
filepath.Join(tmpDir, "file2.json"),
}
err := os.WriteFile(paths[0], content1, 0o600)
require.NoError(t, err)
err = os.WriteFile(paths[1], content2, 0o600)
require.NoError(t, err)
err = conf.LoadFiles(&c, paths...)
require.NoError(t, err)
assert.Equal(t, "foo", c.String)
@ -172,8 +117,21 @@ func testLoadFiles(t *testing.T, ext string) {
t.Parallel()
c := testconf{}
tmpDir := t.TempDir()
err := conf.LoadFiles(&c, "test_data/same1."+ext, "test_data/same2."+ext)
content1 := []byte(`{"String": "foo"}`)
content2 := []byte(`{"String": "bar"}`)
paths := []string{
filepath.Join(tmpDir, "file1.json"),
filepath.Join(tmpDir, "file2.json"),
}
err := os.WriteFile(paths[0], content1, 0o600)
require.NoError(t, err)
err = os.WriteFile(paths[1], content2, 0o600)
require.NoError(t, err)
err = conf.LoadFiles(&c, paths...)
require.NoError(t, err)
assert.Equal(t, "bar", c.String)
@ -183,30 +141,49 @@ func testLoadFiles(t *testing.T, ext string) {
t.Parallel()
c := testconf{}
tmpDir := t.TempDir()
err := conf.LoadFiles(&c, "does-not-exist."+ext, "test_data/valid."+ext)
content2 := []byte(`{"String": "bar"}`)
paths := []string{
"does-not-exist.json",
filepath.Join(tmpDir, "file2.json"),
}
err := os.WriteFile(paths[1], content2, 0o600)
require.NoError(t, err)
assert.Equal(t, "config string", c.String)
err = conf.LoadFiles(&c, paths...)
require.NoError(t, err)
assert.Equal(t, "bar", c.String)
})
t.Run("with one invalid and one valid file", func(t *testing.T) {
t.Run("with one valid and one invalid file", func(t *testing.T) {
t.Parallel()
c := testconf{}
tmpDir := t.TempDir()
err := conf.LoadFiles(&c, "test_data/invalid."+ext, "test_data/valid."+ext)
content1 := []byte(`{"`)
content2 := []byte(`{"String": "bar"}`)
paths := []string{
filepath.Join(tmpDir, "file1.json"),
filepath.Join(tmpDir, "file2.json"),
}
err := os.WriteFile(paths[0], content1, 0o600)
require.NoError(t, err)
err = os.WriteFile(paths[1], content2, 0o600)
require.NoError(t, err)
err = conf.LoadFiles(&c, paths...)
require.Error(t, err)
assert.Equal(t, "", c.String)
})
})
}
func testSaveFile(t *testing.T, ext string) {
t.Helper()
t.Run("SaveFile", func(t *testing.T) {
func TestSaveFile(t *testing.T) {
t.Parallel()
t.Run("with a valid path", func(t *testing.T) {
@ -218,26 +195,24 @@ func testSaveFile(t *testing.T, ext string) {
Int: 1,
}
tmpDir := t.TempDir()
file := filepath.Join(tmpDir, "test."+ext)
file := filepath.Join(tmpDir, "test.conf")
err := conf.SaveFile(file, &c)
require.NoError(t, err)
require.FileExists(t, file)
assert.FileExists(t, file)
expected, err := os.ReadFile("test_data/full." + ext)
require.NoError(t, err)
expected := "{\n \"String\": \"default string\",\n \"Invariant\": \"should not change\",\n \"Int\": 1\n}"
got, err := os.ReadFile(file)
require.NoError(t, err)
assert.Equal(t, string(expected), string(got))
assert.Equal(t, expected, string(got))
})
t.Run("with a valid path and invalid data", func(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
file := filepath.Join(tmpDir, "test."+ext)
file := filepath.Join(tmpDir, "test.conf")
err := conf.SaveFile(file, func() error { return nil })
require.Error(t, err)
@ -253,20 +228,16 @@ func testSaveFile(t *testing.T, ext string) {
Invariant: "should not change",
Int: 1,
}
file := "cannot/write/here." + ext
file := "cannot/write/here.conf"
err := conf.SaveFile(file, &c)
require.Error(t, err)
assert.NoFileExists(t, file)
})
})
}
func testLoadAndUpdateFile(t *testing.T, ext string) {
t.Helper()
t.Run("LoadAndUpdateFile", func(t *testing.T) {
func TestLoadAndUpdateFile(t *testing.T) {
t.Parallel()
t.Run("when the target file does not exist", func(t *testing.T) {
@ -280,13 +251,11 @@ func testLoadAndUpdateFile(t *testing.T, ext string) {
inUpdate: func() { updated = true },
}
tmpDir := t.TempDir()
file := filepath.Join(tmpDir, "does-not-exist."+ext)
file := filepath.Join(tmpDir, "test.conf")
err := conf.LoadAndUpdateFile(file, &c)
require.NoError(t, err)
require.FileExists(t, file)
var c2 testconf
err = conf.LoadFile(file, &c2)
require.NoError(t, err)
@ -308,7 +277,7 @@ func testLoadAndUpdateFile(t *testing.T, ext string) {
inUpdate: func() { updated = true },
}
tmpDir := t.TempDir()
file := filepath.Join(tmpDir, "does-not-exist", "test."+ext)
file := filepath.Join(tmpDir, "does-not-exist", "test.conf")
err := conf.LoadAndUpdateFile(file, &c)
require.Error(t, err)
@ -329,15 +298,13 @@ func testLoadAndUpdateFile(t *testing.T, ext string) {
inUpdate: func() { updated = true },
}
tmpDir := t.TempDir()
file := filepath.Join(tmpDir, "test."+ext)
file := filepath.Join(tmpDir, "test.conf")
content, err := os.ReadFile("test_data/invalid." + ext)
content := []byte(`String: not json`)
err := os.WriteFile(file, content, 0o644)
require.NoError(t, err)
err = os.WriteFile(file, content, 0o600)
require.NoError(t, err)
err = conf.LoadAndUpdateFile("test_data/invalid."+ext, &c)
err = conf.LoadAndUpdateFile(file, &c)
require.Error(t, err)
assert.False(t, updated)
@ -354,12 +321,10 @@ func testLoadAndUpdateFile(t *testing.T, ext string) {
inUpdate: func() { updated = true },
}
tmpDir := t.TempDir()
file := filepath.Join(tmpDir, "test."+ext)
file := filepath.Join(tmpDir, "test.conf")
content, err := os.ReadFile("test_data/valid." + ext)
require.NoError(t, err)
err = os.WriteFile(file, content, 0o600)
content := []byte(`{"String": "config string", "Int": 42}`)
err := os.WriteFile(file, content, 0o644)
require.NoError(t, err)
err = conf.LoadAndUpdateFile(file, &c)
@ -370,7 +335,6 @@ func testLoadAndUpdateFile(t *testing.T, ext string) {
require.NoError(t, err)
assert.Equal(t, "config string", c2.String)
assert.Equal(t, 42, c2.Int)
assert.Equal(t, "should not change", c2.Invariant)
assert.True(t, updated)
})
@ -386,12 +350,10 @@ func testLoadAndUpdateFile(t *testing.T, ext string) {
inUpdate: func() { updated = true },
}
tmpDir := t.TempDir()
file := filepath.Join(tmpDir, "test."+ext)
file := filepath.Join(tmpDir, "test.conf")
content, err := os.ReadFile("test_data/valid." + ext)
require.NoError(t, err)
err = os.WriteFile(file, content, 0o600)
content := []byte(`{"String": "config string"}`)
err := os.WriteFile(file, content, 0o644)
require.NoError(t, err)
err = conf.LoadAndUpdateFile(file, &c)
@ -400,6 +362,7 @@ func testLoadAndUpdateFile(t *testing.T, ext string) {
newContent, err := os.ReadFile(file)
require.NoError(t, err)
assert.Contains(t, string(newContent), "Int") //nolint:usestdlibvars // not the constant here
assert.Contains(t, string(newContent), "Invariant")
assert.True(t, updated)
@ -416,12 +379,10 @@ func testLoadAndUpdateFile(t *testing.T, ext string) {
inUpdate: func() { updated = true },
}
tmpDir := t.TempDir()
file := filepath.Join(tmpDir, "test."+ext)
file := filepath.Join(tmpDir, "test.conf")
content, err := os.ReadFile("test_data/unknown." + ext)
require.NoError(t, err)
err = os.WriteFile(file, content, 0o600)
content := []byte(`{"String": "config string", "Foo": "blah"}`)
err := os.WriteFile(file, content, 0o644)
require.NoError(t, err)
err = conf.LoadAndUpdateFile(file, &c)
@ -429,9 +390,8 @@ func testLoadAndUpdateFile(t *testing.T, ext string) {
newContent, err := os.ReadFile(file)
require.NoError(t, err)
assert.NotContains(t, string(newContent), "Unknown")
assert.NotContains(t, string(newContent), "Foo")
assert.True(t, updated)
})
})
}

1
go.mod
View file

@ -6,7 +6,6 @@ require github.com/stretchr/testify v1.9.0
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

2
go.sum
View file

@ -1,8 +1,6 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View file

@ -1,5 +0,0 @@
{
"String": "default string",
"Invariant": "should not change",
"Int": 1
}

View file

@ -1,3 +0,0 @@
String = 'default string'
Invariant = 'should not change'
Int = 1

View file

@ -1 +0,0 @@
String: not toml

View file

@ -1 +0,0 @@
{"String": "foo"}

View file

@ -1 +0,0 @@
String = "foo"

View file

@ -1 +0,0 @@
{"Int": 42}

View file

@ -1 +0,0 @@
Int = 42

View file

@ -1 +0,0 @@
{"String": "foo"}

View file

@ -1 +0,0 @@
String = "foo"

View file

@ -1 +0,0 @@
{"String": "bar"}

View file

@ -1 +0,0 @@
String = "bar"

View file

@ -1,5 +0,0 @@
{
"String": "config string",
"Int": 42,
"Unknown": "foo"
}

View file

@ -1,3 +0,0 @@
String = "config string"
Int = 42
Unknown = "foo"

View file

@ -1,2 +0,0 @@
string = "config string"
int = 42

View file

@ -1 +0,0 @@
String: not json