config add dump support

This commit is contained in:
siddontang 2014-10-08 16:02:00 +08:00
parent 805de7239b
commit d6f1959da1
2 changed files with 32 additions and 1 deletions

View File

@ -1,7 +1,10 @@
package config package config
import ( import (
"bytes"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/siddontang/go/ioutil2"
"io"
"io/ioutil" "io/ioutil"
) )
@ -123,3 +126,19 @@ func (cfg *LevelDBConfig) Adjust() {
cfg.MaxOpenFiles = 1024 cfg.MaxOpenFiles = 1024
} }
} }
func (cfg *Config) Dump(w io.Writer) error {
e := toml.NewEncoder(w)
e.Indent = ""
return e.Encode(cfg)
}
func (cfg *Config) DumpFile(fileName string) error {
var b bytes.Buffer
if err := cfg.Dump(&b); err != nil {
return err
}
return ioutil2.WriteFileAtomic(fileName, b.Bytes(), 0644)
}

View File

@ -1,13 +1,25 @@
package config package config
import ( import (
"os"
"reflect"
"testing" "testing"
) )
func TestConfig(t *testing.T) { func TestConfig(t *testing.T) {
_, err := NewConfigWithFile("./ledis.toml") cfg, err := NewConfigWithFile("./config.toml")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Remove("./config.toml.bak")
if err := cfg.DumpFile("./config.toml.bak"); err != nil {
t.Fatal(err)
}
if c, err := NewConfigWithFile("./config.toml.bak"); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(cfg, c) {
t.Fatal("must equal")
}
} }