From 623fabcef31853255fa39afcf43cf7138d69079a Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Mon, 25 Apr 2022 09:50:55 +0200 Subject: [PATCH] fix(config): display default config on error --- src/engine/config.go | 5 +---- src/environment/shell.go | 16 +++++++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/engine/config.go b/src/engine/config.go index bb382c11..0cb8cfac 100644 --- a/src/engine/config.go +++ b/src/engine/config.go @@ -92,12 +92,9 @@ func LoadConfig(env environment.Environment) *Config { func loadConfig(env environment.Environment) *Config { var cfg Config configFile := env.Flags().Config - if configFile == "" { + if _, err := os.Stat(configFile); err != nil { return defaultConfig() } - if _, err := os.Stat(configFile); os.IsNotExist(err) { - cfg.exitWithError(err) - } cfg.origin = configFile cfg.format = strings.TrimPrefix(filepath.Ext(configFile), ".") diff --git a/src/environment/shell.go b/src/environment/shell.go index a64f6fe3..9ca33104 100644 --- a/src/environment/shell.go +++ b/src/environment/shell.go @@ -234,8 +234,9 @@ func (env *ShellEnvironment) resolveConfigPath() { env.CmdFlags.Config = fmt.Sprintf("https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/v%s/themes/default.omp.json", env.Version) } if strings.HasPrefix(env.CmdFlags.Config, "https://") { - env.getConfigPath(env.CmdFlags.Config) - return + if err := env.downloadConfig(env.CmdFlags.Config); err == nil { + return + } } // 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. @@ -255,12 +256,12 @@ func (env *ShellEnvironment) resolveConfigPath() { env.CmdFlags.Config = filepath.Clean(configFile) } -func (env *ShellEnvironment) getConfigPath(location string) { +func (env *ShellEnvironment) downloadConfig(location string) error { configFileName := fmt.Sprintf("%s.omp.json", env.Version) configPath := filepath.Join(env.CachePath(), configFileName) if env.HasFilesInDir(env.CachePath(), configFileName) { env.CmdFlags.Config = configPath - return + return nil } // clean old config files cleanCacheDir := func() { @@ -278,18 +279,19 @@ func (env *ShellEnvironment) getConfigPath(location string) { cfg, err := env.HTTPRequest(location, 5000) if err != nil { - return + return err } out, err := os.Create(configPath) if err != nil { - return + return err } defer out.Close() _, err = io.Copy(out, bytes.NewReader(cfg)) if err != nil { - return + return err } env.CmdFlags.Config = configPath + return nil } func (env *ShellEnvironment) trace(start time.Time, function string, args ...string) {