diff --git a/.codegen/service.go.tmpl b/.codegen/service.go.tmpl index 5bab0960..d098432b 100644 --- a/.codegen/service.go.tmpl +++ b/.codegen/service.go.tmpl @@ -26,6 +26,7 @@ var Cmd = &cobra.Command{ {{- end}} } +{{- $serviceName := .KebabName -}} {{range .Methods}} {{- $excludes := list "put-secret" -}} @@ -62,11 +63,14 @@ func init() { {{- end}} {{end}} } +{{- $excludeFromPrompts := list "workspace get-status" -}} +{{- $fullCommandName := (print $serviceName " " .KebabName) -}} +{{- $noPrompt := or .IsCrudCreate (in $excludeFromPrompts $fullCommandName) }} {{ $hasPosArgs := and .Request (or .Request.IsAllRequiredFieldsPrimitive (eq .PascalName "RunNow")) -}} {{- $hasSinglePosArg := and $hasPosArgs (eq 1 (len .Request.RequiredFields)) -}} {{- $serviceHasNamedIdMap := and (and .Service.List .Service.List.NamedIdMap) (not (eq .PascalName "List")) -}} -{{- $hasIdPrompt := and $hasSinglePosArg $serviceHasNamedIdMap -}} +{{- $hasIdPrompt := and (not $noPrompt) (and $hasSinglePosArg $serviceHasNamedIdMap) -}} {{- $wait := and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) -}} {{- $hasRequiredArgs := and (not $hasIdPrompt) $hasPosArgs -}} var {{.CamelName}}Cmd = &cobra.Command{ diff --git a/cmd/account/networks/networks.go b/cmd/account/networks/networks.go index e472f0dd..4b0bf063 100755 --- a/cmd/account/networks/networks.go +++ b/cmd/account/networks/networks.go @@ -47,7 +47,14 @@ var createCmd = &cobra.Command{ pre-existing VPC and subnets.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + 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.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) @@ -57,23 +64,6 @@ var createCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - promptSpinner := cmdio.Spinner(ctx) - promptSpinner <- "No NETWORK_NAME argument specified. Loading names for Networks drop-down." - names, err := a.Networks.NetworkNetworkNameToNetworkIdMap(ctx) - close(promptSpinner) - if err != nil { - return fmt.Errorf("failed to load names for Networks drop-down. Please manually specify required arguments. Original error: %w", err) - } - id, err := cmdio.Select(ctx, names, "The human-readable name of the network configuration") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the human-readable name of the network configuration") - } createReq.NetworkName = args[0] } diff --git a/cmd/account/vpc-endpoints/vpc-endpoints.go b/cmd/account/vpc-endpoints/vpc-endpoints.go index 778e8896..1cb72927 100755 --- a/cmd/account/vpc-endpoints/vpc-endpoints.go +++ b/cmd/account/vpc-endpoints/vpc-endpoints.go @@ -54,7 +54,14 @@ var createCmd = &cobra.Command{ [endpoint service]: https://docs.aws.amazon.com/vpc/latest/privatelink/privatelink-share-your-services.html`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + 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.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) @@ -64,23 +71,6 @@ var createCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - promptSpinner := cmdio.Spinner(ctx) - promptSpinner <- "No VPC_ENDPOINT_NAME argument specified. Loading names for Vpc Endpoints drop-down." - names, err := a.VpcEndpoints.VpcEndpointVpcEndpointNameToVpcEndpointIdMap(ctx) - close(promptSpinner) - if err != nil { - return fmt.Errorf("failed to load names for Vpc Endpoints drop-down. Please manually specify required arguments. Original error: %w", err) - } - id, err := cmdio.Select(ctx, names, "The human-readable name of the storage configuration") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the human-readable name of the storage configuration") - } createReq.VpcEndpointName = args[0] } diff --git a/cmd/account/workspaces/workspaces.go b/cmd/account/workspaces/workspaces.go index 2c7a996e..ebe793cd 100755 --- a/cmd/account/workspaces/workspaces.go +++ b/cmd/account/workspaces/workspaces.go @@ -73,7 +73,14 @@ var createCmd = &cobra.Command{ workspace becomes available when the status changes to RUNNING.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + 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.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) @@ -83,23 +90,6 @@ var createCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - promptSpinner := cmdio.Spinner(ctx) - promptSpinner <- "No WORKSPACE_NAME argument specified. Loading names for Workspaces drop-down." - names, err := a.Workspaces.WorkspaceWorkspaceNameToWorkspaceIdMap(ctx) - close(promptSpinner) - if err != nil { - return fmt.Errorf("failed to load names for Workspaces drop-down. Please manually specify required arguments. Original error: %w", err) - } - id, err := cmdio.Select(ctx, names, "The workspace's human-readable name") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the workspace's human-readable name") - } createReq.WorkspaceName = args[0] } diff --git a/cmd/workspace/cluster-policies/cluster-policies.go b/cmd/workspace/cluster-policies/cluster-policies.go index 66d3d835..5de2067d 100755 --- a/cmd/workspace/cluster-policies/cluster-policies.go +++ b/cmd/workspace/cluster-policies/cluster-policies.go @@ -67,7 +67,14 @@ var createCmd = &cobra.Command{ Creates a new policy with prescribed settings.`, 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) @@ -77,23 +84,6 @@ var createCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - promptSpinner := cmdio.Spinner(ctx) - promptSpinner <- "No NAME argument specified. Loading names for Cluster Policies drop-down." - names, err := w.ClusterPolicies.PolicyNameToPolicyIdMap(ctx, compute.ListClusterPoliciesRequest{}) - close(promptSpinner) - if err != nil { - return fmt.Errorf("failed to load names for Cluster Policies drop-down. Please manually specify required arguments. Original error: %w", err) - } - id, err := cmdio.Select(ctx, names, "Cluster Policy name requested by the user") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have cluster policy name requested by the user") - } createReq.Name = args[0] } diff --git a/cmd/workspace/clusters/clusters.go b/cmd/workspace/clusters/clusters.go index 3a40b2d4..3a1eb642 100755 --- a/cmd/workspace/clusters/clusters.go +++ b/cmd/workspace/clusters/clusters.go @@ -158,7 +158,14 @@ var createCmd = &cobra.Command{ informative error message.`, 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) @@ -168,23 +175,6 @@ var createCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - promptSpinner := cmdio.Spinner(ctx) - promptSpinner <- "No SPARK_VERSION argument specified. Loading names for Clusters drop-down." - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) - close(promptSpinner) - if err != nil { - return fmt.Errorf("failed to load names for Clusters drop-down. Please manually specify required arguments. Original error: %w", err) - } - id, err := cmdio.Select(ctx, names, "The Spark version of the cluster, e.g") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the spark version of the cluster, e.g") - } createReq.SparkVersion = args[0] } diff --git a/cmd/workspace/git-credentials/git-credentials.go b/cmd/workspace/git-credentials/git-credentials.go index 16162988..7ac5b020 100755 --- a/cmd/workspace/git-credentials/git-credentials.go +++ b/cmd/workspace/git-credentials/git-credentials.go @@ -49,7 +49,14 @@ var createCmd = &cobra.Command{ DELETE endpoint to delete existing credentials.`, 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) @@ -59,23 +66,6 @@ var createCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - promptSpinner := cmdio.Spinner(ctx) - promptSpinner <- "No GIT_PROVIDER argument specified. Loading names for Git Credentials drop-down." - names, err := w.GitCredentials.CredentialInfoGitProviderToCredentialIdMap(ctx) - close(promptSpinner) - if err != nil { - return fmt.Errorf("failed to load names for Git Credentials drop-down. Please manually specify required arguments. Original error: %w", err) - } - id, err := cmdio.Select(ctx, names, "Git provider") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have git provider") - } createReq.GitProvider = args[0] } diff --git a/cmd/workspace/workspace/workspace.go b/cmd/workspace/workspace/workspace.go index c942d7aa..14ce4303 100755 --- a/cmd/workspace/workspace/workspace.go +++ b/cmd/workspace/workspace/workspace.go @@ -178,7 +178,14 @@ var getStatusCmd = &cobra.Command{ call returns an error 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) @@ -188,23 +195,6 @@ var getStatusCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - promptSpinner := cmdio.Spinner(ctx) - promptSpinner <- "No PATH argument specified. Loading names for Workspace drop-down." - names, err := w.Workspace.ObjectInfoPathToObjectIdMap(ctx, workspace.ListWorkspaceRequest{}) - close(promptSpinner) - if err != nil { - return fmt.Errorf("failed to load names for Workspace drop-down. Please manually specify required arguments. Original error: %w", 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") - } getStatusReq.Path = args[0] } @@ -244,7 +234,14 @@ var importCmd = &cobra.Command{ use DBC format to import a directory.`, 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) @@ -254,23 +251,6 @@ var importCmd = &cobra.Command{ return err } } else { - if len(args) == 0 { - promptSpinner := cmdio.Spinner(ctx) - promptSpinner <- "No PATH argument specified. Loading names for Workspace drop-down." - names, err := w.Workspace.ObjectInfoPathToObjectIdMap(ctx, workspace.ListWorkspaceRequest{}) - close(promptSpinner) - if err != nil { - return fmt.Errorf("failed to load names for Workspace drop-down. Please manually specify required arguments. Original error: %w", err) - } - id, err := cmdio.Select(ctx, names, "The absolute path of the object 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 object or directory") - } importReq.Path = args[0] } diff --git a/internal/clusters_test.go b/internal/clusters_test.go index 74250972..e8208d04 100644 --- a/internal/clusters_test.go +++ b/internal/clusters_test.go @@ -33,3 +33,8 @@ func TestAccClustersGet(t *testing.T) { assert.Contains(t, outStr, fmt.Sprintf(`"cluster_id":"%s"`, clusterId)) assert.Equal(t, "", stderr.String()) } + +func TestClusterCreateErrorWhenNoArguments(t *testing.T) { + _, _, err := RequireErrorRun(t, "clusters", "create") + assert.Equal(t, "accepts 1 arg(s), received 0", err.Error()) +} diff --git a/internal/workspace_test.go b/internal/workspace_test.go index 83d7be22..f8830990 100644 --- a/internal/workspace_test.go +++ b/internal/workspace_test.go @@ -22,3 +22,8 @@ func TestWorkpaceListErrorWhenNoArguments(t *testing.T) { _, _, err := RequireErrorRun(t, "workspace", "list") assert.Equal(t, "accepts 1 arg(s), received 0", err.Error()) } + +func TestWorkpaceGetStatusErrorWhenNoArguments(t *testing.T) { + _, _, err := RequireErrorRun(t, "workspace", "get-status") + assert.Equal(t, "accepts 1 arg(s), received 0", err.Error()) +}