From 0621d0d97a4d998c2ca1733f09ff83afa06829ae Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 3 Feb 2022 09:20:05 +0100 Subject: [PATCH] chore: backup original config --- src/engine/config.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/engine/config.go b/src/engine/config.go index 2ddd592c..d503dc84 100644 --- a/src/engine/config.go +++ b/src/engine/config.go @@ -5,6 +5,7 @@ import ( json2 "encoding/json" "errors" "fmt" + "io" "oh-my-posh/color" "oh-my-posh/console" "oh-my-posh/environment" @@ -173,16 +174,8 @@ 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(destination, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) + f, err := os.OpenFile(cfg.origin, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) cfg.exitWithError(err) _, err = f.WriteString(content) cfg.exitWithError(err) @@ -191,6 +184,18 @@ func (cfg *Config) write(destination string) { } } +func (cfg *Config) Backup() { + dst := cfg.origin + ".bak" + source, err := os.Open(cfg.origin) + cfg.exitWithError(err) + defer source.Close() + destination, err := os.Create(dst) + cfg.exitWithError(err) + defer destination.Close() + _, err = io.Copy(destination, source) + cfg.exitWithError(err) +} + func escapeGlyphs(s string) string { var builder strings.Builder for _, r := range s {