Skip to content

Commit

Permalink
main: Improve logging for initial config loading (ref #4431)
Browse files Browse the repository at this point in the history
GitHub-Pull-Request: #4436
  • Loading branch information
imsodin authored and AudriusButkevicius committed Oct 21, 2017
1 parent d84b6bb commit 20fac4b
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions cmd/syncthing/main.go
Expand Up @@ -449,7 +449,7 @@ func main() {
}

func openGUI() {
cfg, _ := loadConfig()
cfg, _ := loadOrDefaultConfig()
if cfg.GUI().Enabled {
openURL(cfg.GUI().URL())
} else {
Expand Down Expand Up @@ -488,9 +488,7 @@ func generate(generateDir string) {
l.Warnln("Config exists; will not overwrite.")
return
}
var myName, _ = os.Hostname()
var newCfg = defaultConfig(myName)
var cfg = config.Wrap(cfgFile, newCfg)
var cfg = defaultConfig(cfgFile)
err = cfg.Save()
if err != nil {
l.Warnln("Failed to save config", err)
Expand Down Expand Up @@ -520,7 +518,7 @@ func debugFacilities() string {
}

func checkUpgrade() upgrade.Release {
cfg, _ := loadConfig()
cfg, _ := loadOrDefaultConfig()
opts := cfg.Options()
release, err := upgrade.LatestRelease(opts.ReleasesURL, Version, opts.UpgradeToPreReleases)
if err != nil {
Expand Down Expand Up @@ -558,7 +556,7 @@ func performUpgrade(release upgrade.Release) {
}

func upgradeViaRest() error {
cfg, _ := loadConfig()
cfg, _ := loadOrDefaultConfig()
u, err := url.Parse(cfg.GUI().URL())
if err != nil {
return err
Expand Down Expand Up @@ -663,7 +661,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
"myID": myID.String(),
})

cfg := loadOrCreateConfig()
cfg := loadConfigAtStartup()

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)
Expand Down Expand Up @@ -965,26 +963,28 @@ func setupSignalHandling() {
}()
}

func loadConfig() (*config.Wrapper, error) {
func loadOrDefaultConfig() (*config.Wrapper, error) {
cfgFile := locations[locConfigFile]
cfg, err := config.Load(cfgFile, myID)

if err != nil {
myName, _ := os.Hostname()
newCfg := defaultConfig(myName)
cfg = config.Wrap(cfgFile, newCfg)
cfg = defaultConfig(cfgFile)
}

return cfg, err
}

func loadOrCreateConfig() *config.Wrapper {
cfg, err := loadConfig()
func loadConfigAtStartup() *config.Wrapper {
cfgFile := locations[locConfigFile]
cfg, err := config.Load(cfgFile, myID)
if os.IsNotExist(err) {
cfg := defaultConfig(cfgFile)
cfg.Save()
l.Infof("Defaults saved. Edit %s to taste or use the GUI\n", cfg.ConfigPath())
l.Infof("Default config saved. Edit %s to taste or use the GUI\n", cfg.ConfigPath())
} else if err == io.EOF {
l.Fatalln("Failed to load config: unexpected end of file. Truncated or empty configuration?")
} else if err != nil {
l.Fatalln("Config:", err)
l.Fatalln("Failed to load config:", err)
}

if cfg.RawCopy().OriginalVersion != config.CurrentVersion {
Expand Down Expand Up @@ -1087,7 +1087,9 @@ func setupGUI(mainService *suture.Supervisor, cfg *config.Wrapper, m *model.Mode
}
}

func defaultConfig(myName string) config.Configuration {
func defaultConfig(cfgFile string) *config.Wrapper {
myName, _ := os.Hostname()

var defaultFolder config.FolderConfiguration

if !noDefaultFolder {
Expand Down Expand Up @@ -1132,7 +1134,7 @@ func defaultConfig(myName string) config.Configuration {
}
}

return newCfg
return config.Wrap(cfgFile, newCfg)
}

func resetDB() error {
Expand Down

0 comments on commit 20fac4b

Please sign in to comment.