Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/azd/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func authActions(root *actions.ActionDescriptor) *actions.ActionDescriptor {
Command: newAuthTokenCmd(),
FlagsResolver: newAuthTokenFlags,
ActionResolver: newAuthTokenAction,
OutputFormats: []output.Format{output.JsonFormat},
OutputFormats: []output.Format{output.JsonFormat, output.NoneFormat},
DefaultFormat: output.NoneFormat,
})

Expand Down
9 changes: 7 additions & 2 deletions cli/azd/cmd/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func newAuthTokenFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions

func newAuthTokenCmd() *cobra.Command {
return &cobra.Command{
Use: "token --output json",
Use: "token",
Hidden: true,
}
}
Expand Down Expand Up @@ -193,5 +193,10 @@ func (a *authTokenAction) Run(ctx context.Context) (*actions.ActionResult, error
ExpiresOn: contracts.RFC3339Time(token.ExpiresOn),
}

return nil, a.formatter.Format(res, a.writer, nil)
if a.formatter.Kind() != output.NoneFormat {
return nil, a.formatter.Format(res, a.writer, nil)
}

fmt.Fprintln(a.writer, res.Token)
return nil, nil
}
29 changes: 29 additions & 0 deletions cli/azd/cmd/auth_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ func TestAuthToken(t *testing.T) {
require.Equal(t, time.Unix(1669153000, 0).UTC(), time.Time(res.ExpiresOn))
}

func TestAuthToken_DefaultUnformattedOutput(t *testing.T) {
buf := &bytes.Buffer{}

token := authTokenFn(func(ctx context.Context, options policy.TokenRequestOptions) (azcore.AccessToken, error) {
require.ElementsMatch(t, []string{managementScope}, options.Scopes)

return azcore.AccessToken{
Token: "ABC123",
ExpiresOn: time.Unix(1669153000, 0).UTC(),
}, nil
})

a := newAuthTokenAction(
credentialProviderForTokenFn(token),
&output.NoneFormatter{},
buf,
&authTokenFlags{},
func(ctx context.Context) (*environment.Environment, error) {
return nil, fmt.Errorf("not an azd env directory")
},
&mockSubscriptionTenantResolver{},
cloud.AzurePublic(),
)

_, err := a.Run(t.Context())
require.NoError(t, err)
require.Equal(t, "ABC123\n", buf.String())
}

func TestAuthTokenSysEnv(t *testing.T) {
buf := &bytes.Buffer{}

Expand Down
6 changes: 3 additions & 3 deletions cli/azd/cmd/auto_install_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestExecuteWithAutoInstall_ReturnsCommandErrorWithoutPanicForOutputFlags(t
"--tenant-id",
"00000000-0000-0000-0000-000000000000",
"--output",
"none",
"unknown",
}

rootContainer := ioc.NewNestedContainer(nil)
Expand All @@ -121,9 +121,9 @@ func TestExecuteWithAutoInstall_ReturnsCommandErrorWithoutPanicForOutputFlags(t
stderrBytes, err := io.ReadAll(stderrReader)
require.NoError(t, err)

require.ErrorContains(t, execErr, "unsupported format 'none'")
require.ErrorContains(t, execErr, "unsupported format 'unknown'")
require.NotContains(t, string(stderrBytes), "panic:")
require.Contains(t, string(stderrBytes), "Error: unsupported format 'none'")
require.Contains(t, string(stderrBytes), "Error: unsupported format 'unknown'")
}

// TestAgentDetectionIntegration tests the full agent detection integration flow.
Expand Down
Loading