From 9d07aa006d07855ec2cc3f48845d2bdfd95782cb Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 8 Dec 2014 16:36:15 +0100 Subject: [PATCH] Various style fixes --- cmd/syncthing/gui.go | 76 +++++++++++++-------------- cmd/syncthing/main.go | 11 ++-- internal/config/config.go | 10 ++-- internal/events/events.go | 12 ++--- internal/ignore/ignore.go | 3 +- internal/model/deviceactivity.go | 2 +- internal/model/model.go | 8 +-- internal/scanner/walk.go | 2 +- internal/scanner/walk_test.go | 12 ++--- internal/upgrade/upgrade_common.go | 2 +- internal/upgrade/upgrade_supported.go | 16 +++--- internal/versioner/simple.go | 3 +- internal/versioner/staggered.go | 3 +- 13 files changed, 78 insertions(+), 82 deletions(-) diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 6efc44a37..1ec59dd59 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -334,44 +334,44 @@ func restPostConfig(m *model.Model, w http.ResponseWriter, r *http.Request) { l.Warnln("decoding posted config:", err) http.Error(w, err.Error(), 500) return - } else { - if newCfg.GUI.Password != cfg.GUI().Password { - if newCfg.GUI.Password != "" { - hash, err := bcrypt.GenerateFromPassword([]byte(newCfg.GUI.Password), 0) - if err != nil { - l.Warnln("bcrypting password:", err) - http.Error(w, err.Error(), 500) - return - } else { - newCfg.GUI.Password = string(hash) - } - } - } - - // Start or stop usage reporting as appropriate - - if curAcc := cfg.Options().URAccepted; newCfg.Options.URAccepted > curAcc { - // UR was enabled - newCfg.Options.URAccepted = usageReportVersion - newCfg.Options.URUniqueID = randomString(8) - err := sendUsageReport(m) - if err != nil { - l.Infoln("Usage report:", err) - } - go usageReportingLoop(m) - } else if newCfg.Options.URAccepted < curAcc { - // UR was disabled - newCfg.Options.URAccepted = -1 - newCfg.Options.URUniqueID = "" - stopUsageReporting() - } - - // Activate and save - - configInSync = !config.ChangeRequiresRestart(cfg.Raw(), newCfg) - cfg.Replace(newCfg) - cfg.Save() } + + if newCfg.GUI.Password != cfg.GUI().Password { + if newCfg.GUI.Password != "" { + hash, err := bcrypt.GenerateFromPassword([]byte(newCfg.GUI.Password), 0) + if err != nil { + l.Warnln("bcrypting password:", err) + http.Error(w, err.Error(), 500) + return + } + + newCfg.GUI.Password = string(hash) + } + } + + // Start or stop usage reporting as appropriate + + if curAcc := cfg.Options().URAccepted; newCfg.Options.URAccepted > curAcc { + // UR was enabled + newCfg.Options.URAccepted = usageReportVersion + newCfg.Options.URUniqueID = randomString(8) + err := sendUsageReport(m) + if err != nil { + l.Infoln("Usage report:", err) + } + go usageReportingLoop(m) + } else if newCfg.Options.URAccepted < curAcc { + // UR was disabled + newCfg.Options.URAccepted = -1 + newCfg.Options.URUniqueID = "" + stopUsageReporting() + } + + // Activate and save + + configInSync = !config.ChangeRequiresRestart(cfg.Raw(), newCfg) + cfg.Replace(newCfg) + cfg.Save() } func restGetConfigInSync(w http.ResponseWriter, r *http.Request) { @@ -598,7 +598,7 @@ func restPostUpgrade(w http.ResponseWriter, r *http.Request) { } if upgrade.CompareVersions(rel.Tag, Version) == 1 { - err = upgrade.UpgradeTo(rel) + err = upgrade.To(rel) if err != nil { l.Warnln("upgrading:", err) http.Error(w, err.Error(), 500) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 7dd259a05..ee756cb84 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -105,7 +105,7 @@ var ( cfg *config.ConfigWrapper myID protocol.DeviceID confDir string - logFlags int = log.Ltime + logFlags = log.Ltime writeRateLimit *ratelimit.Bucket readRateLimit *ratelimit.Bucket stop = make(chan int) @@ -332,15 +332,14 @@ func main() { l.Fatalln("Cannot upgrade, database seems to be locked. Is another copy of Syncthing already running?") } - err = upgrade.UpgradeTo(rel) + err = upgrade.To(rel) if err != nil { l.Fatalln("Upgrade:", err) // exits 1 } l.Okf("Upgraded to %q", rel.Tag) - return - } else { - return } + + return } if reset { @@ -1256,7 +1255,7 @@ func autoUpgrade() { } l.Infof("Automatic upgrade (current %q < latest %q)", Version, rel.Tag) - err = upgrade.UpgradeTo(rel) + err = upgrade.To(rel) if err != nil { l.Warnln("Automatic upgrade:", err) continue diff --git a/internal/config/config.go b/internal/config/config.go index 07679f091..9a3299ef7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -92,13 +92,13 @@ func (f *FolderConfiguration) HasMarker() bool { return true } -func (r *FolderConfiguration) DeviceIDs() []protocol.DeviceID { - if r.deviceIDs == nil { - for _, n := range r.Devices { - r.deviceIDs = append(r.deviceIDs, n.DeviceID) +func (f *FolderConfiguration) DeviceIDs() []protocol.DeviceID { + if f.deviceIDs == nil { + for _, n := range f.Devices { + f.deviceIDs = append(f.deviceIDs, n.DeviceID) } } - return r.deviceIDs + return f.deviceIDs } type VersioningConfiguration struct { diff --git a/internal/events/events.go b/internal/events/events.go index 8dd845d38..7fce142ee 100644 --- a/internal/events/events.go +++ b/internal/events/events.go @@ -86,7 +86,7 @@ const BufferSize = 64 type Logger struct { subs map[int]*Subscription - nextId int + nextID int mutex sync.Mutex } @@ -120,15 +120,15 @@ func NewLogger() *Logger { func (l *Logger) Log(t EventType, data interface{}) { l.mutex.Lock() if debug { - dl.Debugln("log", l.nextId, t.String(), data) + dl.Debugln("log", l.nextID, t.String(), data) } e := Event{ - ID: l.nextId, + ID: l.nextID, Time: time.Now(), Type: t, Data: data, } - l.nextId++ + l.nextID++ for _, s := range l.subs { if s.mask&t != 0 { select { @@ -148,10 +148,10 @@ func (l *Logger) Subscribe(mask EventType) *Subscription { } s := &Subscription{ mask: mask, - id: l.nextId, + id: l.nextID, events: make(chan Event, BufferSize), } - l.nextId++ + l.nextID++ l.subs[s.id] = s l.mutex.Unlock() return s diff --git a/internal/ignore/ignore.go b/internal/ignore/ignore.go index 48d2ede75..e74c7ad21 100644 --- a/internal/ignore/ignore.go +++ b/internal/ignore/ignore.go @@ -169,9 +169,8 @@ func parseIgnoreFile(fd io.Reader, currentFile string, seen map[string]bool) (*M includes, err := loadIgnoreFile(includeFile, seen) if err != nil { return err - } else { - exps.patterns = append(exps.patterns, includes.patterns...) } + exps.patterns = append(exps.patterns, includes.patterns...) } else { // Path name or pattern, add it so it matches files both in // current directory and subdirs. diff --git a/internal/model/deviceactivity.go b/internal/model/deviceactivity.go index 19d84400f..2c8fd5ad1 100644 --- a/internal/model/deviceactivity.go +++ b/internal/model/deviceactivity.go @@ -37,7 +37,7 @@ func newDeviceActivity() *deviceActivity { func (m *deviceActivity) leastBusy(availability []protocol.DeviceID) protocol.DeviceID { m.mut.Lock() - var low int = 2<<30 - 1 + low := 2<<30 - 1 var selected protocol.DeviceID for _, device := range availability { if usage := m.act[device]; usage < low { diff --git a/internal/model/model.go b/internal/model/model.go index ab1879ad5..e3faeda8c 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -862,11 +862,11 @@ func (m *Model) deviceStatRef(deviceID protocol.DeviceID) *stats.DeviceStatistic if sr, ok := m.deviceStatRefs[deviceID]; ok { return sr - } else { - sr = stats.NewDeviceStatisticsReference(m.db, deviceID) - m.deviceStatRefs[deviceID] = sr - return sr } + + sr := stats.NewDeviceStatisticsReference(m.db, deviceID) + m.deviceStatRefs[deviceID] = sr + return sr } func (m *Model) deviceWasSeen(deviceID protocol.DeviceID) { diff --git a/internal/scanner/walk.go b/internal/scanner/walk.go index 403958613..d0a1d4819 100644 --- a/internal/scanner/walk.go +++ b/internal/scanner/walk.go @@ -201,7 +201,7 @@ func (w *Walker) walkAndHashFiles(fchan chan protocol.FileInfo) filepath.WalkFun } } - var flags uint32 = protocol.FlagDirectory + flags := uint32(protocol.FlagDirectory) if w.IgnorePerms { flags |= protocol.FlagNoPermBits | 0777 } else { diff --git a/internal/scanner/walk_test.go b/internal/scanner/walk_test.go index 13cc45c7c..859b81d6b 100644 --- a/internal/scanner/walk_test.go +++ b/internal/scanner/walk_test.go @@ -190,16 +190,16 @@ func TestVerify(t *testing.T) { type fileList []protocol.FileInfo -func (f fileList) Len() int { - return len(f) +func (l fileList) Len() int { + return len(l) } -func (f fileList) Less(a, b int) bool { - return f[a].Name < f[b].Name +func (l fileList) Less(a, b int) bool { + return l[a].Name < l[b].Name } -func (f fileList) Swap(a, b int) { - f[a], f[b] = f[b], f[a] +func (l fileList) Swap(a, b int) { + l[a], l[b] = l[b], l[a] } func (l fileList) testfiles() testfileList { diff --git a/internal/upgrade/upgrade_common.go b/internal/upgrade/upgrade_common.go index 6cc6d9582..5ad69754a 100644 --- a/internal/upgrade/upgrade_common.go +++ b/internal/upgrade/upgrade_common.go @@ -48,7 +48,7 @@ func init() { } // A wrapper around actual implementations -func UpgradeTo(rel Release) error { +func To(rel Release) error { select { case <-upgradeUnlocked: path, err := osext.Executable() diff --git a/internal/upgrade/upgrade_supported.go b/internal/upgrade/upgrade_supported.go index dc2ec719d..4eec6f2d1 100644 --- a/internal/upgrade/upgrade_supported.go +++ b/internal/upgrade/upgrade_supported.go @@ -86,15 +86,15 @@ func LatestRelease(prerelease bool) (Release, error) { if prerelease { // We are a beta version. Use the latest. return rels[0], nil - } else { - // We are a regular release. Only consider non-prerelease versions for upgrade. - for _, rel := range rels { - if !rel.Prerelease { - return rel, nil - } - } - return Release{}, ErrVersionUnknown } + + // We are a regular release. Only consider non-prerelease versions for upgrade. + for _, rel := range rels { + if !rel.Prerelease { + return rel, nil + } + } + return Release{}, ErrVersionUnknown } func readTarGZ(url string, dir string) (string, error) { diff --git a/internal/versioner/simple.go b/internal/versioner/simple.go index e51575fe5..9be3434a9 100644 --- a/internal/versioner/simple.go +++ b/internal/versioner/simple.go @@ -62,9 +62,8 @@ func (v Simple) Archive(filePath string) error { l.Debugln("not archiving nonexistent file", filePath) } return nil - } else { - return err } + return err } versionsDir := filepath.Join(v.folderPath, ".stversions") diff --git a/internal/versioner/staggered.go b/internal/versioner/staggered.go index 4ae216066..8330bd6f4 100644 --- a/internal/versioner/staggered.go +++ b/internal/versioner/staggered.go @@ -294,9 +294,8 @@ func (v Staggered) Archive(filePath string) error { l.Debugln("not archiving nonexistent file", filePath) } return nil - } else { - return err } + return err } if _, err := os.Stat(v.versionsPath); err != nil {