file: improve save and packing
This commit is contained in:
parent
9276f128d9
commit
88ca3d142e
|
@ -3,22 +3,9 @@ package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ReadTOML reads a config model from path of a yml file
|
|
||||||
func ReadTOML(path string, data interface{}) error {
|
|
||||||
file, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return toml.Unmarshal(file, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadJSON reads a config model from path of a yml file
|
// ReadJSON reads a config model from path of a yml file
|
||||||
func ReadJSON(path string, data interface{}) error {
|
func ReadJSON(path string, data interface{}) error {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
|
@ -8,24 +8,6 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestReadTOML(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
|
|
||||||
a := struct {
|
|
||||||
Text string `toml:"text"`
|
|
||||||
}{}
|
|
||||||
|
|
||||||
err := ReadTOML("testfiles/donoexists", &a)
|
|
||||||
assert.Error(err, "could find file ^^")
|
|
||||||
|
|
||||||
err = ReadTOML("testfiles/trash.txt", &a)
|
|
||||||
assert.Error(err, "could marshel file ^^")
|
|
||||||
|
|
||||||
err = ReadTOML("testfiles/ok.toml", &a)
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.Equal("hallo", a.Text)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadJSON(t *testing.T) {
|
func TestReadJSON(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
package file
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/naoina/toml"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TOMLDuration a time.Duration inside toml files
|
||||||
|
type TOMLDuration time.Duration
|
||||||
|
|
||||||
|
// UnmarshalText implements encoding.TextUnmarshaler
|
||||||
|
func (d *TOMLDuration) UnmarshalText(data []byte) error {
|
||||||
|
duration, err := time.ParseDuration(string(data))
|
||||||
|
if err == nil {
|
||||||
|
*d = TOMLDuration(duration)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalText implements encoding.TextMarshaler
|
||||||
|
func (d TOMLDuration) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(time.Duration(d).String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadTOML reads a config model from path of a toml file
|
||||||
|
func ReadTOML(file string, data interface{}) error {
|
||||||
|
f, err := os.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
return toml.NewDecoder(f).Decode(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveTOML to path
|
||||||
|
func SaveTOML(outputFile string, data interface{}) error {
|
||||||
|
tmpFile := outputFile + ".tmp"
|
||||||
|
|
||||||
|
file, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = toml.NewEncoder(file).Encode(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
file.Close()
|
||||||
|
return os.Rename(tmpFile, outputFile)
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package file
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestReadTOML(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
a := struct {
|
||||||
|
Text string `toml:"text"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
err := ReadTOML("testfiles/donoexists", &a)
|
||||||
|
assert.Error(err, "could find file ^^")
|
||||||
|
|
||||||
|
err = ReadTOML("testfiles/trash.txt", &a)
|
||||||
|
assert.Error(err, "could marshel file ^^")
|
||||||
|
|
||||||
|
err = ReadTOML("testfiles/ok.toml", &a)
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal("hallo", a.Text)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSaveTOML(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
type to struct {
|
||||||
|
Value int `toml:"v"`
|
||||||
|
}
|
||||||
|
toSave := to{Value: 3}
|
||||||
|
|
||||||
|
tmpfile, _ := ioutil.TempFile("/tmp", "lib-json-testfile.json")
|
||||||
|
err := SaveTOML(tmpfile.Name(), &toSave)
|
||||||
|
assert.NoError(err, "could not save temp")
|
||||||
|
|
||||||
|
err = SaveTOML(tmpfile.Name(), 3)
|
||||||
|
assert.Error(err, "could not save func")
|
||||||
|
|
||||||
|
toSave.Value = 4
|
||||||
|
err = SaveTOML("/proc/readonly", &toSave)
|
||||||
|
assert.Error(err, "could not save to /dev/null")
|
||||||
|
|
||||||
|
testvalue := to{}
|
||||||
|
err = ReadTOML(tmpfile.Name(), &testvalue)
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal(3, testvalue.Value)
|
||||||
|
os.Remove(tmpfile.Name())
|
||||||
|
}
|
4
go.mod
4
go.mod
|
@ -3,7 +3,6 @@ module dev.sum7.eu/genofire/golang-lib
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v0.3.1
|
|
||||||
github.com/bdlm/log v0.1.20
|
github.com/bdlm/log v0.1.20
|
||||||
github.com/bdlm/std v1.0.1 // indirect
|
github.com/bdlm/std v1.0.1 // indirect
|
||||||
github.com/chenjiandongx/ginprom v0.0.0-20201217063207-fe11b7f55a35
|
github.com/chenjiandongx/ginprom v0.0.0-20201217063207-fe11b7f55a35
|
||||||
|
@ -13,6 +12,9 @@ require (
|
||||||
github.com/gin-gonic/gin v1.7.2
|
github.com/gin-gonic/gin v1.7.2
|
||||||
github.com/go-gormigrate/gormigrate/v2 v2.0.0
|
github.com/go-gormigrate/gormigrate/v2 v2.0.0
|
||||||
github.com/google/uuid v1.2.0
|
github.com/google/uuid v1.2.0
|
||||||
|
github.com/kylelemons/godebug v1.1.0 // indirect
|
||||||
|
github.com/naoina/go-stringutil v0.1.0 // indirect
|
||||||
|
github.com/naoina/toml v0.1.1
|
||||||
github.com/prometheus/client_golang v1.10.0
|
github.com/prometheus/client_golang v1.10.0
|
||||||
github.com/stretchr/testify v1.7.0
|
github.com/stretchr/testify v1.7.0
|
||||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
|
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
|
||||||
|
|
7
go.sum
7
go.sum
|
@ -1,6 +1,5 @@
|
||||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
|
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
|
||||||
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
|
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
|
||||||
|
@ -288,6 +287,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
|
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
|
||||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
|
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
|
||||||
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
||||||
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||||
|
@ -333,6 +334,10 @@ github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||||
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||||
|
github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks=
|
||||||
|
github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0=
|
||||||
|
github.com/naoina/toml v0.1.1 h1:PT/lllxVVN0gzzSqSlHEmP8MJB4MY2U7STGxiouV4X8=
|
||||||
|
github.com/naoina/toml v0.1.1/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E=
|
||||||
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
|
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
|
||||||
github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU=
|
github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU=
|
||||||
github.com/nats-io/nats-server/v2 v2.1.2/go.mod h1:Afk+wRZqkMQs/p45uXdrVLuab3gwv3Z8C4HTBu8GD/k=
|
github.com/nats-io/nats-server/v2 v2.1.2/go.mod h1:Afk+wRZqkMQs/p45uXdrVLuab3gwv3Z8C4HTBu8GD/k=
|
||||||
|
|
Loading…
Reference in New Issue