fix(config): log errors

relates to #3441
This commit is contained in:
Jan De Dobbeleer 2023-02-03 07:53:03 +01:00 committed by Jan De Dobbeleer
parent 7aa2ad14fc
commit 7dce0fe3e7
5 changed files with 22 additions and 14 deletions

View file

@ -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))
},
}

View file

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

View file

@ -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")

View file

@ -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)
}
}

View file

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