fix(config): migrate glyphs correctly for all formats

This commit is contained in:
jan De Dobbeleer 2023-02-08 07:09:35 +01:00 committed by Jan De Dobbeleer
parent 42b75241c7
commit 76c7ea536b
3 changed files with 23 additions and 17 deletions

3
.vscode/launch.json vendored
View file

@ -109,7 +109,8 @@
"args": [ "args": [
"config", "config",
"migrate", "migrate",
"glyphs" "glyphs",
"--config=/Users/jan/.posh.omp.yaml"
] ]
}, },
{ {

View file

@ -40,21 +40,28 @@ A backup of the current config can be found at ~/myconfig.omp.json.bak.`,
Config: config, Config: config,
}, },
} }
env.Init() env.Init()
defer env.Close() defer env.Close()
cfg := engine.LoadConfig(env) cfg := engine.LoadConfig(env)
cfg.MigrateGlyphs = true cfg.MigrateGlyphs = true
if len(format) == 0 {
format = cfg.Format
}
if write { if write {
cfg.Backup() cfg.Backup()
cfg.Write(format) cfg.Write(format)
return return
} }
fmt.Print(cfg.Export(format)) fmt.Print(cfg.Export(format))
}, },
} }
func init() { //nolint:gochecknoinits func init() { //nolint:gochecknoinits
migrateGlyphsCmd.Flags().BoolVarP(&write, "write", "w", false, "write the migrated config back to the config file") 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) migrateCmd.AddCommand(migrateGlyphsCmd)
} }

View file

@ -55,8 +55,8 @@ type Config struct {
Output string `json:"-"` Output string `json:"-"`
MigrateGlyphs bool `json:"-"` MigrateGlyphs bool `json:"-"`
Format string `json:"-"`
format string
origin string origin string
// eval bool // eval bool
updated bool updated bool
@ -107,10 +107,10 @@ func loadConfig(env platform.Environment) *Config {
var cfg Config var cfg Config
cfg.origin = configFile cfg.origin = configFile
cfg.format = strings.TrimPrefix(filepath.Ext(configFile), ".") cfg.Format = strings.TrimPrefix(filepath.Ext(configFile), ".")
cfg.env = env cfg.env = env
if cfg.format == "yml" { if cfg.Format == "yml" {
cfg.format = YAML cfg.Format = YAML
} }
config.AddDriver(yaml.Driver) config.AddDriver(yaml.Driver)
@ -166,7 +166,7 @@ func (cfg *Config) Export(format string) string {
cfg.sync() cfg.sync()
if len(format) != 0 { if len(format) != 0 {
cfg.format = format cfg.Format = format
} }
config.AddDriver(yaml.Driver) config.AddDriver(yaml.Driver)
@ -174,7 +174,7 @@ func (cfg *Config) Export(format string) string {
var result bytes.Buffer var result bytes.Buffer
if cfg.format == JSON { if cfg.Format == JSON {
jsonEncoder := json2.NewEncoder(&result) jsonEncoder := json2.NewEncoder(&result)
jsonEncoder.SetEscapeHTML(false) jsonEncoder.SetEscapeHTML(false)
jsonEncoder.SetIndent("", " ") jsonEncoder.SetIndent("", " ")
@ -184,23 +184,21 @@ func (cfg *Config) Export(format string) string {
return escapeGlyphs(data, cfg.MigrateGlyphs) return escapeGlyphs(data, cfg.MigrateGlyphs)
} }
_, _ = config.DumpTo(&result, cfg.format) _, _ = config.DumpTo(&result, cfg.Format)
switch cfg.format { var prefix string
switch cfg.Format {
case YAML: case YAML:
prefix := "# yaml-language-server: $schema=https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n" prefix = "# yaml-language-server: $schema=https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n"
return prefix + result.String()
case TOML: case TOML:
prefix := "#:schema https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json\n\n" 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()
} }
return prefix + escapeGlyphs(result.String(), cfg.MigrateGlyphs)
} }
func (cfg *Config) BackupAndMigrate() { func (cfg *Config) BackupAndMigrate() {
cfg.Backup() cfg.Backup()
cfg.Migrate() cfg.Migrate()
cfg.Write(cfg.format) cfg.Write(cfg.Format)
} }
func (cfg *Config) Write(format string) { func (cfg *Config) Write(format string) {