Repository: syncthing/syncthing
File: cmd/syncthing/main.go (Go)

Code before:

	return cfg, err
}

func loadConfigAtStartup(forceNewConfig bool) (config.Wrapper, error) {
	cfgFile := locations.Get(locations.ConfigFile)
	cfg, err := config.Load(cfgFile, myID)
	if os.IsNotExist(err) {
		cfg, err = defaultConfig(cfgFile)
		if err != nil {
			return nil, errors.Wrap(err, "failed to generate default config")
		}
		err = cfg.Save()
		if err != nil {
			return nil, errors.Wrap(err, "failed to save default config")
		}
		l.Infof("Default config saved. Edit %s to taste (with Syncthing stopped) or use the GUI", cfg.ConfigPath())
	} else if err == io.EOF {
		return nil, errors.New("Failed to load config: unexpected end of file. Truncated or empty configuration?")
	} else if err != nil {
		return nil, errors.Wrap(err, "failed to load config")
	}

	if cfg.RawCopy().OriginalVersion != config.CurrentVersion {
		if cfg.RawCopy().OriginalVersion > config.CurrentVersion && !forceNewConfig {
			return nil, errors.New("Config file version (" + strconv.Itoa(cfg.RawCopy().OriginalVersion) + ") is newer than supported version (" + strconv.Itoa(config.CurrentVersion) + "). If this is expected, use -force-new-config to override.")
		}
		err = archiveAndSaveConfig(cfg)
		if err != nil {
			return nil, errors.Wrap(err, "config archive")
		}
	}

	return cfg, nil
}

func archiveAndSaveConfig(cfg config.Wrapper) error {
	// Copy the existing config to an archive copy
	archivePath := cfg.ConfigPath() + fmt.Sprintf(".v%d", cfg.RawCopy().OriginalVersion)
	l.Infoln("Archiving a copy of old config file format at:", archivePath)
	if err := copyFile(cfg.ConfigPath(), archivePath); err != nil {
		return err
	}

	// Do a regular atomic config sve
	return cfg.Save()
}

func copyFile(src, dst string) error {
	bs, err := ioutil.ReadFile(src)
	if err != nil {

Review comment (suggestion):
`fmt.Errorf("bla bla %d bla", number)`

Also, the flag is not forcing anything, but allowing, so perhaps -allow-newer-config



Code after:

	return cfg, err
}

func loadConfigAtStartup(allowNewerConfig bool) (config.Wrapper, error) {
	cfgFile := locations.Get(locations.ConfigFile)
	cfg, err := config.Load(cfgFile, myID)
	if os.IsNotExist(err) {
		cfg, err = defaultConfig(cfgFile)
		if err != nil {
			return nil, errors.Wrap(err, "failed to generate default config")
		}
		err = cfg.Save()
		if err != nil {
			return nil, errors.Wrap(err, "failed to save default config")
		}
		l.Infof("Default config saved. Edit %s to taste (with Syncthing stopped) or use the GUI", cfg.ConfigPath())
	} else if err == io.EOF {
		return nil, errors.New("Failed to load config: unexpected end of file. Truncated or empty configuration?")
	} else if err != nil {
		return nil, errors.Wrap(err, "failed to load config")
	}

	if cfg.RawCopy().OriginalVersion != config.CurrentVersion {
		if cfg.RawCopy().OriginalVersion > config.CurrentVersion && !allowNewerConfig {
			return nil, fmt.Errorf("Config file version (%d) is newer than supported version (%d). If this is expected, use -allow-newer-config to override.", cfg.RawCopy().OriginalVersion, config.CurrentVersion)
		}
		err = archiveAndSaveConfig(cfg)
		if err != nil {
			return nil, errors.Wrap(err, "config archive")
		}
	}

	return cfg, nil
}

func archiveAndSaveConfig(cfg config.Wrapper) error {
	// Copy the existing config to an archive copy
	archivePath := cfg.ConfigPath() + fmt.Sprintf(".v%d", cfg.RawCopy().OriginalVersion)
	l.Infoln("Archiving a copy of old config file format at:", archivePath)
	if err := copyFile(cfg.ConfigPath(), archivePath); err != nil {
		return err
	}

	// Do a regular atomic config sve
	return cfg.Save()
}

func copyFile(src, dst string) error {
	bs, err := ioutil.ReadFile(src)
	if err != nil {