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
5 changes: 5 additions & 0 deletions acceptance/cmd/psql/completions/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Local = true
Cloud = false

[EnvMatrix]
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
6 changes: 6 additions & 0 deletions acceptance/cmd/psql/completions/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

=== Command should show instances names in autocomplete:
my-database
another-database
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
2 changes: 2 additions & 0 deletions acceptance/cmd/psql/completions/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title "Command should show instances names in autocomplete:\n"
$CLI __complete psql ""
10 changes: 10 additions & 0 deletions acceptance/cmd/psql/completions/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[[Server]]
Pattern = "GET /api/2.0/database/instances"
Response.Body = '''
{
"database_instances": [
{"name": "my-database"},
{"name": "another-database"}
]
}
'''
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 22 additions & 7 deletions cmd/psql/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ You can pass additional arguments to psql after a double-dash (--):
`,
}

// Wrapper for [root.MustWorkspaceClient] that disables loading authentication configuration from a bundle.
mustWorkspaceClient := func(cmd *cobra.Command, args []string) error {
cmd.SetContext(root.SkipLoadBundle(cmd.Context()))
return root.MustWorkspaceClient(cmd, args)
}

cmd.PreRunE = mustWorkspaceClient
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
Expand Down Expand Up @@ -83,5 +77,26 @@ You can pass additional arguments to psql after a double-dash (--):
return lakebase.Connect(cmd.Context(), databaseInstanceName, extraArgs...)
}

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
err := root.MustWorkspaceClient(cmd, args)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
instances, err := w.Database.ListDatabaseInstancesAll(ctx, database.ListDatabaseInstancesRequest{})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var names []string
for _, instance := range instances {
names = append(names, instance.Name)
}

return names, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}
Loading