From 76c7ea536b5d5f739691501dcaadc8348612c6d1 Mon Sep 17 00:00:00 2001 From: jan De Dobbeleer Date: Wed, 8 Feb 2023 07:09:35 +0100 Subject: [PATCH] fix(config): migrate glyphs correctly for all formats --- .vscode/launch.json | 3 ++- src/cli/config_migrate_glyphs.go | 9 ++++++++- src/engine/config.go | 28 +++++++++++++--------------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 736ce94e..afd7e7dd 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -109,7 +109,8 @@ "args": [ "config", "migrate", - "glyphs" + "glyphs", + "--config=/Users/jan/.posh.omp.yaml" ] }, { diff --git a/src/cli/config_migrate_glyphs.go b/src/cli/config_migrate_glyphs.go index 4ea37ec1..3c28e43c 100644 --- a/src/cli/config_migrate_glyphs.go +++ b/src/cli/config_migrate_glyphs.go @@ -40,21 +40,28 @@ A backup of the current config can be found at ~/myconfig.omp.json.bak.`, Config: config, }, } + env.Init() defer env.Close() cfg := engine.LoadConfig(env) + cfg.MigrateGlyphs = true + if len(format) == 0 { + format = cfg.Format + } + if write { cfg.Backup() cfg.Write(format) return } + fmt.Print(cfg.Export(format)) }, } func init() { //nolint:gochecknoinits migrateGlyphsCmd.Flags().BoolVarP(&write, "write", "w", false, "write the migrated config back to the config file") - migrateGlyphsCmd.Flags().StringVarP(&format, "format", "f", "json", "the config format to migrate to") + migrateGlyphsCmd.Flags().StringVarP(&format, "format", "f", "", "the config format to migrate to") migrateCmd.AddCommand(migrateGlyphsCmd) } diff --git a/src/engine/config.go b/src/engine/config.go index b7ae1041..a945598a 100644 --- a/src/engine/config.go +++ b/src/engine/config.go @@ -55,8 +55,8 @@ type Config struct { Output string `json:"-"` MigrateGlyphs bool `json:"-"` + Format string `json:"-"` - format string origin string // eval bool updated bool @@ -107,10 +107,10 @@ func loadConfig(env platform.Environment) *Config { var cfg Config cfg.origin = configFile - cfg.format = strings.TrimPrefix(filepath.Ext(configFile), ".") + cfg.Format = strings.TrimPrefix(filepath.Ext(configFile), ".") cfg.env = env - if cfg.format == "yml" { - cfg.format = YAML + if cfg.Format == "yml" { + cfg.Format = YAML } config.AddDriver(yaml.Driver) @@ -166,7 +166,7 @@ func (cfg *Config) Export(format string) string { cfg.sync() if len(format) != 0 { - cfg.format = format + cfg.Format = format } config.AddDriver(yaml.Driver) @@ -174,7 +174,7 @@ func (cfg *Config) Export(format string) string { var result bytes.Buffer - if cfg.format == JSON { + if cfg.Format == JSON { jsonEncoder := json2.NewEncoder(&result) jsonEncoder.SetEscapeHTML(false) jsonEncoder.SetIndent("", " ") @@ -184,23 +184,21 @@ func (cfg *Config) Export(format string) string { return escapeGlyphs(data, cfg.MigrateGlyphs) } - _, _ = config.DumpTo(&result, cfg.format) - switch cfg.format { + _, _ = config.DumpTo(&result, cfg.Format) + var prefix string + switch cfg.Format { case YAML: - prefix := "# yaml-language-server: $schema=https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n" - return prefix + result.String() + prefix = "# yaml-language-server: $schema=https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n" case TOML: - prefix := "#:schema https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n" - return prefix + escapeGlyphs(result.String(), cfg.MigrateGlyphs) - default: - return result.String() + prefix = "#:schema https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n" } + return prefix + escapeGlyphs(result.String(), cfg.MigrateGlyphs) } func (cfg *Config) BackupAndMigrate() { cfg.Backup() cfg.Migrate() - cfg.Write(cfg.format) + cfg.Write(cfg.Format) } func (cfg *Config) Write(format string) {