From 7dce0fe3e7e66e1f5f7201b236de07ce4cc5a013 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Fri, 3 Feb 2023 07:53:03 +0100 Subject: [PATCH] fix(config): log errors relates to #3441 --- src/cli/config_migrate.go | 4 ++-- src/engine/config.go | 20 ++++++++++++-------- src/engine/migrate.go | 6 +++--- src/engine/migrate_test.go | 3 ++- src/platform/shell.go | 3 +++ 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/cli/config_migrate.go b/src/cli/config_migrate.go index ed71cc48..0b6470b9 100644 --- a/src/cli/config_migrate.go +++ b/src/cli/config_migrate.go @@ -50,10 +50,10 @@ A backup of the current config can be found at ~/myconfig.omp.json.bak.`, defer env.Close() cfg := engine.LoadConfig(env) if write { - cfg.BackupAndMigrate(env) + cfg.BackupAndMigrate() return } - cfg.Migrate(env) + cfg.Migrate() fmt.Print(cfg.Export(format)) }, } diff --git a/src/engine/config.go b/src/engine/config.go index fb0cbc4f..b7ae1041 100644 --- a/src/engine/config.go +++ b/src/engine/config.go @@ -89,10 +89,9 @@ func (cfg *Config) getPalette() ansi.Palette { // LoadConfig returns the default configuration including possible user overrides func LoadConfig(env platform.Environment) *Config { cfg := loadConfig(env) - cfg.env = env // only migrate automatically when the switch isn't set if !env.Flags().Migrate && cfg.Version < configVersion { - cfg.BackupAndMigrate(env) + cfg.BackupAndMigrate() } return cfg } @@ -102,12 +101,14 @@ func loadConfig(env platform.Environment) *Config { configFile := env.Flags().Config if len(configFile) == 0 { - return defaultConfig(false) + env.Debug("no config file specified, using default") + return defaultConfig(env, false) } var cfg Config cfg.origin = configFile cfg.format = strings.TrimPrefix(filepath.Ext(configFile), ".") + cfg.env = env if cfg.format == "yml" { cfg.format = YAML } @@ -126,12 +127,14 @@ func loadConfig(env platform.Environment) *Config { err := config.LoadFiles(configFile) if err != nil { - return defaultConfig(true) + env.Error(err) + return defaultConfig(env, true) } err = config.BindStruct("", &cfg) if err != nil { - return defaultConfig(true) + env.Error(err) + return defaultConfig(env, true) } return &cfg @@ -194,9 +197,9 @@ func (cfg *Config) Export(format string) string { } } -func (cfg *Config) BackupAndMigrate(env platform.Environment) { +func (cfg *Config) BackupAndMigrate() { cfg.Backup() - cfg.Migrate(env) + cfg.Migrate() cfg.Write(cfg.format) } @@ -301,7 +304,7 @@ func escapeGlyphs(s string, migrate bool) string { return builder.String() } -func defaultConfig(warning bool) *Config { +func defaultConfig(env platform.Environment, warning bool) *Config { exitBackgroundTemplate := "{{ if gt .Code 0 }}p:red{{ end }}" exitTemplate := " {{ if gt .Code 0 }}\uf00d{{ else }}\uf00c{{ end }} " if warning { @@ -489,5 +492,6 @@ func defaultConfig(warning bool) *Config { }, }, } + cfg.env = env return cfg } diff --git a/src/engine/migrate.go b/src/engine/migrate.go index 2a707a9f..dc1b362e 100644 --- a/src/engine/migrate.go +++ b/src/engine/migrate.go @@ -17,14 +17,14 @@ const ( segmentTemplate = properties.Property("template") ) -func (cfg *Config) Migrate(env platform.Environment) { +func (cfg *Config) Migrate() { for _, block := range cfg.Blocks { for _, segment := range block.Segments { - segment.migrate(env, cfg.Version) + segment.migrate(cfg.env, cfg.Version) } } for _, segment := range cfg.Tooltips { - segment.migrate(env, cfg.Version) + segment.migrate(cfg.env, cfg.Version) } if strings.Contains(cfg.ConsoleTitleTemplate, ".Path") { cfg.ConsoleTitleTemplate = strings.ReplaceAll(cfg.ConsoleTitleTemplate, ".Path", ".PWD") diff --git a/src/engine/migrate_test.go b/src/engine/migrate_test.go index 000a98c2..06f426ef 100644 --- a/src/engine/migrate_test.go +++ b/src/engine/migrate_test.go @@ -427,8 +427,9 @@ func TestMigrateConfig(t *testing.T) { for _, tc := range cases { cfg := &Config{ ConsoleTitleTemplate: tc.Template, + env: &mock.MockedEnvironment{}, } - cfg.Migrate(&mock.MockedEnvironment{}) + cfg.Migrate() assert.Equal(t, tc.Expected, cfg.ConsoleTitleTemplate, tc.Case) } } diff --git a/src/platform/shell.go b/src/platform/shell.go index 7ebaa43e..c6991e8a 100644 --- a/src/platform/shell.go +++ b/src/platform/shell.go @@ -275,11 +275,13 @@ func (env *Shell) resolveConfigPath() { env.CmdFlags.Config = env.Getenv("POSH_THEME") } if len(env.CmdFlags.Config) == 0 { + env.Debug("No config set, fallback to default config") return } if strings.HasPrefix(env.CmdFlags.Config, "https://") { if err := env.downloadConfig(env.CmdFlags.Config); err != nil { // make it use default config when download fails + env.Error(err) env.CmdFlags.Config = "" return } @@ -287,6 +289,7 @@ func (env *Shell) resolveConfigPath() { // Cygwin path always needs the full path as we're on Windows but not really. // Doing filepath actions will convert it to a Windows path and break the init script. if env.Platform() == WINDOWS && env.Shell() == "bash" { + env.Debug("Cygwin detected, using full path for config") return } configFile := env.CmdFlags.Config