From aed6450baf9774e645cc5fbcd5fff644d18ac118 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Fri, 26 May 2023 16:02:53 +0200 Subject: [PATCH] Do not prompt for List methods (#411) ## Changes Do not prompt for List methods ## Tests Running ``` cli workspace list ``` Before ``` cli workspace list Error: Path () doesn't start with '/' ``` After ``` cli workspace list Error: accepts 1 arg(s), received 0 ``` --- .codegen/service.go.tmpl | 2 +- cmd/workspace/schemas/schemas.go | 23 ++++++++--------------- cmd/workspace/workspace/workspace.go | 23 ++++++++--------------- internal/clusters_test.go | 3 --- internal/helpers.go | 2 ++ internal/secrets_test.go | 2 -- internal/workspace_test.go | 24 ++++++++++++++++++++++++ 7 files changed, 43 insertions(+), 36 deletions(-) create mode 100644 internal/workspace_test.go diff --git a/.codegen/service.go.tmpl b/.codegen/service.go.tmpl index 5510978a..aa6d3fd1 100644 --- a/.codegen/service.go.tmpl +++ b/.codegen/service.go.tmpl @@ -60,7 +60,7 @@ func init() { {{ $hasPosArgs := and .Request (or .Request.IsAllRequiredFieldsPrimitive (eq .PascalName "RunNow")) -}} {{- $hasSinglePosArg := and $hasPosArgs (eq 1 (len .Request.RequiredFields)) -}} -{{- $serviceHasNamedIdMap := and .Service.List .Service.List.NamedIdMap -}} +{{- $serviceHasNamedIdMap := and (and .Service.List .Service.List.NamedIdMap) (not (eq .PascalName "List")) -}} {{- $hasIdPrompt := and $hasSinglePosArg $serviceHasNamedIdMap -}} {{- $wait := and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) -}} {{- $hasRequiredArgs := and (not $hasIdPrompt) $hasPosArgs -}} diff --git a/cmd/workspace/schemas/schemas.go b/cmd/workspace/schemas/schemas.go index 1f62d0cf..16e0387a 100755 --- a/cmd/workspace/schemas/schemas.go +++ b/cmd/workspace/schemas/schemas.go @@ -214,7 +214,14 @@ var listCmd = &cobra.Command{ no guarantee of a specific ordering of the elements in the array.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) @@ -224,20 +231,6 @@ var listCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - names, err := w.Schemas.SchemaInfoNameToFullNameMap(ctx, catalog.ListSchemasRequest{}) - if err != nil { - return err - } - id, err := cmdio.Select(ctx, names, "Parent catalog for schemas of interest") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have parent catalog for schemas of interest") - } listReq.CatalogName = args[0] } diff --git a/cmd/workspace/workspace/workspace.go b/cmd/workspace/workspace/workspace.go index 7cfe39dc..04623424 100755 --- a/cmd/workspace/workspace/workspace.go +++ b/cmd/workspace/workspace/workspace.go @@ -294,7 +294,14 @@ var listCmd = &cobra.Command{ RESOURCE_DOES_NOT_EXIST.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) @@ -304,20 +311,6 @@ var listCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - names, err := w.Workspace.ObjectInfoPathToObjectIdMap(ctx, workspace.ListWorkspaceRequest{}) - if err != nil { - return err - } - id, err := cmdio.Select(ctx, names, "The absolute path of the notebook or directory") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the absolute path of the notebook or directory") - } listReq.Path = args[0] } diff --git a/internal/clusters_test.go b/internal/clusters_test.go index 827015d2..74250972 100644 --- a/internal/clusters_test.go +++ b/internal/clusters_test.go @@ -6,8 +6,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - - _ "github.com/databricks/cli/cmd/workspace" ) var clusterId string @@ -24,7 +22,6 @@ func TestAccClustersList(t *testing.T) { idRegExp := regexp.MustCompile(`[0-9]{4}\-[0-9]{6}-[a-z0-9]{8}`) clusterId = idRegExp.FindString(outStr) - fmt.Println(clusterId) assert.NotEmpty(t, clusterId) } diff --git a/internal/helpers.go b/internal/helpers.go index 135ebbdb..b51d005b 100644 --- a/internal/helpers.go +++ b/internal/helpers.go @@ -15,6 +15,8 @@ import ( "github.com/databricks/cli/cmd/root" _ "github.com/databricks/cli/cmd/version" "github.com/stretchr/testify/require" + + _ "github.com/databricks/cli/cmd/workspace" ) const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" diff --git a/internal/secrets_test.go b/internal/secrets_test.go index 69423d17..1fdc48bd 100644 --- a/internal/secrets_test.go +++ b/internal/secrets_test.go @@ -4,8 +4,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - - _ "github.com/databricks/cli/cmd/workspace" ) func TestSecretsCreateScopeErrWhenNoArguments(t *testing.T) { diff --git a/internal/workspace_test.go b/internal/workspace_test.go new file mode 100644 index 00000000..83d7be22 --- /dev/null +++ b/internal/workspace_test.go @@ -0,0 +1,24 @@ +package internal + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAccWorkspaceList(t *testing.T) { + t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV")) + + stdout, stderr := RequireSuccessfulRun(t, "workspace", "list", "/") + outStr := stdout.String() + assert.Contains(t, outStr, "ID") + assert.Contains(t, outStr, "Type") + assert.Contains(t, outStr, "Language") + assert.Contains(t, outStr, "Path") + assert.Equal(t, "", stderr.String()) +} + +func TestWorkpaceListErrorWhenNoArguments(t *testing.T) { + _, _, err := RequireErrorRun(t, "workspace", "list") + assert.Equal(t, "accepts 1 arg(s), received 0", err.Error()) +}