Centralize config loading logic

This gets rid of redundant checks and centralizes the logic of loading
the config files so that we don't have to keep doing the same thing in
multiple places.
This commit is contained in:
Anderson Mesquita 2015-12-18 18:39:36 -05:00
parent 22d0ed8225
commit 9b9912ba9e

View File

@ -359,10 +359,7 @@ func main() {
} }
func openGUI() { func openGUI() {
cfg, _, err := loadConfig(locations[locConfigFile]) cfg, _ := loadConfig()
if err != nil {
l.Fatalln("Config:", err)
}
if cfg.GUI().Enabled { if cfg.GUI().Enabled {
openURL(cfg.GUI().URL()) openURL(cfg.GUI().URL())
} else { } else {
@ -433,10 +430,8 @@ func debugFacilities() string {
} }
func checkUpgrade() upgrade.Release { func checkUpgrade() upgrade.Release {
releasesURL := "https://api.github.com/repos/syncthing/syncthing/releases?per_page=30" cfg, _ := loadConfig()
if cfg, _, err := loadConfig(locations[locConfigFile]); err == nil { releasesURL := cfg.Options().ReleasesURL
releasesURL = cfg.Options().ReleasesURL
}
release, err := upgrade.LatestRelease(releasesURL, Version) release, err := upgrade.LatestRelease(releasesURL, Version)
if err != nil { if err != nil {
l.Fatalln("Upgrade:", err) l.Fatalln("Upgrade:", err)
@ -473,10 +468,7 @@ func performUpgrade(release upgrade.Release) {
} }
func upgradeViaRest() error { func upgradeViaRest() error {
cfg, err := config.Load(locations[locConfigFile], protocol.LocalDeviceID) cfg, _ := loadConfig()
if err != nil {
return err
}
target := cfg.GUI().URL() target := cfg.GUI().URL()
r, _ := http.NewRequest("POST", target+"/rest/system/upgrade", nil) r, _ := http.NewRequest("POST", target+"/rest/system/upgrade", nil)
r.Header.Set("X-API-Key", cfg.GUI().APIKey()) r.Header.Set("X-API-Key", cfg.GUI().APIKey())
@ -573,34 +565,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
"myID": myID.String(), "myID": myID.String(),
}) })
// Prepare to be able to save configuration cfg := loadOrCreateConfig()
cfgFile := locations[locConfigFile]
// Load the configuration file, if it exists.
// If it does not, create a template.
cfg, myName, err := loadConfig(cfgFile)
if err != nil {
if os.IsNotExist(err) {
l.Infoln("No config file; starting with empty defaults")
myName, _ = os.Hostname()
newCfg := defaultConfig(myName)
cfg = config.Wrap(cfgFile, newCfg)
cfg.Save()
l.Infof("Edit %s to taste or use the GUI\n", cfgFile)
} else {
l.Fatalln("Loading config:", err)
}
}
if cfg.Raw().OriginalVersion != config.CurrentVersion {
l.Infoln("Archiving a copy of old config file format")
// Archive a copy
osutil.Rename(cfgFile, cfgFile+fmt.Sprintf(".v%d", cfg.Raw().OriginalVersion))
// Save the new version
cfg.Save()
}
if err := checkShortIDs(cfg); err != nil { if err := checkShortIDs(cfg); err != nil {
l.Fatalln("Short device IDs are in conflict. Unlucky!\n Regenerate the device ID of one of the following:\n ", err) l.Fatalln("Short device IDs are in conflict. Unlucky!\n Regenerate the device ID of one of the following:\n ", err)
@ -693,7 +658,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
l.Infoln("Compacting database:", err) l.Infoln("Compacting database:", err)
} }
m := model.NewModel(cfg, myID, myName, "syncthing", Version, ldb, protectedFiles) m := model.NewModel(cfg, myID, myDeviceName(cfg), "syncthing", Version, ldb, protectedFiles)
cfg.Subscribe(m) cfg.Subscribe(m)
if t := os.Getenv("STDEADLOCKTIMEOUT"); len(t) > 0 { if t := os.Getenv("STDEADLOCKTIMEOUT"); len(t) > 0 {
@ -886,6 +851,15 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
os.Exit(code) os.Exit(code)
} }
func myDeviceName(cfg *config.Wrapper) string {
devices := cfg.Devices()
myName := devices[myID].Name
if myName == "" {
myName, _ = os.Hostname()
}
return myName
}
func setupSignalHandling() { func setupSignalHandling() {
// Exit cleanly with "restarting" code on SIGHUP. // Exit cleanly with "restarting" code on SIGHUP.
@ -924,27 +898,38 @@ func printHashRate() {
l.Infof("Single thread hash performance is ~%.*f MB/s", decimals, hashRate) l.Infof("Single thread hash performance is ~%.*f MB/s", decimals, hashRate)
} }
func loadConfig(cfgFile string) (*config.Wrapper, string, error) { func loadConfig() (*config.Wrapper, error) {
info, err := os.Stat(cfgFile) cfgFile := locations[locConfigFile]
if err != nil {
return nil, "", err
}
if !info.Mode().IsRegular() {
return nil, "", errors.New("configuration is not a file")
}
cfg, err := config.Load(cfgFile, myID) cfg, err := config.Load(cfgFile, myID)
if err != nil { if err != nil {
return nil, "", err l.Infoln("Error loading config file; using defaults for now")
myName, _ := os.Hostname()
newCfg := defaultConfig(myName)
cfg = config.Wrap(cfgFile, newCfg)
} }
myCfg := cfg.Devices()[myID] return cfg, err
myName := myCfg.Name }
if myName == "" {
myName, _ = os.Hostname() func loadOrCreateConfig() *config.Wrapper {
cfg, err := loadConfig()
if os.IsNotExist(err) {
cfg.Save()
l.Infof("Defaults saved. Edit %s to taste or use the GUI\n", cfg.ConfigPath())
} else if err != nil {
l.Fatalln("Config:", err)
} }
return cfg, myName, nil if cfg.Raw().OriginalVersion != config.CurrentVersion {
// Archive previous version and save new one
archivePath := cfg.ConfigPath() + fmt.Sprintf(".v%d", cfg.Raw().OriginalVersion)
l.Infoln("Archiving a copy of old config file format at:", archivePath)
osutil.Rename(cfg.ConfigPath(), archivePath)
cfg.Save()
}
return cfg
} }
func startAuditing(mainService *suture.Supervisor) { func startAuditing(mainService *suture.Supervisor) {