fix(az): validate config dir before continuing

resolves 
This commit is contained in:
Jan De Dobbeleer 2022-05-17 07:22:14 +02:00 committed by Jan De Dobbeleer
parent e041c23fe1
commit 8c43438340
2 changed files with 22 additions and 8 deletions
src/segments

View file

@ -2,6 +2,7 @@ package segments
import ( import (
"encoding/json" "encoding/json"
"errors"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/properties" "oh-my-posh/properties"
"path/filepath" "path/filepath"
@ -14,6 +15,8 @@ type Az struct {
AzureSubscription AzureSubscription
Origin string Origin string
configDir string
} }
const ( const (
@ -100,6 +103,11 @@ func (a *Az) Init(props properties.Properties, env environment.Environment) {
func (a *Az) Enabled() bool { func (a *Az) Enabled() bool {
source := a.props.GetString(Source, firstMatch) source := a.props.GetString(Source, firstMatch)
var err error
a.configDir, err = a.ConfigDir()
if err != nil {
return false
}
switch source { switch source {
case firstMatch: case firstMatch:
return a.getCLISubscription() || a.getModuleSubscription() return a.getCLISubscription() || a.getModuleSubscription()
@ -119,7 +127,7 @@ func (a *Az) FileContentWithoutBom(file string) string {
func (a *Az) getCLISubscription() bool { func (a *Az) getCLISubscription() bool {
var content string var content string
profile := filepath.Join(a.ConfigHome(), "azureProfile.json") profile := filepath.Join(a.configDir, "azureProfile.json")
if content = a.FileContentWithoutBom(profile); len(content) == 0 { if content = a.FileContentWithoutBom(profile); len(content) == 0 {
return false return false
} }
@ -139,9 +147,8 @@ func (a *Az) getCLISubscription() bool {
func (a *Az) getModuleSubscription() bool { func (a *Az) getModuleSubscription() bool {
var content string var content string
cfgHome := a.ConfigHome()
profiles := []string{ profiles := []string{
filepath.Join(cfgHome, "AzureRmContext.json"), filepath.Join(a.configDir, "AzureRmContext.json"),
} }
for _, profile := range profiles { for _, profile := range profiles {
if content = a.FileContentWithoutBom(profile); len(content) != 0 { if content = a.FileContentWithoutBom(profile); len(content) != 0 {
@ -173,10 +180,16 @@ func (a *Az) getModuleSubscription() bool {
return true return true
} }
func (a *Az) ConfigHome() string { func (a *Az) ConfigDir() (string, error) {
cfgHome := a.env.Getenv("AZURE_CONFIG_DIR") configDirs := []string{
if len(cfgHome) != 0 { a.env.Getenv("AZURE_CONFIG_DIR"),
return cfgHome filepath.Join(a.env.Home(), ".azure"),
filepath.Join(a.env.Home(), ".Azure"),
} }
return filepath.Join(a.env.Home(), ".azure") for _, dir := range configDirs {
if len(dir) != 0 && a.env.HasFolder(dir) {
return dir, nil
}
}
return "", errors.New("azure config dir not found")
} }

View file

@ -124,6 +124,7 @@ func TestAzSegment(t *testing.T) {
env.On("FileContent", filepath.Join(home, ".azure", "azureProfile.json")).Return(azureProfile) env.On("FileContent", filepath.Join(home, ".azure", "azureProfile.json")).Return(azureProfile)
env.On("FileContent", filepath.Join(home, ".azure", "AzureRmContext.json")).Return(azureRmContext) env.On("FileContent", filepath.Join(home, ".azure", "AzureRmContext.json")).Return(azureRmContext)
env.On("Getenv", "AZURE_CONFIG_DIR").Return("") env.On("Getenv", "AZURE_CONFIG_DIR").Return("")
env.On("HasFolder", filepath.Clean("/Users/posh/.azure")).Return(true)
if tc.Source == "" { if tc.Source == "" {
tc.Source = firstMatch tc.Source = firstMatch
} }