chore: backup original config

This commit is contained in:
Jan De Dobbeleer 2022-02-03 09:20:05 +01:00 committed by Jan De Dobbeleer
parent 6b6aec43d5
commit 0621d0d97a

View file

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