From f8e34c083ec64f93a1f53aed213d2a79edcb5b46 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 14 May 2014 21:18:09 -0300 Subject: [PATCH] Refactor config into separate package --- cmd/syncthing/gui.go | 3 ++- cmd/syncthing/main.go | 15 ++++++++------- cmd/syncthing/model.go | 3 ++- cmd/syncthing/puller.go | 15 +++++++++++++-- {cmd/syncthing => config}/config.go | 19 ++++++------------- {cmd/syncthing => config}/config_test.go | 12 ++++++------ 6 files changed, 37 insertions(+), 30 deletions(-) rename {cmd/syncthing => config}/config.go (95%) rename {cmd/syncthing => config}/config_test.go (94%) diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 9cc9050c6..db2575ca9 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -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 diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 48af656ec..e79a52f2a 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -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() diff --git a/cmd/syncthing/model.go b/cmd/syncthing/model.go index eb7f92d02..91f2ceeef 100644 --- a/cmd/syncthing/model.go +++ b/cmd/syncthing/model.go @@ -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") } diff --git a/cmd/syncthing/puller.go b/cmd/syncthing/puller.go index 35d1aab7c..32f988327 100644 --- a/cmd/syncthing/puller.go +++ b/cmd/syncthing/puller.go @@ -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 + } + } +} diff --git a/cmd/syncthing/config.go b/config/config.go similarity index 95% rename from cmd/syncthing/config.go rename to config/config.go index fdda3170a..2ddc0f346 100644 --- a/cmd/syncthing/config.go +++ b/config/config.go @@ -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 - } - } -} diff --git a/cmd/syncthing/config_test.go b/config/config_test.go similarity index 94% rename from cmd/syncthing/config_test.go rename to config/config_test.go index 91a954e75..3025a3d30 100644 --- a/cmd/syncthing/config_test.go +++ b/config/config_test.go @@ -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) { `) - 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) }