-
Notifications
You must be signed in to change notification settings - Fork 149
Add text output mode for auth token and auth env #4725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
simonfaltum
wants to merge
8
commits into
main
Choose a base branch
from
simonfaltum/auth-text-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+270
−14
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce79a10
Add text output mode for auth token and auth env commands
simonfaltum aeae5b6
Fix review findings: shell escaping, test coverage, nits
simonfaltum 0a4a1c9
Switch to single-quote shell escaping and improve test isolation
simonfaltum 72ebfd9
Fix gofmt formatting in quoteEnvValue
simonfaltum 0339754
Merge branch 'main' into simonfaltum/auth-text-output
simonfaltum 0c68a51
Fix quoteEnvValue doc comment to match actual behavior
simonfaltum a6000ec
Fix quoteEnvValue to treat newline and carriage return as shell-speci…
simonfaltum 23d833e
Merge remote-tracking branch 'origin/main' into simonfaltum/auth-text…
simonfaltum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/libs/cmdio" | ||
| "github.com/databricks/cli/libs/flags" | ||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestQuoteEnvValue(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| in string | ||
| want string | ||
| }{ | ||
| {name: "simple value", in: "hello", want: "hello"}, | ||
| {name: "empty value", in: "", want: `''`}, | ||
| {name: "value with space", in: "hello world", want: "'hello world'"}, | ||
| {name: "value with tab", in: "hello\tworld", want: "'hello\tworld'"}, | ||
| {name: "value with double quote", in: `say "hi"`, want: "'say \"hi\"'"}, | ||
| {name: "value with backslash", in: `path\to`, want: "'path\\to'"}, | ||
| {name: "url value", in: "https://example.com", want: "https://example.com"}, | ||
| {name: "value with dollar", in: "price$5", want: "'price$5'"}, | ||
| {name: "value with backtick", in: "hello`world", want: "'hello`world'"}, | ||
| {name: "value with bang", in: "hello!world", want: "'hello!world'"}, | ||
| {name: "value with single quote", in: "it's", want: "'it'\\''s'"}, | ||
| {name: "value with newline", in: "line1\nline2", want: "'line1\nline2'"}, | ||
| {name: "value with carriage return", in: "line1\rline2", want: "'line1\rline2'"}, | ||
| } | ||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| got := quoteEnvValue(c.in) | ||
| assert.Equal(t, c.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestEnvCommand_TextOutput(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| args []string | ||
| wantJSON bool | ||
| }{ | ||
| { | ||
| name: "default output is JSON", | ||
| args: []string{"--host", "https://test.cloud.databricks.com"}, | ||
| wantJSON: true, | ||
| }, | ||
| { | ||
| name: "explicit --output text produces KEY=VALUE lines", | ||
| args: []string{"--host", "https://test.cloud.databricks.com", "--output", "text"}, | ||
| wantJSON: false, | ||
| }, | ||
| { | ||
| name: "explicit --output json produces JSON", | ||
| args: []string{"--host", "https://test.cloud.databricks.com", "--output", "json"}, | ||
| wantJSON: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| parent := &cobra.Command{Use: "databricks"} | ||
| outputFlag := flags.OutputText | ||
| parent.PersistentFlags().VarP(&outputFlag, "output", "o", "output type: text or json") | ||
|
|
||
| envCmd := newEnvCommand() | ||
| parent.AddCommand(envCmd) | ||
| parent.SetContext(cmdio.MockDiscard(t.Context())) | ||
|
|
||
| // Set DATABRICKS_TOKEN so the SDK's config.Authenticate succeeds | ||
| // without hitting a real endpoint. | ||
| t.Setenv("DATABRICKS_TOKEN", "test-token-value") | ||
|
|
||
| var buf bytes.Buffer | ||
| parent.SetOut(&buf) | ||
| parent.SetArgs(append([]string{"env"}, c.args...)) | ||
|
|
||
| err := parent.Execute() | ||
| require.NoError(t, err) | ||
|
|
||
| output := buf.String() | ||
| if c.wantJSON { | ||
| assert.Contains(t, output, "{") | ||
| assert.Contains(t, output, "DATABRICKS_HOST") | ||
| } else { | ||
| assert.NotContains(t, output, "{") | ||
| assert.Contains(t, output, "DATABRICKS_HOST=") | ||
| assert.Contains(t, output, "=") | ||
| // Verify KEY=VALUE format (no JSON structure) | ||
| assert.NotContains(t, output, `"env"`) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have the
auth.Envandauth.ProcessEnvfunctions. Worth looking into using those.This command in general is pretty old and is also useless. It only returns the env corresponding to a profile. We should be able to make it more general i.e. return the env vars I need to set, to authenticate the same as the current CLI context.
I think a breaking change is pretty justified here (after double checking once with the usage numbers)