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.
This commit is contained in:
Eugene Bujak 2018-10-15 16:13:03 +03:00
parent 3e2a3afc52
commit 1af11c4e45
1 changed files with 37 additions and 0 deletions

37
app.go
View File

@ -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
}