Skip to content

Commit da3daf5

Browse files
authored
Add text output mode for auth token (#4903)
## Why `auth token` always outputs JSON, ignoring the `--output` flag. If you want to pipe just the token string into another tool, you have to parse JSON. That's unnecessary friction for a common scripting pattern. ## Changes `auth token` now respects `--output text` when explicitly set, outputting just the access token string (with a trailing newline) suitable for piping. JSON remains the default for backward compatibility. Only honors the explicit `--output text` flag, not implicit text mode (e.g. from `DATABRICKS_OUTPUT_FORMAT`), so existing scripts that parse JSON output won't break. Also fixes discarded write errors (`_, _` replaced with proper error returns). ## Test plan - [x] Unit tests for default (JSON), explicit `--output json`, and `--output text` modes - [x] Existing `cmd/auth` tests pass
1 parent 6426d26 commit da3daf5

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

cmd/auth/token.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"io"
89
"strings"
910
"time"
1011

12+
"github.com/databricks/cli/cmd/root"
1113
"github.com/databricks/cli/libs/auth"
1214
"github.com/databricks/cli/libs/cmdio"
1315
"github.com/databricks/cli/libs/databrickscfg"
1416
"github.com/databricks/cli/libs/databrickscfg/profile"
1517
"github.com/databricks/cli/libs/env"
18+
"github.com/databricks/cli/libs/flags"
1619
"github.com/databricks/databricks-sdk-go/config"
1720
"github.com/databricks/databricks-sdk-go/credentials/u2m"
1821
"github.com/databricks/databricks-sdk-go/credentials/u2m/cache"
@@ -88,17 +91,30 @@ and secret is not supported.`,
8891
if err != nil {
8992
return err
9093
}
91-
raw, err := json.MarshalIndent(t, "", " ")
92-
if err != nil {
93-
return err
94-
}
95-
_, _ = cmd.OutOrStdout().Write(raw)
96-
return nil
94+
// Only honor the explicit --output text flag, not implicit text mode
95+
// (e.g. from DATABRICKS_OUTPUT_FORMAT). auth token defaults to JSON,
96+
// and changing that implicitly would break scripts that parse JSON output.
97+
textMode := cmd.Flag("output").Changed && root.OutputType(cmd) == flags.OutputText
98+
return writeTokenOutput(cmd.OutOrStdout(), t, textMode)
9799
}
98100

99101
return cmd
100102
}
101103

104+
func writeTokenOutput(w io.Writer, t *oauth2.Token, textMode bool) error {
105+
if textMode {
106+
_, err := fmt.Fprintln(w, t.AccessToken)
107+
return err
108+
}
109+
110+
raw, err := json.MarshalIndent(t, "", " ")
111+
if err != nil {
112+
return err
113+
}
114+
_, err = w.Write(raw)
115+
return err
116+
}
117+
102118
type loadTokenArgs struct {
103119
// authArguments is the parsed auth arguments, including the host and optionally the account ID.
104120
authArguments *auth.AuthArguments

cmd/auth/token_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package auth
22

33
import (
4+
"bytes"
45
"context"
6+
"encoding/json"
57
"errors"
68
"net/http"
79
"testing"
@@ -798,3 +800,27 @@ func (e errProfiler) LoadProfiles(context.Context, profile.ProfileMatchFunction)
798800
func (e errProfiler) GetPath(context.Context) (string, error) {
799801
return "<error>", nil
800802
}
803+
804+
func TestWriteTokenOutput(t *testing.T) {
805+
token := &oauth2.Token{
806+
AccessToken: "my-access-token",
807+
TokenType: "Bearer",
808+
}
809+
810+
t.Run("json mode", func(t *testing.T) {
811+
var buf bytes.Buffer
812+
err := writeTokenOutput(&buf, token, false)
813+
assert.NoError(t, err)
814+
815+
raw, err := json.MarshalIndent(token, "", " ")
816+
assert.NoError(t, err)
817+
assert.Equal(t, string(raw), buf.String())
818+
})
819+
820+
t.Run("text mode", func(t *testing.T) {
821+
var buf bytes.Buffer
822+
err := writeTokenOutput(&buf, token, true)
823+
assert.NoError(t, err)
824+
assert.Equal(t, "my-access-token\n", buf.String())
825+
})
826+
}

0 commit comments

Comments
 (0)