fix(az): find config in all possible folders

resolves #2771
This commit is contained in:
Jan De Dobbeleer 2022-09-12 19:34:52 +02:00 committed by Jan De Dobbeleer
parent 1d02bbc684
commit 6bc6fa23aa
2 changed files with 27 additions and 21 deletions

View file

@ -119,13 +119,12 @@ func (a *Az) FileContentWithoutBom(file string) string {
} }
func (a *Az) getCLISubscription() bool { func (a *Az) getCLISubscription() bool {
var content string cfg, err := a.findConfig("azureProfile.json")
configDir, err := a.ConfigDir(true)
if err != nil { if err != nil {
return false return false
} }
profile := filepath.Join(configDir, "azureProfile.json") content := a.FileContentWithoutBom(cfg)
if content = a.FileContentWithoutBom(profile); len(content) == 0 { if len(content) == 0 {
return false return false
} }
var config AzureConfig var config AzureConfig
@ -143,19 +142,11 @@ func (a *Az) getCLISubscription() bool {
} }
func (a *Az) getModuleSubscription() bool { func (a *Az) getModuleSubscription() bool {
var content string cfg, err := a.findConfig("AzureRmContext.json")
configDir, err := a.ConfigDir(false)
if err != nil { if err != nil {
return false return false
} }
profiles := []string{ content := a.FileContentWithoutBom(cfg)
filepath.Join(configDir, "AzureRmContext.json"),
}
for _, profile := range profiles {
if content = a.FileContentWithoutBom(profile); len(content) != 0 {
break
}
}
if len(content) == 0 { if len(content) == 0 {
return false return false
} }
@ -181,17 +172,15 @@ func (a *Az) getModuleSubscription() bool {
return true return true
} }
func (a *Az) ConfigDir(cli bool) (string, error) { func (a *Az) findConfig(fileName string) (string, error) {
configDirs := []string{ configDirs := []string{
filepath.Join(a.env.Home(), ".azure"), filepath.Join(a.env.Home(), ".azure"),
filepath.Join(a.env.Home(), ".Azure"), filepath.Join(a.env.Home(), ".Azure"),
} a.env.Getenv("AZURE_CONFIG_DIR"),
if cli {
configDirs = append([]string{a.env.Getenv("AZURE_CONFIG_DIR")}, configDirs...)
} }
for _, dir := range configDirs { for _, dir := range configDirs {
if len(dir) != 0 && a.env.HasFolder(dir) { if len(dir) != 0 && a.env.HasFilesInDir(dir, fileName) {
return dir, nil return filepath.Join(dir, fileName), nil
} }
} }
return "", errors.New("azure config dir not found") return "", errors.New("azure config dir not found")

View file

@ -112,6 +112,7 @@ func TestAzSegment(t *testing.T) {
home := "/Users/posh" home := "/Users/posh"
env.On("Home").Return(home) env.On("Home").Return(home)
var azureProfile, azureRmContext string var azureProfile, azureRmContext string
if tc.HasCLI { if tc.HasCLI {
content, _ := os.ReadFile("../test/azureProfile.json") content, _ := os.ReadFile("../test/azureProfile.json")
azureProfile = string(content) azureProfile = string(content)
@ -120,14 +121,30 @@ func TestAzSegment(t *testing.T) {
content, _ := os.ReadFile("../test/AzureRmContext.json") content, _ := os.ReadFile("../test/AzureRmContext.json")
azureRmContext = string(content) azureRmContext = string(content)
} }
env.On("GOOS").Return(environment.LINUX) env.On("GOOS").Return(environment.LINUX)
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.HasCLI {
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "azureProfile.json").Return(true)
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "AzureRmContext.json").Return(false)
} else if tc.HasPowerShell {
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "azureProfile.json").Return(false)
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.Azure"), "azureProfile.json").Return(false)
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "AzureRmContext.json").Return(true)
} else {
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "azureProfile.json").Return(false)
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.Azure"), "azureProfile.json").Return(false)
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "AzureRmContext.json").Return(false)
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.Azure"), "AzureRmContext.json").Return(false)
}
if tc.Source == "" { if tc.Source == "" {
tc.Source = firstMatch tc.Source = firstMatch
} }
az := &Az{ az := &Az{
env: env, env: env,
props: properties.Map{ props: properties.Map{