chore(migrate): backup config on migration

This commit is contained in:
Jan De Dobbeleer 2022-02-02 13:27:07 +01:00 committed by Jan De Dobbeleer
parent 0185299263
commit 6fcae4207e
2 changed files with 13 additions and 2 deletions

View file

@ -76,6 +76,7 @@ func (cfg *Config) exitWithError(err error) {
func LoadConfig(env environment.Environment) *Config {
cfg := loadConfig(env)
if cfg.Version != configVersion {
cfg.Backup()
cfg.Migrate(env)
cfg.Write()
}
@ -171,8 +172,16 @@ func (cfg *Config) Export(format string) string {
}
func (cfg *Config) Write() {
cfg.write(cfg.origin)
}
func (cfg *Config) Backup() {
cfg.write(cfg.origin + ".bak")
}
func (cfg *Config) write(destination string) {
content := cfg.Export(cfg.format)
f, err := os.OpenFile(cfg.origin, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
f, err := os.OpenFile(destination, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
cfg.exitWithError(err)
_, err = f.WriteString(content)
cfg.exitWithError(err)

View file

@ -166,11 +166,13 @@ func main() {
return
}
if *args.Migrate {
cfg.Migrate(env)
if *args.Write {
cfg.Backup()
cfg.Migrate(env)
cfg.Write()
return
}
cfg.Migrate(env)
fmt.Print(cfg.Export(*args.ConfigFormat))
return
}