diff --git a/app.go b/app.go index 0f33b112..90a3c1de 100644 --- a/app.go +++ b/app.go @@ -28,6 +28,8 @@ func main() { config.ourBinaryDir = filepath.Dir(executable) } + doConfigRename := true + // config can be specified, which reads options from there, but other command line flags have to override config values // therefore, we must do it manually instead of using a lib { @@ -96,9 +98,18 @@ func main() { } } if configFilename != nil { + // config was manually specified, don't do anything + doConfigRename = false config.ourConfigFilename = *configFilename } + if doConfigRename { + err := renameOldConfigIfNeccessary() + if err != nil { + panic(err) + } + } + err := askUsernamePasswordIfPossible() if err != nil { log.Fatal(err) @@ -229,3 +240,29 @@ func askUsernamePasswordIfPossible() error { config.AuthPass = password return nil } + +func renameOldConfigIfNeccessary() error { + oldConfigFile := filepath.Join(config.ourBinaryDir, "AdguardDNS.yaml") + _, err := os.Stat(oldConfigFile) + if os.IsNotExist(err) { + // do nothing, file doesn't exist + trace("File %s doesn't exist, nothing to do", oldConfigFile) + return nil + } + + newConfigFile := filepath.Join(config.ourBinaryDir, config.ourConfigFilename) + _, err = os.Stat(newConfigFile) + if !os.IsNotExist(err) { + // do nothing, file doesn't exist + trace("File %s already exists, will not overwrite", newConfigFile) + return nil + } + + err = os.Rename(oldConfigFile, newConfigFile) + if err != nil { + log.Printf("Failed to rename %s to %s: %s", oldConfigFile, newConfigFile, err) + return err + } + + return nil +}