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 { func LoadConfig(env environment.Environment) *Config {
cfg := loadConfig(env) cfg := loadConfig(env)
if cfg.Version != configVersion { if cfg.Version != configVersion {
cfg.Backup()
cfg.Migrate(env) cfg.Migrate(env)
cfg.Write() cfg.Write()
} }
@ -171,8 +172,16 @@ func (cfg *Config) Export(format string) string {
} }
func (cfg *Config) Write() { 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) 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) cfg.exitWithError(err)
_, err = f.WriteString(content) _, err = f.WriteString(content)
cfg.exitWithError(err) cfg.exitWithError(err)

View file

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