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

View file

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