diff --git a/cli/azd/cmd/auth.go b/cli/azd/cmd/auth.go index 17fcd17b654..edc7c160ccf 100644 --- a/cli/azd/cmd/auth.go +++ b/cli/azd/cmd/auth.go @@ -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, }) diff --git a/cli/azd/cmd/auth_token.go b/cli/azd/cmd/auth_token.go index 3c3dabf0bbf..481ab3f6ae6 100644 --- a/cli/azd/cmd/auth_token.go +++ b/cli/azd/cmd/auth_token.go @@ -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, } } @@ -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 } diff --git a/cli/azd/cmd/auth_token_test.go b/cli/azd/cmd/auth_token_test.go index 40441bcfcc5..215ce1d1512 100644 --- a/cli/azd/cmd/auth_token_test.go +++ b/cli/azd/cmd/auth_token_test.go @@ -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{} diff --git a/cli/azd/cmd/auto_install_integration_test.go b/cli/azd/cmd/auto_install_integration_test.go index a8fe0cae325..34b5a0e7abe 100644 --- a/cli/azd/cmd/auto_install_integration_test.go +++ b/cli/azd/cmd/auto_install_integration_test.go @@ -98,7 +98,7 @@ func TestExecuteWithAutoInstall_ReturnsCommandErrorWithoutPanicForOutputFlags(t "--tenant-id", "00000000-0000-0000-0000-000000000000", "--output", - "none", + "unknown", } rootContainer := ioc.NewNestedContainer(nil) @@ -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.