feat: show config errors in default prompt

relates to #20
This commit is contained in:
Jan De Dobbeleer 2020-11-11 18:52:45 +01:00 committed by Jan De Dobbeleer
parent 6d3e05e4e0
commit 27ed8daeb0

View file

@ -2,7 +2,7 @@ package main
import (
"encoding/json"
"fmt"
"errors"
"os"
)
@ -43,30 +43,36 @@ type Block struct {
func GetSettings(env environmentInfo) *Settings {
settings, err := loadUserConfiguration(env)
if err != nil {
return getDefaultSettings()
return getDefaultSettings(err.Error())
}
return settings
}
func loadUserConfiguration(env environmentInfo) (*Settings, error) {
var settings Settings
settingsFileLocation := fmt.Sprintf("%s/.go_my_posh", env.getenv("HOME"))
if _, err := os.Stat(*env.getArgs().Config); !os.IsNotExist(err) {
settingsFileLocation = *env.getArgs().Config
settingsFile := *env.getArgs().Config
if settingsFile == "" {
return nil, errors.New("NO CONFIG")
}
defaultSettings, err := os.Open(settingsFileLocation)
if _, err := os.Stat(settingsFile); os.IsNotExist(err) {
return nil, errors.New("INVALID CONFIG PATH")
}
defaultSettings, err := os.Open(settingsFile)
defer func() {
_ = defaultSettings.Close()
}()
if err != nil {
return nil, err
return nil, errors.New("UNABLE TO OPEN CONFIG")
}
jsonParser := json.NewDecoder(defaultSettings)
err = jsonParser.Decode(&settings)
return &settings, err
if err != nil {
return nil, errors.New("INVALID CONFIG")
}
return &settings, nil
}
func getDefaultSettings() *Settings {
func getDefaultSettings(info string) *Settings {
settings := &Settings{
FinalSpace: true,
Blocks: []*Block{
@ -147,6 +153,16 @@ func getDefaultSettings() *Settings {
Background: "#ffff66",
Foreground: "#ffffff",
},
{
Type: Text,
Style: Powerline,
PowerlineSymbol: "\uE0B0",
Background: "#ffffff",
Foreground: "#111111",
Properties: map[Property]interface{}{
TextProperty: info,
},
},
{
Type: Exit,
Style: Diamond,