diff --git a/acceptance/cmd/psql/completions/out.test.toml b/acceptance/cmd/psql/completions/out.test.toml new file mode 100644 index 00000000000..8f3575be7b5 --- /dev/null +++ b/acceptance/cmd/psql/completions/out.test.toml @@ -0,0 +1,5 @@ +Local = true +Cloud = false + +[EnvMatrix] + DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"] diff --git a/acceptance/cmd/psql/completions/output.txt b/acceptance/cmd/psql/completions/output.txt new file mode 100644 index 00000000000..94241d4a31b --- /dev/null +++ b/acceptance/cmd/psql/completions/output.txt @@ -0,0 +1,6 @@ + +=== Command should show instances names in autocomplete: +my-database +another-database +:4 +Completion ended with directive: ShellCompDirectiveNoFileComp diff --git a/acceptance/cmd/psql/completions/script b/acceptance/cmd/psql/completions/script new file mode 100644 index 00000000000..b521c171224 --- /dev/null +++ b/acceptance/cmd/psql/completions/script @@ -0,0 +1,2 @@ +title "Command should show instances names in autocomplete:\n" +$CLI __complete psql "" diff --git a/acceptance/cmd/psql/completions/test.toml b/acceptance/cmd/psql/completions/test.toml new file mode 100644 index 00000000000..a01fd713ea1 --- /dev/null +++ b/acceptance/cmd/psql/completions/test.toml @@ -0,0 +1,10 @@ +[[Server]] +Pattern = "GET /api/2.0/database/instances" +Response.Body = ''' +{ + "database_instances": [ + {"name": "my-database"}, + {"name": "another-database"} + ] +} +''' diff --git a/acceptance/cmd/psql/echo-arguments.sh b/acceptance/cmd/psql/simple/echo-arguments.sh similarity index 100% rename from acceptance/cmd/psql/echo-arguments.sh rename to acceptance/cmd/psql/simple/echo-arguments.sh diff --git a/acceptance/cmd/psql/out.test.toml b/acceptance/cmd/psql/simple/out.test.toml similarity index 100% rename from acceptance/cmd/psql/out.test.toml rename to acceptance/cmd/psql/simple/out.test.toml diff --git a/acceptance/cmd/psql/output.txt b/acceptance/cmd/psql/simple/output.txt similarity index 100% rename from acceptance/cmd/psql/output.txt rename to acceptance/cmd/psql/simple/output.txt diff --git a/acceptance/cmd/psql/script b/acceptance/cmd/psql/simple/script similarity index 100% rename from acceptance/cmd/psql/script rename to acceptance/cmd/psql/simple/script diff --git a/acceptance/cmd/psql/test.toml b/acceptance/cmd/psql/simple/test.toml similarity index 100% rename from acceptance/cmd/psql/test.toml rename to acceptance/cmd/psql/simple/test.toml diff --git a/cmd/psql/psql.go b/cmd/psql/psql.go index 21f2f633517..928784a50be 100644 --- a/cmd/psql/psql.go +++ b/cmd/psql/psql.go @@ -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) @@ -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 }