fix(config): display default config on error

This commit is contained in:
Jan De Dobbeleer 2022-04-25 09:50:55 +02:00 committed by Jan De Dobbeleer
parent 56af13e140
commit 623fabcef3
2 changed files with 10 additions and 11 deletions

View file

@ -92,12 +92,9 @@ func LoadConfig(env environment.Environment) *Config {
func loadConfig(env environment.Environment) *Config { func loadConfig(env environment.Environment) *Config {
var cfg Config var cfg Config
configFile := env.Flags().Config configFile := env.Flags().Config
if configFile == "" { if _, err := os.Stat(configFile); err != nil {
return defaultConfig() return defaultConfig()
} }
if _, err := os.Stat(configFile); os.IsNotExist(err) {
cfg.exitWithError(err)
}
cfg.origin = configFile cfg.origin = configFile
cfg.format = strings.TrimPrefix(filepath.Ext(configFile), ".") cfg.format = strings.TrimPrefix(filepath.Ext(configFile), ".")

View file

@ -234,9 +234,10 @@ 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) 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://") { if strings.HasPrefix(env.CmdFlags.Config, "https://") {
env.getConfigPath(env.CmdFlags.Config) if err := env.downloadConfig(env.CmdFlags.Config); err == nil {
return return
} }
}
// Cygwin path always needs the full path as we're on Windows but not really. // 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. // Doing filepath actions will convert it to a Windows path and break the init script.
if env.Platform() == WindowsPlatform && env.Shell() == "constants.BASH" { if env.Platform() == WindowsPlatform && env.Shell() == "constants.BASH" {
@ -255,12 +256,12 @@ func (env *ShellEnvironment) resolveConfigPath() {
env.CmdFlags.Config = filepath.Clean(configFile) 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) configFileName := fmt.Sprintf("%s.omp.json", env.Version)
configPath := filepath.Join(env.CachePath(), configFileName) configPath := filepath.Join(env.CachePath(), configFileName)
if env.HasFilesInDir(env.CachePath(), configFileName) { if env.HasFilesInDir(env.CachePath(), configFileName) {
env.CmdFlags.Config = configPath env.CmdFlags.Config = configPath
return return nil
} }
// clean old config files // clean old config files
cleanCacheDir := func() { cleanCacheDir := func() {
@ -278,18 +279,19 @@ func (env *ShellEnvironment) getConfigPath(location string) {
cfg, err := env.HTTPRequest(location, 5000) cfg, err := env.HTTPRequest(location, 5000)
if err != nil { if err != nil {
return return err
} }
out, err := os.Create(configPath) out, err := os.Create(configPath)
if err != nil { if err != nil {
return return err
} }
defer out.Close() defer out.Close()
_, err = io.Copy(out, bytes.NewReader(cfg)) _, err = io.Copy(out, bytes.NewReader(cfg))
if err != nil { if err != nil {
return return err
} }
env.CmdFlags.Config = configPath env.CmdFlags.Config = configPath
return nil
} }
func (env *ShellEnvironment) trace(start time.Time, function string, args ...string) { func (env *ShellEnvironment) trace(start time.Time, function string, args ...string) {