Refactor config into separate package

This commit is contained in:
Jakob Borg 2014-05-14 21:18:09 -03:00
parent cba554d0fa
commit f8e34c083e
6 changed files with 37 additions and 30 deletions

View File

@ -14,6 +14,7 @@ import (
"time"
"code.google.com/p/go.crypto/bcrypt"
"github.com/calmh/syncthing/config"
"github.com/calmh/syncthing/logger"
"github.com/codegangsta/martini"
)
@ -39,7 +40,7 @@ func init() {
l.AddHandler(logger.LevelWarn, showGuiError)
}
func startGUI(cfg GUIConfiguration, m *Model) error {
func startGUI(cfg config.GUIConfiguration, m *Model) error {
listener, err := net.Listen("tcp", cfg.Address)
if err != nil {
return err

View File

@ -20,6 +20,7 @@ import (
"strings"
"time"
"github.com/calmh/syncthing/config"
"github.com/calmh/syncthing/discover"
"github.com/calmh/syncthing/logger"
"github.com/calmh/syncthing/protocol"
@ -53,7 +54,7 @@ func init() {
}
var (
cfg Configuration
cfg config.Configuration
myID string
confDir string
rateBucket *ratelimit.Bucket
@ -170,7 +171,7 @@ func main() {
cf, err := os.Open(cfgFile)
if err == nil {
// Read config.xml
cfg, err = readConfigXML(cf, myID)
cfg, err = config.Load(cf, myID)
if err != nil {
l.Fatalln(err)
}
@ -181,15 +182,15 @@ func main() {
defaultRepo := filepath.Join(getHomeDir(), "Sync")
ensureDir(defaultRepo, 0755)
cfg, err = readConfigXML(nil, myID)
cfg.Repositories = []RepositoryConfiguration{
cfg, err = config.Load(nil, myID)
cfg.Repositories = []config.RepositoryConfiguration{
{
ID: "default",
Directory: defaultRepo,
Nodes: []NodeConfiguration{{NodeID: myID}},
Nodes: []config.NodeConfiguration{{NodeID: myID}},
},
}
cfg.Nodes = []NodeConfiguration{
cfg.Nodes = []config.NodeConfiguration{
{
NodeID: myID,
Addresses: []string{"dynamic"},
@ -452,7 +453,7 @@ func saveConfigLoop(cfgFile string) {
continue
}
err = writeConfigXML(fd, cfg)
err = config.Save(fd, cfg)
if err != nil {
l.Warnln(err)
fd.Close()

View File

@ -14,6 +14,7 @@ import (
"github.com/calmh/syncthing/buffers"
"github.com/calmh/syncthing/cid"
"github.com/calmh/syncthing/config"
"github.com/calmh/syncthing/files"
"github.com/calmh/syncthing/lamport"
"github.com/calmh/syncthing/protocol"
@ -526,7 +527,7 @@ func (m *Model) broadcastIndexLoop() {
}
}
func (m *Model) AddRepo(id, dir string, nodes []NodeConfiguration) {
func (m *Model) AddRepo(id, dir string, nodes []config.NodeConfiguration) {
if m.started {
panic("cannot add repo to started model")
}

View File

@ -9,6 +9,7 @@ import (
"github.com/calmh/syncthing/buffers"
"github.com/calmh/syncthing/cid"
"github.com/calmh/syncthing/config"
"github.com/calmh/syncthing/protocol"
"github.com/calmh/syncthing/scanner"
)
@ -175,7 +176,7 @@ func (p *puller) run() {
}
err := p.model.ScanRepo(p.repo)
if err != nil {
invalidateRepo(p.repo, err)
invalidateRepo(cfg, p.repo, err)
return
}
@ -196,7 +197,7 @@ func (p *puller) runRO() {
}
err := p.model.ScanRepo(p.repo)
if err != nil {
invalidateRepo(p.repo, err)
invalidateRepo(cfg, p.repo, err)
return
}
}
@ -562,3 +563,13 @@ func (p *puller) closeFile(f scanner.File) {
l.Debugf("pull: error: %q / %q: %v", p.repo, f.Name, err)
}
}
func invalidateRepo(cfg config.Configuration, repoID string, err error) {
for i := range cfg.Repositories {
repo := &cfg.Repositories[i]
if repo.ID == repoID {
repo.Invalid = err.Error()
return
}
}
}

View File

@ -1,4 +1,4 @@
package main
package config
import (
"encoding/xml"
@ -10,8 +10,11 @@ import (
"strconv"
"code.google.com/p/go.crypto/bcrypt"
"github.com/calmh/syncthing/logger"
)
var l = logger.DefaultLogger
type Configuration struct {
Version int `xml:"version,attr" default:"2"`
Repositories []RepositoryConfiguration `xml:"repository"`
@ -131,7 +134,7 @@ func fillNilSlices(data interface{}) error {
return nil
}
func writeConfigXML(wr io.Writer, cfg Configuration) error {
func Save(wr io.Writer, cfg Configuration) error {
e := xml.NewEncoder(wr)
e.Indent("", " ")
err := e.Encode(cfg)
@ -156,7 +159,7 @@ func uniqueStrings(ss []string) []string {
return us
}
func readConfigXML(rd io.Reader, myID string) (Configuration, error) {
func Load(rd io.Reader, myID string) (Configuration, error) {
var cfg Configuration
setDefaults(&cfg)
@ -304,13 +307,3 @@ func ensureNodePresent(nodes []NodeConfiguration, myID string) []NodeConfigurati
return nodes
}
func invalidateRepo(repoID string, err error) {
for i := range cfg.Repositories {
repo := &cfg.Repositories[i]
if repo.ID == repoID {
repo.Invalid = err.Error()
return
}
}
}

View File

@ -1,4 +1,4 @@
package main
package config
import (
"bytes"
@ -23,7 +23,7 @@ func TestDefaultValues(t *testing.T) {
UPnPEnabled: true,
}
cfg, err := readConfigXML(bytes.NewReader(nil), "nodeID")
cfg, err := Load(bytes.NewReader(nil), "nodeID")
if err != io.EOF {
t.Error(err)
}
@ -66,7 +66,7 @@ func TestNodeConfig(t *testing.T) {
`)
for i, data := range [][]byte{v1data, v2data} {
cfg, err := readConfigXML(bytes.NewReader(data), "node1")
cfg, err := Load(bytes.NewReader(data), "node1")
if err != nil {
t.Error(err)
}
@ -121,7 +121,7 @@ func TestNoListenAddress(t *testing.T) {
</configuration>
`)
cfg, err := readConfigXML(bytes.NewReader(data), "nodeID")
cfg, err := Load(bytes.NewReader(data), "nodeID")
if err != nil {
t.Error(err)
}
@ -170,7 +170,7 @@ func TestOverriddenValues(t *testing.T) {
UPnPEnabled: false,
}
cfg, err := readConfigXML(bytes.NewReader(data), "nodeID")
cfg, err := Load(bytes.NewReader(data), "nodeID")
if err != nil {
t.Error(err)
}
@ -215,7 +215,7 @@ func TestNodeAddresses(t *testing.T) {
},
}
cfg, err := readConfigXML(bytes.NewReader(data), "n4")
cfg, err := Load(bytes.NewReader(data), "n4")
if err != nil {
t.Error(err)
}