From b1c427abaa953db4fa6b01030717fe68a912be08 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 13 Jan 2022 20:25:24 +0100 Subject: [PATCH] fix(az): use platform specific folder resolves #1567 --- src/segment_az.go | 22 +++++++++++++--------- src/segment_az_test.go | 5 +++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/segment_az.go b/src/segment_az.go index e322d599..52bd9e68 100644 --- a/src/segment_az.go +++ b/src/segment_az.go @@ -96,20 +96,24 @@ func (a *az) init(props Properties, env Environment) { a.env = env } -func (a *az) getFileContentWithoutBom(file string) string { - const ByteOrderMark = "\ufeff" - file = filepath.Join(a.env.homeDir(), file) - config := a.env.getFileContent(file) - return strings.TrimLeft(config, ByteOrderMark) -} - func (a *az) enabled() bool { return a.getAzureProfile() || a.getAzureRmContext() } +func (a *az) getFileContentWithoutBom(file string) string { + azFolder := ".azure" + if a.env.getRuntimeGOOS() == linuxPlatform { + azFolder = ".Azure" + } + file = filepath.Join(a.env.homeDir(), azFolder, file) + config := a.env.getFileContent(file) + const ByteOrderMark = "\ufeff" + return strings.TrimLeft(config, ByteOrderMark) +} + func (a *az) getAzureProfile() bool { var content string - if content = a.getFileContentWithoutBom(".azure/azureProfile.json"); len(content) == 0 { + if content = a.getFileContentWithoutBom("azureProfile.json"); len(content) == 0 { return false } var config AzureConfig @@ -127,7 +131,7 @@ func (a *az) getAzureProfile() bool { func (a *az) getAzureRmContext() bool { var content string - if content = a.getFileContentWithoutBom(".azure/AzureRmContext.json"); len(content) == 0 { + if content = a.getFileContentWithoutBom("AzureRmContext.json"); len(content) == 0 { return false } var config AzurePowerShellConfig diff --git a/src/segment_az_test.go b/src/segment_az_test.go index bf0fed44..ba80b4b8 100644 --- a/src/segment_az_test.go +++ b/src/segment_az_test.go @@ -57,8 +57,9 @@ func TestAzSegment(t *testing.T) { content, _ := ioutil.ReadFile("./test/AzureRmContext.json") azureRmContext = string(content) } - env.On("getFileContent", filepath.Join(home, ".azure/azureProfile.json")).Return(azureProfile) - env.On("getFileContent", filepath.Join(home, ".azure/AzureRmContext.json")).Return(azureRmContext) + env.On("getRuntimeGOOS", nil).Return(linuxPlatform) + env.On("getFileContent", filepath.Join(home, ".Azure", "azureProfile.json")).Return(azureProfile) + env.On("getFileContent", filepath.Join(home, ".Azure", "AzureRmContext.json")).Return(azureRmContext) env.onTemplate() props := properties{ SegmentTemplate: tc.Template,