From 1af11c4e45e13bcbeb9fe37f7867760f09ae8c2b Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Mon, 15 Oct 2018 16:13:03 +0300 Subject: [PATCH] Name migration -- rename config from AdguardDNS.yaml to AdGuardHome.yaml It's done only if user didn't specify it in parameters and target filename doesn't exist yet. --- app.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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 +}