Skip to content

Commit

Permalink
Add service principal flag and handle cmd for .azure dir
Browse files Browse the repository at this point in the history
- Refactor some common code into method.
- Use `true` as a no-op command when using an existin .azure directory.
- Fix indentation.
  • Loading branch information
lbergnehr committed Jan 21, 2025
1 parent df4760d commit 539b976
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
14 changes: 12 additions & 2 deletions pkg/az/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ func (c *LoginCommand) SetAction(action string) {
}

func (c *LoginCommand) GetCommand() string {
if c.azureDirExists() {
// Use a no-op command since we don't have to log in.
return "true"
}

return "az"
}

func (c *LoginCommand) GetArguments() []string {
if _, err := os.Stat(filepath.Join(os.Getenv("HOME"), ".azure")); err == nil {
if c.azureDirExists() {
return []string{}
}
return []string{"login"}
Expand All @@ -46,7 +51,7 @@ func (c *LoginCommand) GetArguments() []string {
func (c *LoginCommand) GetFlags() builder.Flags {
flags := builder.Flags{}

if _, err := os.Stat(filepath.Join(os.Getenv("HOME"), ".azure")); err == nil {
if c.azureDirExists() {
return flags
}

Expand All @@ -71,3 +76,8 @@ func (c *LoginCommand) GetFlags() builder.Flags {
func (c *LoginCommand) SuppressesOutput() bool {
return false
}

func (c *LoginCommand) azureDirExists() bool {
_, err := os.Stat(filepath.Join(os.Getenv("HOME"), ".azure"))
return err == nil
}
22 changes: 12 additions & 10 deletions pkg/az/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestLoginCommand_GetArguments_ServicePrincipal(t *testing.T) {
assert.Equal(t, expectedArgs, args)
}

func TestLoginCommand_GetArguments_ExistingAzureDirectory(t *testing.T) {
func TestLoginCommand_GetCommandAndGetArguments_ExistingAzureDirectory(t *testing.T) {
tempHome := t.TempDir()
os.Setenv("HOME", tempHome)
homeDir := os.Getenv("HOME")
Expand All @@ -37,6 +37,7 @@ func TestLoginCommand_GetArguments_ExistingAzureDirectory(t *testing.T) {
args := cmd.GetArguments()

expectedArgs := []string{}
assert.Equal(t, "true", cmd.GetCommand())
assert.Equal(t, expectedArgs, args)
}

Expand Down Expand Up @@ -68,6 +69,7 @@ func TestLoginCommand_GetFlags_ServicePrincipal(t *testing.T) {
flags := cmd.GetFlags()

expectedFlags := builder.Flags{
builder.NewFlag("service-principal", ""),
builder.NewFlag("username", "test-client-id"),
builder.NewFlag("password", "test-client-secret"),
builder.NewFlag("tenant", "test-tenant-id"),
Expand Down Expand Up @@ -105,15 +107,15 @@ func TestLoginCommand_GetFlags_SystemManagedIdentity(t *testing.T) {
}

func TestLoginCommand_GetFlags_ExistingAzureDirectory(t *testing.T) {
tempHome := t.TempDir()
os.Setenv("HOME", tempHome)
homeDir := os.Getenv("HOME")
os.MkdirAll(filepath.Join(homeDir, ".azure"), 0755)
defer os.RemoveAll(filepath.Join(homeDir, ".azure"))
tempHome := t.TempDir()
os.Setenv("HOME", tempHome)
homeDir := os.Getenv("HOME")
os.MkdirAll(filepath.Join(homeDir, ".azure"), 0755)
defer os.RemoveAll(filepath.Join(homeDir, ".azure"))

cmd := &LoginCommand{}
flags := cmd.GetFlags()
cmd := &LoginCommand{}
flags := cmd.GetFlags()

expectedFlags := builder.Flags{}
assert.Equal(t, expectedFlags, flags)
expectedFlags := builder.Flags{}
assert.Equal(t, expectedFlags, flags)
}

0 comments on commit 539b976

Please sign in to comment.