From 4349103c79c19f16c5ceb470df7fa3f275122764 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Wed, 24 May 2023 17:42:23 +0200 Subject: [PATCH 1/5] Changed service template to correctly handle required positional arguments --- .codegen/service.go.tmpl | 8 ++- .../custom-app-integration.go | 3 +- cmd/account/groups/groups.go | 49 +++++++++++++++-- cmd/account/log-delivery/log-delivery.go | 1 - cmd/account/networks/networks.go | 16 +++++- .../o-auth-enrollment/o-auth-enrollment.go | 1 - cmd/account/private-access/private-access.go | 6 ++- .../published-app-integration.go | 3 +- .../service-principals/service-principals.go | 49 +++++++++++++++-- .../storage-credentials.go | 6 ++- cmd/account/users/users.go | 49 +++++++++++++++-- cmd/account/vpc-endpoints/vpc-endpoints.go | 16 +++++- cmd/account/workspaces/workspaces.go | 16 +++++- cmd/workspace/catalogs/catalogs.go | 6 ++- .../cluster-policies/cluster-policies.go | 1 - cmd/workspace/clusters/clusters.go | 52 +++++++++++++++++-- cmd/workspace/dashboards/dashboards.go | 1 - cmd/workspace/experiments/experiments.go | 21 +++++--- cmd/workspace/grants/grants.go | 3 +- cmd/workspace/groups/groups.go | 49 +++++++++++++++-- .../instance-pools/instance-pools.go | 6 ++- cmd/workspace/jobs/jobs.go | 34 ++++++++++-- .../model-registry/model-registry.go | 19 ++++--- cmd/workspace/permissions/permissions.go | 6 ++- cmd/workspace/pipelines/pipelines.go | 48 +++++++++++++++-- .../policy-families/policy-families.go | 1 - cmd/workspace/providers/providers.go | 1 - cmd/workspace/queries/queries.go | 17 +++++- cmd/workspace/recipients/recipients.go | 20 +++++-- cmd/workspace/repos/repos.go | 20 +++++-- cmd/workspace/schemas/schemas.go | 19 ++++++- cmd/workspace/secrets/secrets.go | 3 +- .../service-principals/service-principals.go | 49 +++++++++++++++-- cmd/workspace/shares/shares.go | 6 ++- .../storage-credentials.go | 6 ++- .../token-management/token-management.go | 1 - cmd/workspace/tokens/tokens.go | 1 - cmd/workspace/users/users.go | 49 +++++++++++++++-- cmd/workspace/warehouses/warehouses.go | 17 +++++- .../workspace-bindings/workspace-bindings.go | 3 +- .../workspace-conf/workspace-conf.go | 1 - 41 files changed, 589 insertions(+), 94 deletions(-) diff --git a/.codegen/service.go.tmpl b/.codegen/service.go.tmpl index 49dce3068f..cd9e14b151 100644 --- a/.codegen/service.go.tmpl +++ b/.codegen/service.go.tmpl @@ -57,7 +57,13 @@ func init() { {{- end}} {{end}} } -{{ $hasPosArgs := and .Request (or .Request.IsOnlyPrimitiveFields (eq .PascalName "RunNow")) -}} +{{$isAllRequiredFieldsPrimitive := or (not .Request) (gt (len .Request.RequiredFields) 0)}} +{{ if .Request }} + {{- range .Request.RequiredFields -}} + {{- if not .Entity.IsPrimitive -}}{{ $isAllRequiredFieldsPrimitive = false }}{{- end -}} + {{- end -}} +{{ end }} +{{ $hasPosArgs := and .Request (or $isAllRequiredFieldsPrimitive (eq .PascalName "RunNow")) -}} {{- $hasSinglePosArg := and $hasPosArgs (eq 1 (len .Request.RequiredFields)) -}} {{- $serviceHasNamedIdMap := and .Service.List .Service.List.NamedIdMap -}} {{- $hasIdPrompt := and $hasSinglePosArg $serviceHasNamedIdMap -}} diff --git a/cmd/account/custom-app-integration/custom-app-integration.go b/cmd/account/custom-app-integration/custom-app-integration.go index ca2fe9136a..a1e29092aa 100755 --- a/cmd/account/custom-app-integration/custom-app-integration.go +++ b/cmd/account/custom-app-integration/custom-app-integration.go @@ -183,7 +183,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update INTEGRATION_ID", Short: `Updates Custom OAuth App Integration.`, Long: `Updates Custom OAuth App Integration. @@ -191,6 +191,7 @@ var updateCmd = &cobra.Command{ oauth app integration via :method:CustomAppIntegration/get.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/account/groups/groups.go b/cmd/account/groups/groups.go index fb3da6f7dd..0fa02626ea 100755 --- a/cmd/account/groups/groups.go +++ b/cmd/account/groups/groups.go @@ -45,7 +45,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create ID", Short: `Create a new group.`, Long: `Create a new group. @@ -57,6 +57,20 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks group ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks group id") + } err = createJson.Unmarshal(&createReq) if err != nil { return err @@ -189,7 +203,6 @@ var listCmd = &cobra.Command{ Gets all details of the groups associated with the Databricks account.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -218,7 +231,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update group details.`, Long: `Update group details. @@ -229,6 +242,20 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks account") + } err = patchJson.Unmarshal(&patchReq) if err != nil { return err @@ -264,7 +291,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace a group.`, Long: `Replace a group. @@ -275,6 +302,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks group ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks group id") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/account/log-delivery/log-delivery.go b/cmd/account/log-delivery/log-delivery.go index 2e91b218a8..c8a81ccfa5 100755 --- a/cmd/account/log-delivery/log-delivery.go +++ b/cmd/account/log-delivery/log-delivery.go @@ -211,7 +211,6 @@ var listCmd = &cobra.Command{ specified by ID.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/account/networks/networks.go b/cmd/account/networks/networks.go index 34584d6f21..60704770a3 100755 --- a/cmd/account/networks/networks.go +++ b/cmd/account/networks/networks.go @@ -38,7 +38,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create NETWORK_NAME", Short: `Create network configuration.`, Long: `Create network configuration. @@ -51,6 +51,20 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.Networks.NetworkNetworkNameToNetworkIdMap(ctx) + if err != nil { + return 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") + } err = createJson.Unmarshal(&createReq) if err != nil { return err diff --git a/cmd/account/o-auth-enrollment/o-auth-enrollment.go b/cmd/account/o-auth-enrollment/o-auth-enrollment.go index 2003fb89b9..9a19b9f766 100755 --- a/cmd/account/o-auth-enrollment/o-auth-enrollment.go +++ b/cmd/account/o-auth-enrollment/o-auth-enrollment.go @@ -47,7 +47,6 @@ var createCmd = &cobra.Command{ status via API :method:OAuthEnrollment/get.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/account/private-access/private-access.go b/cmd/account/private-access/private-access.go index 34092ac9d0..333a25a558 100755 --- a/cmd/account/private-access/private-access.go +++ b/cmd/account/private-access/private-access.go @@ -35,7 +35,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create PRIVATE_ACCESS_SETTINGS_NAME REGION PRIVATE_ACCESS_SETTINGS_ID", Short: `Create private access settings.`, Long: `Create private access settings. @@ -56,6 +56,7 @@ var createCmd = &cobra.Command{ [Databricks article about PrivateLink]: https://docs.databricks.com/administration-guide/cloud-configurations/aws/privatelink.html`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(3), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -227,7 +228,7 @@ func init() { } var replaceCmd = &cobra.Command{ - Use: "replace", + Use: "replace PRIVATE_ACCESS_SETTINGS_NAME REGION PRIVATE_ACCESS_SETTINGS_ID", Short: `Replace private access settings.`, Long: `Replace private access settings. @@ -254,6 +255,7 @@ var replaceCmd = &cobra.Command{ [Databricks article about PrivateLink]: https://docs.databricks.com/administration-guide/cloud-configurations/aws/privatelink.html`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(3), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/account/published-app-integration/published-app-integration.go b/cmd/account/published-app-integration/published-app-integration.go index 9931f3dcf8..94e39b4db2 100755 --- a/cmd/account/published-app-integration/published-app-integration.go +++ b/cmd/account/published-app-integration/published-app-integration.go @@ -175,7 +175,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update INTEGRATION_ID", Short: `Updates Published OAuth App Integration.`, Long: `Updates Published OAuth App Integration. @@ -183,6 +183,7 @@ var updateCmd = &cobra.Command{ published oauth app integration via :method:PublishedAppIntegration/get.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/account/service-principals/service-principals.go b/cmd/account/service-principals/service-principals.go index 695d497b34..28f6549507 100755 --- a/cmd/account/service-principals/service-principals.go +++ b/cmd/account/service-principals/service-principals.go @@ -45,7 +45,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create ID", Short: `Create a service principal.`, Long: `Create a service principal. @@ -56,6 +56,20 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks service principal ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks service principal id") + } err = createJson.Unmarshal(&createReq) if err != nil { return err @@ -189,7 +203,6 @@ var listCmd = &cobra.Command{ Gets the set of service principals associated with a Databricks account.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -218,7 +231,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update service principal details.`, Long: `Update service principal details. @@ -230,6 +243,20 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks account") + } err = patchJson.Unmarshal(&patchReq) if err != nil { return err @@ -266,7 +293,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace service principal.`, Long: `Replace service principal. @@ -279,6 +306,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks service principal ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks service principal id") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/account/storage-credentials/storage-credentials.go b/cmd/account/storage-credentials/storage-credentials.go index 2dc3d497a8..94cc19fcbd 100755 --- a/cmd/account/storage-credentials/storage-credentials.go +++ b/cmd/account/storage-credentials/storage-credentials.go @@ -36,7 +36,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create NAME METASTORE_ID", Short: `Create a storage credential.`, Long: `Create a storage credential. @@ -49,6 +49,7 @@ var createCmd = &cobra.Command{ **CREATE_STORAGE_CREDENTIAL** privilege on the metastore.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -196,7 +197,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update METASTORE_ID NAME", Short: `Updates a storage credential.`, Long: `Updates a storage credential. @@ -205,6 +206,7 @@ var updateCmd = &cobra.Command{ credential can be changed.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/account/users/users.go b/cmd/account/users/users.go index d4faca2044..600faf5b4d 100755 --- a/cmd/account/users/users.go +++ b/cmd/account/users/users.go @@ -52,7 +52,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create ID", Short: `Create a new user.`, Long: `Create a new user. @@ -64,6 +64,20 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks user ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks user id") + } err = createJson.Unmarshal(&createReq) if err != nil { return err @@ -197,7 +211,6 @@ var listCmd = &cobra.Command{ Gets details for all the users associated with a Databricks account.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -226,7 +239,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update user details.`, Long: `Update user details. @@ -238,6 +251,20 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks account") + } err = patchJson.Unmarshal(&patchReq) if err != nil { return err @@ -276,7 +303,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace a user.`, Long: `Replace a user. @@ -287,6 +314,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks user ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks user id") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/account/vpc-endpoints/vpc-endpoints.go b/cmd/account/vpc-endpoints/vpc-endpoints.go index d4cd591144..94b33580d3 100755 --- a/cmd/account/vpc-endpoints/vpc-endpoints.go +++ b/cmd/account/vpc-endpoints/vpc-endpoints.go @@ -35,7 +35,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create VPC_ENDPOINT_NAME", Short: `Create VPC endpoint configuration.`, Long: `Create VPC endpoint configuration. @@ -58,6 +58,20 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.VpcEndpoints.VpcEndpointVpcEndpointNameToVpcEndpointIdMap(ctx) + if err != nil { + return 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") + } err = createJson.Unmarshal(&createReq) if err != nil { return err diff --git a/cmd/account/workspaces/workspaces.go b/cmd/account/workspaces/workspaces.go index 7919eaa096..20df94a23c 100755 --- a/cmd/account/workspaces/workspaces.go +++ b/cmd/account/workspaces/workspaces.go @@ -58,7 +58,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create WORKSPACE_NAME", Short: `Create a new workspace.`, Long: `Create a new workspace. @@ -77,6 +77,20 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) + if len(args) == 0 { + names, err := a.Workspaces.WorkspaceWorkspaceNameToWorkspaceIdMap(ctx) + if err != nil { + return 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") + } err = createJson.Unmarshal(&createReq) if err != nil { return err diff --git a/cmd/workspace/catalogs/catalogs.go b/cmd/workspace/catalogs/catalogs.go index 025f318c78..da36ba771c 100755 --- a/cmd/workspace/catalogs/catalogs.go +++ b/cmd/workspace/catalogs/catalogs.go @@ -42,7 +42,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create NAME", Short: `Create a catalog.`, Long: `Create a catalog. @@ -50,6 +50,7 @@ var createCmd = &cobra.Command{ metastore admin or has the **CREATE_CATALOG** privilege.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -189,7 +190,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update NAME", Short: `Update a catalog.`, Long: `Update a catalog. @@ -198,6 +199,7 @@ var updateCmd = &cobra.Command{ of the catalog).`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/cluster-policies/cluster-policies.go b/cmd/workspace/cluster-policies/cluster-policies.go index b56da9e2f6..465206ac44 100755 --- a/cmd/workspace/cluster-policies/cluster-policies.go +++ b/cmd/workspace/cluster-policies/cluster-policies.go @@ -247,7 +247,6 @@ var listCmd = &cobra.Command{ Returns a list of policies accessible by the requesting user.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/clusters/clusters.go b/cmd/workspace/clusters/clusters.go index 3a530f8a6e..1ed0f3eeb6 100755 --- a/cmd/workspace/clusters/clusters.go +++ b/cmd/workspace/clusters/clusters.go @@ -124,7 +124,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create SPARK_VERSION", Short: `Create new cluster.`, Long: `Create new cluster. @@ -147,6 +147,20 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return 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") + } err = createJson.Unmarshal(&createReq) if err != nil { return err @@ -293,7 +307,7 @@ func init() { } var editCmd = &cobra.Command{ - Use: "edit", + Use: "edit CLUSTER_ID SPARK_VERSION", Short: `Update cluster configuration.`, Long: `Update cluster configuration. @@ -311,6 +325,7 @@ var editCmd = &cobra.Command{ Clusters created by the Databricks Jobs service cannot be edited.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -367,7 +382,7 @@ func init() { } var eventsCmd = &cobra.Command{ - Use: "events", + Use: "events CLUSTER_ID", Short: `List cluster activity events.`, Long: `List cluster activity events. @@ -380,6 +395,20 @@ var eventsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID of the cluster to retrieve events about") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the id of the cluster to retrieve events about") + } err = eventsJson.Unmarshal(&eventsReq) if err != nil { return err @@ -475,7 +504,6 @@ var listCmd = &cobra.Command{ terminated job clusters.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -665,7 +693,7 @@ func init() { } var resizeCmd = &cobra.Command{ - Use: "resize", + Use: "resize CLUSTER_ID", Short: `Resize cluster.`, Long: `Resize cluster. @@ -677,6 +705,20 @@ var resizeCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The cluster to be resized") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the cluster to be resized") + } err = resizeJson.Unmarshal(&resizeReq) if err != nil { return err diff --git a/cmd/workspace/dashboards/dashboards.go b/cmd/workspace/dashboards/dashboards.go index c64d3935bf..c72369af7f 100755 --- a/cmd/workspace/dashboards/dashboards.go +++ b/cmd/workspace/dashboards/dashboards.go @@ -180,7 +180,6 @@ var listCmd = &cobra.Command{ Fetch a paginated list of dashboard objects.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/experiments/experiments.go b/cmd/workspace/experiments/experiments.go index 8d4e22d730..c9d79b7a65 100755 --- a/cmd/workspace/experiments/experiments.go +++ b/cmd/workspace/experiments/experiments.go @@ -13,7 +13,17 @@ import ( ) var Cmd = &cobra.Command{ - Use: "experiments", + Use: "experiments", + Short: `Experiments are the primary unit of organization in MLflow; all MLflow runs belong to an experiment.`, + Long: `Experiments are the primary unit of organization in MLflow; all MLflow runs + belong to an experiment. Each experiment lets you visualize, search, and + compare runs, as well as download run artifacts or metadata for analysis in + other tools. Experiments are maintained in a Databricks hosted MLflow tracking + server. + + Experiments are located in the workspace file tree. You manage experiments + using the same tools you use to manage other workspace objects such as + folders, notebooks, and libraries.`, } // start create-experiment command @@ -32,7 +42,7 @@ func init() { } var createExperimentCmd = &cobra.Command{ - Use: "create-experiment", + Use: "create-experiment NAME", Short: `Create experiment.`, Long: `Create experiment. @@ -44,6 +54,7 @@ var createExperimentCmd = &cobra.Command{ Throws RESOURCE_ALREADY_EXISTS if a experiment with the given name exists.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -386,7 +397,6 @@ var listArtifactsCmd = &cobra.Command{ specified, the response contains only artifacts with the specified prefix.",`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -422,7 +432,6 @@ var listExperimentsCmd = &cobra.Command{ Gets a list of all experiments.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -485,7 +494,7 @@ var logBatchCmd = &cobra.Command{ Request Limits ------------------------------- A single JSON-serialized API request may be up to 1 MB in size and contain: - * No more than 1000 metrics, params, and tags in total * Up to 1000 metrics - + * No more than 1000 metrics, params, and tags in total * Up to 1000 metrics * Up to 100 params * Up to 100 tags For example, a valid request might contain 900 metrics, 50 params, and 50 @@ -624,7 +633,6 @@ var logModelCmd = &cobra.Command{ without warning.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -969,7 +977,6 @@ var updateRunCmd = &cobra.Command{ Updates run metadata.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/grants/grants.go b/cmd/workspace/grants/grants.go index 4453e9d0ae..8ce474a5d5 100755 --- a/cmd/workspace/grants/grants.go +++ b/cmd/workspace/grants/grants.go @@ -121,13 +121,14 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update SECURABLE_TYPE FULL_NAME", Short: `Update permissions.`, Long: `Update permissions. Updates the permissions for a securable.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/groups/groups.go b/cmd/workspace/groups/groups.go index d05e086308..c9444d5423 100755 --- a/cmd/workspace/groups/groups.go +++ b/cmd/workspace/groups/groups.go @@ -45,7 +45,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create ID", Short: `Create a new group.`, Long: `Create a new group. @@ -57,6 +57,20 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks group ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks group id") + } err = createJson.Unmarshal(&createReq) if err != nil { return err @@ -189,7 +203,6 @@ var listCmd = &cobra.Command{ Gets all details of the groups associated with the Databricks workspace.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -218,7 +231,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update group details.`, Long: `Update group details. @@ -229,6 +242,20 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks account") + } err = patchJson.Unmarshal(&patchReq) if err != nil { return err @@ -264,7 +291,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace a group.`, Long: `Replace a group. @@ -275,6 +302,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks group ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks group id") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/workspace/instance-pools/instance-pools.go b/cmd/workspace/instance-pools/instance-pools.go index e98962e53b..d083cd6f18 100755 --- a/cmd/workspace/instance-pools/instance-pools.go +++ b/cmd/workspace/instance-pools/instance-pools.go @@ -60,13 +60,14 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create INSTANCE_POOL_NAME NODE_TYPE_ID", Short: `Create a new instance pool.`, Long: `Create a new instance pool. Creates a new instance pool using idle and ready-to-use cloud instances.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -158,13 +159,14 @@ func init() { } var editCmd = &cobra.Command{ - Use: "edit", + Use: "edit INSTANCE_POOL_ID INSTANCE_POOL_NAME NODE_TYPE_ID", Short: `Edit an existing instance pool.`, Long: `Edit an existing instance pool. Modifies the configuration of an existing instance pool.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(3), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/jobs/jobs.go b/cmd/workspace/jobs/jobs.go index cbad973170..95864eba8c 100755 --- a/cmd/workspace/jobs/jobs.go +++ b/cmd/workspace/jobs/jobs.go @@ -556,7 +556,6 @@ var listCmd = &cobra.Command{ Retrieves a list of jobs.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -598,7 +597,6 @@ var listRunsCmd = &cobra.Command{ List runs in descending order by start time.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -642,7 +640,7 @@ func init() { } var repairRunCmd = &cobra.Command{ - Use: "repair-run", + Use: "repair-run RUN_ID", Short: `Repair a job run.`, Long: `Repair a job run. @@ -655,6 +653,20 @@ var repairRunCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The job run ID of the run to repair") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the job run id of the run to repair") + } err = repairRunJson.Unmarshal(&repairRunReq) if err != nil { return err @@ -930,7 +942,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update JOB_ID", Short: `Partially update a job.`, Long: `Partially update a job. @@ -942,6 +954,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The canonical identifier of the job to update") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the canonical identifier of the job to update") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/workspace/model-registry/model-registry.go b/cmd/workspace/model-registry/model-registry.go index ea7b9efad1..4e69226e38 100755 --- a/cmd/workspace/model-registry/model-registry.go +++ b/cmd/workspace/model-registry/model-registry.go @@ -13,7 +13,10 @@ import ( ) var Cmd = &cobra.Command{ - Use: "model-registry", + Use: "model-registry", + Short: `MLflow Model Registry is a centralized model repository and a UI and set of APIs that enable you to manage the full lifecycle of MLflow Models.`, + Long: `MLflow Model Registry is a centralized model repository and a UI and set of + APIs that enable you to manage the full lifecycle of MLflow Models.`, } // start approve-transition-request command @@ -113,7 +116,7 @@ func init() { } var createModelCmd = &cobra.Command{ - Use: "create-model", + Use: "create-model NAME", Short: `Create a model.`, Long: `Create a model. @@ -123,6 +126,7 @@ var createModelCmd = &cobra.Command{ exists.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -159,13 +163,14 @@ func init() { } var createModelVersionCmd = &cobra.Command{ - Use: "create-model-version", + Use: "create-model-version NAME SOURCE", Short: `Create a model version.`, Long: `Create a model version. Creates a model version.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -506,7 +511,6 @@ var deleteWebhookCmd = &cobra.Command{ Deletes a registry webhook.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -535,13 +539,14 @@ func init() { } var getLatestVersionsCmd = &cobra.Command{ - Use: "get-latest-versions", + Use: "get-latest-versions NAME", Short: `Get the latest version.`, Long: `Get the latest version. Gets the latest version of a registered model.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -687,7 +692,6 @@ var listModelsCmd = &cobra.Command{ __max_results__.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -1217,7 +1221,7 @@ func init() { } var updateWebhookCmd = &cobra.Command{ - Use: "update-webhook", + Use: "update-webhook ID", Short: `Update a webhook.`, Long: `Update a webhook. @@ -1226,6 +1230,7 @@ var updateWebhookCmd = &cobra.Command{ Updates a registry webhook.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/permissions/permissions.go b/cmd/workspace/permissions/permissions.go index 82d03ca701..b34832a83b 100755 --- a/cmd/workspace/permissions/permissions.go +++ b/cmd/workspace/permissions/permissions.go @@ -101,7 +101,7 @@ func init() { } var setCmd = &cobra.Command{ - Use: "set", + Use: "set REQUEST_OBJECT_TYPE REQUEST_OBJECT_ID", Short: `Set permissions.`, Long: `Set permissions. @@ -109,6 +109,7 @@ var setCmd = &cobra.Command{ objects and root objects.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -143,13 +144,14 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update REQUEST_OBJECT_TYPE REQUEST_OBJECT_ID", Short: `Update permission.`, Long: `Update permission. Updates the permissions on an object.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/pipelines/pipelines.go b/cmd/workspace/pipelines/pipelines.go index 94d24eb385..3ecb556e42 100755 --- a/cmd/workspace/pipelines/pipelines.go +++ b/cmd/workspace/pipelines/pipelines.go @@ -238,7 +238,7 @@ func init() { } var listPipelineEventsCmd = &cobra.Command{ - Use: "list-pipeline-events", + Use: "list-pipeline-events PIPELINE_ID", Short: `List pipeline events.`, Long: `List pipeline events. @@ -249,6 +249,20 @@ var listPipelineEventsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } err = listPipelineEventsJson.Unmarshal(&listPipelineEventsReq) if err != nil { return err @@ -442,7 +456,7 @@ func init() { } var startUpdateCmd = &cobra.Command{ - Use: "start-update", + Use: "start-update PIPELINE_ID", Short: `Queue a pipeline update.`, Long: `Queue a pipeline update. @@ -453,6 +467,20 @@ var startUpdateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } err = startUpdateJson.Unmarshal(&startUpdateReq) if err != nil { return err @@ -569,7 +597,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update PIPELINE_ID", Short: `Edit a pipeline.`, Long: `Edit a pipeline. @@ -580,6 +608,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique identifier for this pipeline") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have unique identifier for this pipeline") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/workspace/policy-families/policy-families.go b/cmd/workspace/policy-families/policy-families.go index 5c65af5ff3..9dbe471d92 100755 --- a/cmd/workspace/policy-families/policy-families.go +++ b/cmd/workspace/policy-families/policy-families.go @@ -69,7 +69,6 @@ var listCmd = &cobra.Command{ Use: "list", Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/providers/providers.go b/cmd/workspace/providers/providers.go index 50d13e75c2..a5c17356b5 100755 --- a/cmd/workspace/providers/providers.go +++ b/cmd/workspace/providers/providers.go @@ -176,7 +176,6 @@ var listCmd = &cobra.Command{ ordering of the elements in the array.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/queries/queries.go b/cmd/workspace/queries/queries.go index 5f4c948ff4..0cd240c802 100755 --- a/cmd/workspace/queries/queries.go +++ b/cmd/workspace/queries/queries.go @@ -192,7 +192,6 @@ var listCmd = &cobra.Command{ term.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -272,7 +271,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update QUERY_ID", Short: `Change a query definition.`, Long: `Change a query definition. @@ -285,6 +284,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Queries.QueryNameToIdMap(ctx, sql.ListQueriesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/workspace/recipients/recipients.go b/cmd/workspace/recipients/recipients.go index d90f24a9a5..a9bd39f763 100755 --- a/cmd/workspace/recipients/recipients.go +++ b/cmd/workspace/recipients/recipients.go @@ -38,7 +38,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create NAME AUTHENTICATION_TYPE", Short: `Create a share recipient.`, Long: `Create a share recipient. @@ -47,6 +47,7 @@ var createCmd = &cobra.Command{ **CREATE_RECIPIENT** privilege on the metastore.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -187,7 +188,6 @@ var listCmd = &cobra.Command{ guarantee of a specific ordering of the elements in the array.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -305,7 +305,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update NAME", Short: `Update a share recipient.`, Long: `Update a share recipient. @@ -318,6 +318,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Recipients.RecipientInfoNameToMetastoreIdMap(ctx, sharing.ListRecipientsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Name of Recipient") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have name of recipient") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/workspace/repos/repos.go b/cmd/workspace/repos/repos.go index a46f0b81d1..dac08e5234 100755 --- a/cmd/workspace/repos/repos.go +++ b/cmd/workspace/repos/repos.go @@ -43,7 +43,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create URL PROVIDER", Short: `Create a repo.`, Long: `Create a repo. @@ -52,6 +52,7 @@ var createCmd = &cobra.Command{ unlike repos created in the browser.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -191,7 +192,6 @@ var listCmd = &cobra.Command{ paginated with each page containing twenty repos.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -222,7 +222,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update REPO_ID", Short: `Update a repo.`, Long: `Update a repo. @@ -234,6 +234,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Repos.RepoInfoPathToIdMap(ctx, workspace.ListReposRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding repo to access") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding repo to access") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/workspace/schemas/schemas.go b/cmd/workspace/schemas/schemas.go index ccaa494e2c..41e8fb32ba 100755 --- a/cmd/workspace/schemas/schemas.go +++ b/cmd/workspace/schemas/schemas.go @@ -39,7 +39,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create NAME CATALOG_NAME", Short: `Create a schema.`, Long: `Create a schema. @@ -48,6 +48,7 @@ var createCmd = &cobra.Command{ catalog.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -230,7 +231,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update FULL_NAME", Short: `Update a schema.`, Long: `Update a schema. @@ -245,6 +246,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Schemas.SchemaInfoNameToFullNameMap(ctx, catalog.ListSchemasRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Full name of the schema") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have full name of the schema") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/workspace/secrets/secrets.go b/cmd/workspace/secrets/secrets.go index 32fbe9d312..995746c814 100755 --- a/cmd/workspace/secrets/secrets.go +++ b/cmd/workspace/secrets/secrets.go @@ -46,7 +46,7 @@ func init() { } var createScopeCmd = &cobra.Command{ - Use: "create-scope", + Use: "create-scope SCOPE", Short: `Create a new secret scope.`, Long: `Create a new secret scope. @@ -55,6 +55,7 @@ var createScopeCmd = &cobra.Command{ in a workspace is 100.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/service-principals/service-principals.go b/cmd/workspace/service-principals/service-principals.go index 7fb0640eb5..06c19173bc 100755 --- a/cmd/workspace/service-principals/service-principals.go +++ b/cmd/workspace/service-principals/service-principals.go @@ -45,7 +45,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create ID", Short: `Create a service principal.`, Long: `Create a service principal. @@ -56,6 +56,20 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks service principal ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks service principal id") + } err = createJson.Unmarshal(&createReq) if err != nil { return err @@ -189,7 +203,6 @@ var listCmd = &cobra.Command{ Gets the set of service principals associated with a Databricks workspace.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -218,7 +231,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update service principal details.`, Long: `Update service principal details. @@ -230,6 +243,20 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks account") + } err = patchJson.Unmarshal(&patchReq) if err != nil { return err @@ -266,7 +293,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace service principal.`, Long: `Replace service principal. @@ -279,6 +306,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks service principal ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks service principal id") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/workspace/shares/shares.go b/cmd/workspace/shares/shares.go index b937160117..ff52dde3f1 100755 --- a/cmd/workspace/shares/shares.go +++ b/cmd/workspace/shares/shares.go @@ -204,7 +204,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update NAME", Short: `Update a share.`, Long: `Update a share. @@ -224,6 +224,7 @@ var updateCmd = &cobra.Command{ Table removals through **update** do not require additional privileges.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -257,7 +258,7 @@ func init() { } var updatePermissionsCmd = &cobra.Command{ - Use: "update-permissions", + Use: "update-permissions NAME", Short: `Update permissions.`, Long: `Update permissions. @@ -268,6 +269,7 @@ var updatePermissionsCmd = &cobra.Command{ recipient revocations do not require additional privileges.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/storage-credentials/storage-credentials.go b/cmd/workspace/storage-credentials/storage-credentials.go index 512ede412c..a6c59d1681 100755 --- a/cmd/workspace/storage-credentials/storage-credentials.go +++ b/cmd/workspace/storage-credentials/storage-credentials.go @@ -50,7 +50,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create NAME METASTORE_ID", Short: `Create a storage credential.`, Long: `Create a storage credential. @@ -63,6 +63,7 @@ var createCmd = &cobra.Command{ **CREATE_STORAGE_CREDENTIAL** privilege on the metastore.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -233,7 +234,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update METASTORE_ID NAME", Short: `Update a credential.`, Long: `Update a credential. @@ -242,6 +243,7 @@ var updateCmd = &cobra.Command{ admin, only the __owner__ credential can be changed.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/token-management/token-management.go b/cmd/workspace/token-management/token-management.go index e9a8db47e0..55b9957e2e 100755 --- a/cmd/workspace/token-management/token-management.go +++ b/cmd/workspace/token-management/token-management.go @@ -171,7 +171,6 @@ var listCmd = &cobra.Command{ Lists all tokens associated with the specified workspace or user.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/tokens/tokens.go b/cmd/workspace/tokens/tokens.go index 94435d8e82..f83db0dafb 100755 --- a/cmd/workspace/tokens/tokens.go +++ b/cmd/workspace/tokens/tokens.go @@ -42,7 +42,6 @@ var createCmd = &cobra.Command{ an error **QUOTA_EXCEEDED**.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/users/users.go b/cmd/workspace/users/users.go index 1c509ef2c1..cb8a7fdb5b 100755 --- a/cmd/workspace/users/users.go +++ b/cmd/workspace/users/users.go @@ -52,7 +52,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create", + Use: "create ID", Short: `Create a new user.`, Long: `Create a new user. @@ -64,6 +64,20 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks user ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks user id") + } err = createJson.Unmarshal(&createReq) if err != nil { return err @@ -197,7 +211,6 @@ var listCmd = &cobra.Command{ Gets details for all the users associated with a Databricks workspace.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -226,7 +239,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update user details.`, Long: `Update user details. @@ -238,6 +251,20 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks account") + } err = patchJson.Unmarshal(&patchReq) if err != nil { return err @@ -276,7 +303,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace a user.`, Long: `Replace a user. @@ -287,6 +314,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks user ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have databricks user id") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err diff --git a/cmd/workspace/warehouses/warehouses.go b/cmd/workspace/warehouses/warehouses.go index b66445d75f..b4bac1bcfa 100755 --- a/cmd/workspace/warehouses/warehouses.go +++ b/cmd/workspace/warehouses/warehouses.go @@ -210,7 +210,7 @@ func init() { } var editCmd = &cobra.Command{ - Use: "edit", + Use: "edit ID", Short: `Update a warehouse.`, Long: `Update a warehouse. @@ -221,6 +221,20 @@ var editCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Required") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have required") + } err = editJson.Unmarshal(&editReq) if err != nil { return err @@ -359,7 +373,6 @@ var listCmd = &cobra.Command{ Lists all SQL warehouses that a user has manager permissions on.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/workspace-bindings/workspace-bindings.go b/cmd/workspace/workspace-bindings/workspace-bindings.go index 2874269d8b..d01aea92d8 100755 --- a/cmd/workspace/workspace-bindings/workspace-bindings.go +++ b/cmd/workspace/workspace-bindings/workspace-bindings.go @@ -71,7 +71,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update NAME", Short: `Update catalog workspace bindings.`, Long: `Update catalog workspace bindings. @@ -79,6 +79,7 @@ var updateCmd = &cobra.Command{ admin or an owner of the catalog.`, Annotations: map[string]string{}, + Args: cobra.ExactArgs(1), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() diff --git a/cmd/workspace/workspace-conf/workspace-conf.go b/cmd/workspace/workspace-conf/workspace-conf.go index d37f58f356..4d6d649e86 100755 --- a/cmd/workspace/workspace-conf/workspace-conf.go +++ b/cmd/workspace/workspace-conf/workspace-conf.go @@ -67,7 +67,6 @@ var setStatusCmd = &cobra.Command{ it.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() From ce86dcc6db379e7e9e9600caab1474f456d39b37 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Thu, 25 May 2023 12:36:11 +0200 Subject: [PATCH 2/5] Regenerated with fixed generator --- cmd/account/budgets/budgets.go | 1 - cmd/account/groups/groups.go | 17 +-------- cmd/account/metastores/metastores.go | 5 +-- cmd/account/private-access/private-access.go | 5 +-- .../service-principals/service-principals.go | 21 ++-------- cmd/account/users/users.go | 21 ++-------- cmd/workspace/groups/groups.go | 21 ++-------- cmd/workspace/metastores/metastores.go | 38 +++++++++++++++---- .../service-principals/service-principals.go | 21 ++-------- .../storage-credentials.go | 38 +++++++++++++++---- cmd/workspace/users/users.go | 21 ++-------- 11 files changed, 82 insertions(+), 127 deletions(-) diff --git a/cmd/account/budgets/budgets.go b/cmd/account/budgets/budgets.go index 05ef4ac47f..23de43923d 100755 --- a/cmd/account/budgets/budgets.go +++ b/cmd/account/budgets/budgets.go @@ -51,7 +51,6 @@ var createCmd = &cobra.Command{ if err != nil { return fmt.Errorf("invalid BUDGET: %s", args[0]) } - createReq.BudgetId = args[1] response, err := a.Budgets.Create(ctx, createReq) if err != nil { diff --git a/cmd/account/groups/groups.go b/cmd/account/groups/groups.go index 0fa02626ea..5c9f8779db 100755 --- a/cmd/account/groups/groups.go +++ b/cmd/account/groups/groups.go @@ -45,7 +45,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create ID", + Use: "create", Short: `Create a new group.`, Long: `Create a new group. @@ -57,25 +57,10 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) - if err != nil { - return err - } - id, err := cmdio.Select(ctx, names, "Databricks group ID") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks group id") - } err = createJson.Unmarshal(&createReq) if err != nil { return err } - createReq.Id = args[0] response, err := a.Groups.Create(ctx, createReq) if err != nil { diff --git a/cmd/account/metastores/metastores.go b/cmd/account/metastores/metastores.go index 89fb5ea391..6ae9b5d4c9 100755 --- a/cmd/account/metastores/metastores.go +++ b/cmd/account/metastores/metastores.go @@ -164,20 +164,19 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update METASTORE_ID ID", + Use: "update METASTORE_ID", Short: `Update a metastore.`, Long: `Update a metastore. Updates an existing Unity Catalog metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), + Args: cobra.ExactArgs(1), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) updateReq.MetastoreId = args[0] - updateReq.Id = args[1] response, err := a.Metastores.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/private-access/private-access.go b/cmd/account/private-access/private-access.go index 333a25a558..765836db7f 100755 --- a/cmd/account/private-access/private-access.go +++ b/cmd/account/private-access/private-access.go @@ -35,7 +35,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create PRIVATE_ACCESS_SETTINGS_NAME REGION PRIVATE_ACCESS_SETTINGS_ID", + Use: "create PRIVATE_ACCESS_SETTINGS_NAME REGION", Short: `Create private access settings.`, Long: `Create private access settings. @@ -56,7 +56,7 @@ var createCmd = &cobra.Command{ [Databricks article about PrivateLink]: https://docs.databricks.com/administration-guide/cloud-configurations/aws/privatelink.html`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), + Args: cobra.ExactArgs(2), PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() @@ -67,7 +67,6 @@ var createCmd = &cobra.Command{ } createReq.PrivateAccessSettingsName = args[0] createReq.Region = args[1] - createReq.PrivateAccessSettingsId = args[2] response, err := a.PrivateAccess.Create(ctx, createReq) if err != nil { diff --git a/cmd/account/service-principals/service-principals.go b/cmd/account/service-principals/service-principals.go index 28f6549507..6c8e4f7c69 100755 --- a/cmd/account/service-principals/service-principals.go +++ b/cmd/account/service-principals/service-principals.go @@ -45,7 +45,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create ID", + Use: "create", Short: `Create a service principal.`, Long: `Create a service principal. @@ -56,25 +56,10 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) - if err != nil { - return err - } - id, err := cmdio.Select(ctx, names, "Databricks service principal ID") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks service principal id") - } err = createJson.Unmarshal(&createReq) if err != nil { return err } - createReq.Id = args[0] response, err := a.ServicePrincipals.Create(ctx, createReq) if err != nil { @@ -248,14 +233,14 @@ var patchCmd = &cobra.Command{ if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks account") if err != nil { return err } args = append(args, id) } if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks account") + return fmt.Errorf("expected to have unique id for a service principal in the databricks account") } err = patchJson.Unmarshal(&patchReq) if err != nil { diff --git a/cmd/account/users/users.go b/cmd/account/users/users.go index 600faf5b4d..586d29a46d 100755 --- a/cmd/account/users/users.go +++ b/cmd/account/users/users.go @@ -52,7 +52,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create ID", + Use: "create", Short: `Create a new user.`, Long: `Create a new user. @@ -64,25 +64,10 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) - if err != nil { - return err - } - id, err := cmdio.Select(ctx, names, "Databricks user ID") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks user id") - } err = createJson.Unmarshal(&createReq) if err != nil { return err } - createReq.Id = args[0] response, err := a.Users.Create(ctx, createReq) if err != nil { @@ -256,14 +241,14 @@ var patchCmd = &cobra.Command{ if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks account") if err != nil { return err } args = append(args, id) } if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks account") + return fmt.Errorf("expected to have unique id for a user in the databricks account") } err = patchJson.Unmarshal(&patchReq) if err != nil { diff --git a/cmd/workspace/groups/groups.go b/cmd/workspace/groups/groups.go index c9444d5423..d51cca37b7 100755 --- a/cmd/workspace/groups/groups.go +++ b/cmd/workspace/groups/groups.go @@ -45,7 +45,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create ID", + Use: "create", Short: `Create a new group.`, Long: `Create a new group. @@ -57,25 +57,10 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) - if err != nil { - return err - } - id, err := cmdio.Select(ctx, names, "Databricks group ID") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks group id") - } err = createJson.Unmarshal(&createReq) if err != nil { return err } - createReq.Id = args[0] response, err := w.Groups.Create(ctx, createReq) if err != nil { @@ -247,14 +232,14 @@ var patchCmd = &cobra.Command{ if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks workspace") if err != nil { return err } args = append(args, id) } if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks account") + return fmt.Errorf("expected to have unique id for a group in the databricks workspace") } err = patchJson.Unmarshal(&patchReq) if err != nil { diff --git a/cmd/workspace/metastores/metastores.go b/cmd/workspace/metastores/metastores.go index 0d3003790c..7ed5e668b6 100755 --- a/cmd/workspace/metastores/metastores.go +++ b/cmd/workspace/metastores/metastores.go @@ -377,7 +377,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update METASTORE_ID ID", + Use: "update ID", Short: `Update a metastore.`, Long: `Update a metastore. @@ -385,13 +385,25 @@ var updateCmd = &cobra.Command{ admin.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - updateReq.MetastoreId = args[0] - updateReq.Id = args[1] + if len(args) == 0 { + names, err := w.Metastores.MetastoreInfoNameToMetastoreIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID of the metastore") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have unique id of the metastore") + } + updateReq.Id = args[0] response, err := w.Metastores.Update(ctx, updateReq) if err != nil { @@ -415,7 +427,7 @@ func init() { } var updateAssignmentCmd = &cobra.Command{ - Use: "update-assignment WORKSPACE_ID METASTORE_ID", + Use: "update-assignment WORKSPACE_ID", Short: `Update an assignment.`, Long: `Update an assignment. @@ -425,16 +437,28 @@ var updateAssignmentCmd = &cobra.Command{ to update __metastore_id__; otherwise, the caller can be a Workspace admin.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.Metastores.MetastoreInfoNameToMetastoreIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "A workspace ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have a workspace id") + } _, err = fmt.Sscan(args[0], &updateAssignmentReq.WorkspaceId) if err != nil { return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) } - updateAssignmentReq.MetastoreId = args[1] err = w.Metastores.UpdateAssignment(ctx, updateAssignmentReq) if err != nil { diff --git a/cmd/workspace/service-principals/service-principals.go b/cmd/workspace/service-principals/service-principals.go index 06c19173bc..6c57e660e6 100755 --- a/cmd/workspace/service-principals/service-principals.go +++ b/cmd/workspace/service-principals/service-principals.go @@ -45,7 +45,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create ID", + Use: "create", Short: `Create a service principal.`, Long: `Create a service principal. @@ -56,25 +56,10 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) - if err != nil { - return err - } - id, err := cmdio.Select(ctx, names, "Databricks service principal ID") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks service principal id") - } err = createJson.Unmarshal(&createReq) if err != nil { return err } - createReq.Id = args[0] response, err := w.ServicePrincipals.Create(ctx, createReq) if err != nil { @@ -248,14 +233,14 @@ var patchCmd = &cobra.Command{ if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks workspace") if err != nil { return err } args = append(args, id) } if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks account") + return fmt.Errorf("expected to have unique id for a service principal in the databricks workspace") } err = patchJson.Unmarshal(&patchReq) if err != nil { diff --git a/cmd/workspace/storage-credentials/storage-credentials.go b/cmd/workspace/storage-credentials/storage-credentials.go index a6c59d1681..06c68edf1b 100755 --- a/cmd/workspace/storage-credentials/storage-credentials.go +++ b/cmd/workspace/storage-credentials/storage-credentials.go @@ -50,7 +50,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create NAME METASTORE_ID", + Use: "create NAME", Short: `Create a storage credential.`, Long: `Create a storage credential. @@ -63,17 +63,29 @@ var createCmd = &cobra.Command{ **CREATE_STORAGE_CREDENTIAL** privilege on the metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.StorageCredentials.StorageCredentialInfoNameToIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The credential name") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the credential name") + } err = createJson.Unmarshal(&createReq) if err != nil { return err } createReq.Name = args[0] - createReq.MetastoreId = args[1] response, err := w.StorageCredentials.Create(ctx, createReq) if err != nil { @@ -234,7 +246,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update METASTORE_ID NAME", + Use: "update NAME", Short: `Update a credential.`, Long: `Update a credential. @@ -243,17 +255,29 @@ var updateCmd = &cobra.Command{ admin, only the __owner__ credential can be changed.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) + if len(args) == 0 { + names, err := w.StorageCredentials.StorageCredentialInfoNameToIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The credential name") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the credential name") + } err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - updateReq.MetastoreId = args[0] - updateReq.Name = args[1] + updateReq.Name = args[0] response, err := w.StorageCredentials.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/users/users.go b/cmd/workspace/users/users.go index cb8a7fdb5b..296fdc070a 100755 --- a/cmd/workspace/users/users.go +++ b/cmd/workspace/users/users.go @@ -52,7 +52,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create ID", + Use: "create", Short: `Create a new user.`, Long: `Create a new user. @@ -64,25 +64,10 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) - if err != nil { - return err - } - id, err := cmdio.Select(ctx, names, "Databricks user ID") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks user id") - } err = createJson.Unmarshal(&createReq) if err != nil { return err } - createReq.Id = args[0] response, err := w.Users.Create(ctx, createReq) if err != nil { @@ -256,14 +241,14 @@ var patchCmd = &cobra.Command{ if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks workspace") if err != nil { return err } args = append(args, id) } if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks account") + return fmt.Errorf("expected to have unique id for a user in the databricks workspace") } err = patchJson.Unmarshal(&patchReq) if err != nil { From 4f69d6c007689e4714ab43060fbb70552fe4f937 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Thu, 25 May 2023 15:48:39 +0200 Subject: [PATCH 3/5] Make args optional if there is a prompt or accepts JSON --- .codegen/service.go.tmpl | 49 ++++--- cmd/account/budgets/budgets.go | 4 +- cmd/account/credentials/credentials.go | 4 +- cmd/account/groups/groups.go | 72 +++++----- .../ip-access-lists/ip-access-lists.go | 4 +- cmd/account/log-delivery/log-delivery.go | 2 +- cmd/account/networks/networks.go | 38 ++--- cmd/account/private-access/private-access.go | 4 +- .../service-principals/service-principals.go | 72 +++++----- cmd/account/storage/storage.go | 4 +- cmd/account/users/users.go | 72 +++++----- cmd/account/vpc-endpoints/vpc-endpoints.go | 38 ++--- cmd/account/workspaces/workspaces.go | 40 +++--- cmd/workspace/alerts/alerts.go | 4 +- .../cluster-policies/cluster-policies.go | 6 +- cmd/workspace/clusters/clusters.go | 116 +++++++++------- cmd/workspace/dashboards/dashboards.go | 6 +- .../git-credentials/git-credentials.go | 8 +- .../global-init-scripts.go | 4 +- cmd/workspace/groups/groups.go | 72 +++++----- .../instance-pools/instance-pools.go | 4 +- .../ip-access-lists/ip-access-lists.go | 4 +- cmd/workspace/jobs/jobs.go | 130 ++++++++++-------- cmd/workspace/metastores/metastores.go | 8 +- cmd/workspace/pipelines/pipelines.go | 112 ++++++++------- cmd/workspace/providers/providers.go | 8 +- cmd/workspace/queries/queries.go | 40 +++--- cmd/workspace/recipients/recipients.go | 40 +++--- cmd/workspace/repos/repos.go | 42 +++--- cmd/workspace/schemas/schemas.go | 40 +++--- .../service-principals/service-principals.go | 72 +++++----- .../storage-credentials.go | 72 +++++----- cmd/workspace/tables/tables.go | 6 +- .../token-management/token-management.go | 4 +- cmd/workspace/tokens/tokens.go | 2 +- cmd/workspace/users/users.go | 72 +++++----- cmd/workspace/volumes/volumes.go | 6 +- cmd/workspace/warehouses/warehouses.go | 42 +++--- cmd/workspace/workspace/workspace.go | 12 +- 39 files changed, 737 insertions(+), 598 deletions(-) diff --git a/.codegen/service.go.tmpl b/.codegen/service.go.tmpl index cd9e14b151..b1fa257c18 100644 --- a/.codegen/service.go.tmpl +++ b/.codegen/service.go.tmpl @@ -57,6 +57,7 @@ func init() { {{- end}} {{end}} } + {{$isAllRequiredFieldsPrimitive := or (not .Request) (gt (len .Request.RequiredFields) 0)}} {{ if .Request }} {{- range .Request.RequiredFields -}} @@ -68,37 +69,48 @@ func init() { {{- $serviceHasNamedIdMap := and .Service.List .Service.List.NamedIdMap -}} {{- $hasIdPrompt := and $hasSinglePosArg $serviceHasNamedIdMap -}} {{- $wait := and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) -}} +{{- $canAcceptJson := and .Request (not .Request.IsOnlyPrimitiveFields) -}} +{{- $canAcceptJsonAndIdPrompts := and $hasIdPrompt $canAcceptJson -}} +{{- $hasRequiredArgs := and (not $hasIdPrompt) $hasPosArgs -}} var {{.CamelName}}Cmd = &cobra.Command{ - Use: "{{.KebabName}}{{if $hasPosArgs}}{{range .Request.RequiredFields}} {{.ConstantName}}{{end}}{{end}}", + Use: "{{.KebabName}}{{if $hasPosArgs}}{{range .Request.RequiredFields}} {{if $hasIdPrompt}}[{{end}}{{.ConstantName}}{{if $hasIdPrompt}}]{{end}}{{end}}{{end}}", {{if .Description -}} Short: `{{.Summary | without "`"}}`, Long: `{{.Comment " " 80 | without "`"}}`, {{end}} - Annotations: map[string]string{},{{if and (not $hasIdPrompt) $hasPosArgs }} + Annotations: map[string]string{},{{if $hasRequiredArgs }} Args: cobra.ExactArgs({{len .Request.RequiredFields}}),{{end}} PreRunE: root.Must{{if .Service.IsAccounts}}Account{{else}}Workspace{{end}}Client, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() {{if .Service.IsAccounts}}a := root.AccountClient(ctx){{else}}w := root.WorkspaceClient(ctx){{end}} {{- if .Request -}} + {{if $canAcceptJsonAndIdPrompts}} + if cmd.Flags().Changed("json") { + {{- end -}} + {{if $canAcceptJson }} + err = {{.CamelName}}Json.Unmarshal(&{{.CamelName}}Req) + if err != nil { + return err + } + {{- end -}} + {{if $canAcceptJsonAndIdPrompts}} + } else { + {{- end -}} {{if $hasIdPrompt}} if len(args) == 0 { - names, err := {{if .Service.IsAccounts}}a{{else}}w{{end}}.{{(.Service.TrimPrefix "account").PascalName}}.{{.Service.List.NamedIdMap.PascalName}}(ctx{{if .Service.List.Request}}, {{.Service.Package.Name}}.{{.Service.List.Request.PascalName}}{}{{end}}) - if err != nil { - return err + names, err := {{if .Service.IsAccounts}}a{{else}}w{{end}}.{{(.Service.TrimPrefix "account").PascalName}}.{{.Service.List.NamedIdMap.PascalName}}(ctx{{if .Service.List.Request}}, {{.Service.Package.Name}}.{{.Service.List.Request.PascalName}}{}{{end}}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "{{range .Request.RequiredFields}}{{.Summary | trimSuffix "."}}{{end}}") + if err != nil { + return err + } + args = append(args, id) } - id, err := cmdio.Select(ctx, names, "{{range .Request.RequiredFields}}{{.Summary | trimSuffix "."}}{{end}}") - if err != nil { - return err - } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have {{range .Request.RequiredFields}}{{.Summary | trimSuffix "." | lower}}{{end}}") - }{{end}}{{if not .Request.IsOnlyPrimitiveFields}} - err = {{.CamelName}}Json.Unmarshal(&{{.CamelName}}Req) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have {{range .Request.RequiredFields}}{{.Summary | trimSuffix "." | lower}}{{end}}") } {{- end -}} {{$method := .}} @@ -110,6 +122,9 @@ var {{.CamelName}}Cmd = &cobra.Command{ }{{else -}} {{$method.CamelName}}Req.{{$field.PascalName}} = args[{{$arg}}] {{- end -}}{{end}} + {{if $canAcceptJsonAndIdPrompts}} + } + {{end}} {{end}} {{if $wait -}} if {{.CamelName}}SkipWait { diff --git a/cmd/account/budgets/budgets.go b/cmd/account/budgets/budgets.go index 23de43923d..bf1fee4647 100755 --- a/cmd/account/budgets/budgets.go +++ b/cmd/account/budgets/budgets.go @@ -71,7 +71,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete BUDGET_ID", + Use: "delete [BUDGET_ID]", Short: `Delete budget.`, Long: `Delete budget. @@ -117,7 +117,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get BUDGET_ID", + Use: "get [BUDGET_ID]", Short: `Get budget and its status.`, Long: `Get budget and its status. diff --git a/cmd/account/credentials/credentials.go b/cmd/account/credentials/credentials.go index 3ff0fe2180..06c8e5a175 100755 --- a/cmd/account/credentials/credentials.go +++ b/cmd/account/credentials/credentials.go @@ -88,7 +88,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete CREDENTIALS_ID", + Use: "delete [CREDENTIALS_ID]", Short: `Delete credential configuration.`, Long: `Delete credential configuration. @@ -136,7 +136,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get CREDENTIALS_ID", + Use: "get [CREDENTIALS_ID]", Short: `Get credential configuration.`, Long: `Get credential configuration. diff --git a/cmd/account/groups/groups.go b/cmd/account/groups/groups.go index 5c9f8779db..00666ae7af 100755 --- a/cmd/account/groups/groups.go +++ b/cmd/account/groups/groups.go @@ -81,7 +81,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete ID", + Use: "delete [ID]", Short: `Delete a group.`, Long: `Delete a group. @@ -127,7 +127,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get ID", + Use: "get [ID]", Short: `Get group details.`, Long: `Get group details. @@ -216,7 +216,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch ID", + Use: "patch [ID]", Short: `Update group details.`, Long: `Update group details. @@ -227,25 +227,29 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks account") - } - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks account") + } + patchReq.Id = args[0] + } - patchReq.Id = args[0] err = a.Groups.Patch(ctx, patchReq) if err != nil { @@ -276,7 +280,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update ID", + Use: "update [ID]", Short: `Replace a group.`, Long: `Replace a group. @@ -287,25 +291,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks group ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks group ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks group id") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have databricks group id") + } + updateReq.Id = args[0] + } - updateReq.Id = args[0] err = a.Groups.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/ip-access-lists/ip-access-lists.go b/cmd/account/ip-access-lists/ip-access-lists.go index 86dd0a9f07..6fbdf2801c 100755 --- a/cmd/account/ip-access-lists/ip-access-lists.go +++ b/cmd/account/ip-access-lists/ip-access-lists.go @@ -109,7 +109,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete IP_ACCESS_LIST_ID", + Use: "delete [IP_ACCESS_LIST_ID]", Short: `Delete access list.`, Long: `Delete access list. @@ -155,7 +155,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get IP_ACCESS_LIST_ID", + Use: "get [IP_ACCESS_LIST_ID]", Short: `Get IP access list.`, Long: `Get IP access list. diff --git a/cmd/account/log-delivery/log-delivery.go b/cmd/account/log-delivery/log-delivery.go index c8a81ccfa5..f687599301 100755 --- a/cmd/account/log-delivery/log-delivery.go +++ b/cmd/account/log-delivery/log-delivery.go @@ -152,7 +152,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get LOG_DELIVERY_CONFIGURATION_ID", + Use: "get [LOG_DELIVERY_CONFIGURATION_ID]", Short: `Get log delivery configuration.`, Long: `Get log delivery configuration. diff --git a/cmd/account/networks/networks.go b/cmd/account/networks/networks.go index 60704770a3..adc120a08a 100755 --- a/cmd/account/networks/networks.go +++ b/cmd/account/networks/networks.go @@ -38,7 +38,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create NETWORK_NAME", + Use: "create [NETWORK_NAME]", Short: `Create network configuration.`, Long: `Create network configuration. @@ -51,25 +51,29 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Networks.NetworkNetworkNameToNetworkIdMap(ctx) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The human-readable name of the network configuration") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Networks.NetworkNetworkNameToNetworkIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The human-readable name of the network configuration") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the human-readable name of the network configuration") - } - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have the human-readable name of the network configuration") + } + createReq.NetworkName = args[0] + } - createReq.NetworkName = args[0] response, err := a.Networks.Create(ctx, createReq) if err != nil { @@ -90,7 +94,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete NETWORK_ID", + Use: "delete [NETWORK_ID]", Short: `Delete a network configuration.`, Long: `Delete a network configuration. @@ -141,7 +145,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get NETWORK_ID", + Use: "get [NETWORK_ID]", Short: `Get a network configuration.`, Long: `Get a network configuration. diff --git a/cmd/account/private-access/private-access.go b/cmd/account/private-access/private-access.go index 765836db7f..e9696a086e 100755 --- a/cmd/account/private-access/private-access.go +++ b/cmd/account/private-access/private-access.go @@ -87,7 +87,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete PRIVATE_ACCESS_SETTINGS_ID", + Use: "delete [PRIVATE_ACCESS_SETTINGS_ID]", Short: `Delete a private access settings object.`, Long: `Delete a private access settings object. @@ -140,7 +140,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get PRIVATE_ACCESS_SETTINGS_ID", + Use: "get [PRIVATE_ACCESS_SETTINGS_ID]", Short: `Get a private access settings object.`, Long: `Get a private access settings object. diff --git a/cmd/account/service-principals/service-principals.go b/cmd/account/service-principals/service-principals.go index 6c8e4f7c69..2f57f90adc 100755 --- a/cmd/account/service-principals/service-principals.go +++ b/cmd/account/service-principals/service-principals.go @@ -80,7 +80,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete ID", + Use: "delete [ID]", Short: `Delete a service principal.`, Long: `Delete a service principal. @@ -126,7 +126,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get ID", + Use: "get [ID]", Short: `Get service principal details.`, Long: `Get service principal details. @@ -216,7 +216,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch ID", + Use: "patch [ID]", Short: `Update service principal details.`, Long: `Update service principal details. @@ -228,25 +228,29 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks account") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks account") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a service principal in the databricks account") - } - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a service principal in the databricks account") + } + patchReq.Id = args[0] + } - patchReq.Id = args[0] err = a.ServicePrincipals.Patch(ctx, patchReq) if err != nil { @@ -278,7 +282,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update ID", + Use: "update [ID]", Short: `Replace service principal.`, Long: `Replace service principal. @@ -291,25 +295,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks service principal ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks service principal ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks service principal id") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have databricks service principal id") + } + updateReq.Id = args[0] + } - updateReq.Id = args[0] err = a.ServicePrincipals.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/storage/storage.go b/cmd/account/storage/storage.go index 37d99f2fbb..1a6291d61f 100755 --- a/cmd/account/storage/storage.go +++ b/cmd/account/storage/storage.go @@ -85,7 +85,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete STORAGE_CONFIGURATION_ID", + Use: "delete [STORAGE_CONFIGURATION_ID]", Short: `Delete storage configuration.`, Long: `Delete storage configuration. @@ -132,7 +132,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get STORAGE_CONFIGURATION_ID", + Use: "get [STORAGE_CONFIGURATION_ID]", Short: `Get storage configuration.`, Long: `Get storage configuration. diff --git a/cmd/account/users/users.go b/cmd/account/users/users.go index 586d29a46d..72e9ba85d6 100755 --- a/cmd/account/users/users.go +++ b/cmd/account/users/users.go @@ -88,7 +88,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete ID", + Use: "delete [ID]", Short: `Delete a user.`, Long: `Delete a user. @@ -135,7 +135,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get ID", + Use: "get [ID]", Short: `Get user details.`, Long: `Get user details. @@ -224,7 +224,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch ID", + Use: "patch [ID]", Short: `Update user details.`, Long: `Update user details. @@ -236,25 +236,29 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks account") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks account") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a user in the databricks account") - } - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a user in the databricks account") + } + patchReq.Id = args[0] + } - patchReq.Id = args[0] err = a.Users.Patch(ctx, patchReq) if err != nil { @@ -288,7 +292,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update ID", + Use: "update [ID]", Short: `Replace a user.`, Long: `Replace a user. @@ -299,25 +303,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks user ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks user ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks user id") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have databricks user id") + } + updateReq.Id = args[0] + } - updateReq.Id = args[0] err = a.Users.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/vpc-endpoints/vpc-endpoints.go b/cmd/account/vpc-endpoints/vpc-endpoints.go index 94b33580d3..5dd0b9bb36 100755 --- a/cmd/account/vpc-endpoints/vpc-endpoints.go +++ b/cmd/account/vpc-endpoints/vpc-endpoints.go @@ -35,7 +35,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create VPC_ENDPOINT_NAME", + Use: "create [VPC_ENDPOINT_NAME]", Short: `Create VPC endpoint configuration.`, Long: `Create VPC endpoint configuration. @@ -58,25 +58,29 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.VpcEndpoints.VpcEndpointVpcEndpointNameToVpcEndpointIdMap(ctx) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The human-readable name of the storage configuration") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.VpcEndpoints.VpcEndpointVpcEndpointNameToVpcEndpointIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The human-readable name of the storage configuration") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the human-readable name of the storage configuration") - } - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have the human-readable name of the storage configuration") + } + createReq.VpcEndpointName = args[0] + } - createReq.VpcEndpointName = args[0] response, err := a.VpcEndpoints.Create(ctx, createReq) if err != nil { @@ -97,7 +101,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete VPC_ENDPOINT_ID", + Use: "delete [VPC_ENDPOINT_ID]", Short: `Delete VPC endpoint configuration.`, Long: `Delete VPC endpoint configuration. @@ -151,7 +155,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get VPC_ENDPOINT_ID", + Use: "get [VPC_ENDPOINT_ID]", Short: `Get a VPC endpoint configuration.`, Long: `Get a VPC endpoint configuration. diff --git a/cmd/account/workspaces/workspaces.go b/cmd/account/workspaces/workspaces.go index 20df94a23c..79f13a919b 100755 --- a/cmd/account/workspaces/workspaces.go +++ b/cmd/account/workspaces/workspaces.go @@ -58,7 +58,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create WORKSPACE_NAME", + Use: "create [WORKSPACE_NAME]", Short: `Create a new workspace.`, Long: `Create a new workspace. @@ -77,25 +77,29 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Workspaces.WorkspaceWorkspaceNameToWorkspaceIdMap(ctx) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The workspace's human-readable name") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Workspaces.WorkspaceWorkspaceNameToWorkspaceIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The workspace's human-readable name") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the workspace's human-readable name") - } - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have the workspace's human-readable name") + } + createReq.WorkspaceName = args[0] + } - createReq.WorkspaceName = args[0] if createSkipWait { response, err := a.Workspaces.Create(ctx, createReq) @@ -133,7 +137,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete WORKSPACE_ID", + Use: "delete [WORKSPACE_ID]", Short: `Delete a workspace.`, Long: `Delete a workspace. @@ -189,7 +193,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get WORKSPACE_ID", + Use: "get [WORKSPACE_ID]", Short: `Get a workspace.`, Long: `Get a workspace. @@ -295,7 +299,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update WORKSPACE_ID", + Use: "update [WORKSPACE_ID]", Short: `Update workspace configuration.`, Long: `Update workspace configuration. diff --git a/cmd/workspace/alerts/alerts.go b/cmd/workspace/alerts/alerts.go index 5c969880ec..aaa46eb4bb 100755 --- a/cmd/workspace/alerts/alerts.go +++ b/cmd/workspace/alerts/alerts.go @@ -81,7 +81,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete ALERT_ID", + Use: "delete [ALERT_ID]", Short: `Delete an alert.`, Long: `Delete an alert. @@ -129,7 +129,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get ALERT_ID", + Use: "get [ALERT_ID]", Short: `Get an alert.`, Long: `Get an alert. diff --git a/cmd/workspace/cluster-policies/cluster-policies.go b/cmd/workspace/cluster-policies/cluster-policies.go index 465206ac44..9eceb08aa7 100755 --- a/cmd/workspace/cluster-policies/cluster-policies.go +++ b/cmd/workspace/cluster-policies/cluster-policies.go @@ -57,7 +57,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create NAME", + Use: "create [NAME]", Short: `Create a new policy.`, Long: `Create a new policy. @@ -103,7 +103,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete POLICY_ID", + Use: "delete [POLICY_ID]", Short: `Delete a cluster policy.`, Long: `Delete a cluster policy. @@ -191,7 +191,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get POLICY_ID", + Use: "get [POLICY_ID]", Short: `Get entity.`, Long: `Get entity. diff --git a/cmd/workspace/clusters/clusters.go b/cmd/workspace/clusters/clusters.go index 1ed0f3eeb6..ba9aa528f2 100755 --- a/cmd/workspace/clusters/clusters.go +++ b/cmd/workspace/clusters/clusters.go @@ -124,7 +124,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create SPARK_VERSION", + Use: "create [SPARK_VERSION]", Short: `Create new cluster.`, Long: `Create new cluster. @@ -147,25 +147,29 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The Spark version of the cluster, e.g") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The Spark version of the cluster, e.g") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the spark version of the cluster, e.g") - } - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have the spark version of the cluster, e.g") + } + createReq.SparkVersion = args[0] + } - createReq.SparkVersion = args[0] if createSkipWait { response, err := w.Clusters.Create(ctx, createReq) @@ -209,7 +213,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete CLUSTER_ID", + Use: "delete [CLUSTER_ID]", Short: `Terminate cluster.`, Long: `Terminate cluster. @@ -382,7 +386,7 @@ func init() { } var eventsCmd = &cobra.Command{ - Use: "events CLUSTER_ID", + Use: "events [CLUSTER_ID]", Short: `List cluster activity events.`, Long: `List cluster activity events. @@ -395,25 +399,29 @@ var eventsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if cmd.Flags().Changed("json") { + err = eventsJson.Unmarshal(&eventsReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID of the cluster to retrieve events about") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID of the cluster to retrieve events about") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id of the cluster to retrieve events about") - } - err = eventsJson.Unmarshal(&eventsReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have the id of the cluster to retrieve events about") + } + eventsReq.ClusterId = args[0] + } - eventsReq.ClusterId = args[0] response, err := w.Clusters.EventsAll(ctx, eventsReq) if err != nil { @@ -440,7 +448,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get CLUSTER_ID", + Use: "get [CLUSTER_ID]", Short: `Get cluster info.`, Long: `Get cluster info. @@ -584,7 +592,7 @@ func init() { } var permanentDeleteCmd = &cobra.Command{ - Use: "permanent-delete CLUSTER_ID", + Use: "permanent-delete [CLUSTER_ID]", Short: `Permanently delete cluster.`, Long: `Permanently delete cluster. @@ -635,7 +643,7 @@ func init() { } var pinCmd = &cobra.Command{ - Use: "pin CLUSTER_ID", + Use: "pin [CLUSTER_ID]", Short: `Pin cluster.`, Long: `Pin cluster. @@ -693,7 +701,7 @@ func init() { } var resizeCmd = &cobra.Command{ - Use: "resize CLUSTER_ID", + Use: "resize [CLUSTER_ID]", Short: `Resize cluster.`, Long: `Resize cluster. @@ -705,25 +713,29 @@ var resizeCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if cmd.Flags().Changed("json") { + err = resizeJson.Unmarshal(&resizeReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The cluster to be resized") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The cluster to be resized") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the cluster to be resized") - } - err = resizeJson.Unmarshal(&resizeReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have the cluster to be resized") + } + resizeReq.ClusterId = args[0] + } - resizeReq.ClusterId = args[0] if resizeSkipWait { err = w.Clusters.Resize(ctx, resizeReq) @@ -769,7 +781,7 @@ func init() { } var restartCmd = &cobra.Command{ - Use: "restart CLUSTER_ID", + Use: "restart [CLUSTER_ID]", Short: `Restart cluster.`, Long: `Restart cluster. @@ -867,7 +879,7 @@ func init() { } var startCmd = &cobra.Command{ - Use: "start CLUSTER_ID", + Use: "start [CLUSTER_ID]", Short: `Start terminated cluster.`, Long: `Start terminated cluster. @@ -937,7 +949,7 @@ func init() { } var unpinCmd = &cobra.Command{ - Use: "unpin CLUSTER_ID", + Use: "unpin [CLUSTER_ID]", Short: `Unpin cluster.`, Long: `Unpin cluster. diff --git a/cmd/workspace/dashboards/dashboards.go b/cmd/workspace/dashboards/dashboards.go index c72369af7f..9c948d0e50 100755 --- a/cmd/workspace/dashboards/dashboards.go +++ b/cmd/workspace/dashboards/dashboards.go @@ -74,7 +74,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete DASHBOARD_ID", + Use: "delete [DASHBOARD_ID]", Short: `Remove a dashboard.`, Long: `Remove a dashboard. @@ -121,7 +121,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get DASHBOARD_ID", + Use: "get [DASHBOARD_ID]", Short: `Retrieve a definition.`, Long: `Retrieve a definition. @@ -204,7 +204,7 @@ func init() { } var restoreCmd = &cobra.Command{ - Use: "restore DASHBOARD_ID", + Use: "restore [DASHBOARD_ID]", Short: `Restore a dashboard.`, Long: `Restore a dashboard. diff --git a/cmd/workspace/git-credentials/git-credentials.go b/cmd/workspace/git-credentials/git-credentials.go index 1705f99947..87af76d58d 100755 --- a/cmd/workspace/git-credentials/git-credentials.go +++ b/cmd/workspace/git-credentials/git-credentials.go @@ -36,7 +36,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create GIT_PROVIDER", + Use: "create [GIT_PROVIDER]", Short: `Create a credential entry.`, Long: `Create a credential entry. @@ -85,7 +85,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete CREDENTIAL_ID", + Use: "delete [CREDENTIAL_ID]", Short: `Delete a credential.`, Long: `Delete a credential. @@ -134,7 +134,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get CREDENTIAL_ID", + Use: "get [CREDENTIAL_ID]", Short: `Get a credential entry.`, Long: `Get a credential entry. @@ -215,7 +215,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update CREDENTIAL_ID", + Use: "update [CREDENTIAL_ID]", Short: `Update a credential.`, Long: `Update a credential. diff --git a/cmd/workspace/global-init-scripts/global-init-scripts.go b/cmd/workspace/global-init-scripts/global-init-scripts.go index d0f562ad55..f8d2753231 100755 --- a/cmd/workspace/global-init-scripts/global-init-scripts.go +++ b/cmd/workspace/global-init-scripts/global-init-scripts.go @@ -73,7 +73,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete SCRIPT_ID", + Use: "delete [SCRIPT_ID]", Short: `Delete init script.`, Long: `Delete init script. @@ -119,7 +119,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get SCRIPT_ID", + Use: "get [SCRIPT_ID]", Short: `Get an init script.`, Long: `Get an init script. diff --git a/cmd/workspace/groups/groups.go b/cmd/workspace/groups/groups.go index d51cca37b7..7648598614 100755 --- a/cmd/workspace/groups/groups.go +++ b/cmd/workspace/groups/groups.go @@ -81,7 +81,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete ID", + Use: "delete [ID]", Short: `Delete a group.`, Long: `Delete a group. @@ -127,7 +127,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get ID", + Use: "get [ID]", Short: `Get group details.`, Long: `Get group details. @@ -216,7 +216,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch ID", + Use: "patch [ID]", Short: `Update group details.`, Long: `Update group details. @@ -227,25 +227,29 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks workspace") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks workspace") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks workspace") - } - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks workspace") + } + patchReq.Id = args[0] + } - patchReq.Id = args[0] err = w.Groups.Patch(ctx, patchReq) if err != nil { @@ -276,7 +280,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update ID", + Use: "update [ID]", Short: `Replace a group.`, Long: `Replace a group. @@ -287,25 +291,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks group ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks group ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks group id") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have databricks group id") + } + updateReq.Id = args[0] + } - updateReq.Id = args[0] err = w.Groups.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/instance-pools/instance-pools.go b/cmd/workspace/instance-pools/instance-pools.go index d083cd6f18..d1a81801be 100755 --- a/cmd/workspace/instance-pools/instance-pools.go +++ b/cmd/workspace/instance-pools/instance-pools.go @@ -98,7 +98,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete INSTANCE_POOL_ID", + Use: "delete [INSTANCE_POOL_ID]", Short: `Delete an instance pool.`, Long: `Delete an instance pool. @@ -198,7 +198,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get INSTANCE_POOL_ID", + Use: "get [INSTANCE_POOL_ID]", Short: `Get instance pool information.`, Long: `Get instance pool information. diff --git a/cmd/workspace/ip-access-lists/ip-access-lists.go b/cmd/workspace/ip-access-lists/ip-access-lists.go index 381fc262bd..aae6ffd9d6 100755 --- a/cmd/workspace/ip-access-lists/ip-access-lists.go +++ b/cmd/workspace/ip-access-lists/ip-access-lists.go @@ -110,7 +110,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete IP_ACCESS_LIST_ID", + Use: "delete [IP_ACCESS_LIST_ID]", Short: `Delete access list.`, Long: `Delete access list. @@ -156,7 +156,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get IP_ACCESS_LIST_ID", + Use: "get [IP_ACCESS_LIST_ID]", Short: `Get access list.`, Long: `Get access list. diff --git a/cmd/workspace/jobs/jobs.go b/cmd/workspace/jobs/jobs.go index 95864eba8c..94693d3022 100755 --- a/cmd/workspace/jobs/jobs.go +++ b/cmd/workspace/jobs/jobs.go @@ -48,7 +48,7 @@ func init() { } var cancelAllRunsCmd = &cobra.Command{ - Use: "cancel-all-runs JOB_ID", + Use: "cancel-all-runs [JOB_ID]", Short: `Cancel all runs of a job.`, Long: `Cancel all runs of a job. @@ -104,7 +104,7 @@ func init() { } var cancelRunCmd = &cobra.Command{ - Use: "cancel-run RUN_ID", + Use: "cancel-run [RUN_ID]", Short: `Cancel a job run.`, Long: `Cancel a job run. @@ -231,7 +231,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete JOB_ID", + Use: "delete [JOB_ID]", Short: `Delete a job.`, Long: `Delete a job. @@ -280,7 +280,7 @@ func init() { } var deleteRunCmd = &cobra.Command{ - Use: "delete-run RUN_ID", + Use: "delete-run [RUN_ID]", Short: `Delete a job run.`, Long: `Delete a job run. @@ -331,7 +331,7 @@ func init() { } var exportRunCmd = &cobra.Command{ - Use: "export-run RUN_ID", + Use: "export-run [RUN_ID]", Short: `Export and retrieve a job run.`, Long: `Export and retrieve a job run. @@ -380,7 +380,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get JOB_ID", + Use: "get [JOB_ID]", Short: `Get a single job.`, Long: `Get a single job. @@ -437,7 +437,7 @@ func init() { } var getRunCmd = &cobra.Command{ - Use: "get-run RUN_ID", + Use: "get-run [RUN_ID]", Short: `Get a single job run.`, Long: `Get a single job run. @@ -486,7 +486,7 @@ func init() { } var getRunOutputCmd = &cobra.Command{ - Use: "get-run-output RUN_ID", + Use: "get-run-output [RUN_ID]", Short: `Get the output for a single run.`, Long: `Get the output for a single run. @@ -640,7 +640,7 @@ func init() { } var repairRunCmd = &cobra.Command{ - Use: "repair-run RUN_ID", + Use: "repair-run [RUN_ID]", Short: `Repair a job run.`, Long: `Repair a job run. @@ -653,27 +653,31 @@ var repairRunCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = repairRunJson.Unmarshal(&repairRunReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The job run ID of the run to repair") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The job run ID of the run to repair") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the job run id of the run to repair") + } + _, err = fmt.Sscan(args[0], &repairRunReq.RunId) if err != nil { - return err + return fmt.Errorf("invalid RUN_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the job run id of the run to repair") - } - err = repairRunJson.Unmarshal(&repairRunReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &repairRunReq.RunId) - if err != nil { - return fmt.Errorf("invalid RUN_ID: %s", args[0]) + } if repairRunSkipWait { @@ -782,7 +786,7 @@ func init() { } var runNowCmd = &cobra.Command{ - Use: "run-now JOB_ID", + Use: "run-now [JOB_ID]", Short: `Trigger a new job run.`, Long: `Trigger a new job run. @@ -793,27 +797,31 @@ var runNowCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = runNowJson.Unmarshal(&runNowReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID of the job to be executed") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID of the job to be executed") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the id of the job to be executed") + } + _, err = fmt.Sscan(args[0], &runNowReq.JobId) if err != nil { - return err + return fmt.Errorf("invalid JOB_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id of the job to be executed") - } - err = runNowJson.Unmarshal(&runNowReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &runNowReq.JobId) - if err != nil { - return fmt.Errorf("invalid JOB_ID: %s", args[0]) + } if runNowSkipWait { @@ -942,7 +950,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update JOB_ID", + Use: "update [JOB_ID]", Short: `Partially update a job.`, Long: `Partially update a job. @@ -954,27 +962,31 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The canonical identifier of the job to update") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The canonical identifier of the job to update") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the canonical identifier of the job to update") + } + _, err = fmt.Sscan(args[0], &updateReq.JobId) if err != nil { - return err + return fmt.Errorf("invalid JOB_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the canonical identifier of the job to update") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &updateReq.JobId) - if err != nil { - return fmt.Errorf("invalid JOB_ID: %s", args[0]) + } err = w.Jobs.Update(ctx, updateReq) diff --git a/cmd/workspace/metastores/metastores.go b/cmd/workspace/metastores/metastores.go index 7ed5e668b6..48e5f60683 100755 --- a/cmd/workspace/metastores/metastores.go +++ b/cmd/workspace/metastores/metastores.go @@ -145,7 +145,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete ID", + Use: "delete [ID]", Short: `Delete a metastore.`, Long: `Delete a metastore. @@ -191,7 +191,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get ID", + Use: "get [ID]", Short: `Get a metastore.`, Long: `Get a metastore. @@ -377,7 +377,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update ID", + Use: "update [ID]", Short: `Update a metastore.`, Long: `Update a metastore. @@ -427,7 +427,7 @@ func init() { } var updateAssignmentCmd = &cobra.Command{ - Use: "update-assignment WORKSPACE_ID", + Use: "update-assignment [WORKSPACE_ID]", Short: `Update an assignment.`, Long: `Update an assignment. diff --git a/cmd/workspace/pipelines/pipelines.go b/cmd/workspace/pipelines/pipelines.go index 3ecb556e42..52108c7148 100755 --- a/cmd/workspace/pipelines/pipelines.go +++ b/cmd/workspace/pipelines/pipelines.go @@ -101,7 +101,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete PIPELINE_ID", + Use: "delete [PIPELINE_ID]", Short: `Delete a pipeline.`, Long: `Delete a pipeline. @@ -153,7 +153,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get PIPELINE_ID", + Use: "get [PIPELINE_ID]", Short: `Get a pipeline.`, Long: `Get a pipeline.`, @@ -238,7 +238,7 @@ func init() { } var listPipelineEventsCmd = &cobra.Command{ - Use: "list-pipeline-events PIPELINE_ID", + Use: "list-pipeline-events [PIPELINE_ID]", Short: `List pipeline events.`, Long: `List pipeline events. @@ -249,25 +249,29 @@ var listPipelineEventsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if cmd.Flags().Changed("json") { + err = listPipelineEventsJson.Unmarshal(&listPipelineEventsReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") - } - err = listPipelineEventsJson.Unmarshal(&listPipelineEventsReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + listPipelineEventsReq.PipelineId = args[0] + } - listPipelineEventsReq.PipelineId = args[0] response, err := w.Pipelines.ListPipelineEventsAll(ctx, listPipelineEventsReq) if err != nil { @@ -334,7 +338,7 @@ func init() { } var listUpdatesCmd = &cobra.Command{ - Use: "list-updates PIPELINE_ID", + Use: "list-updates [PIPELINE_ID]", Short: `List pipeline updates.`, Long: `List pipeline updates. @@ -386,7 +390,7 @@ func init() { } var resetCmd = &cobra.Command{ - Use: "reset PIPELINE_ID", + Use: "reset [PIPELINE_ID]", Short: `Reset a pipeline.`, Long: `Reset a pipeline. @@ -456,7 +460,7 @@ func init() { } var startUpdateCmd = &cobra.Command{ - Use: "start-update PIPELINE_ID", + Use: "start-update [PIPELINE_ID]", Short: `Queue a pipeline update.`, Long: `Queue a pipeline update. @@ -467,25 +471,29 @@ var startUpdateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if cmd.Flags().Changed("json") { + err = startUpdateJson.Unmarshal(&startUpdateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") - } - err = startUpdateJson.Unmarshal(&startUpdateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + startUpdateReq.PipelineId = args[0] + } - startUpdateReq.PipelineId = args[0] response, err := w.Pipelines.StartUpdate(ctx, startUpdateReq) if err != nil { @@ -512,7 +520,7 @@ func init() { } var stopCmd = &cobra.Command{ - Use: "stop PIPELINE_ID", + Use: "stop [PIPELINE_ID]", Short: `Stop a pipeline.`, Long: `Stop a pipeline. @@ -597,7 +605,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update PIPELINE_ID", + Use: "update [PIPELINE_ID]", Short: `Edit a pipeline.`, Long: `Edit a pipeline. @@ -608,25 +616,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique identifier for this pipeline") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique identifier for this pipeline") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique identifier for this pipeline") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have unique identifier for this pipeline") + } + updateReq.PipelineId = args[0] + } - updateReq.PipelineId = args[0] err = w.Pipelines.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/providers/providers.go b/cmd/workspace/providers/providers.go index a5c17356b5..86283abf1c 100755 --- a/cmd/workspace/providers/providers.go +++ b/cmd/workspace/providers/providers.go @@ -69,7 +69,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete NAME", + Use: "delete [NAME]", Short: `Delete a provider.`, Long: `Delete a provider. @@ -116,7 +116,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get NAME", + Use: "get [NAME]", Short: `Get a provider.`, Long: `Get a provider. @@ -200,7 +200,7 @@ func init() { } var listSharesCmd = &cobra.Command{ - Use: "list-shares NAME", + Use: "list-shares [NAME]", Short: `List shares by Provider.`, Long: `List shares by Provider. @@ -253,7 +253,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update NAME", + Use: "update [NAME]", Short: `Update a provider.`, Long: `Update a provider. diff --git a/cmd/workspace/queries/queries.go b/cmd/workspace/queries/queries.go index 0cd240c802..26c57ceebb 100755 --- a/cmd/workspace/queries/queries.go +++ b/cmd/workspace/queries/queries.go @@ -84,7 +84,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete QUERY_ID", + Use: "delete [QUERY_ID]", Short: `Delete a query.`, Long: `Delete a query. @@ -132,7 +132,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get QUERY_ID", + Use: "get [QUERY_ID]", Short: `Get a query definition.`, Long: `Get a query definition. @@ -216,7 +216,7 @@ func init() { } var restoreCmd = &cobra.Command{ - Use: "restore QUERY_ID", + Use: "restore [QUERY_ID]", Short: `Restore a query.`, Long: `Restore a query. @@ -271,7 +271,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update QUERY_ID", + Use: "update [QUERY_ID]", Short: `Change a query definition.`, Long: `Change a query definition. @@ -284,25 +284,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Queries.QueryNameToIdMap(ctx, sql.ListQueriesRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Queries.QueryNameToIdMap(ctx, sql.ListQueriesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + updateReq.QueryId = args[0] + } - updateReq.QueryId = args[0] response, err := w.Queries.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/recipients/recipients.go b/cmd/workspace/recipients/recipients.go index a9bd39f763..208a98bccb 100755 --- a/cmd/workspace/recipients/recipients.go +++ b/cmd/workspace/recipients/recipients.go @@ -81,7 +81,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete NAME", + Use: "delete [NAME]", Short: `Delete a share recipient.`, Long: `Delete a share recipient. @@ -128,7 +128,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get NAME", + Use: "get [NAME]", Short: `Get a share recipient.`, Long: `Get a share recipient. @@ -250,7 +250,7 @@ func init() { } var sharePermissionsCmd = &cobra.Command{ - Use: "share-permissions NAME", + Use: "share-permissions [NAME]", Short: `Get recipient share permissions.`, Long: `Get recipient share permissions. @@ -305,7 +305,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update NAME", + Use: "update [NAME]", Short: `Update a share recipient.`, Long: `Update a share recipient. @@ -318,25 +318,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Recipients.RecipientInfoNameToMetastoreIdMap(ctx, sharing.ListRecipientsRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Name of Recipient") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Recipients.RecipientInfoNameToMetastoreIdMap(ctx, sharing.ListRecipientsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Name of Recipient") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have name of recipient") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have name of recipient") + } + updateReq.Name = args[0] + } - updateReq.Name = args[0] err = w.Recipients.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/repos/repos.go b/cmd/workspace/repos/repos.go index dac08e5234..a0c2878ff1 100755 --- a/cmd/workspace/repos/repos.go +++ b/cmd/workspace/repos/repos.go @@ -83,7 +83,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete REPO_ID", + Use: "delete [REPO_ID]", Short: `Delete a repo.`, Long: `Delete a repo. @@ -132,7 +132,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get REPO_ID", + Use: "get [REPO_ID]", Short: `Get a repo.`, Long: `Get a repo. @@ -222,7 +222,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update REPO_ID", + Use: "update [REPO_ID]", Short: `Update a repo.`, Long: `Update a repo. @@ -234,27 +234,31 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Repos.RepoInfoPathToIdMap(ctx, workspace.ListReposRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID for the corresponding repo to access") + } else { + if len(args) == 0 { + names, err := w.Repos.RepoInfoPathToIdMap(ctx, workspace.ListReposRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding repo to access") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding repo to access") + } + _, err = fmt.Sscan(args[0], &updateReq.RepoId) if err != nil { - return err + return fmt.Errorf("invalid REPO_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id for the corresponding repo to access") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &updateReq.RepoId) - if err != nil { - return fmt.Errorf("invalid REPO_ID: %s", args[0]) + } err = w.Repos.Update(ctx, updateReq) diff --git a/cmd/workspace/schemas/schemas.go b/cmd/workspace/schemas/schemas.go index 41e8fb32ba..635f4e6094 100755 --- a/cmd/workspace/schemas/schemas.go +++ b/cmd/workspace/schemas/schemas.go @@ -79,7 +79,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete FULL_NAME", + Use: "delete [FULL_NAME]", Short: `Delete a schema.`, Long: `Delete a schema. @@ -126,7 +126,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get FULL_NAME", + Use: "get [FULL_NAME]", Short: `Get a schema.`, Long: `Get a schema. @@ -174,7 +174,7 @@ func init() { } var listCmd = &cobra.Command{ - Use: "list CATALOG_NAME", + Use: "list [CATALOG_NAME]", Short: `List schemas.`, Long: `List schemas. @@ -231,7 +231,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update FULL_NAME", + Use: "update [FULL_NAME]", Short: `Update a schema.`, Long: `Update a schema. @@ -246,25 +246,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Schemas.SchemaInfoNameToFullNameMap(ctx, catalog.ListSchemasRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Full name of the schema") - if err != nil { - 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, "Full name of the schema") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have full name of the schema") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have full name of the schema") + } + updateReq.FullName = args[0] + } - updateReq.FullName = args[0] response, err := w.Schemas.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/service-principals/service-principals.go b/cmd/workspace/service-principals/service-principals.go index 6c57e660e6..dc1abde08e 100755 --- a/cmd/workspace/service-principals/service-principals.go +++ b/cmd/workspace/service-principals/service-principals.go @@ -80,7 +80,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete ID", + Use: "delete [ID]", Short: `Delete a service principal.`, Long: `Delete a service principal. @@ -126,7 +126,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get ID", + Use: "get [ID]", Short: `Get service principal details.`, Long: `Get service principal details. @@ -216,7 +216,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch ID", + Use: "patch [ID]", Short: `Update service principal details.`, Long: `Update service principal details. @@ -228,25 +228,29 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks workspace") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks workspace") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a service principal in the databricks workspace") - } - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a service principal in the databricks workspace") + } + patchReq.Id = args[0] + } - patchReq.Id = args[0] err = w.ServicePrincipals.Patch(ctx, patchReq) if err != nil { @@ -278,7 +282,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update ID", + Use: "update [ID]", Short: `Replace service principal.`, Long: `Replace service principal. @@ -291,25 +295,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks service principal ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks service principal ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks service principal id") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have databricks service principal id") + } + updateReq.Id = args[0] + } - updateReq.Id = args[0] err = w.ServicePrincipals.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/storage-credentials/storage-credentials.go b/cmd/workspace/storage-credentials/storage-credentials.go index 06c68edf1b..bacbfef134 100755 --- a/cmd/workspace/storage-credentials/storage-credentials.go +++ b/cmd/workspace/storage-credentials/storage-credentials.go @@ -50,7 +50,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create NAME", + Use: "create [NAME]", Short: `Create a storage credential.`, Long: `Create a storage credential. @@ -67,25 +67,29 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.StorageCredentials.StorageCredentialInfoNameToIdMap(ctx) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The credential name") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.StorageCredentials.StorageCredentialInfoNameToIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The credential name") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the credential name") - } - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have the credential name") + } + createReq.Name = args[0] + } - createReq.Name = args[0] response, err := w.StorageCredentials.Create(ctx, createReq) if err != nil { @@ -108,7 +112,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete NAME", + Use: "delete [NAME]", Short: `Delete a credential.`, Long: `Delete a credential. @@ -155,7 +159,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get NAME", + Use: "get [NAME]", Short: `Get a credential.`, Long: `Get a credential. @@ -246,7 +250,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update NAME", + Use: "update [NAME]", Short: `Update a credential.`, Long: `Update a credential. @@ -259,25 +263,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.StorageCredentials.StorageCredentialInfoNameToIdMap(ctx) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The credential name") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.StorageCredentials.StorageCredentialInfoNameToIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The credential name") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the credential name") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have the credential name") + } + updateReq.Name = args[0] + } - updateReq.Name = args[0] response, err := w.StorageCredentials.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/tables/tables.go b/cmd/workspace/tables/tables.go index c5afc67221..c832d2f16b 100755 --- a/cmd/workspace/tables/tables.go +++ b/cmd/workspace/tables/tables.go @@ -36,7 +36,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete FULL_NAME", + Use: "delete [FULL_NAME]", Short: `Delete a table.`, Long: `Delete a table. @@ -88,7 +88,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get FULL_NAME", + Use: "get [FULL_NAME]", Short: `Get a table.`, Long: `Get a table. @@ -186,7 +186,7 @@ func init() { } var listSummariesCmd = &cobra.Command{ - Use: "list-summaries CATALOG_NAME", + Use: "list-summaries [CATALOG_NAME]", Short: `List table summaries.`, Long: `List table summaries. diff --git a/cmd/workspace/token-management/token-management.go b/cmd/workspace/token-management/token-management.go index 55b9957e2e..74a43b8d64 100755 --- a/cmd/workspace/token-management/token-management.go +++ b/cmd/workspace/token-management/token-management.go @@ -69,7 +69,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete TOKEN_ID", + Use: "delete [TOKEN_ID]", Short: `Delete a token.`, Long: `Delete a token. @@ -115,7 +115,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get TOKEN_ID", + Use: "get [TOKEN_ID]", Short: `Get token info.`, Long: `Get token info. diff --git a/cmd/workspace/tokens/tokens.go b/cmd/workspace/tokens/tokens.go index f83db0dafb..37880360f8 100755 --- a/cmd/workspace/tokens/tokens.go +++ b/cmd/workspace/tokens/tokens.go @@ -66,7 +66,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete TOKEN_ID", + Use: "delete [TOKEN_ID]", Short: `Revoke token.`, Long: `Revoke token. diff --git a/cmd/workspace/users/users.go b/cmd/workspace/users/users.go index 296fdc070a..8b79531fae 100755 --- a/cmd/workspace/users/users.go +++ b/cmd/workspace/users/users.go @@ -88,7 +88,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete ID", + Use: "delete [ID]", Short: `Delete a user.`, Long: `Delete a user. @@ -135,7 +135,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get ID", + Use: "get [ID]", Short: `Get user details.`, Long: `Get user details. @@ -224,7 +224,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch ID", + Use: "patch [ID]", Short: `Update user details.`, Long: `Update user details. @@ -236,25 +236,29 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks workspace") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks workspace") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a user in the databricks workspace") - } - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a user in the databricks workspace") + } + patchReq.Id = args[0] + } - patchReq.Id = args[0] err = w.Users.Patch(ctx, patchReq) if err != nil { @@ -288,7 +292,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update ID", + Use: "update [ID]", Short: `Replace a user.`, Long: `Replace a user. @@ -299,25 +303,29 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks user ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks user ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks user id") - } - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have databricks user id") + } + updateReq.Id = args[0] + } - updateReq.Id = args[0] err = w.Users.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/volumes/volumes.go b/cmd/workspace/volumes/volumes.go index efd73c7e97..63e0e211a0 100755 --- a/cmd/workspace/volumes/volumes.go +++ b/cmd/workspace/volumes/volumes.go @@ -94,7 +94,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete FULL_NAME_ARG", + Use: "delete [FULL_NAME_ARG]", Short: `Delete a Volume.`, Long: `Delete a Volume. @@ -188,7 +188,7 @@ func init() { } var readCmd = &cobra.Command{ - Use: "read FULL_NAME_ARG", + Use: "read [FULL_NAME_ARG]", Short: `Get a Volume.`, Long: `Get a Volume. @@ -243,7 +243,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update FULL_NAME_ARG", + Use: "update [FULL_NAME_ARG]", Short: `Update a Volume.`, Long: `Update a Volume. diff --git a/cmd/workspace/warehouses/warehouses.go b/cmd/workspace/warehouses/warehouses.go index b4bac1bcfa..858bfed711 100755 --- a/cmd/workspace/warehouses/warehouses.go +++ b/cmd/workspace/warehouses/warehouses.go @@ -119,7 +119,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete ID", + Use: "delete [ID]", Short: `Delete a warehouse.`, Long: `Delete a warehouse. @@ -210,7 +210,7 @@ func init() { } var editCmd = &cobra.Command{ - Use: "edit ID", + Use: "edit [ID]", Short: `Update a warehouse.`, Long: `Update a warehouse. @@ -221,25 +221,29 @@ var editCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if cmd.Flags().Changed("json") { + err = editJson.Unmarshal(&editReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Required") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Required") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have required") - } - err = editJson.Unmarshal(&editReq) - if err != nil { - return err + if len(args) != 1 { + return fmt.Errorf("expected to have required") + } + editReq.Id = args[0] + } - editReq.Id = args[0] if editSkipWait { err = w.Warehouses.Edit(ctx, editReq) @@ -290,7 +294,7 @@ func init() { } var getCmd = &cobra.Command{ - Use: "get ID", + Use: "get [ID]", Short: `Get warehouse info.`, Long: `Get warehouse info. @@ -451,7 +455,7 @@ func init() { } var startCmd = &cobra.Command{ - Use: "start ID", + Use: "start [ID]", Short: `Start a warehouse.`, Long: `Start a warehouse. @@ -527,7 +531,7 @@ func init() { } var stopCmd = &cobra.Command{ - Use: "stop ID", + Use: "stop [ID]", Short: `Stop a warehouse.`, Long: `Stop a warehouse. diff --git a/cmd/workspace/workspace/workspace.go b/cmd/workspace/workspace/workspace.go index 2b4bff9058..e263f7fc42 100755 --- a/cmd/workspace/workspace/workspace.go +++ b/cmd/workspace/workspace/workspace.go @@ -34,7 +34,7 @@ func init() { } var deleteCmd = &cobra.Command{ - Use: "delete PATH", + Use: "delete [PATH]", Short: `Delete a workspace object.`, Long: `Delete a workspace object. @@ -90,7 +90,7 @@ func init() { } var exportCmd = &cobra.Command{ - Use: "export PATH", + Use: "export [PATH]", Short: `Export a workspace object.`, Long: `Export a workspace object. @@ -143,7 +143,7 @@ func init() { } var getStatusCmd = &cobra.Command{ - Use: "get-status PATH", + Use: "get-status [PATH]", Short: `Get status.`, Long: `Get status. @@ -195,7 +195,7 @@ func init() { } var importCmd = &cobra.Command{ - Use: "import PATH", + Use: "import [PATH]", Short: `Import a workspace object.`, Long: `Import a workspace object. @@ -246,7 +246,7 @@ func init() { } var listCmd = &cobra.Command{ - Use: "list PATH", + Use: "list [PATH]", Short: `List contents.`, Long: `List contents. @@ -294,7 +294,7 @@ func init() { } var mkdirsCmd = &cobra.Command{ - Use: "mkdirs PATH", + Use: "mkdirs [PATH]", Short: `Create a directory.`, Long: `Create a directory. From 3853759943a8b5ae00f45efd6cc83d2a5dde8296 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Thu, 25 May 2023 17:18:07 +0200 Subject: [PATCH 4/5] Always allow json flag --- .codegen/service.go.tmpl | 42 +- cmd/account/billable-usage/billable-usage.go | 24 +- cmd/account/budgets/budgets.go | 98 ++- cmd/account/credentials/credentials.go | 81 +- .../custom-app-integration.go | 82 +- .../encryption-keys/encryption-keys.go | 59 +- cmd/account/groups/groups.go | 104 ++- .../ip-access-lists/ip-access-lists.go | 163 ++-- cmd/account/log-delivery/log-delivery.go | 94 ++- .../metastore-assignments.go | 130 ++- cmd/account/metastores/metastores.go | 87 +- cmd/account/networks/networks.go | 65 +- .../o-auth-enrollment/o-auth-enrollment.go | 19 +- cmd/account/private-access/private-access.go | 110 ++- .../published-app-integration.go | 81 +- .../service-principal-secrets.go | 78 +- .../service-principals/service-principals.go | 104 ++- .../storage-credentials.go | 113 ++- cmd/account/storage/storage.go | 81 +- cmd/account/users/users.go | 104 ++- cmd/account/vpc-endpoints/vpc-endpoints.go | 65 +- .../workspace-assignment.go | 116 ++- cmd/account/workspaces/workspaces.go | 109 ++- cmd/workspace/alerts/alerts.go | 106 ++- cmd/workspace/catalogs/catalogs.go | 84 +- .../cluster-policies/cluster-policies.go | 135 +++- cmd/workspace/clusters/clusters.go | 294 ++++--- cmd/workspace/dashboards/dashboards.go | 129 ++- cmd/workspace/experiments/experiments.go | 501 ++++++++++-- .../external-locations/external-locations.go | 89 ++- cmd/workspace/functions/functions.go | 187 +++-- .../git-credentials/git-credentials.go | 137 ++-- .../global-init-scripts.go | 111 ++- cmd/workspace/grants/grants.go | 83 +- cmd/workspace/groups/groups.go | 104 ++- .../instance-pools/instance-pools.go | 110 ++- .../instance-profiles/instance-profiles.go | 64 +- .../ip-access-lists/ip-access-lists.go | 163 ++-- cmd/workspace/jobs/jobs.go | 388 +++++---- cmd/workspace/libraries/libraries.go | 59 +- cmd/workspace/metastores/metastores.go | 235 ++++-- .../model-registry/model-registry.go | 745 ++++++++++++++---- cmd/workspace/permissions/permissions.go | 92 ++- cmd/workspace/pipelines/pipelines.go | 226 ++++-- .../policy-families/policy-families.go | 40 +- cmd/workspace/providers/providers.go | 170 ++-- cmd/workspace/queries/queries.go | 132 +++- cmd/workspace/query-history/query-history.go | 18 +- .../recipient-activation.go | 43 +- cmd/workspace/recipients/recipients.go | 170 ++-- cmd/workspace/repos/repos.go | 114 ++- cmd/workspace/schemas/schemas.go | 119 ++- cmd/workspace/secrets/secrets.go | 205 ++++- .../service-principals/service-principals.go | 104 ++- .../serving-endpoints/serving-endpoints.go | 168 +++- cmd/workspace/shares/shares.go | 126 ++- .../storage-credentials.go | 86 +- .../table-constraints/table-constraints.go | 48 +- cmd/workspace/tables/tables.go | 117 ++- .../token-management/token-management.go | 108 ++- cmd/workspace/tokens/tokens.go | 50 +- cmd/workspace/users/users.go | 104 ++- cmd/workspace/volumes/volumes.go | 148 ++-- cmd/workspace/warehouses/warehouses.go | 185 +++-- .../workspace-bindings/workspace-bindings.go | 42 +- .../workspace-conf/workspace-conf.go | 40 +- cmd/workspace/workspace/workspace.go | 187 +++-- 67 files changed, 6154 insertions(+), 2421 deletions(-) diff --git a/.codegen/service.go.tmpl b/.codegen/service.go.tmpl index b1fa257c18..5510978a23 100644 --- a/.codegen/service.go.tmpl +++ b/.codegen/service.go.tmpl @@ -30,7 +30,7 @@ var Cmd = &cobra.Command{ // start {{.KebabName}} command {{if .Request}}var {{.CamelName}}Req {{.Service.Package.Name}}.{{.Request.PascalName}} -{{if not .Request.IsOnlyPrimitiveFields}}var {{.CamelName}}Json flags.JsonFlag{{end}} +var {{.CamelName}}Json flags.JsonFlag {{- end}} {{if .Wait}}var {{.CamelName}}SkipWait bool var {{.CamelName}}Timeout time.Duration{{end}} @@ -42,7 +42,7 @@ func init() { {{.CamelName}}Cmd.Flags().DurationVar(&{{.CamelName}}Timeout, "timeout", {{.Wait.Timeout}}*time.Minute, `maximum amount of time to reach {{range $i, $e := .Wait.Success}}{{if $i}} or {{end}}{{.Content}}{{end}} state`) {{end -}} {{if .Request}}// TODO: short flags - {{if not .Request.IsOnlyPrimitiveFields}}{{.CamelName}}Cmd.Flags().Var(&{{.CamelName}}Json, "json", `either inline JSON string or @path/to/file.json with request body`){{end}} + {{.CamelName}}Cmd.Flags().Var(&{{.CamelName}}Json, "json", `either inline JSON string or @path/to/file.json with request body`) {{$method := .}} {{range .Request.Fields -}} {{- if not .Required -}} @@ -58,46 +58,38 @@ func init() { {{end}} } -{{$isAllRequiredFieldsPrimitive := or (not .Request) (gt (len .Request.RequiredFields) 0)}} -{{ if .Request }} - {{- range .Request.RequiredFields -}} - {{- if not .Entity.IsPrimitive -}}{{ $isAllRequiredFieldsPrimitive = false }}{{- end -}} - {{- end -}} -{{ end }} -{{ $hasPosArgs := and .Request (or $isAllRequiredFieldsPrimitive (eq .PascalName "RunNow")) -}} +{{ $hasPosArgs := and .Request (or .Request.IsAllRequiredFieldsPrimitive (eq .PascalName "RunNow")) -}} {{- $hasSinglePosArg := and $hasPosArgs (eq 1 (len .Request.RequiredFields)) -}} {{- $serviceHasNamedIdMap := and .Service.List .Service.List.NamedIdMap -}} {{- $hasIdPrompt := and $hasSinglePosArg $serviceHasNamedIdMap -}} {{- $wait := and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) -}} -{{- $canAcceptJson := and .Request (not .Request.IsOnlyPrimitiveFields) -}} -{{- $canAcceptJsonAndIdPrompts := and $hasIdPrompt $canAcceptJson -}} {{- $hasRequiredArgs := and (not $hasIdPrompt) $hasPosArgs -}} var {{.CamelName}}Cmd = &cobra.Command{ - Use: "{{.KebabName}}{{if $hasPosArgs}}{{range .Request.RequiredFields}} {{if $hasIdPrompt}}[{{end}}{{.ConstantName}}{{if $hasIdPrompt}}]{{end}}{{end}}{{end}}", + Use: "{{.KebabName}}{{if $hasPosArgs}}{{range .Request.RequiredFields}} {{.ConstantName}}{{end}}{{end}}", {{if .Description -}} Short: `{{.Summary | without "`"}}`, Long: `{{.Comment " " 80 | without "`"}}`, {{end}} Annotations: map[string]string{},{{if $hasRequiredArgs }} - Args: cobra.ExactArgs({{len .Request.RequiredFields}}),{{end}} + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs({{len .Request.RequiredFields}}) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + },{{end}} PreRunE: root.Must{{if .Service.IsAccounts}}Account{{else}}Workspace{{end}}Client, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() {{if .Service.IsAccounts}}a := root.AccountClient(ctx){{else}}w := root.WorkspaceClient(ctx){{end}} - {{- if .Request -}} - {{if $canAcceptJsonAndIdPrompts}} - if cmd.Flags().Changed("json") { - {{- end -}} - {{if $canAcceptJson }} + {{- if .Request }} + if cmd.Flags().Changed("json") { err = {{.CamelName}}Json.Unmarshal(&{{.CamelName}}Req) if err != nil { return err } - {{- end -}} - {{if $canAcceptJsonAndIdPrompts}} - } else { - {{- end -}} - {{if $hasIdPrompt}} + } else { + {{- if $hasIdPrompt}} if len(args) == 0 { names, err := {{if .Service.IsAccounts}}a{{else}}w{{end}}.{{(.Service.TrimPrefix "account").PascalName}}.{{.Service.List.NamedIdMap.PascalName}}(ctx{{if .Service.List.Request}}, {{.Service.Package.Name}}.{{.Service.List.Request.PascalName}}{}{{end}}) if err != nil { @@ -122,9 +114,7 @@ var {{.CamelName}}Cmd = &cobra.Command{ }{{else -}} {{$method.CamelName}}Req.{{$field.PascalName}} = args[{{$arg}}] {{- end -}}{{end}} - {{if $canAcceptJsonAndIdPrompts}} - } - {{end}} + } {{end}} {{if $wait -}} if {{.CamelName}}SkipWait { diff --git a/cmd/account/billable-usage/billable-usage.go b/cmd/account/billable-usage/billable-usage.go index c701c7d37a..3dc4eef4c8 100755 --- a/cmd/account/billable-usage/billable-usage.go +++ b/cmd/account/billable-usage/billable-usage.go @@ -4,6 +4,7 @@ package billable_usage import ( "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/billing" "github.com/spf13/cobra" ) @@ -18,10 +19,12 @@ var Cmd = &cobra.Command{ // start download command var downloadReq billing.DownloadRequest +var downloadJson flags.JsonFlag func init() { Cmd.AddCommand(downloadCmd) // TODO: short flags + downloadCmd.Flags().Var(&downloadJson, "json", `either inline JSON string or @path/to/file.json with request body`) downloadCmd.Flags().BoolVar(&downloadReq.PersonalData, "personal-data", downloadReq.PersonalData, `Specify whether to include personally identifiable information in the billable usage logs, for example the email addresses of cluster creators.`) @@ -39,13 +42,26 @@ var downloadCmd = &cobra.Command{ [CSV file schema]: https://docs.databricks.com/administration-guide/account-settings/usage-analysis.html#schema`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - downloadReq.StartMonth = args[0] - downloadReq.EndMonth = args[1] + if cmd.Flags().Changed("json") { + err = downloadJson.Unmarshal(&downloadReq) + if err != nil { + return err + } + } else { + downloadReq.StartMonth = args[0] + downloadReq.EndMonth = args[1] + } err = a.BillableUsage.Download(ctx, downloadReq) if err != nil { diff --git a/cmd/account/budgets/budgets.go b/cmd/account/budgets/budgets.go index bf1fee4647..dedcdd802c 100755 --- a/cmd/account/budgets/budgets.go +++ b/cmd/account/budgets/budgets.go @@ -43,13 +43,16 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &createReq.Budget) - if err != nil { - return fmt.Errorf("invalid BUDGET: %s", args[0]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &createReq.Budget) + if err != nil { + return fmt.Errorf("invalid BUDGET: %s", args[0]) + } } response, err := a.Budgets.Create(ctx, createReq) @@ -63,15 +66,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq billing.DeleteBudgetRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [BUDGET_ID]", + Use: "delete BUDGET_ID", Short: `Delete budget.`, Long: `Delete budget. @@ -82,21 +87,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Budgets.BudgetWithStatusNameToBudgetIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Budget ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Budgets.BudgetWithStatusNameToBudgetIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Budget ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have budget id") + if len(args) != 1 { + return fmt.Errorf("expected to have budget id") + } + deleteReq.BudgetId = args[0] } - deleteReq.BudgetId = args[0] err = a.Budgets.Delete(ctx, deleteReq) if err != nil { @@ -109,15 +121,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq billing.GetBudgetRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [BUDGET_ID]", + Use: "get BUDGET_ID", Short: `Get budget and its status.`, Long: `Get budget and its status. @@ -129,21 +143,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Budgets.BudgetWithStatusNameToBudgetIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Budget ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Budgets.BudgetWithStatusNameToBudgetIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Budget ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have budget id") + if len(args) != 1 { + return fmt.Errorf("expected to have budget id") + } + getReq.BudgetId = args[0] } - getReq.BudgetId = args[0] response, err := a.Budgets.Get(ctx, getReq) if err != nil { @@ -206,15 +227,18 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &updateReq.Budget) - if err != nil { - return fmt.Errorf("invalid BUDGET: %s", args[0]) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &updateReq.Budget) + if err != nil { + return fmt.Errorf("invalid BUDGET: %s", args[0]) + } + updateReq.BudgetId = args[1] } - updateReq.BudgetId = args[1] err = a.Budgets.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/credentials/credentials.go b/cmd/account/credentials/credentials.go index 06c8e5a175..1f3f526143 100755 --- a/cmd/account/credentials/credentials.go +++ b/cmd/account/credentials/credentials.go @@ -59,14 +59,17 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - createReq.CredentialsName = args[0] - _, err = fmt.Sscan(args[1], &createReq.AwsCredentials) - if err != nil { - return fmt.Errorf("invalid AWS_CREDENTIALS: %s", args[1]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.CredentialsName = args[0] + _, err = fmt.Sscan(args[1], &createReq.AwsCredentials) + if err != nil { + return fmt.Errorf("invalid AWS_CREDENTIALS: %s", args[1]) + } } response, err := a.Credentials.Create(ctx, createReq) @@ -80,15 +83,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq provisioning.DeleteCredentialRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [CREDENTIALS_ID]", + Use: "delete CREDENTIALS_ID", Short: `Delete credential configuration.`, Long: `Delete credential configuration. @@ -101,21 +106,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Credentials.CredentialCredentialsNameToCredentialsIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks Account API credential configuration ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Credentials.CredentialCredentialsNameToCredentialsIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks Account API credential configuration ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks account api credential configuration id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks account api credential configuration id") + } + deleteReq.CredentialsId = args[0] } - deleteReq.CredentialsId = args[0] err = a.Credentials.Delete(ctx, deleteReq) if err != nil { @@ -128,15 +140,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq provisioning.GetCredentialRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [CREDENTIALS_ID]", + Use: "get CREDENTIALS_ID", Short: `Get credential configuration.`, Long: `Get credential configuration. @@ -148,21 +162,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Credentials.CredentialCredentialsNameToCredentialsIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks Account API credential configuration ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Credentials.CredentialCredentialsNameToCredentialsIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks Account API credential configuration ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks account api credential configuration id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks account api credential configuration id") + } + getReq.CredentialsId = args[0] } - getReq.CredentialsId = args[0] response, err := a.Credentials.Get(ctx, getReq) if err != nil { diff --git a/cmd/account/custom-app-integration/custom-app-integration.go b/cmd/account/custom-app-integration/custom-app-integration.go index a1e29092aa..953a7c124a 100755 --- a/cmd/account/custom-app-integration/custom-app-integration.go +++ b/cmd/account/custom-app-integration/custom-app-integration.go @@ -54,14 +54,17 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - createReq.Name = args[0] - _, err = fmt.Sscan(args[1], &createReq.RedirectUrls) - if err != nil { - return fmt.Errorf("invalid REDIRECT_URLS: %s", args[1]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + _, err = fmt.Sscan(args[1], &createReq.RedirectUrls) + if err != nil { + return fmt.Errorf("invalid REDIRECT_URLS: %s", args[1]) + } } response, err := a.CustomAppIntegration.Create(ctx, createReq) @@ -75,10 +78,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq oauth2.DeleteCustomAppIntegrationRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -91,12 +96,25 @@ var deleteCmd = &cobra.Command{ oauth app integration via :method:CustomAppIntegration/get.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteReq.IntegrationId = args[0] + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.IntegrationId = args[0] + } err = a.CustomAppIntegration.Delete(ctx, deleteReq) if err != nil { @@ -109,10 +127,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq oauth2.GetCustomAppIntegrationRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -124,12 +144,25 @@ var getCmd = &cobra.Command{ Gets the Custom OAuth App Integration for the given integration id.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.IntegrationId = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.IntegrationId = args[0] + } response, err := a.CustomAppIntegration.Get(ctx, getReq) if err != nil { @@ -191,16 +224,25 @@ var updateCmd = &cobra.Command{ oauth app integration via :method:CustomAppIntegration/get.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.IntegrationId = args[0] } - updateReq.IntegrationId = args[0] err = a.CustomAppIntegration.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/encryption-keys/encryption-keys.go b/cmd/account/encryption-keys/encryption-keys.go index 10888bac05..4cd9c71f8c 100755 --- a/cmd/account/encryption-keys/encryption-keys.go +++ b/cmd/account/encryption-keys/encryption-keys.go @@ -76,13 +76,16 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &createReq.UseCases) - if err != nil { - return fmt.Errorf("invalid USE_CASES: %s", args[0]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &createReq.UseCases) + if err != nil { + return fmt.Errorf("invalid USE_CASES: %s", args[0]) + } } response, err := a.EncryptionKeys.Create(ctx, createReq) @@ -96,10 +99,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq provisioning.DeleteEncryptionKeyRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -112,12 +117,25 @@ var deleteCmd = &cobra.Command{ delete a configuration that is associated with a running workspace.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteReq.CustomerManagedKeyId = args[0] + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.CustomerManagedKeyId = args[0] + } err = a.EncryptionKeys.Delete(ctx, deleteReq) if err != nil { @@ -130,10 +148,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq provisioning.GetEncryptionKeyRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -159,12 +179,25 @@ var getCmd = &cobra.Command{ platform.",`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.CustomerManagedKeyId = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.CustomerManagedKeyId = args[0] + } response, err := a.EncryptionKeys.Get(ctx, getReq) if err != nil { diff --git a/cmd/account/groups/groups.go b/cmd/account/groups/groups.go index 00666ae7af..f76711d008 100755 --- a/cmd/account/groups/groups.go +++ b/cmd/account/groups/groups.go @@ -53,13 +53,23 @@ var createCmd = &cobra.Command{ supplied group details.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := a.Groups.Create(ctx, createReq) @@ -73,15 +83,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq iam.DeleteAccountGroupRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [ID]", + Use: "delete ID", Short: `Delete a group.`, Long: `Delete a group. @@ -92,21 +104,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks account") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks account") + } + deleteReq.Id = args[0] } - deleteReq.Id = args[0] err = a.Groups.Delete(ctx, deleteReq) if err != nil { @@ -119,15 +138,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq iam.GetAccountGroupRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [ID]", + Use: "get ID", Short: `Get group details.`, Long: `Get group details. @@ -138,21 +159,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Groups.GroupDisplayNameToIdMap(ctx, iam.ListAccountGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks account") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks account") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks account") + } + getReq.Id = args[0] } - getReq.Id = args[0] response, err := a.Groups.Get(ctx, getReq) if err != nil { @@ -165,10 +193,12 @@ var getCmd = &cobra.Command{ // start list command var listReq iam.ListAccountGroupsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.Attributes, "attributes", listReq.Attributes, `Comma-separated list of attributes to return in response.`) listCmd.Flags().IntVar(&listReq.Count, "count", listReq.Count, `Desired number of results per page.`) @@ -188,10 +218,24 @@ var listCmd = &cobra.Command{ Gets all details of the groups associated with the Databricks account.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := a.Groups.ListAll(ctx, listReq) if err != nil { @@ -216,7 +260,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch [ID]", + Use: "patch ID", Short: `Update group details.`, Long: `Update group details. @@ -248,7 +292,6 @@ var patchCmd = &cobra.Command{ return fmt.Errorf("expected to have unique id for a group in the databricks account") } patchReq.Id = args[0] - } err = a.Groups.Patch(ctx, patchReq) @@ -280,7 +323,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [ID]", + Use: "update ID", Short: `Replace a group.`, Long: `Replace a group. @@ -312,7 +355,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have databricks group id") } updateReq.Id = args[0] - } err = a.Groups.Update(ctx, updateReq) diff --git a/cmd/account/ip-access-lists/ip-access-lists.go b/cmd/account/ip-access-lists/ip-access-lists.go index 6fbdf2801c..e525c57ea5 100755 --- a/cmd/account/ip-access-lists/ip-access-lists.go +++ b/cmd/account/ip-access-lists/ip-access-lists.go @@ -76,18 +76,21 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - createReq.Label = args[0] - _, err = fmt.Sscan(args[1], &createReq.ListType) - if err != nil { - return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) - } - _, err = fmt.Sscan(args[2], &createReq.IpAddresses) - if err != nil { - return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Label = args[0] + _, err = fmt.Sscan(args[1], &createReq.ListType) + if err != nil { + return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) + } + _, err = fmt.Sscan(args[2], &createReq.IpAddresses) + if err != nil { + return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) + } } response, err := a.IpAccessLists.Create(ctx, createReq) @@ -101,15 +104,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq settings.DeleteAccountIpAccessListRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [IP_ACCESS_LIST_ID]", + Use: "delete IP_ACCESS_LIST_ID", Short: `Delete access list.`, Long: `Delete access list. @@ -120,21 +125,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.IpAccessLists.IpAccessListInfoLabelToListIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID for the corresponding IP access list") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.IpAccessLists.IpAccessListInfoLabelToListIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding IP access list") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id for the corresponding ip access list") + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding ip access list") + } + deleteReq.IpAccessListId = args[0] } - deleteReq.IpAccessListId = args[0] err = a.IpAccessLists.Delete(ctx, deleteReq) if err != nil { @@ -147,15 +159,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq settings.GetAccountIpAccessListRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [IP_ACCESS_LIST_ID]", + Use: "get IP_ACCESS_LIST_ID", Short: `Get IP access list.`, Long: `Get IP access list. @@ -166,21 +180,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.IpAccessLists.IpAccessListInfoLabelToListIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID for the corresponding IP access list") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.IpAccessLists.IpAccessListInfoLabelToListIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding IP access list") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id for the corresponding ip access list") + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding ip access list") + } + getReq.IpAccessListId = args[0] } - getReq.IpAccessListId = args[0] response, err := a.IpAccessLists.Get(ctx, getReq) if err != nil { @@ -252,24 +273,27 @@ var replaceCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = replaceJson.Unmarshal(&replaceReq) - if err != nil { - return err - } - replaceReq.Label = args[0] - _, err = fmt.Sscan(args[1], &replaceReq.ListType) - if err != nil { - return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) - } - _, err = fmt.Sscan(args[2], &replaceReq.IpAddresses) - if err != nil { - return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) - } - _, err = fmt.Sscan(args[3], &replaceReq.Enabled) - if err != nil { - return fmt.Errorf("invalid ENABLED: %s", args[3]) + if cmd.Flags().Changed("json") { + err = replaceJson.Unmarshal(&replaceReq) + if err != nil { + return err + } + } else { + replaceReq.Label = args[0] + _, err = fmt.Sscan(args[1], &replaceReq.ListType) + if err != nil { + return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) + } + _, err = fmt.Sscan(args[2], &replaceReq.IpAddresses) + if err != nil { + return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) + } + _, err = fmt.Sscan(args[3], &replaceReq.Enabled) + if err != nil { + return fmt.Errorf("invalid ENABLED: %s", args[3]) + } + replaceReq.IpAccessListId = args[4] } - replaceReq.IpAccessListId = args[4] err = a.IpAccessLists.Replace(ctx, replaceReq) if err != nil { @@ -318,24 +342,27 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err - } - updateReq.Label = args[0] - _, err = fmt.Sscan(args[1], &updateReq.ListType) - if err != nil { - return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) - } - _, err = fmt.Sscan(args[2], &updateReq.IpAddresses) - if err != nil { - return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) - } - _, err = fmt.Sscan(args[3], &updateReq.Enabled) - if err != nil { - return fmt.Errorf("invalid ENABLED: %s", args[3]) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.Label = args[0] + _, err = fmt.Sscan(args[1], &updateReq.ListType) + if err != nil { + return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) + } + _, err = fmt.Sscan(args[2], &updateReq.IpAddresses) + if err != nil { + return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) + } + _, err = fmt.Sscan(args[3], &updateReq.Enabled) + if err != nil { + return fmt.Errorf("invalid ENABLED: %s", args[3]) + } + updateReq.IpAccessListId = args[4] } - updateReq.IpAccessListId = args[4] err = a.IpAccessLists.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/log-delivery/log-delivery.go b/cmd/account/log-delivery/log-delivery.go index f687599301..a38a661adf 100755 --- a/cmd/account/log-delivery/log-delivery.go +++ b/cmd/account/log-delivery/log-delivery.go @@ -124,13 +124,23 @@ var createCmd = &cobra.Command{ [Deliver and access billable usage logs]: https://docs.databricks.com/administration-guide/account-settings/billable-usage-delivery.html`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := a.LogDelivery.Create(ctx, createReq) @@ -144,15 +154,17 @@ var createCmd = &cobra.Command{ // start get command var getReq billing.GetLogDeliveryRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [LOG_DELIVERY_CONFIGURATION_ID]", + Use: "get LOG_DELIVERY_CONFIGURATION_ID", Short: `Get log delivery configuration.`, Long: `Get log delivery configuration. @@ -164,21 +176,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.LogDelivery.LogDeliveryConfigurationConfigNameToConfigIdMap(ctx, billing.ListLogDeliveryRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks log delivery configuration ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.LogDelivery.LogDeliveryConfigurationConfigNameToConfigIdMap(ctx, billing.ListLogDeliveryRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks log delivery configuration ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks log delivery configuration id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks log delivery configuration id") + } + getReq.LogDeliveryConfigurationId = args[0] } - getReq.LogDeliveryConfigurationId = args[0] response, err := a.LogDelivery.Get(ctx, getReq) if err != nil { @@ -191,10 +210,12 @@ var getCmd = &cobra.Command{ // start list command var listReq billing.ListLogDeliveryRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.CredentialsId, "credentials-id", listReq.CredentialsId, `Filter by credential configuration ID.`) listCmd.Flags().Var(&listReq.Status, "status", `Filter by status ENABLED or DISABLED.`) @@ -211,10 +232,24 @@ var listCmd = &cobra.Command{ specified by ID.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := a.LogDelivery.ListAll(ctx, listReq) if err != nil { @@ -227,10 +262,12 @@ var listCmd = &cobra.Command{ // start patch-status command var patchStatusReq billing.UpdateLogDeliveryConfigurationStatusRequest +var patchStatusJson flags.JsonFlag func init() { Cmd.AddCommand(patchStatusCmd) // TODO: short flags + patchStatusCmd.Flags().Var(&patchStatusJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -246,16 +283,29 @@ var patchStatusCmd = &cobra.Command{ [Create log delivery](#operation/create-log-delivery-config).`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - _, err = fmt.Sscan(args[0], &patchStatusReq.Status) - if err != nil { - return fmt.Errorf("invalid STATUS: %s", args[0]) + if cmd.Flags().Changed("json") { + err = patchStatusJson.Unmarshal(&patchStatusReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &patchStatusReq.Status) + if err != nil { + return fmt.Errorf("invalid STATUS: %s", args[0]) + } + patchStatusReq.LogDeliveryConfigurationId = args[1] } - patchStatusReq.LogDeliveryConfigurationId = args[1] err = a.LogDelivery.PatchStatus(ctx, patchStatusReq) if err != nil { diff --git a/cmd/account/metastore-assignments/metastore-assignments.go b/cmd/account/metastore-assignments/metastore-assignments.go index d0de947bc0..98846685d6 100755 --- a/cmd/account/metastore-assignments/metastore-assignments.go +++ b/cmd/account/metastore-assignments/metastore-assignments.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" ) @@ -20,10 +21,12 @@ var Cmd = &cobra.Command{ // start create command var createReq catalog.CreateMetastoreAssignment +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -35,16 +38,29 @@ var createCmd = &cobra.Command{ Creates an assignment to a metastore for a workspace`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - createReq.MetastoreId = args[0] - createReq.DefaultCatalogName = args[1] - _, err = fmt.Sscan(args[2], &createReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[2]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.MetastoreId = args[0] + createReq.DefaultCatalogName = args[1] + _, err = fmt.Sscan(args[2], &createReq.WorkspaceId) + if err != nil { + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[2]) + } } response, err := a.MetastoreAssignments.Create(ctx, createReq) @@ -58,10 +74,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteAccountMetastoreAssignmentRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -74,16 +92,29 @@ var deleteCmd = &cobra.Command{ metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - _, err = fmt.Sscan(args[0], &deleteReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &deleteReq.WorkspaceId) + if err != nil { + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + } + deleteReq.MetastoreId = args[1] } - deleteReq.MetastoreId = args[1] err = a.MetastoreAssignments.Delete(ctx, deleteReq) if err != nil { @@ -96,10 +127,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq catalog.GetAccountMetastoreAssignmentRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -114,14 +147,27 @@ var getCmd = &cobra.Command{ 404 returned.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - _, err = fmt.Sscan(args[0], &getReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &getReq.WorkspaceId) + if err != nil { + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + } } response, err := a.MetastoreAssignments.Get(ctx, getReq) @@ -135,10 +181,12 @@ var getCmd = &cobra.Command{ // start list command var listReq catalog.ListAccountMetastoreAssignmentsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -151,12 +199,25 @@ var listCmd = &cobra.Command{ metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - listReq.MetastoreId = args[0] + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + listReq.MetastoreId = args[0] + } response, err := a.MetastoreAssignments.List(ctx, listReq) if err != nil { @@ -169,10 +230,12 @@ var listCmd = &cobra.Command{ // start update command var updateReq catalog.UpdateMetastoreAssignment +var updateJson flags.JsonFlag func init() { Cmd.AddCommand(updateCmd) // TODO: short flags + updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateCmd.Flags().StringVar(&updateReq.DefaultCatalogName, "default-catalog-name", updateReq.DefaultCatalogName, `The name of the default catalog for the metastore.`) updateCmd.Flags().StringVar(&updateReq.MetastoreId, "metastore-id", updateReq.MetastoreId, `The unique ID of the metastore.`) @@ -188,16 +251,29 @@ var updateCmd = &cobra.Command{ default catalog may be updated`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - _, err = fmt.Sscan(args[0], &updateReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &updateReq.WorkspaceId) + if err != nil { + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + } + updateReq.MetastoreId = args[1] } - updateReq.MetastoreId = args[1] response, err := a.MetastoreAssignments.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/metastores/metastores.go b/cmd/account/metastores/metastores.go index 6ae9b5d4c9..20072f6c95 100755 --- a/cmd/account/metastores/metastores.go +++ b/cmd/account/metastores/metastores.go @@ -5,6 +5,7 @@ package metastores import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" ) @@ -19,10 +20,12 @@ var Cmd = &cobra.Command{ // start create command var createReq catalog.CreateMetastore +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().StringVar(&createReq.Region, "region", createReq.Region, `Cloud region which the metastore serves (e.g., us-west-2, westus).`) @@ -36,13 +39,26 @@ var createCmd = &cobra.Command{ Creates a Unity Catalog metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - createReq.Name = args[0] - createReq.StorageRoot = args[1] + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + createReq.StorageRoot = args[1] + } response, err := a.Metastores.Create(ctx, createReq) if err != nil { @@ -55,10 +71,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteAccountMetastoreRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -70,12 +88,25 @@ var deleteCmd = &cobra.Command{ Deletes a Unity Catalog metastore for an account, both specified by ID.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteReq.MetastoreId = args[0] + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.MetastoreId = args[0] + } err = a.Metastores.Delete(ctx, deleteReq) if err != nil { @@ -88,10 +119,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq catalog.GetAccountMetastoreRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -103,12 +136,25 @@ var getCmd = &cobra.Command{ Gets a Unity Catalog metastore from an account, both specified by ID.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.MetastoreId = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.MetastoreId = args[0] + } response, err := a.Metastores.Get(ctx, getReq) if err != nil { @@ -148,10 +194,12 @@ var listCmd = &cobra.Command{ // start update command var updateReq catalog.UpdateMetastore +var updateJson flags.JsonFlag func init() { Cmd.AddCommand(updateCmd) // TODO: short flags + updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateCmd.Flags().StringVar(&updateReq.DeltaSharingOrganizationName, "delta-sharing-organization-name", updateReq.DeltaSharingOrganizationName, `The organization name of a Delta Sharing entity, to be used in Databricks-to-Databricks Delta Sharing as the official name.`) updateCmd.Flags().Int64Var(&updateReq.DeltaSharingRecipientTokenLifetimeInSeconds, "delta-sharing-recipient-token-lifetime-in-seconds", updateReq.DeltaSharingRecipientTokenLifetimeInSeconds, `The lifetime of delta sharing recipient token in seconds.`) @@ -171,12 +219,25 @@ var updateCmd = &cobra.Command{ Updates an existing Unity Catalog metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - updateReq.MetastoreId = args[0] + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.MetastoreId = args[0] + } response, err := a.Metastores.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/networks/networks.go b/cmd/account/networks/networks.go index adc120a08a..04355e5a3a 100755 --- a/cmd/account/networks/networks.go +++ b/cmd/account/networks/networks.go @@ -38,7 +38,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create [NETWORK_NAME]", + Use: "create NETWORK_NAME", Short: `Create network configuration.`, Long: `Create network configuration. @@ -72,7 +72,6 @@ var createCmd = &cobra.Command{ return fmt.Errorf("expected to have the human-readable name of the network configuration") } createReq.NetworkName = args[0] - } response, err := a.Networks.Create(ctx, createReq) @@ -86,15 +85,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq provisioning.DeleteNetworkRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [NETWORK_ID]", + Use: "delete NETWORK_ID", Short: `Delete a network configuration.`, Long: `Delete a network configuration. @@ -110,21 +111,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Networks.NetworkNetworkNameToNetworkIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks Account API network configuration ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Networks.NetworkNetworkNameToNetworkIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks Account API network configuration ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks account api network configuration id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks account api network configuration id") + } + deleteReq.NetworkId = args[0] } - deleteReq.NetworkId = args[0] err = a.Networks.Delete(ctx, deleteReq) if err != nil { @@ -137,15 +145,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq provisioning.GetNetworkRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [NETWORK_ID]", + Use: "get NETWORK_ID", Short: `Get a network configuration.`, Long: `Get a network configuration. @@ -157,21 +167,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Networks.NetworkNetworkNameToNetworkIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks Account API network configuration ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Networks.NetworkNetworkNameToNetworkIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks Account API network configuration ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks account api network configuration id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks account api network configuration id") + } + getReq.NetworkId = args[0] } - getReq.NetworkId = args[0] response, err := a.Networks.Get(ctx, getReq) if err != nil { diff --git a/cmd/account/o-auth-enrollment/o-auth-enrollment.go b/cmd/account/o-auth-enrollment/o-auth-enrollment.go index 9a19b9f766..b790e2d7a1 100755 --- a/cmd/account/o-auth-enrollment/o-auth-enrollment.go +++ b/cmd/account/o-auth-enrollment/o-auth-enrollment.go @@ -5,6 +5,7 @@ package o_auth_enrollment import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/oauth2" "github.com/spf13/cobra" ) @@ -22,10 +23,12 @@ var Cmd = &cobra.Command{ // start create command var createReq oauth2.CreateOAuthEnrollment +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().BoolVar(&createReq.EnableAllPublishedApps, "enable-all-published-apps", createReq.EnableAllPublishedApps, `If true, enable OAuth for all the published applications in the account.`) @@ -47,10 +50,24 @@ var createCmd = &cobra.Command{ status via API :method:OAuthEnrollment/get.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + } err = a.OAuthEnrollment.Create(ctx, createReq) if err != nil { diff --git a/cmd/account/private-access/private-access.go b/cmd/account/private-access/private-access.go index e9696a086e..e7235f37c1 100755 --- a/cmd/account/private-access/private-access.go +++ b/cmd/account/private-access/private-access.go @@ -56,17 +56,26 @@ var createCmd = &cobra.Command{ [Databricks article about PrivateLink]: https://docs.databricks.com/administration-guide/cloud-configurations/aws/privatelink.html`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.PrivateAccessSettingsName = args[0] + createReq.Region = args[1] } - createReq.PrivateAccessSettingsName = args[0] - createReq.Region = args[1] response, err := a.PrivateAccess.Create(ctx, createReq) if err != nil { @@ -79,15 +88,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq provisioning.DeletePrivateAccesRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [PRIVATE_ACCESS_SETTINGS_ID]", + Use: "delete PRIVATE_ACCESS_SETTINGS_ID", Short: `Delete a private access settings object.`, Long: `Delete a private access settings object. @@ -105,21 +116,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.PrivateAccess.PrivateAccessSettingsPrivateAccessSettingsNameToPrivateAccessSettingsIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks Account API private access settings ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.PrivateAccess.PrivateAccessSettingsPrivateAccessSettingsNameToPrivateAccessSettingsIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks Account API private access settings ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks account api private access settings id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks account api private access settings id") + } + deleteReq.PrivateAccessSettingsId = args[0] } - deleteReq.PrivateAccessSettingsId = args[0] err = a.PrivateAccess.Delete(ctx, deleteReq) if err != nil { @@ -132,15 +150,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq provisioning.GetPrivateAccesRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [PRIVATE_ACCESS_SETTINGS_ID]", + Use: "get PRIVATE_ACCESS_SETTINGS_ID", Short: `Get a private access settings object.`, Long: `Get a private access settings object. @@ -158,21 +178,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.PrivateAccess.PrivateAccessSettingsPrivateAccessSettingsNameToPrivateAccessSettingsIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks Account API private access settings ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.PrivateAccess.PrivateAccessSettingsPrivateAccessSettingsNameToPrivateAccessSettingsIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks Account API private access settings ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks account api private access settings id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks account api private access settings id") + } + getReq.PrivateAccessSettingsId = args[0] } - getReq.PrivateAccessSettingsId = args[0] response, err := a.PrivateAccess.Get(ctx, getReq) if err != nil { @@ -254,18 +281,27 @@ var replaceCmd = &cobra.Command{ [Databricks article about PrivateLink]: https://docs.databricks.com/administration-guide/cloud-configurations/aws/privatelink.html`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - err = replaceJson.Unmarshal(&replaceReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = replaceJson.Unmarshal(&replaceReq) + if err != nil { + return err + } + } else { + replaceReq.PrivateAccessSettingsName = args[0] + replaceReq.Region = args[1] + replaceReq.PrivateAccessSettingsId = args[2] } - replaceReq.PrivateAccessSettingsName = args[0] - replaceReq.Region = args[1] - replaceReq.PrivateAccessSettingsId = args[2] err = a.PrivateAccess.Replace(ctx, replaceReq) if err != nil { diff --git a/cmd/account/published-app-integration/published-app-integration.go b/cmd/account/published-app-integration/published-app-integration.go index 94e39b4db2..cc5448005c 100755 --- a/cmd/account/published-app-integration/published-app-integration.go +++ b/cmd/account/published-app-integration/published-app-integration.go @@ -48,13 +48,23 @@ var createCmd = &cobra.Command{ :method:PublishedAppIntegration/get.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := a.PublishedAppIntegration.Create(ctx, createReq) @@ -68,10 +78,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq oauth2.DeletePublishedAppIntegrationRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -84,12 +96,25 @@ var deleteCmd = &cobra.Command{ published oauth app integration via :method:PublishedAppIntegration/get.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteReq.IntegrationId = args[0] + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.IntegrationId = args[0] + } err = a.PublishedAppIntegration.Delete(ctx, deleteReq) if err != nil { @@ -102,10 +127,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq oauth2.GetPublishedAppIntegrationRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -117,12 +144,25 @@ var getCmd = &cobra.Command{ Gets the Published OAuth App Integration for the given integration id.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.IntegrationId = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.IntegrationId = args[0] + } response, err := a.PublishedAppIntegration.Get(ctx, getReq) if err != nil { @@ -183,16 +223,25 @@ var updateCmd = &cobra.Command{ published oauth app integration via :method:PublishedAppIntegration/get.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.IntegrationId = args[0] } - updateReq.IntegrationId = args[0] err = a.PublishedAppIntegration.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/service-principal-secrets/service-principal-secrets.go b/cmd/account/service-principal-secrets/service-principal-secrets.go index cbc909e6fd..ef69afe094 100755 --- a/cmd/account/service-principal-secrets/service-principal-secrets.go +++ b/cmd/account/service-principal-secrets/service-principal-secrets.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/oauth2" "github.com/spf13/cobra" ) @@ -32,10 +33,12 @@ var Cmd = &cobra.Command{ // start create command var createReq oauth2.CreateServicePrincipalSecretRequest +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -47,14 +50,27 @@ var createCmd = &cobra.Command{ Create a secret for the given service principal.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - _, err = fmt.Sscan(args[0], &createReq.ServicePrincipalId) - if err != nil { - return fmt.Errorf("invalid SERVICE_PRINCIPAL_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &createReq.ServicePrincipalId) + if err != nil { + return fmt.Errorf("invalid SERVICE_PRINCIPAL_ID: %s", args[0]) + } } response, err := a.ServicePrincipalSecrets.Create(ctx, createReq) @@ -68,10 +84,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq oauth2.DeleteServicePrincipalSecretRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -83,16 +101,29 @@ var deleteCmd = &cobra.Command{ Delete a secret from the given service principal.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - _, err = fmt.Sscan(args[0], &deleteReq.ServicePrincipalId) - if err != nil { - return fmt.Errorf("invalid SERVICE_PRINCIPAL_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &deleteReq.ServicePrincipalId) + if err != nil { + return fmt.Errorf("invalid SERVICE_PRINCIPAL_ID: %s", args[0]) + } + deleteReq.SecretId = args[1] } - deleteReq.SecretId = args[1] err = a.ServicePrincipalSecrets.Delete(ctx, deleteReq) if err != nil { @@ -105,10 +136,12 @@ var deleteCmd = &cobra.Command{ // start list command var listReq oauth2.ListServicePrincipalSecretsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -122,14 +155,27 @@ var listCmd = &cobra.Command{ secret values.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - _, err = fmt.Sscan(args[0], &listReq.ServicePrincipalId) - if err != nil { - return fmt.Errorf("invalid SERVICE_PRINCIPAL_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &listReq.ServicePrincipalId) + if err != nil { + return fmt.Errorf("invalid SERVICE_PRINCIPAL_ID: %s", args[0]) + } } response, err := a.ServicePrincipalSecrets.ListAll(ctx, listReq) diff --git a/cmd/account/service-principals/service-principals.go b/cmd/account/service-principals/service-principals.go index 2f57f90adc..6432ed2d58 100755 --- a/cmd/account/service-principals/service-principals.go +++ b/cmd/account/service-principals/service-principals.go @@ -52,13 +52,23 @@ var createCmd = &cobra.Command{ Creates a new service principal in the Databricks account.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := a.ServicePrincipals.Create(ctx, createReq) @@ -72,15 +82,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq iam.DeleteAccountServicePrincipalRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [ID]", + Use: "delete ID", Short: `Delete a service principal.`, Long: `Delete a service principal. @@ -91,21 +103,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks account") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks account") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a service principal in the databricks account") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a service principal in the databricks account") + } + deleteReq.Id = args[0] } - deleteReq.Id = args[0] err = a.ServicePrincipals.Delete(ctx, deleteReq) if err != nil { @@ -118,15 +137,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq iam.GetAccountServicePrincipalRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [ID]", + Use: "get ID", Short: `Get service principal details.`, Long: `Get service principal details. @@ -138,21 +159,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks account") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListAccountServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks account") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a service principal in the databricks account") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a service principal in the databricks account") + } + getReq.Id = args[0] } - getReq.Id = args[0] response, err := a.ServicePrincipals.Get(ctx, getReq) if err != nil { @@ -165,10 +193,12 @@ var getCmd = &cobra.Command{ // start list command var listReq iam.ListAccountServicePrincipalsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.Attributes, "attributes", listReq.Attributes, `Comma-separated list of attributes to return in response.`) listCmd.Flags().IntVar(&listReq.Count, "count", listReq.Count, `Desired number of results per page.`) @@ -188,10 +218,24 @@ var listCmd = &cobra.Command{ Gets the set of service principals associated with a Databricks account.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := a.ServicePrincipals.ListAll(ctx, listReq) if err != nil { @@ -216,7 +260,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch [ID]", + Use: "patch ID", Short: `Update service principal details.`, Long: `Update service principal details. @@ -249,7 +293,6 @@ var patchCmd = &cobra.Command{ return fmt.Errorf("expected to have unique id for a service principal in the databricks account") } patchReq.Id = args[0] - } err = a.ServicePrincipals.Patch(ctx, patchReq) @@ -282,7 +325,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [ID]", + Use: "update ID", Short: `Replace service principal.`, Long: `Replace service principal. @@ -316,7 +359,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have databricks service principal id") } updateReq.Id = args[0] - } err = a.ServicePrincipals.Update(ctx, updateReq) diff --git a/cmd/account/storage-credentials/storage-credentials.go b/cmd/account/storage-credentials/storage-credentials.go index 94cc19fcbd..8c633e24fb 100755 --- a/cmd/account/storage-credentials/storage-credentials.go +++ b/cmd/account/storage-credentials/storage-credentials.go @@ -49,17 +49,26 @@ var createCmd = &cobra.Command{ **CREATE_STORAGE_CREDENTIAL** privilege on the metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + createReq.MetastoreId = args[1] } - createReq.Name = args[0] - createReq.MetastoreId = args[1] response, err := a.StorageCredentials.Create(ctx, createReq) if err != nil { @@ -72,10 +81,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteAccountStorageCredentialRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -88,13 +99,26 @@ var deleteCmd = &cobra.Command{ of the storage credential.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - deleteReq.MetastoreId = args[0] - deleteReq.Name = args[1] + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.MetastoreId = args[0] + deleteReq.Name = args[1] + } err = a.StorageCredentials.Delete(ctx, deleteReq) if err != nil { @@ -107,10 +131,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq catalog.GetAccountStorageCredentialRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -124,13 +150,26 @@ var getCmd = &cobra.Command{ the storage credential.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - getReq.MetastoreId = args[0] - getReq.Name = args[1] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.MetastoreId = args[0] + getReq.Name = args[1] + } response, err := a.StorageCredentials.Get(ctx, getReq) if err != nil { @@ -143,10 +182,12 @@ var getCmd = &cobra.Command{ // start list command var listReq catalog.ListAccountStorageCredentialsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -159,12 +200,25 @@ var listCmd = &cobra.Command{ metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - listReq.MetastoreId = args[0] + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + listReq.MetastoreId = args[0] + } response, err := a.StorageCredentials.List(ctx, listReq) if err != nil { @@ -206,17 +260,26 @@ var updateCmd = &cobra.Command{ credential can be changed.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.MetastoreId = args[0] + updateReq.Name = args[1] } - updateReq.MetastoreId = args[0] - updateReq.Name = args[1] response, err := a.StorageCredentials.Update(ctx, updateReq) if err != nil { diff --git a/cmd/account/storage/storage.go b/cmd/account/storage/storage.go index 1a6291d61f..a8512ffa33 100755 --- a/cmd/account/storage/storage.go +++ b/cmd/account/storage/storage.go @@ -56,14 +56,17 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - createReq.StorageConfigurationName = args[0] - _, err = fmt.Sscan(args[1], &createReq.RootBucketInfo) - if err != nil { - return fmt.Errorf("invalid ROOT_BUCKET_INFO: %s", args[1]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.StorageConfigurationName = args[0] + _, err = fmt.Sscan(args[1], &createReq.RootBucketInfo) + if err != nil { + return fmt.Errorf("invalid ROOT_BUCKET_INFO: %s", args[1]) + } } response, err := a.Storage.Create(ctx, createReq) @@ -77,15 +80,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq provisioning.DeleteStorageRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [STORAGE_CONFIGURATION_ID]", + Use: "delete STORAGE_CONFIGURATION_ID", Short: `Delete storage configuration.`, Long: `Delete storage configuration. @@ -97,21 +102,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Storage.StorageConfigurationStorageConfigurationNameToStorageConfigurationIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks Account API storage configuration ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Storage.StorageConfigurationStorageConfigurationNameToStorageConfigurationIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks Account API storage configuration ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks account api storage configuration id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks account api storage configuration id") + } + deleteReq.StorageConfigurationId = args[0] } - deleteReq.StorageConfigurationId = args[0] err = a.Storage.Delete(ctx, deleteReq) if err != nil { @@ -124,15 +136,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq provisioning.GetStorageRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [STORAGE_CONFIGURATION_ID]", + Use: "get STORAGE_CONFIGURATION_ID", Short: `Get storage configuration.`, Long: `Get storage configuration. @@ -143,21 +157,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Storage.StorageConfigurationStorageConfigurationNameToStorageConfigurationIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks Account API storage configuration ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Storage.StorageConfigurationStorageConfigurationNameToStorageConfigurationIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks Account API storage configuration ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks account api storage configuration id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks account api storage configuration id") + } + getReq.StorageConfigurationId = args[0] } - getReq.StorageConfigurationId = args[0] response, err := a.Storage.Get(ctx, getReq) if err != nil { diff --git a/cmd/account/users/users.go b/cmd/account/users/users.go index 72e9ba85d6..6ccb39f53e 100755 --- a/cmd/account/users/users.go +++ b/cmd/account/users/users.go @@ -60,13 +60,23 @@ var createCmd = &cobra.Command{ to the Databricks account.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := a.Users.Create(ctx, createReq) @@ -80,15 +90,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq iam.DeleteAccountUserRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [ID]", + Use: "delete ID", Short: `Delete a user.`, Long: `Delete a user. @@ -100,21 +112,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks account") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks account") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a user in the databricks account") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a user in the databricks account") + } + deleteReq.Id = args[0] } - deleteReq.Id = args[0] err = a.Users.Delete(ctx, deleteReq) if err != nil { @@ -127,15 +146,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq iam.GetAccountUserRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [ID]", + Use: "get ID", Short: `Get user details.`, Long: `Get user details. @@ -146,21 +167,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks account") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.Users.UserUserNameToIdMap(ctx, iam.ListAccountUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks account") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a user in the databricks account") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a user in the databricks account") + } + getReq.Id = args[0] } - getReq.Id = args[0] response, err := a.Users.Get(ctx, getReq) if err != nil { @@ -173,10 +201,12 @@ var getCmd = &cobra.Command{ // start list command var listReq iam.ListAccountUsersRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.Attributes, "attributes", listReq.Attributes, `Comma-separated list of attributes to return in response.`) listCmd.Flags().IntVar(&listReq.Count, "count", listReq.Count, `Desired number of results per page.`) @@ -196,10 +226,24 @@ var listCmd = &cobra.Command{ Gets details for all the users associated with a Databricks account.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := a.Users.ListAll(ctx, listReq) if err != nil { @@ -224,7 +268,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch [ID]", + Use: "patch ID", Short: `Update user details.`, Long: `Update user details. @@ -257,7 +301,6 @@ var patchCmd = &cobra.Command{ return fmt.Errorf("expected to have unique id for a user in the databricks account") } patchReq.Id = args[0] - } err = a.Users.Patch(ctx, patchReq) @@ -292,7 +335,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [ID]", + Use: "update ID", Short: `Replace a user.`, Long: `Replace a user. @@ -324,7 +367,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have databricks user id") } updateReq.Id = args[0] - } err = a.Users.Update(ctx, updateReq) diff --git a/cmd/account/vpc-endpoints/vpc-endpoints.go b/cmd/account/vpc-endpoints/vpc-endpoints.go index 5dd0b9bb36..0572c44c34 100755 --- a/cmd/account/vpc-endpoints/vpc-endpoints.go +++ b/cmd/account/vpc-endpoints/vpc-endpoints.go @@ -35,7 +35,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create [VPC_ENDPOINT_NAME]", + Use: "create VPC_ENDPOINT_NAME", Short: `Create VPC endpoint configuration.`, Long: `Create VPC endpoint configuration. @@ -79,7 +79,6 @@ var createCmd = &cobra.Command{ return fmt.Errorf("expected to have the human-readable name of the storage configuration") } createReq.VpcEndpointName = args[0] - } response, err := a.VpcEndpoints.Create(ctx, createReq) @@ -93,15 +92,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq provisioning.DeleteVpcEndpointRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [VPC_ENDPOINT_ID]", + Use: "delete VPC_ENDPOINT_ID", Short: `Delete VPC endpoint configuration.`, Long: `Delete VPC endpoint configuration. @@ -120,21 +121,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.VpcEndpoints.VpcEndpointVpcEndpointNameToVpcEndpointIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks VPC endpoint ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.VpcEndpoints.VpcEndpointVpcEndpointNameToVpcEndpointIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks VPC endpoint ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks vpc endpoint id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks vpc endpoint id") + } + deleteReq.VpcEndpointId = args[0] } - deleteReq.VpcEndpointId = args[0] err = a.VpcEndpoints.Delete(ctx, deleteReq) if err != nil { @@ -147,15 +155,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq provisioning.GetVpcEndpointRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [VPC_ENDPOINT_ID]", + Use: "get VPC_ENDPOINT_ID", Short: `Get a VPC endpoint configuration.`, Long: `Get a VPC endpoint configuration. @@ -170,21 +180,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.VpcEndpoints.VpcEndpointVpcEndpointNameToVpcEndpointIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Databricks VPC endpoint ID") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := a.VpcEndpoints.VpcEndpointVpcEndpointNameToVpcEndpointIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Databricks VPC endpoint ID") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have databricks vpc endpoint id") + if len(args) != 1 { + return fmt.Errorf("expected to have databricks vpc endpoint id") + } + getReq.VpcEndpointId = args[0] } - getReq.VpcEndpointId = args[0] response, err := a.VpcEndpoints.Get(ctx, getReq) if err != nil { diff --git a/cmd/account/workspace-assignment/workspace-assignment.go b/cmd/account/workspace-assignment/workspace-assignment.go index f1f1092961..f9a25badbc 100755 --- a/cmd/account/workspace-assignment/workspace-assignment.go +++ b/cmd/account/workspace-assignment/workspace-assignment.go @@ -22,10 +22,12 @@ var Cmd = &cobra.Command{ // start delete command var deleteReq iam.DeleteWorkspaceAssignmentRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -38,18 +40,31 @@ var deleteCmd = &cobra.Command{ for the specified principal.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - _, err = fmt.Sscan(args[0], &deleteReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) - } - _, err = fmt.Sscan(args[1], &deleteReq.PrincipalId) - if err != nil { - return fmt.Errorf("invalid PRINCIPAL_ID: %s", args[1]) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &deleteReq.WorkspaceId) + if err != nil { + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + } + _, err = fmt.Sscan(args[1], &deleteReq.PrincipalId) + if err != nil { + return fmt.Errorf("invalid PRINCIPAL_ID: %s", args[1]) + } } err = a.WorkspaceAssignment.Delete(ctx, deleteReq) @@ -63,10 +78,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq iam.GetWorkspaceAssignmentRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -78,14 +95,27 @@ var getCmd = &cobra.Command{ Get an array of workspace permissions for the specified account and workspace.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - _, err = fmt.Sscan(args[0], &getReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &getReq.WorkspaceId) + if err != nil { + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + } } response, err := a.WorkspaceAssignment.Get(ctx, getReq) @@ -99,10 +129,12 @@ var getCmd = &cobra.Command{ // start list command var listReq iam.ListWorkspaceAssignmentRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -115,14 +147,27 @@ var listCmd = &cobra.Command{ Databricks workspace.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - _, err = fmt.Sscan(args[0], &listReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &listReq.WorkspaceId) + if err != nil { + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + } } response, err := a.WorkspaceAssignment.ListAll(ctx, listReq) @@ -158,21 +203,24 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &updateReq.Permissions) - if err != nil { - return fmt.Errorf("invalid PERMISSIONS: %s", args[0]) - } - _, err = fmt.Sscan(args[1], &updateReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[1]) - } - _, err = fmt.Sscan(args[2], &updateReq.PrincipalId) - if err != nil { - return fmt.Errorf("invalid PRINCIPAL_ID: %s", args[2]) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &updateReq.Permissions) + if err != nil { + return fmt.Errorf("invalid PERMISSIONS: %s", args[0]) + } + _, err = fmt.Sscan(args[1], &updateReq.WorkspaceId) + if err != nil { + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[1]) + } + _, err = fmt.Sscan(args[2], &updateReq.PrincipalId) + if err != nil { + return fmt.Errorf("invalid PRINCIPAL_ID: %s", args[2]) + } } err = a.WorkspaceAssignment.Update(ctx, updateReq) diff --git a/cmd/account/workspaces/workspaces.go b/cmd/account/workspaces/workspaces.go index 79f13a919b..f611fa8d28 100755 --- a/cmd/account/workspaces/workspaces.go +++ b/cmd/account/workspaces/workspaces.go @@ -58,7 +58,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create [WORKSPACE_NAME]", + Use: "create WORKSPACE_NAME", Short: `Create a new workspace.`, Long: `Create a new workspace. @@ -98,7 +98,6 @@ var createCmd = &cobra.Command{ return fmt.Errorf("expected to have the workspace's human-readable name") } createReq.WorkspaceName = args[0] - } if createSkipWait { @@ -129,15 +128,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq provisioning.DeleteWorkspaceRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [WORKSPACE_ID]", + Use: "delete WORKSPACE_ID", Short: `Delete a workspace.`, Long: `Delete a workspace. @@ -155,23 +156,30 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Workspaces.WorkspaceWorkspaceNameToWorkspaceIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Workspace ID") + } else { + if len(args) == 0 { + names, err := a.Workspaces.WorkspaceWorkspaceNameToWorkspaceIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Workspace ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have workspace id") + } + _, err = fmt.Sscan(args[0], &deleteReq.WorkspaceId) if err != nil { - return err + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have workspace id") - } - _, err = fmt.Sscan(args[0], &deleteReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) } err = a.Workspaces.Delete(ctx, deleteReq) @@ -185,15 +193,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq provisioning.GetWorkspaceRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [WORKSPACE_ID]", + Use: "get WORKSPACE_ID", Short: `Get a workspace.`, Long: `Get a workspace. @@ -217,23 +227,30 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Workspaces.WorkspaceWorkspaceNameToWorkspaceIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Workspace ID") + } else { + if len(args) == 0 { + names, err := a.Workspaces.WorkspaceWorkspaceNameToWorkspaceIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Workspace ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have workspace id") + } + _, err = fmt.Sscan(args[0], &getReq.WorkspaceId) if err != nil { - return err + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have workspace id") - } - _, err = fmt.Sscan(args[0], &getReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) } response, err := a.Workspaces.Get(ctx, getReq) @@ -278,7 +295,7 @@ var listCmd = &cobra.Command{ // start update command var updateReq provisioning.UpdateWorkspaceRequest - +var updateJson flags.JsonFlag var updateSkipWait bool var updateTimeout time.Duration @@ -288,6 +305,7 @@ func init() { updateCmd.Flags().BoolVar(&updateSkipWait, "no-wait", updateSkipWait, `do not wait to reach RUNNING state`) updateCmd.Flags().DurationVar(&updateTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach RUNNING state`) // TODO: short flags + updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateCmd.Flags().StringVar(&updateReq.AwsRegion, "aws-region", updateReq.AwsRegion, `The AWS region of the workspace's data plane (for example, us-west-2).`) updateCmd.Flags().StringVar(&updateReq.CredentialsId, "credentials-id", updateReq.CredentialsId, `ID of the workspace's credential configuration object.`) @@ -299,7 +317,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [WORKSPACE_ID]", + Use: "update WORKSPACE_ID", Short: `Update workspace configuration.`, Long: `Update workspace configuration. @@ -420,23 +438,30 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - if len(args) == 0 { - names, err := a.Workspaces.WorkspaceWorkspaceNameToWorkspaceIdMap(ctx) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Workspace ID") + } else { + if len(args) == 0 { + names, err := a.Workspaces.WorkspaceWorkspaceNameToWorkspaceIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Workspace ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have workspace id") + } + _, err = fmt.Sscan(args[0], &updateReq.WorkspaceId) if err != nil { - return err + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have workspace id") - } - _, err = fmt.Sscan(args[0], &updateReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) } if updateSkipWait { diff --git a/cmd/workspace/alerts/alerts.go b/cmd/workspace/alerts/alerts.go index aaa46eb4bb..cd52408f18 100755 --- a/cmd/workspace/alerts/alerts.go +++ b/cmd/workspace/alerts/alerts.go @@ -51,16 +51,19 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - createReq.Name = args[0] - _, err = fmt.Sscan(args[1], &createReq.Options) - if err != nil { - return fmt.Errorf("invalid OPTIONS: %s", args[1]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + _, err = fmt.Sscan(args[1], &createReq.Options) + if err != nil { + return fmt.Errorf("invalid OPTIONS: %s", args[1]) + } + createReq.QueryId = args[2] } - createReq.QueryId = args[2] response, err := w.Alerts.Create(ctx, createReq) if err != nil { @@ -73,15 +76,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq sql.DeleteAlertRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [ALERT_ID]", + Use: "delete ALERT_ID", Short: `Delete an alert.`, Long: `Delete an alert. @@ -94,21 +99,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Alerts.AlertNameToIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Alerts.AlertNameToIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + deleteReq.AlertId = args[0] } - deleteReq.AlertId = args[0] err = w.Alerts.Delete(ctx, deleteReq) if err != nil { @@ -121,15 +133,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq sql.GetAlertRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [ALERT_ID]", + Use: "get ALERT_ID", Short: `Get an alert.`, Long: `Get an alert. @@ -140,21 +154,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Alerts.AlertNameToIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Alerts.AlertNameToIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + getReq.AlertId = args[0] } - getReq.AlertId = args[0] response, err := w.Alerts.Get(ctx, getReq) if err != nil { @@ -217,17 +238,20 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err - } - updateReq.Name = args[0] - _, err = fmt.Sscan(args[1], &updateReq.Options) - if err != nil { - return fmt.Errorf("invalid OPTIONS: %s", args[1]) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.Name = args[0] + _, err = fmt.Sscan(args[1], &updateReq.Options) + if err != nil { + return fmt.Errorf("invalid OPTIONS: %s", args[1]) + } + updateReq.QueryId = args[2] + updateReq.AlertId = args[3] } - updateReq.QueryId = args[2] - updateReq.AlertId = args[3] err = w.Alerts.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/catalogs/catalogs.go b/cmd/workspace/catalogs/catalogs.go index da36ba771c..4e8aa9d717 100755 --- a/cmd/workspace/catalogs/catalogs.go +++ b/cmd/workspace/catalogs/catalogs.go @@ -50,16 +50,25 @@ var createCmd = &cobra.Command{ metastore admin or has the **CREATE_CATALOG** privilege.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] } - createReq.Name = args[0] response, err := w.Catalogs.Create(ctx, createReq) if err != nil { @@ -72,10 +81,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteCatalogRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) deleteCmd.Flags().BoolVar(&deleteReq.Force, "force", deleteReq.Force, `Force deletion even if the catalog is not empty.`) @@ -90,12 +101,25 @@ var deleteCmd = &cobra.Command{ metastore admin or the owner of the catalog.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.Name = args[0] + } err = w.Catalogs.Delete(ctx, deleteReq) if err != nil { @@ -108,10 +132,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq catalog.GetCatalogRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -125,12 +151,25 @@ var getCmd = &cobra.Command{ privilege set for their account.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.Name = args[0] + } response, err := w.Catalogs.Get(ctx, getReq) if err != nil { @@ -199,16 +238,25 @@ var updateCmd = &cobra.Command{ of the catalog).`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.Name = args[0] } - updateReq.Name = args[0] response, err := w.Catalogs.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/cluster-policies/cluster-policies.go b/cmd/workspace/cluster-policies/cluster-policies.go index 9eceb08aa7..7fbdce9bbc 100755 --- a/cmd/workspace/cluster-policies/cluster-policies.go +++ b/cmd/workspace/cluster-policies/cluster-policies.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/compute" "github.com/spf13/cobra" ) @@ -43,10 +44,12 @@ var Cmd = &cobra.Command{ // start create command var createReq compute.CreatePolicy +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().StringVar(&createReq.Definition, "definition", createReq.Definition, `Policy definition document expressed in Databricks Cluster Policy Definition Language.`) createCmd.Flags().StringVar(&createReq.Description, "description", createReq.Description, `Additional human-readable description of the cluster policy.`) @@ -57,7 +60,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create [NAME]", + Use: "create NAME", Short: `Create a new policy.`, Long: `Create a new policy. @@ -68,21 +71,28 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.ClusterPolicies.PolicyNameToPolicyIdMap(ctx, compute.ListClusterPoliciesRequest{}) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Cluster Policy name requested by the user") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.ClusterPolicies.PolicyNameToPolicyIdMap(ctx, compute.ListClusterPoliciesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Cluster Policy name requested by the user") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have cluster policy name requested by the user") + if len(args) != 1 { + return fmt.Errorf("expected to have cluster policy name requested by the user") + } + createReq.Name = args[0] } - createReq.Name = args[0] response, err := w.ClusterPolicies.Create(ctx, createReq) if err != nil { @@ -95,15 +105,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq compute.DeletePolicy +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [POLICY_ID]", + Use: "delete POLICY_ID", Short: `Delete a cluster policy.`, Long: `Delete a cluster policy. @@ -115,21 +127,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.ClusterPolicies.PolicyNameToPolicyIdMap(ctx, compute.ListClusterPoliciesRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID of the policy to delete") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.ClusterPolicies.PolicyNameToPolicyIdMap(ctx, compute.ListClusterPoliciesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID of the policy to delete") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id of the policy to delete") + if len(args) != 1 { + return fmt.Errorf("expected to have the id of the policy to delete") + } + deleteReq.PolicyId = args[0] } - deleteReq.PolicyId = args[0] err = w.ClusterPolicies.Delete(ctx, deleteReq) if err != nil { @@ -142,10 +161,12 @@ var deleteCmd = &cobra.Command{ // start edit command var editReq compute.EditPolicy +var editJson flags.JsonFlag func init() { Cmd.AddCommand(editCmd) // TODO: short flags + editCmd.Flags().Var(&editJson, "json", `either inline JSON string or @path/to/file.json with request body`) editCmd.Flags().StringVar(&editReq.Definition, "definition", editReq.Definition, `Policy definition document expressed in Databricks Cluster Policy Definition Language.`) editCmd.Flags().StringVar(&editReq.Description, "description", editReq.Description, `Additional human-readable description of the cluster policy.`) @@ -164,13 +185,26 @@ var editCmd = &cobra.Command{ governed by the previous policy invalid.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - editReq.PolicyId = args[0] - editReq.Name = args[1] + if cmd.Flags().Changed("json") { + err = editJson.Unmarshal(&editReq) + if err != nil { + return err + } + } else { + editReq.PolicyId = args[0] + editReq.Name = args[1] + } err = w.ClusterPolicies.Edit(ctx, editReq) if err != nil { @@ -183,15 +217,17 @@ var editCmd = &cobra.Command{ // start get command var getReq compute.GetClusterPolicyRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [POLICY_ID]", + Use: "get POLICY_ID", Short: `Get entity.`, Long: `Get entity. @@ -202,21 +238,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.ClusterPolicies.PolicyNameToPolicyIdMap(ctx, compute.ListClusterPoliciesRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Canonical unique identifier for the cluster policy") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.ClusterPolicies.PolicyNameToPolicyIdMap(ctx, compute.ListClusterPoliciesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Canonical unique identifier for the cluster policy") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have canonical unique identifier for the cluster policy") + if len(args) != 1 { + return fmt.Errorf("expected to have canonical unique identifier for the cluster policy") + } + getReq.PolicyId = args[0] } - getReq.PolicyId = args[0] response, err := w.ClusterPolicies.Get(ctx, getReq) if err != nil { @@ -229,10 +272,12 @@ var getCmd = &cobra.Command{ // start list command var listReq compute.ListClusterPoliciesRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().Var(&listReq.SortColumn, "sort-column", `The cluster policy attribute to sort by.`) listCmd.Flags().Var(&listReq.SortOrder, "sort-order", `The order in which the policies get listed.`) @@ -247,10 +292,24 @@ var listCmd = &cobra.Command{ Returns a list of policies accessible by the requesting user.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.ClusterPolicies.ListAll(ctx, listReq) if err != nil { diff --git a/cmd/workspace/clusters/clusters.go b/cmd/workspace/clusters/clusters.go index ba9aa528f2..d609f573a7 100755 --- a/cmd/workspace/clusters/clusters.go +++ b/cmd/workspace/clusters/clusters.go @@ -49,10 +49,12 @@ var Cmd = &cobra.Command{ // start change-owner command var changeOwnerReq compute.ChangeClusterOwner +var changeOwnerJson flags.JsonFlag func init() { Cmd.AddCommand(changeOwnerCmd) // TODO: short flags + changeOwnerCmd.Flags().Var(&changeOwnerJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -65,13 +67,26 @@ var changeOwnerCmd = &cobra.Command{ operation.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - changeOwnerReq.ClusterId = args[0] - changeOwnerReq.OwnerUsername = args[1] + if cmd.Flags().Changed("json") { + err = changeOwnerJson.Unmarshal(&changeOwnerReq) + if err != nil { + return err + } + } else { + changeOwnerReq.ClusterId = args[0] + changeOwnerReq.OwnerUsername = args[1] + } err = w.Clusters.ChangeOwner(ctx, changeOwnerReq) if err != nil { @@ -124,7 +139,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create [SPARK_VERSION]", + Use: "create SPARK_VERSION", Short: `Create new cluster.`, Long: `Create new cluster. @@ -168,7 +183,6 @@ var createCmd = &cobra.Command{ return fmt.Errorf("expected to have the spark version of the cluster, e.g") } createReq.SparkVersion = args[0] - } if createSkipWait { @@ -199,7 +213,7 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq compute.DeleteCluster - +var deleteJson flags.JsonFlag var deleteSkipWait bool var deleteTimeout time.Duration @@ -209,11 +223,12 @@ func init() { deleteCmd.Flags().BoolVar(&deleteSkipWait, "no-wait", deleteSkipWait, `do not wait to reach TERMINATED state`) deleteCmd.Flags().DurationVar(&deleteTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach TERMINATED state`) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [CLUSTER_ID]", + Use: "delete CLUSTER_ID", Short: `Terminate cluster.`, Long: `Terminate cluster. @@ -227,21 +242,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The cluster to be terminated") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The cluster to be terminated") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the cluster to be terminated") + if len(args) != 1 { + return fmt.Errorf("expected to have the cluster to be terminated") + } + deleteReq.ClusterId = args[0] } - deleteReq.ClusterId = args[0] if deleteSkipWait { err = w.Clusters.Delete(ctx, deleteReq) @@ -329,17 +351,26 @@ var editCmd = &cobra.Command{ Clusters created by the Databricks Jobs service cannot be edited.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = editJson.Unmarshal(&editReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = editJson.Unmarshal(&editReq) + if err != nil { + return err + } + } else { + editReq.ClusterId = args[0] + editReq.SparkVersion = args[1] } - editReq.ClusterId = args[0] - editReq.SparkVersion = args[1] if editSkipWait { err = w.Clusters.Edit(ctx, editReq) @@ -386,7 +417,7 @@ func init() { } var eventsCmd = &cobra.Command{ - Use: "events [CLUSTER_ID]", + Use: "events CLUSTER_ID", Short: `List cluster activity events.`, Long: `List cluster activity events. @@ -420,7 +451,6 @@ var eventsCmd = &cobra.Command{ return fmt.Errorf("expected to have the id of the cluster to retrieve events about") } eventsReq.ClusterId = args[0] - } response, err := w.Clusters.EventsAll(ctx, eventsReq) @@ -434,7 +464,7 @@ var eventsCmd = &cobra.Command{ // start get command var getReq compute.GetClusterRequest - +var getJson flags.JsonFlag var getSkipWait bool var getTimeout time.Duration @@ -444,11 +474,12 @@ func init() { getCmd.Flags().BoolVar(&getSkipWait, "no-wait", getSkipWait, `do not wait to reach RUNNING state`) getCmd.Flags().DurationVar(&getTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach RUNNING state`) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [CLUSTER_ID]", + Use: "get CLUSTER_ID", Short: `Get cluster info.`, Long: `Get cluster info. @@ -460,21 +491,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The cluster about which to retrieve information") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The cluster about which to retrieve information") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the cluster about which to retrieve information") + if len(args) != 1 { + return fmt.Errorf("expected to have the cluster about which to retrieve information") + } + getReq.ClusterId = args[0] } - getReq.ClusterId = args[0] response, err := w.Clusters.Get(ctx, getReq) if err != nil { @@ -487,10 +525,12 @@ var getCmd = &cobra.Command{ // start list command var listReq compute.ListClustersRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.CanUseClient, "can-use-client", listReq.CanUseClient, `Filter clusters based on what type of client it can be used for.`) @@ -512,10 +552,24 @@ var listCmd = &cobra.Command{ terminated job clusters.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.Clusters.ListAll(ctx, listReq) if err != nil { @@ -584,15 +638,17 @@ var listZonesCmd = &cobra.Command{ // start permanent-delete command var permanentDeleteReq compute.PermanentDeleteCluster +var permanentDeleteJson flags.JsonFlag func init() { Cmd.AddCommand(permanentDeleteCmd) // TODO: short flags + permanentDeleteCmd.Flags().Var(&permanentDeleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var permanentDeleteCmd = &cobra.Command{ - Use: "permanent-delete [CLUSTER_ID]", + Use: "permanent-delete CLUSTER_ID", Short: `Permanently delete cluster.`, Long: `Permanently delete cluster. @@ -608,21 +664,28 @@ var permanentDeleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if cmd.Flags().Changed("json") { + err = permanentDeleteJson.Unmarshal(&permanentDeleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The cluster to be deleted") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The cluster to be deleted") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the cluster to be deleted") + if len(args) != 1 { + return fmt.Errorf("expected to have the cluster to be deleted") + } + permanentDeleteReq.ClusterId = args[0] } - permanentDeleteReq.ClusterId = args[0] err = w.Clusters.PermanentDelete(ctx, permanentDeleteReq) if err != nil { @@ -635,15 +698,17 @@ var permanentDeleteCmd = &cobra.Command{ // start pin command var pinReq compute.PinCluster +var pinJson flags.JsonFlag func init() { Cmd.AddCommand(pinCmd) // TODO: short flags + pinCmd.Flags().Var(&pinJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var pinCmd = &cobra.Command{ - Use: "pin [CLUSTER_ID]", + Use: "pin CLUSTER_ID", Short: `Pin cluster.`, Long: `Pin cluster. @@ -656,21 +721,28 @@ var pinCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if cmd.Flags().Changed("json") { + err = pinJson.Unmarshal(&pinReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + pinReq.ClusterId = args[0] } - pinReq.ClusterId = args[0] err = w.Clusters.Pin(ctx, pinReq) if err != nil { @@ -701,7 +773,7 @@ func init() { } var resizeCmd = &cobra.Command{ - Use: "resize [CLUSTER_ID]", + Use: "resize CLUSTER_ID", Short: `Resize cluster.`, Long: `Resize cluster. @@ -734,7 +806,6 @@ var resizeCmd = &cobra.Command{ return fmt.Errorf("expected to have the cluster to be resized") } resizeReq.ClusterId = args[0] - } if resizeSkipWait { @@ -765,7 +836,7 @@ var resizeCmd = &cobra.Command{ // start restart command var restartReq compute.RestartCluster - +var restartJson flags.JsonFlag var restartSkipWait bool var restartTimeout time.Duration @@ -775,13 +846,14 @@ func init() { restartCmd.Flags().BoolVar(&restartSkipWait, "no-wait", restartSkipWait, `do not wait to reach RUNNING state`) restartCmd.Flags().DurationVar(&restartTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach RUNNING state`) // TODO: short flags + restartCmd.Flags().Var(&restartJson, "json", `either inline JSON string or @path/to/file.json with request body`) restartCmd.Flags().StringVar(&restartReq.RestartUser, "restart-user", restartReq.RestartUser, `.`) } var restartCmd = &cobra.Command{ - Use: "restart [CLUSTER_ID]", + Use: "restart CLUSTER_ID", Short: `Restart cluster.`, Long: `Restart cluster. @@ -793,21 +865,28 @@ var restartCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if cmd.Flags().Changed("json") { + err = restartJson.Unmarshal(&restartReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The cluster to be started") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The cluster to be started") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the cluster to be started") + if len(args) != 1 { + return fmt.Errorf("expected to have the cluster to be started") + } + restartReq.ClusterId = args[0] } - restartReq.ClusterId = args[0] if restartSkipWait { err = w.Clusters.Restart(ctx, restartReq) @@ -865,7 +944,7 @@ var sparkVersionsCmd = &cobra.Command{ // start start command var startReq compute.StartCluster - +var startJson flags.JsonFlag var startSkipWait bool var startTimeout time.Duration @@ -875,11 +954,12 @@ func init() { startCmd.Flags().BoolVar(&startSkipWait, "no-wait", startSkipWait, `do not wait to reach RUNNING state`) startCmd.Flags().DurationVar(&startTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach RUNNING state`) // TODO: short flags + startCmd.Flags().Var(&startJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var startCmd = &cobra.Command{ - Use: "start [CLUSTER_ID]", + Use: "start CLUSTER_ID", Short: `Start terminated cluster.`, Long: `Start terminated cluster. @@ -897,21 +977,28 @@ var startCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if cmd.Flags().Changed("json") { + err = startJson.Unmarshal(&startReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The cluster to be started") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The cluster to be started") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the cluster to be started") + if len(args) != 1 { + return fmt.Errorf("expected to have the cluster to be started") + } + startReq.ClusterId = args[0] } - startReq.ClusterId = args[0] if startSkipWait { err = w.Clusters.Start(ctx, startReq) @@ -941,15 +1028,17 @@ var startCmd = &cobra.Command{ // start unpin command var unpinReq compute.UnpinCluster +var unpinJson flags.JsonFlag func init() { Cmd.AddCommand(unpinCmd) // TODO: short flags + unpinCmd.Flags().Var(&unpinJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var unpinCmd = &cobra.Command{ - Use: "unpin [CLUSTER_ID]", + Use: "unpin CLUSTER_ID", Short: `Unpin cluster.`, Long: `Unpin cluster. @@ -962,21 +1051,28 @@ var unpinCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if cmd.Flags().Changed("json") { + err = unpinJson.Unmarshal(&unpinReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Clusters.ClusterInfoClusterNameToClusterIdMap(ctx, compute.ListClustersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + unpinReq.ClusterId = args[0] } - unpinReq.ClusterId = args[0] err = w.Clusters.Unpin(ctx, unpinReq) if err != nil { diff --git a/cmd/workspace/dashboards/dashboards.go b/cmd/workspace/dashboards/dashboards.go index 9c948d0e50..b2f21d5742 100755 --- a/cmd/workspace/dashboards/dashboards.go +++ b/cmd/workspace/dashboards/dashboards.go @@ -46,13 +46,23 @@ var createCmd = &cobra.Command{ Long: `Create a dashboard object.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := w.Dashboards.Create(ctx, createReq) @@ -66,15 +76,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq sql.DeleteDashboardRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [DASHBOARD_ID]", + Use: "delete DASHBOARD_ID", Short: `Remove a dashboard.`, Long: `Remove a dashboard. @@ -86,21 +98,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Dashboards.DashboardNameToIdMap(ctx, sql.ListDashboardsRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Dashboards.DashboardNameToIdMap(ctx, sql.ListDashboardsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + deleteReq.DashboardId = args[0] } - deleteReq.DashboardId = args[0] err = w.Dashboards.Delete(ctx, deleteReq) if err != nil { @@ -113,15 +132,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq sql.GetDashboardRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [DASHBOARD_ID]", + Use: "get DASHBOARD_ID", Short: `Retrieve a definition.`, Long: `Retrieve a definition. @@ -133,21 +154,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Dashboards.DashboardNameToIdMap(ctx, sql.ListDashboardsRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Dashboards.DashboardNameToIdMap(ctx, sql.ListDashboardsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + getReq.DashboardId = args[0] } - getReq.DashboardId = args[0] response, err := w.Dashboards.Get(ctx, getReq) if err != nil { @@ -160,10 +188,12 @@ var getCmd = &cobra.Command{ // start list command var listReq sql.ListDashboardsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().Var(&listReq.Order, "order", `Name of dashboard attribute to order by.`) listCmd.Flags().IntVar(&listReq.Page, "page", listReq.Page, `Page number to retrieve.`) @@ -180,10 +210,24 @@ var listCmd = &cobra.Command{ Fetch a paginated list of dashboard objects.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.Dashboards.ListAll(ctx, listReq) if err != nil { @@ -196,15 +240,17 @@ var listCmd = &cobra.Command{ // start restore command var restoreReq sql.RestoreDashboardRequest +var restoreJson flags.JsonFlag func init() { Cmd.AddCommand(restoreCmd) // TODO: short flags + restoreCmd.Flags().Var(&restoreJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var restoreCmd = &cobra.Command{ - Use: "restore [DASHBOARD_ID]", + Use: "restore DASHBOARD_ID", Short: `Restore a dashboard.`, Long: `Restore a dashboard. @@ -215,21 +261,28 @@ var restoreCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Dashboards.DashboardNameToIdMap(ctx, sql.ListDashboardsRequest{}) + if cmd.Flags().Changed("json") { + err = restoreJson.Unmarshal(&restoreReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Dashboards.DashboardNameToIdMap(ctx, sql.ListDashboardsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + restoreReq.DashboardId = args[0] } - restoreReq.DashboardId = args[0] err = w.Dashboards.Restore(ctx, restoreReq) if err != nil { diff --git a/cmd/workspace/experiments/experiments.go b/cmd/workspace/experiments/experiments.go index c9d79b7a65..97b05c1337 100755 --- a/cmd/workspace/experiments/experiments.go +++ b/cmd/workspace/experiments/experiments.go @@ -54,16 +54,25 @@ var createExperimentCmd = &cobra.Command{ Throws RESOURCE_ALREADY_EXISTS if a experiment with the given name exists.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = createExperimentJson.Unmarshal(&createExperimentReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createExperimentJson.Unmarshal(&createExperimentReq) + if err != nil { + return err + } + } else { + createExperimentReq.Name = args[0] } - createExperimentReq.Name = args[0] response, err := w.Experiments.CreateExperiment(ctx, createExperimentReq) if err != nil { @@ -101,13 +110,23 @@ var createRunCmd = &cobra.Command{ execution.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createRunJson.Unmarshal(&createRunReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createRunJson.Unmarshal(&createRunReq) + if err != nil { + return err + } + } else { } response, err := w.Experiments.CreateRun(ctx, createRunReq) @@ -121,10 +140,12 @@ var createRunCmd = &cobra.Command{ // start delete-experiment command var deleteExperimentReq ml.DeleteExperiment +var deleteExperimentJson flags.JsonFlag func init() { Cmd.AddCommand(deleteExperimentCmd) // TODO: short flags + deleteExperimentCmd.Flags().Var(&deleteExperimentJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -138,12 +159,25 @@ var deleteExperimentCmd = &cobra.Command{ experiment are also deleted.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteExperimentReq.ExperimentId = args[0] + if cmd.Flags().Changed("json") { + err = deleteExperimentJson.Unmarshal(&deleteExperimentReq) + if err != nil { + return err + } + } else { + deleteExperimentReq.ExperimentId = args[0] + } err = w.Experiments.DeleteExperiment(ctx, deleteExperimentReq) if err != nil { @@ -156,10 +190,12 @@ var deleteExperimentCmd = &cobra.Command{ // start delete-run command var deleteRunReq ml.DeleteRun +var deleteRunJson flags.JsonFlag func init() { Cmd.AddCommand(deleteRunCmd) // TODO: short flags + deleteRunCmd.Flags().Var(&deleteRunJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -171,12 +207,25 @@ var deleteRunCmd = &cobra.Command{ Marks a run for deletion.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteRunReq.RunId = args[0] + if cmd.Flags().Changed("json") { + err = deleteRunJson.Unmarshal(&deleteRunReq) + if err != nil { + return err + } + } else { + deleteRunReq.RunId = args[0] + } err = w.Experiments.DeleteRun(ctx, deleteRunReq) if err != nil { @@ -189,10 +238,12 @@ var deleteRunCmd = &cobra.Command{ // start delete-tag command var deleteTagReq ml.DeleteTag +var deleteTagJson flags.JsonFlag func init() { Cmd.AddCommand(deleteTagCmd) // TODO: short flags + deleteTagCmd.Flags().Var(&deleteTagJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -205,13 +256,26 @@ var deleteTagCmd = &cobra.Command{ and after a run completes.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - deleteTagReq.RunId = args[0] - deleteTagReq.Key = args[1] + if cmd.Flags().Changed("json") { + err = deleteTagJson.Unmarshal(&deleteTagReq) + if err != nil { + return err + } + } else { + deleteTagReq.RunId = args[0] + deleteTagReq.Key = args[1] + } err = w.Experiments.DeleteTag(ctx, deleteTagReq) if err != nil { @@ -224,10 +288,12 @@ var deleteTagCmd = &cobra.Command{ // start get-by-name command var getByNameReq ml.GetByNameRequest +var getByNameJson flags.JsonFlag func init() { Cmd.AddCommand(getByNameCmd) // TODO: short flags + getByNameCmd.Flags().Var(&getByNameJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -247,12 +313,25 @@ var getByNameCmd = &cobra.Command{ exists.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getByNameReq.ExperimentName = args[0] + if cmd.Flags().Changed("json") { + err = getByNameJson.Unmarshal(&getByNameReq) + if err != nil { + return err + } + } else { + getByNameReq.ExperimentName = args[0] + } response, err := w.Experiments.GetByName(ctx, getByNameReq) if err != nil { @@ -265,10 +344,12 @@ var getByNameCmd = &cobra.Command{ // start get-experiment command var getExperimentReq ml.GetExperimentRequest +var getExperimentJson flags.JsonFlag func init() { Cmd.AddCommand(getExperimentCmd) // TODO: short flags + getExperimentCmd.Flags().Var(&getExperimentJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -280,12 +361,25 @@ var getExperimentCmd = &cobra.Command{ Gets metadata for an experiment. This method works on deleted experiments.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getExperimentReq.ExperimentId = args[0] + if cmd.Flags().Changed("json") { + err = getExperimentJson.Unmarshal(&getExperimentReq) + if err != nil { + return err + } + } else { + getExperimentReq.ExperimentId = args[0] + } response, err := w.Experiments.GetExperiment(ctx, getExperimentReq) if err != nil { @@ -298,10 +392,12 @@ var getExperimentCmd = &cobra.Command{ // start get-history command var getHistoryReq ml.GetHistoryRequest +var getHistoryJson flags.JsonFlag func init() { Cmd.AddCommand(getHistoryCmd) // TODO: short flags + getHistoryCmd.Flags().Var(&getHistoryJson, "json", `either inline JSON string or @path/to/file.json with request body`) getHistoryCmd.Flags().IntVar(&getHistoryReq.MaxResults, "max-results", getHistoryReq.MaxResults, `Maximum number of Metric records to return per paginated request.`) getHistoryCmd.Flags().StringVar(&getHistoryReq.PageToken, "page-token", getHistoryReq.PageToken, `Token indicating the page of metric histories to fetch.`) @@ -318,12 +414,25 @@ var getHistoryCmd = &cobra.Command{ Gets a list of all values for the specified metric for a given run.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getHistoryReq.MetricKey = args[0] + if cmd.Flags().Changed("json") { + err = getHistoryJson.Unmarshal(&getHistoryReq) + if err != nil { + return err + } + } else { + getHistoryReq.MetricKey = args[0] + } response, err := w.Experiments.GetHistory(ctx, getHistoryReq) if err != nil { @@ -336,10 +445,12 @@ var getHistoryCmd = &cobra.Command{ // start get-run command var getRunReq ml.GetRunRequest +var getRunJson flags.JsonFlag func init() { Cmd.AddCommand(getRunCmd) // TODO: short flags + getRunCmd.Flags().Var(&getRunJson, "json", `either inline JSON string or @path/to/file.json with request body`) getRunCmd.Flags().StringVar(&getRunReq.RunUuid, "run-uuid", getRunReq.RunUuid, `[Deprecated, use run_id instead] ID of the run to fetch.`) @@ -358,12 +469,25 @@ var getRunCmd = &cobra.Command{ these values.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getRunReq.RunId = args[0] + if cmd.Flags().Changed("json") { + err = getRunJson.Unmarshal(&getRunReq) + if err != nil { + return err + } + } else { + getRunReq.RunId = args[0] + } response, err := w.Experiments.GetRun(ctx, getRunReq) if err != nil { @@ -376,10 +500,12 @@ var getRunCmd = &cobra.Command{ // start list-artifacts command var listArtifactsReq ml.ListArtifactsRequest +var listArtifactsJson flags.JsonFlag func init() { Cmd.AddCommand(listArtifactsCmd) // TODO: short flags + listArtifactsCmd.Flags().Var(&listArtifactsJson, "json", `either inline JSON string or @path/to/file.json with request body`) listArtifactsCmd.Flags().StringVar(&listArtifactsReq.PageToken, "page-token", listArtifactsReq.PageToken, `Token indicating the page of artifact results to fetch.`) listArtifactsCmd.Flags().StringVar(&listArtifactsReq.Path, "path", listArtifactsReq.Path, `Filter artifacts matching this path (a relative path from the root artifact directory).`) @@ -397,10 +523,24 @@ var listArtifactsCmd = &cobra.Command{ specified, the response contains only artifacts with the specified prefix.",`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listArtifactsJson.Unmarshal(&listArtifactsReq) + if err != nil { + return err + } + } else { + } response, err := w.Experiments.ListArtifactsAll(ctx, listArtifactsReq) if err != nil { @@ -413,10 +553,12 @@ var listArtifactsCmd = &cobra.Command{ // start list-experiments command var listExperimentsReq ml.ListExperimentsRequest +var listExperimentsJson flags.JsonFlag func init() { Cmd.AddCommand(listExperimentsCmd) // TODO: short flags + listExperimentsCmd.Flags().Var(&listExperimentsJson, "json", `either inline JSON string or @path/to/file.json with request body`) listExperimentsCmd.Flags().IntVar(&listExperimentsReq.MaxResults, "max-results", listExperimentsReq.MaxResults, `Maximum number of experiments desired.`) listExperimentsCmd.Flags().StringVar(&listExperimentsReq.PageToken, "page-token", listExperimentsReq.PageToken, `Token indicating the page of experiments to fetch.`) @@ -432,10 +574,24 @@ var listExperimentsCmd = &cobra.Command{ Gets a list of all experiments.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listExperimentsJson.Unmarshal(&listExperimentsReq) + if err != nil { + return err + } + } else { + } response, err := w.Experiments.ListExperimentsAll(ctx, listExperimentsReq) if err != nil { @@ -506,13 +662,23 @@ var logBatchCmd = &cobra.Command{ * Parameter and tag values can be up to 250 characters in length`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = logBatchJson.Unmarshal(&logBatchReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = logBatchJson.Unmarshal(&logBatchReq) + if err != nil { + return err + } + } else { } err = w.Experiments.LogBatch(ctx, logBatchReq) @@ -547,13 +713,23 @@ var logInputsCmd = &cobra.Command{ without warning.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = logInputsJson.Unmarshal(&logInputsReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = logInputsJson.Unmarshal(&logInputsReq) + if err != nil { + return err + } + } else { } err = w.Experiments.LogInputs(ctx, logInputsReq) @@ -567,10 +743,12 @@ var logInputsCmd = &cobra.Command{ // start log-metric command var logMetricReq ml.LogMetric +var logMetricJson flags.JsonFlag func init() { Cmd.AddCommand(logMetricCmd) // TODO: short flags + logMetricCmd.Flags().Var(&logMetricJson, "json", `either inline JSON string or @path/to/file.json with request body`) logMetricCmd.Flags().StringVar(&logMetricReq.RunId, "run-id", logMetricReq.RunId, `ID of the run under which to log the metric.`) logMetricCmd.Flags().StringVar(&logMetricReq.RunUuid, "run-uuid", logMetricReq.RunUuid, `[Deprecated, use run_id instead] ID of the run under which to log the metric.`) @@ -588,19 +766,32 @@ var logMetricCmd = &cobra.Command{ represent ML model accuracy. A metric can be logged multiple times.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - logMetricReq.Key = args[0] - _, err = fmt.Sscan(args[1], &logMetricReq.Value) - if err != nil { - return fmt.Errorf("invalid VALUE: %s", args[1]) - } - _, err = fmt.Sscan(args[2], &logMetricReq.Timestamp) - if err != nil { - return fmt.Errorf("invalid TIMESTAMP: %s", args[2]) + if cmd.Flags().Changed("json") { + err = logMetricJson.Unmarshal(&logMetricReq) + if err != nil { + return err + } + } else { + logMetricReq.Key = args[0] + _, err = fmt.Sscan(args[1], &logMetricReq.Value) + if err != nil { + return fmt.Errorf("invalid VALUE: %s", args[1]) + } + _, err = fmt.Sscan(args[2], &logMetricReq.Timestamp) + if err != nil { + return fmt.Errorf("invalid TIMESTAMP: %s", args[2]) + } } err = w.Experiments.LogMetric(ctx, logMetricReq) @@ -614,10 +805,12 @@ var logMetricCmd = &cobra.Command{ // start log-model command var logModelReq ml.LogModel +var logModelJson flags.JsonFlag func init() { Cmd.AddCommand(logModelCmd) // TODO: short flags + logModelCmd.Flags().Var(&logModelJson, "json", `either inline JSON string or @path/to/file.json with request body`) logModelCmd.Flags().StringVar(&logModelReq.ModelJson, "model-json", logModelReq.ModelJson, `MLmodel file in json format.`) logModelCmd.Flags().StringVar(&logModelReq.RunId, "run-id", logModelReq.RunId, `ID of the run to log under.`) @@ -633,10 +826,24 @@ var logModelCmd = &cobra.Command{ without warning.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = logModelJson.Unmarshal(&logModelReq) + if err != nil { + return err + } + } else { + } err = w.Experiments.LogModel(ctx, logModelReq) if err != nil { @@ -649,10 +856,12 @@ var logModelCmd = &cobra.Command{ // start log-param command var logParamReq ml.LogParam +var logParamJson flags.JsonFlag func init() { Cmd.AddCommand(logParamCmd) // TODO: short flags + logParamCmd.Flags().Var(&logParamJson, "json", `either inline JSON string or @path/to/file.json with request body`) logParamCmd.Flags().StringVar(&logParamReq.RunId, "run-id", logParamReq.RunId, `ID of the run under which to log the param.`) logParamCmd.Flags().StringVar(&logParamReq.RunUuid, "run-uuid", logParamReq.RunUuid, `[Deprecated, use run_id instead] ID of the run under which to log the param.`) @@ -670,13 +879,26 @@ var logParamCmd = &cobra.Command{ once for a run.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - logParamReq.Key = args[0] - logParamReq.Value = args[1] + if cmd.Flags().Changed("json") { + err = logParamJson.Unmarshal(&logParamReq) + if err != nil { + return err + } + } else { + logParamReq.Key = args[0] + logParamReq.Value = args[1] + } err = w.Experiments.LogParam(ctx, logParamReq) if err != nil { @@ -689,10 +911,12 @@ var logParamCmd = &cobra.Command{ // start restore-experiment command var restoreExperimentReq ml.RestoreExperiment +var restoreExperimentJson flags.JsonFlag func init() { Cmd.AddCommand(restoreExperimentCmd) // TODO: short flags + restoreExperimentCmd.Flags().Var(&restoreExperimentJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -709,12 +933,25 @@ var restoreExperimentCmd = &cobra.Command{ permanently deleted.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - restoreExperimentReq.ExperimentId = args[0] + if cmd.Flags().Changed("json") { + err = restoreExperimentJson.Unmarshal(&restoreExperimentReq) + if err != nil { + return err + } + } else { + restoreExperimentReq.ExperimentId = args[0] + } err = w.Experiments.RestoreExperiment(ctx, restoreExperimentReq) if err != nil { @@ -727,10 +964,12 @@ var restoreExperimentCmd = &cobra.Command{ // start restore-run command var restoreRunReq ml.RestoreRun +var restoreRunJson flags.JsonFlag func init() { Cmd.AddCommand(restoreRunCmd) // TODO: short flags + restoreRunCmd.Flags().Var(&restoreRunJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -742,12 +981,25 @@ var restoreRunCmd = &cobra.Command{ Restores a deleted run.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - restoreRunReq.RunId = args[0] + if cmd.Flags().Changed("json") { + err = restoreRunJson.Unmarshal(&restoreRunReq) + if err != nil { + return err + } + } else { + restoreRunReq.RunId = args[0] + } err = w.Experiments.RestoreRun(ctx, restoreRunReq) if err != nil { @@ -783,13 +1035,23 @@ var searchExperimentsCmd = &cobra.Command{ Searches for experiments that satisfy specified search criteria.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = searchExperimentsJson.Unmarshal(&searchExperimentsReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = searchExperimentsJson.Unmarshal(&searchExperimentsReq) + if err != nil { + return err + } + } else { } response, err := w.Experiments.SearchExperimentsAll(ctx, searchExperimentsReq) @@ -829,13 +1091,23 @@ var searchRunsCmd = &cobra.Command{ Search expressions can use mlflowMetric and mlflowParam keys.",`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = searchRunsJson.Unmarshal(&searchRunsReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = searchRunsJson.Unmarshal(&searchRunsReq) + if err != nil { + return err + } + } else { } response, err := w.Experiments.SearchRunsAll(ctx, searchRunsReq) @@ -849,10 +1121,12 @@ var searchRunsCmd = &cobra.Command{ // start set-experiment-tag command var setExperimentTagReq ml.SetExperimentTag +var setExperimentTagJson flags.JsonFlag func init() { Cmd.AddCommand(setExperimentTagCmd) // TODO: short flags + setExperimentTagCmd.Flags().Var(&setExperimentTagJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -864,14 +1138,27 @@ var setExperimentTagCmd = &cobra.Command{ Sets a tag on an experiment. Experiment tags are metadata that can be updated.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - setExperimentTagReq.ExperimentId = args[0] - setExperimentTagReq.Key = args[1] - setExperimentTagReq.Value = args[2] + if cmd.Flags().Changed("json") { + err = setExperimentTagJson.Unmarshal(&setExperimentTagReq) + if err != nil { + return err + } + } else { + setExperimentTagReq.ExperimentId = args[0] + setExperimentTagReq.Key = args[1] + setExperimentTagReq.Value = args[2] + } err = w.Experiments.SetExperimentTag(ctx, setExperimentTagReq) if err != nil { @@ -884,10 +1171,12 @@ var setExperimentTagCmd = &cobra.Command{ // start set-tag command var setTagReq ml.SetTag +var setTagJson flags.JsonFlag func init() { Cmd.AddCommand(setTagCmd) // TODO: short flags + setTagCmd.Flags().Var(&setTagJson, "json", `either inline JSON string or @path/to/file.json with request body`) setTagCmd.Flags().StringVar(&setTagReq.RunId, "run-id", setTagReq.RunId, `ID of the run under which to log the tag.`) setTagCmd.Flags().StringVar(&setTagReq.RunUuid, "run-uuid", setTagReq.RunUuid, `[Deprecated, use run_id instead] ID of the run under which to log the tag.`) @@ -903,13 +1192,26 @@ var setTagCmd = &cobra.Command{ and after a run completes.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - setTagReq.Key = args[0] - setTagReq.Value = args[1] + if cmd.Flags().Changed("json") { + err = setTagJson.Unmarshal(&setTagReq) + if err != nil { + return err + } + } else { + setTagReq.Key = args[0] + setTagReq.Value = args[1] + } err = w.Experiments.SetTag(ctx, setTagReq) if err != nil { @@ -922,10 +1224,12 @@ var setTagCmd = &cobra.Command{ // start update-experiment command var updateExperimentReq ml.UpdateExperiment +var updateExperimentJson flags.JsonFlag func init() { Cmd.AddCommand(updateExperimentCmd) // TODO: short flags + updateExperimentCmd.Flags().Var(&updateExperimentJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateExperimentCmd.Flags().StringVar(&updateExperimentReq.NewName, "new-name", updateExperimentReq.NewName, `If provided, the experiment's name is changed to the new name.`) @@ -939,12 +1243,25 @@ var updateExperimentCmd = &cobra.Command{ Updates experiment metadata.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - updateExperimentReq.ExperimentId = args[0] + if cmd.Flags().Changed("json") { + err = updateExperimentJson.Unmarshal(&updateExperimentReq) + if err != nil { + return err + } + } else { + updateExperimentReq.ExperimentId = args[0] + } err = w.Experiments.UpdateExperiment(ctx, updateExperimentReq) if err != nil { @@ -957,10 +1274,12 @@ var updateExperimentCmd = &cobra.Command{ // start update-run command var updateRunReq ml.UpdateRun +var updateRunJson flags.JsonFlag func init() { Cmd.AddCommand(updateRunCmd) // TODO: short flags + updateRunCmd.Flags().Var(&updateRunJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateRunCmd.Flags().Int64Var(&updateRunReq.EndTime, "end-time", updateRunReq.EndTime, `Unix timestamp in milliseconds of when the run ended.`) updateRunCmd.Flags().StringVar(&updateRunReq.RunId, "run-id", updateRunReq.RunId, `ID of the run to update.`) @@ -977,10 +1296,24 @@ var updateRunCmd = &cobra.Command{ Updates run metadata.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = updateRunJson.Unmarshal(&updateRunReq) + if err != nil { + return err + } + } else { + } response, err := w.Experiments.UpdateRun(ctx, updateRunReq) if err != nil { diff --git a/cmd/workspace/external-locations/external-locations.go b/cmd/workspace/external-locations/external-locations.go index cf409966dc..947460af24 100755 --- a/cmd/workspace/external-locations/external-locations.go +++ b/cmd/workspace/external-locations/external-locations.go @@ -5,6 +5,7 @@ package external_locations import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" ) @@ -30,10 +31,12 @@ var Cmd = &cobra.Command{ // start create command var createReq catalog.CreateExternalLocation +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().StringVar(&createReq.Comment, "comment", createReq.Comment, `User-provided free-form text description.`) createCmd.Flags().BoolVar(&createReq.ReadOnly, "read-only", createReq.ReadOnly, `Indicates whether the external location is read-only.`) @@ -51,14 +54,27 @@ var createCmd = &cobra.Command{ metastore and the associated storage credential.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - createReq.Name = args[0] - createReq.Url = args[1] - createReq.CredentialName = args[2] + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + createReq.Url = args[1] + createReq.CredentialName = args[2] + } response, err := w.ExternalLocations.Create(ctx, createReq) if err != nil { @@ -71,10 +87,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteExternalLocationRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) deleteCmd.Flags().BoolVar(&deleteReq.Force, "force", deleteReq.Force, `Force deletion even if there are dependent external tables or mounts.`) @@ -89,12 +107,25 @@ var deleteCmd = &cobra.Command{ the owner of the external location.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.Name = args[0] + } err = w.ExternalLocations.Delete(ctx, deleteReq) if err != nil { @@ -107,10 +138,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq catalog.GetExternalLocationRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -124,12 +157,25 @@ var getCmd = &cobra.Command{ privilege on the external location.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.Name = args[0] + } response, err := w.ExternalLocations.Get(ctx, getReq) if err != nil { @@ -172,10 +218,12 @@ var listCmd = &cobra.Command{ // start update command var updateReq catalog.UpdateExternalLocation +var updateJson flags.JsonFlag func init() { Cmd.AddCommand(updateCmd) // TODO: short flags + updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateCmd.Flags().StringVar(&updateReq.Comment, "comment", updateReq.Comment, `User-provided free-form text description.`) updateCmd.Flags().StringVar(&updateReq.CredentialName, "credential-name", updateReq.CredentialName, `Name of the storage credential used with this location.`) @@ -197,12 +245,25 @@ var updateCmd = &cobra.Command{ can only update the name of the external location.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - updateReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.Name = args[0] + } response, err := w.ExternalLocations.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/functions/functions.go b/cmd/workspace/functions/functions.go index 71a2a08978..fb801f10f4 100755 --- a/cmd/workspace/functions/functions.go +++ b/cmd/workspace/functions/functions.go @@ -57,56 +57,59 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - createReq.Name = args[0] - createReq.CatalogName = args[1] - createReq.SchemaName = args[2] - _, err = fmt.Sscan(args[3], &createReq.InputParams) - if err != nil { - return fmt.Errorf("invalid INPUT_PARAMS: %s", args[3]) - } - _, err = fmt.Sscan(args[4], &createReq.DataType) - if err != nil { - return fmt.Errorf("invalid DATA_TYPE: %s", args[4]) - } - createReq.FullDataType = args[5] - _, err = fmt.Sscan(args[6], &createReq.ReturnParams) - if err != nil { - return fmt.Errorf("invalid RETURN_PARAMS: %s", args[6]) - } - _, err = fmt.Sscan(args[7], &createReq.RoutineBody) - if err != nil { - return fmt.Errorf("invalid ROUTINE_BODY: %s", args[7]) - } - createReq.RoutineDefinition = args[8] - _, err = fmt.Sscan(args[9], &createReq.RoutineDependencies) - if err != nil { - return fmt.Errorf("invalid ROUTINE_DEPENDENCIES: %s", args[9]) - } - _, err = fmt.Sscan(args[10], &createReq.ParameterStyle) - if err != nil { - return fmt.Errorf("invalid PARAMETER_STYLE: %s", args[10]) - } - _, err = fmt.Sscan(args[11], &createReq.IsDeterministic) - if err != nil { - return fmt.Errorf("invalid IS_DETERMINISTIC: %s", args[11]) - } - _, err = fmt.Sscan(args[12], &createReq.SqlDataAccess) - if err != nil { - return fmt.Errorf("invalid SQL_DATA_ACCESS: %s", args[12]) - } - _, err = fmt.Sscan(args[13], &createReq.IsNullCall) - if err != nil { - return fmt.Errorf("invalid IS_NULL_CALL: %s", args[13]) - } - _, err = fmt.Sscan(args[14], &createReq.SecurityType) - if err != nil { - return fmt.Errorf("invalid SECURITY_TYPE: %s", args[14]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + createReq.CatalogName = args[1] + createReq.SchemaName = args[2] + _, err = fmt.Sscan(args[3], &createReq.InputParams) + if err != nil { + return fmt.Errorf("invalid INPUT_PARAMS: %s", args[3]) + } + _, err = fmt.Sscan(args[4], &createReq.DataType) + if err != nil { + return fmt.Errorf("invalid DATA_TYPE: %s", args[4]) + } + createReq.FullDataType = args[5] + _, err = fmt.Sscan(args[6], &createReq.ReturnParams) + if err != nil { + return fmt.Errorf("invalid RETURN_PARAMS: %s", args[6]) + } + _, err = fmt.Sscan(args[7], &createReq.RoutineBody) + if err != nil { + return fmt.Errorf("invalid ROUTINE_BODY: %s", args[7]) + } + createReq.RoutineDefinition = args[8] + _, err = fmt.Sscan(args[9], &createReq.RoutineDependencies) + if err != nil { + return fmt.Errorf("invalid ROUTINE_DEPENDENCIES: %s", args[9]) + } + _, err = fmt.Sscan(args[10], &createReq.ParameterStyle) + if err != nil { + return fmt.Errorf("invalid PARAMETER_STYLE: %s", args[10]) + } + _, err = fmt.Sscan(args[11], &createReq.IsDeterministic) + if err != nil { + return fmt.Errorf("invalid IS_DETERMINISTIC: %s", args[11]) + } + _, err = fmt.Sscan(args[12], &createReq.SqlDataAccess) + if err != nil { + return fmt.Errorf("invalid SQL_DATA_ACCESS: %s", args[12]) + } + _, err = fmt.Sscan(args[13], &createReq.IsNullCall) + if err != nil { + return fmt.Errorf("invalid IS_NULL_CALL: %s", args[13]) + } + _, err = fmt.Sscan(args[14], &createReq.SecurityType) + if err != nil { + return fmt.Errorf("invalid SECURITY_TYPE: %s", args[14]) + } + createReq.SpecificName = args[15] } - createReq.SpecificName = args[15] response, err := w.Functions.Create(ctx, createReq) if err != nil { @@ -119,10 +122,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteFunctionRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) deleteCmd.Flags().BoolVar(&deleteReq.Force, "force", deleteReq.Force, `Force deletion even if the function is notempty.`) @@ -141,12 +146,25 @@ var deleteCmd = &cobra.Command{ its parent catalog and the **USE_SCHEMA** privilege on its parent schema`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.Name = args[0] + } err = w.Functions.Delete(ctx, deleteReq) if err != nil { @@ -159,10 +177,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq catalog.GetFunctionRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -180,12 +200,25 @@ var getCmd = &cobra.Command{ **EXECUTE** privilege on the function itself`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.Name = args[0] + } response, err := w.Functions.Get(ctx, getReq) if err != nil { @@ -198,10 +231,12 @@ var getCmd = &cobra.Command{ // start list command var listReq catalog.ListFunctionsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -219,13 +254,26 @@ var listCmd = &cobra.Command{ the array.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - listReq.CatalogName = args[0] - listReq.SchemaName = args[1] + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + listReq.CatalogName = args[0] + listReq.SchemaName = args[1] + } response, err := w.Functions.List(ctx, listReq) if err != nil { @@ -238,10 +286,12 @@ var listCmd = &cobra.Command{ // start update command var updateReq catalog.UpdateFunction +var updateJson flags.JsonFlag func init() { Cmd.AddCommand(updateCmd) // TODO: short flags + updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateCmd.Flags().StringVar(&updateReq.Owner, "owner", updateReq.Owner, `Username of current owner of function.`) @@ -262,12 +312,25 @@ var updateCmd = &cobra.Command{ function's parent schema.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - updateReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.Name = args[0] + } response, err := w.Functions.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/git-credentials/git-credentials.go b/cmd/workspace/git-credentials/git-credentials.go index 87af76d58d..3cad28471d 100755 --- a/cmd/workspace/git-credentials/git-credentials.go +++ b/cmd/workspace/git-credentials/git-credentials.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/workspace" "github.com/spf13/cobra" ) @@ -25,10 +26,12 @@ var Cmd = &cobra.Command{ // start create command var createReq workspace.CreateCredentials +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().StringVar(&createReq.GitUsername, "git-username", createReq.GitUsername, `Git username.`) createCmd.Flags().StringVar(&createReq.PersonalAccessToken, "personal-access-token", createReq.PersonalAccessToken, `The personal access token used to authenticate to the corresponding Git provider.`) @@ -36,7 +39,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create [GIT_PROVIDER]", + Use: "create GIT_PROVIDER", Short: `Create a credential entry.`, Long: `Create a credential entry. @@ -50,21 +53,28 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.GitCredentials.CredentialInfoGitProviderToCredentialIdMap(ctx) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Git provider") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.GitCredentials.CredentialInfoGitProviderToCredentialIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Git provider") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have git provider") + if len(args) != 1 { + return fmt.Errorf("expected to have git provider") + } + createReq.GitProvider = args[0] } - createReq.GitProvider = args[0] response, err := w.GitCredentials.Create(ctx, createReq) if err != nil { @@ -77,15 +87,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq workspace.DeleteGitCredentialRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [CREDENTIAL_ID]", + Use: "delete CREDENTIAL_ID", Short: `Delete a credential.`, Long: `Delete a credential. @@ -96,23 +108,30 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.GitCredentials.CredentialInfoGitProviderToCredentialIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID for the corresponding credential to access") + } else { + if len(args) == 0 { + names, err := w.GitCredentials.CredentialInfoGitProviderToCredentialIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding credential to access") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding credential to access") + } + _, err = fmt.Sscan(args[0], &deleteReq.CredentialId) if err != nil { - return err + return fmt.Errorf("invalid CREDENTIAL_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id for the corresponding credential to access") - } - _, err = fmt.Sscan(args[0], &deleteReq.CredentialId) - if err != nil { - return fmt.Errorf("invalid CREDENTIAL_ID: %s", args[0]) } err = w.GitCredentials.Delete(ctx, deleteReq) @@ -126,15 +145,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq workspace.GetGitCredentialRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [CREDENTIAL_ID]", + Use: "get CREDENTIAL_ID", Short: `Get a credential entry.`, Long: `Get a credential entry. @@ -145,23 +166,30 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.GitCredentials.CredentialInfoGitProviderToCredentialIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID for the corresponding credential to access") + } else { + if len(args) == 0 { + names, err := w.GitCredentials.CredentialInfoGitProviderToCredentialIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding credential to access") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding credential to access") + } + _, err = fmt.Sscan(args[0], &getReq.CredentialId) if err != nil { - return err + return fmt.Errorf("invalid CREDENTIAL_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id for the corresponding credential to access") - } - _, err = fmt.Sscan(args[0], &getReq.CredentialId) - if err != nil { - return fmt.Errorf("invalid CREDENTIAL_ID: %s", args[0]) } response, err := w.GitCredentials.Get(ctx, getReq) @@ -203,10 +231,12 @@ var listCmd = &cobra.Command{ // start update command var updateReq workspace.UpdateCredentials +var updateJson flags.JsonFlag func init() { Cmd.AddCommand(updateCmd) // TODO: short flags + updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateCmd.Flags().StringVar(&updateReq.GitProvider, "git-provider", updateReq.GitProvider, `Git provider.`) updateCmd.Flags().StringVar(&updateReq.GitUsername, "git-username", updateReq.GitUsername, `Git username.`) @@ -215,7 +245,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [CREDENTIAL_ID]", + Use: "update CREDENTIAL_ID", Short: `Update a credential.`, Long: `Update a credential. @@ -226,23 +256,30 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.GitCredentials.CredentialInfoGitProviderToCredentialIdMap(ctx) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID for the corresponding credential to access") + } else { + if len(args) == 0 { + names, err := w.GitCredentials.CredentialInfoGitProviderToCredentialIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding credential to access") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding credential to access") + } + _, err = fmt.Sscan(args[0], &updateReq.CredentialId) if err != nil { - return err + return fmt.Errorf("invalid CREDENTIAL_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id for the corresponding credential to access") - } - _, err = fmt.Sscan(args[0], &updateReq.CredentialId) - if err != nil { - return fmt.Errorf("invalid CREDENTIAL_ID: %s", args[0]) } err = w.GitCredentials.Update(ctx, updateReq) diff --git a/cmd/workspace/global-init-scripts/global-init-scripts.go b/cmd/workspace/global-init-scripts/global-init-scripts.go index f8d2753231..9df0596a08 100755 --- a/cmd/workspace/global-init-scripts/global-init-scripts.go +++ b/cmd/workspace/global-init-scripts/global-init-scripts.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/compute" "github.com/spf13/cobra" ) @@ -28,10 +29,12 @@ var Cmd = &cobra.Command{ // start create command var createReq compute.GlobalInitScriptCreateRequest +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().BoolVar(&createReq.Enabled, "enabled", createReq.Enabled, `Specifies whether the script is enabled.`) createCmd.Flags().IntVar(&createReq.Position, "position", createReq.Position, `The position of a global init script, where 0 represents the first script to run, 1 is the second script to run, in ascending order.`) @@ -46,13 +49,26 @@ var createCmd = &cobra.Command{ Creates a new global init script in this workspace.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - createReq.Name = args[0] - createReq.Script = args[1] + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + createReq.Script = args[1] + } response, err := w.GlobalInitScripts.Create(ctx, createReq) if err != nil { @@ -65,15 +81,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq compute.DeleteGlobalInitScriptRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [SCRIPT_ID]", + Use: "delete SCRIPT_ID", Short: `Delete init script.`, Long: `Delete init script. @@ -84,21 +102,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.GlobalInitScripts.GlobalInitScriptDetailsNameToScriptIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID of the global init script") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.GlobalInitScripts.GlobalInitScriptDetailsNameToScriptIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID of the global init script") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id of the global init script") + if len(args) != 1 { + return fmt.Errorf("expected to have the id of the global init script") + } + deleteReq.ScriptId = args[0] } - deleteReq.ScriptId = args[0] err = w.GlobalInitScripts.Delete(ctx, deleteReq) if err != nil { @@ -111,15 +136,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq compute.GetGlobalInitScriptRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [SCRIPT_ID]", + Use: "get SCRIPT_ID", Short: `Get an init script.`, Long: `Get an init script. @@ -130,21 +157,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.GlobalInitScripts.GlobalInitScriptDetailsNameToScriptIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID of the global init script") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.GlobalInitScripts.GlobalInitScriptDetailsNameToScriptIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID of the global init script") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id of the global init script") + if len(args) != 1 { + return fmt.Errorf("expected to have the id of the global init script") + } + getReq.ScriptId = args[0] } - getReq.ScriptId = args[0] response, err := w.GlobalInitScripts.Get(ctx, getReq) if err != nil { @@ -187,10 +221,12 @@ var listCmd = &cobra.Command{ // start update command var updateReq compute.GlobalInitScriptUpdateRequest +var updateJson flags.JsonFlag func init() { Cmd.AddCommand(updateCmd) // TODO: short flags + updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateCmd.Flags().BoolVar(&updateReq.Enabled, "enabled", updateReq.Enabled, `Specifies whether the script is enabled.`) updateCmd.Flags().IntVar(&updateReq.Position, "position", updateReq.Position, `The position of a script, where 0 represents the first script to run, 1 is the second script to run, in ascending order.`) @@ -206,14 +242,27 @@ var updateCmd = &cobra.Command{ are optional. Unspecified fields retain their current value.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - updateReq.Name = args[0] - updateReq.Script = args[1] - updateReq.ScriptId = args[2] + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.Name = args[0] + updateReq.Script = args[1] + updateReq.ScriptId = args[2] + } err = w.GlobalInitScripts.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/grants/grants.go b/cmd/workspace/grants/grants.go index 8ce474a5d5..c6fa81a771 100755 --- a/cmd/workspace/grants/grants.go +++ b/cmd/workspace/grants/grants.go @@ -31,10 +31,12 @@ var Cmd = &cobra.Command{ // start get command var getReq catalog.GetGrantRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) getCmd.Flags().StringVar(&getReq.Principal, "principal", getReq.Principal, `If provided, only the permissions for the specified principal (user or group) are returned.`) @@ -48,16 +50,29 @@ var getCmd = &cobra.Command{ Gets the permissions for a securable.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - _, err = fmt.Sscan(args[0], &getReq.SecurableType) - if err != nil { - return fmt.Errorf("invalid SECURABLE_TYPE: %s", args[0]) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &getReq.SecurableType) + if err != nil { + return fmt.Errorf("invalid SECURABLE_TYPE: %s", args[0]) + } + getReq.FullName = args[1] } - getReq.FullName = args[1] response, err := w.Grants.Get(ctx, getReq) if err != nil { @@ -70,10 +85,12 @@ var getCmd = &cobra.Command{ // start get-effective command var getEffectiveReq catalog.GetEffectiveRequest +var getEffectiveJson flags.JsonFlag func init() { Cmd.AddCommand(getEffectiveCmd) // TODO: short flags + getEffectiveCmd.Flags().Var(&getEffectiveJson, "json", `either inline JSON string or @path/to/file.json with request body`) getEffectiveCmd.Flags().StringVar(&getEffectiveReq.Principal, "principal", getEffectiveReq.Principal, `If provided, only the effective permissions for the specified principal (user or group) are returned.`) @@ -87,16 +104,29 @@ var getEffectiveCmd = &cobra.Command{ Gets the effective permissions for a securable.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - _, err = fmt.Sscan(args[0], &getEffectiveReq.SecurableType) - if err != nil { - return fmt.Errorf("invalid SECURABLE_TYPE: %s", args[0]) + if cmd.Flags().Changed("json") { + err = getEffectiveJson.Unmarshal(&getEffectiveReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &getEffectiveReq.SecurableType) + if err != nil { + return fmt.Errorf("invalid SECURABLE_TYPE: %s", args[0]) + } + getEffectiveReq.FullName = args[1] } - getEffectiveReq.FullName = args[1] response, err := w.Grants.GetEffective(ctx, getEffectiveReq) if err != nil { @@ -128,20 +158,29 @@ var updateCmd = &cobra.Command{ Updates the permissions for a securable.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &updateReq.SecurableType) - if err != nil { - return fmt.Errorf("invalid SECURABLE_TYPE: %s", args[0]) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &updateReq.SecurableType) + if err != nil { + return fmt.Errorf("invalid SECURABLE_TYPE: %s", args[0]) + } + updateReq.FullName = args[1] } - updateReq.FullName = args[1] response, err := w.Grants.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/groups/groups.go b/cmd/workspace/groups/groups.go index 7648598614..3bfc83dba3 100755 --- a/cmd/workspace/groups/groups.go +++ b/cmd/workspace/groups/groups.go @@ -53,13 +53,23 @@ var createCmd = &cobra.Command{ supplied group details.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := w.Groups.Create(ctx, createReq) @@ -73,15 +83,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq iam.DeleteGroupRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [ID]", + Use: "delete ID", Short: `Delete a group.`, Long: `Delete a group. @@ -92,21 +104,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks workspace") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks workspace") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks workspace") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks workspace") + } + deleteReq.Id = args[0] } - deleteReq.Id = args[0] err = w.Groups.Delete(ctx, deleteReq) if err != nil { @@ -119,15 +138,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq iam.GetGroupRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [ID]", + Use: "get ID", Short: `Get group details.`, Long: `Get group details. @@ -138,21 +159,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks workspace") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Groups.GroupDisplayNameToIdMap(ctx, iam.ListGroupsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a group in the Databricks workspace") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a group in the databricks workspace") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a group in the databricks workspace") + } + getReq.Id = args[0] } - getReq.Id = args[0] response, err := w.Groups.Get(ctx, getReq) if err != nil { @@ -165,10 +193,12 @@ var getCmd = &cobra.Command{ // start list command var listReq iam.ListGroupsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.Attributes, "attributes", listReq.Attributes, `Comma-separated list of attributes to return in response.`) listCmd.Flags().IntVar(&listReq.Count, "count", listReq.Count, `Desired number of results per page.`) @@ -188,10 +218,24 @@ var listCmd = &cobra.Command{ Gets all details of the groups associated with the Databricks workspace.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.Groups.ListAll(ctx, listReq) if err != nil { @@ -216,7 +260,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch [ID]", + Use: "patch ID", Short: `Update group details.`, Long: `Update group details. @@ -248,7 +292,6 @@ var patchCmd = &cobra.Command{ return fmt.Errorf("expected to have unique id for a group in the databricks workspace") } patchReq.Id = args[0] - } err = w.Groups.Patch(ctx, patchReq) @@ -280,7 +323,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [ID]", + Use: "update ID", Short: `Replace a group.`, Long: `Replace a group. @@ -312,7 +355,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have databricks group id") } updateReq.Id = args[0] - } err = w.Groups.Update(ctx, updateReq) diff --git a/cmd/workspace/instance-pools/instance-pools.go b/cmd/workspace/instance-pools/instance-pools.go index d1a81801be..7d39cfd805 100755 --- a/cmd/workspace/instance-pools/instance-pools.go +++ b/cmd/workspace/instance-pools/instance-pools.go @@ -67,17 +67,26 @@ var createCmd = &cobra.Command{ Creates a new instance pool using idle and ready-to-use cloud instances.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.InstancePoolName = args[0] + createReq.NodeTypeId = args[1] } - createReq.InstancePoolName = args[0] - createReq.NodeTypeId = args[1] response, err := w.InstancePools.Create(ctx, createReq) if err != nil { @@ -90,15 +99,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq compute.DeleteInstancePool +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [INSTANCE_POOL_ID]", + Use: "delete INSTANCE_POOL_ID", Short: `Delete an instance pool.`, Long: `Delete an instance pool. @@ -110,21 +121,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.InstancePools.InstancePoolAndStatsInstancePoolNameToInstancePoolIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The instance pool to be terminated") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.InstancePools.InstancePoolAndStatsInstancePoolNameToInstancePoolIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The instance pool to be terminated") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the instance pool to be terminated") + if len(args) != 1 { + return fmt.Errorf("expected to have the instance pool to be terminated") + } + deleteReq.InstancePoolId = args[0] } - deleteReq.InstancePoolId = args[0] err = w.InstancePools.Delete(ctx, deleteReq) if err != nil { @@ -166,18 +184,27 @@ var editCmd = &cobra.Command{ Modifies the configuration of an existing instance pool.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - err = editJson.Unmarshal(&editReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = editJson.Unmarshal(&editReq) + if err != nil { + return err + } + } else { + editReq.InstancePoolId = args[0] + editReq.InstancePoolName = args[1] + editReq.NodeTypeId = args[2] } - editReq.InstancePoolId = args[0] - editReq.InstancePoolName = args[1] - editReq.NodeTypeId = args[2] err = w.InstancePools.Edit(ctx, editReq) if err != nil { @@ -190,15 +217,17 @@ var editCmd = &cobra.Command{ // start get command var getReq compute.GetInstancePoolRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [INSTANCE_POOL_ID]", + Use: "get INSTANCE_POOL_ID", Short: `Get instance pool information.`, Long: `Get instance pool information. @@ -209,21 +238,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.InstancePools.InstancePoolAndStatsInstancePoolNameToInstancePoolIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The canonical unique identifier for the instance pool") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.InstancePools.InstancePoolAndStatsInstancePoolNameToInstancePoolIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The canonical unique identifier for the instance pool") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the canonical unique identifier for the instance pool") + if len(args) != 1 { + return fmt.Errorf("expected to have the canonical unique identifier for the instance pool") + } + getReq.InstancePoolId = args[0] } - getReq.InstancePoolId = args[0] response, err := w.InstancePools.Get(ctx, getReq) if err != nil { diff --git a/cmd/workspace/instance-profiles/instance-profiles.go b/cmd/workspace/instance-profiles/instance-profiles.go index b00bca09ff..c68b8f3d83 100755 --- a/cmd/workspace/instance-profiles/instance-profiles.go +++ b/cmd/workspace/instance-profiles/instance-profiles.go @@ -5,6 +5,7 @@ package instance_profiles import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/compute" "github.com/spf13/cobra" ) @@ -23,10 +24,12 @@ var Cmd = &cobra.Command{ // start add command var addReq compute.AddInstanceProfile +var addJson flags.JsonFlag func init() { Cmd.AddCommand(addCmd) // TODO: short flags + addCmd.Flags().Var(&addJson, "json", `either inline JSON string or @path/to/file.json with request body`) addCmd.Flags().StringVar(&addReq.IamRoleArn, "iam-role-arn", addReq.IamRoleArn, `The AWS IAM role ARN of the role associated with the instance profile.`) addCmd.Flags().BoolVar(&addReq.IsMetaInstanceProfile, "is-meta-instance-profile", addReq.IsMetaInstanceProfile, `By default, Databricks validates that it has sufficient permissions to launch instances with the instance profile.`) @@ -43,12 +46,25 @@ var addCmd = &cobra.Command{ API is only available to admin users.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - addReq.InstanceProfileArn = args[0] + if cmd.Flags().Changed("json") { + err = addJson.Unmarshal(&addReq) + if err != nil { + return err + } + } else { + addReq.InstanceProfileArn = args[0] + } err = w.InstanceProfiles.Add(ctx, addReq) if err != nil { @@ -61,10 +77,12 @@ var addCmd = &cobra.Command{ // start edit command var editReq compute.InstanceProfile +var editJson flags.JsonFlag func init() { Cmd.AddCommand(editCmd) // TODO: short flags + editCmd.Flags().Var(&editJson, "json", `either inline JSON string or @path/to/file.json with request body`) editCmd.Flags().StringVar(&editReq.IamRoleArn, "iam-role-arn", editReq.IamRoleArn, `The AWS IAM role ARN of the role associated with the instance profile.`) editCmd.Flags().BoolVar(&editReq.IsMetaInstanceProfile, "is-meta-instance-profile", editReq.IsMetaInstanceProfile, `By default, Databricks validates that it has sufficient permissions to launch instances with the instance profile.`) @@ -93,12 +111,25 @@ var editCmd = &cobra.Command{ [Enable serverless SQL warehouses]: https://docs.databricks.com/sql/admin/serverless.html`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - editReq.InstanceProfileArn = args[0] + if cmd.Flags().Changed("json") { + err = editJson.Unmarshal(&editReq) + if err != nil { + return err + } + } else { + editReq.InstanceProfileArn = args[0] + } err = w.InstanceProfiles.Edit(ctx, editReq) if err != nil { @@ -140,10 +171,12 @@ var listCmd = &cobra.Command{ // start remove command var removeReq compute.RemoveInstanceProfile +var removeJson flags.JsonFlag func init() { Cmd.AddCommand(removeCmd) // TODO: short flags + removeCmd.Flags().Var(&removeJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -158,12 +191,25 @@ var removeCmd = &cobra.Command{ This API is only accessible to admin users.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - removeReq.InstanceProfileArn = args[0] + if cmd.Flags().Changed("json") { + err = removeJson.Unmarshal(&removeReq) + if err != nil { + return err + } + } else { + removeReq.InstanceProfileArn = args[0] + } err = w.InstanceProfiles.Remove(ctx, removeReq) if err != nil { diff --git a/cmd/workspace/ip-access-lists/ip-access-lists.go b/cmd/workspace/ip-access-lists/ip-access-lists.go index aae6ffd9d6..6e46c47bd8 100755 --- a/cmd/workspace/ip-access-lists/ip-access-lists.go +++ b/cmd/workspace/ip-access-lists/ip-access-lists.go @@ -77,18 +77,21 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - createReq.Label = args[0] - _, err = fmt.Sscan(args[1], &createReq.ListType) - if err != nil { - return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) - } - _, err = fmt.Sscan(args[2], &createReq.IpAddresses) - if err != nil { - return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Label = args[0] + _, err = fmt.Sscan(args[1], &createReq.ListType) + if err != nil { + return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) + } + _, err = fmt.Sscan(args[2], &createReq.IpAddresses) + if err != nil { + return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) + } } response, err := w.IpAccessLists.Create(ctx, createReq) @@ -102,15 +105,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq settings.DeleteIpAccessListRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [IP_ACCESS_LIST_ID]", + Use: "delete IP_ACCESS_LIST_ID", Short: `Delete access list.`, Long: `Delete access list. @@ -121,21 +126,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.IpAccessLists.IpAccessListInfoLabelToListIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID for the corresponding IP access list to modify") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.IpAccessLists.IpAccessListInfoLabelToListIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding IP access list to modify") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id for the corresponding ip access list to modify") + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding ip access list to modify") + } + deleteReq.IpAccessListId = args[0] } - deleteReq.IpAccessListId = args[0] err = w.IpAccessLists.Delete(ctx, deleteReq) if err != nil { @@ -148,15 +160,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq settings.GetIpAccessListRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [IP_ACCESS_LIST_ID]", + Use: "get IP_ACCESS_LIST_ID", Short: `Get access list.`, Long: `Get access list. @@ -167,21 +181,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.IpAccessLists.IpAccessListInfoLabelToListIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID for the corresponding IP access list to modify") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.IpAccessLists.IpAccessListInfoLabelToListIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding IP access list to modify") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id for the corresponding ip access list to modify") + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding ip access list to modify") + } + getReq.IpAccessListId = args[0] } - getReq.IpAccessListId = args[0] response, err := w.IpAccessLists.Get(ctx, getReq) if err != nil { @@ -255,24 +276,27 @@ var replaceCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = replaceJson.Unmarshal(&replaceReq) - if err != nil { - return err - } - replaceReq.Label = args[0] - _, err = fmt.Sscan(args[1], &replaceReq.ListType) - if err != nil { - return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) - } - _, err = fmt.Sscan(args[2], &replaceReq.IpAddresses) - if err != nil { - return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) - } - _, err = fmt.Sscan(args[3], &replaceReq.Enabled) - if err != nil { - return fmt.Errorf("invalid ENABLED: %s", args[3]) + if cmd.Flags().Changed("json") { + err = replaceJson.Unmarshal(&replaceReq) + if err != nil { + return err + } + } else { + replaceReq.Label = args[0] + _, err = fmt.Sscan(args[1], &replaceReq.ListType) + if err != nil { + return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) + } + _, err = fmt.Sscan(args[2], &replaceReq.IpAddresses) + if err != nil { + return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) + } + _, err = fmt.Sscan(args[3], &replaceReq.Enabled) + if err != nil { + return fmt.Errorf("invalid ENABLED: %s", args[3]) + } + replaceReq.IpAccessListId = args[4] } - replaceReq.IpAccessListId = args[4] err = w.IpAccessLists.Replace(ctx, replaceReq) if err != nil { @@ -323,24 +347,27 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err - } - updateReq.Label = args[0] - _, err = fmt.Sscan(args[1], &updateReq.ListType) - if err != nil { - return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) - } - _, err = fmt.Sscan(args[2], &updateReq.IpAddresses) - if err != nil { - return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) - } - _, err = fmt.Sscan(args[3], &updateReq.Enabled) - if err != nil { - return fmt.Errorf("invalid ENABLED: %s", args[3]) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.Label = args[0] + _, err = fmt.Sscan(args[1], &updateReq.ListType) + if err != nil { + return fmt.Errorf("invalid LIST_TYPE: %s", args[1]) + } + _, err = fmt.Sscan(args[2], &updateReq.IpAddresses) + if err != nil { + return fmt.Errorf("invalid IP_ADDRESSES: %s", args[2]) + } + _, err = fmt.Sscan(args[3], &updateReq.Enabled) + if err != nil { + return fmt.Errorf("invalid ENABLED: %s", args[3]) + } + updateReq.IpAccessListId = args[4] } - updateReq.IpAccessListId = args[4] err = w.IpAccessLists.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/jobs/jobs.go b/cmd/workspace/jobs/jobs.go index 94693d3022..2d8dbd0de1 100755 --- a/cmd/workspace/jobs/jobs.go +++ b/cmd/workspace/jobs/jobs.go @@ -40,15 +40,17 @@ var Cmd = &cobra.Command{ // start cancel-all-runs command var cancelAllRunsReq jobs.CancelAllRuns +var cancelAllRunsJson flags.JsonFlag func init() { Cmd.AddCommand(cancelAllRunsCmd) // TODO: short flags + cancelAllRunsCmd.Flags().Var(&cancelAllRunsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var cancelAllRunsCmd = &cobra.Command{ - Use: "cancel-all-runs [JOB_ID]", + Use: "cancel-all-runs JOB_ID", Short: `Cancel all runs of a job.`, Long: `Cancel all runs of a job. @@ -60,23 +62,30 @@ var cancelAllRunsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = cancelAllRunsJson.Unmarshal(&cancelAllRunsReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The canonical identifier of the job to cancel all runs of") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The canonical identifier of the job to cancel all runs of") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the canonical identifier of the job to cancel all runs of") + } + _, err = fmt.Sscan(args[0], &cancelAllRunsReq.JobId) if err != nil { - return err + return fmt.Errorf("invalid JOB_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the canonical identifier of the job to cancel all runs of") - } - _, err = fmt.Sscan(args[0], &cancelAllRunsReq.JobId) - if err != nil { - return fmt.Errorf("invalid JOB_ID: %s", args[0]) } err = w.Jobs.CancelAllRuns(ctx, cancelAllRunsReq) @@ -90,7 +99,7 @@ var cancelAllRunsCmd = &cobra.Command{ // start cancel-run command var cancelRunReq jobs.CancelRun - +var cancelRunJson flags.JsonFlag var cancelRunSkipWait bool var cancelRunTimeout time.Duration @@ -100,11 +109,12 @@ func init() { cancelRunCmd.Flags().BoolVar(&cancelRunSkipWait, "no-wait", cancelRunSkipWait, `do not wait to reach TERMINATED or SKIPPED state`) cancelRunCmd.Flags().DurationVar(&cancelRunTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach TERMINATED or SKIPPED state`) // TODO: short flags + cancelRunCmd.Flags().Var(&cancelRunJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var cancelRunCmd = &cobra.Command{ - Use: "cancel-run [RUN_ID]", + Use: "cancel-run RUN_ID", Short: `Cancel a job run.`, Long: `Cancel a job run. @@ -116,23 +126,30 @@ var cancelRunCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = cancelRunJson.Unmarshal(&cancelRunReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "This field is required") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "This field is required") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have this field is required") + } + _, err = fmt.Sscan(args[0], &cancelRunReq.RunId) if err != nil { - return err + return fmt.Errorf("invalid RUN_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have this field is required") - } - _, err = fmt.Sscan(args[0], &cancelRunReq.RunId) - if err != nil { - return fmt.Errorf("invalid RUN_ID: %s", args[0]) } if cancelRunSkipWait { @@ -203,13 +220,23 @@ var createCmd = &cobra.Command{ Create a new job.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := w.Jobs.Create(ctx, createReq) @@ -223,15 +250,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq jobs.DeleteJob +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [JOB_ID]", + Use: "delete JOB_ID", Short: `Delete a job.`, Long: `Delete a job. @@ -242,23 +271,30 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The canonical identifier of the job to delete") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The canonical identifier of the job to delete") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the canonical identifier of the job to delete") + } + _, err = fmt.Sscan(args[0], &deleteReq.JobId) if err != nil { - return err + return fmt.Errorf("invalid JOB_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the canonical identifier of the job to delete") - } - _, err = fmt.Sscan(args[0], &deleteReq.JobId) - if err != nil { - return fmt.Errorf("invalid JOB_ID: %s", args[0]) } err = w.Jobs.Delete(ctx, deleteReq) @@ -272,15 +308,17 @@ var deleteCmd = &cobra.Command{ // start delete-run command var deleteRunReq jobs.DeleteRun +var deleteRunJson flags.JsonFlag func init() { Cmd.AddCommand(deleteRunCmd) // TODO: short flags + deleteRunCmd.Flags().Var(&deleteRunJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteRunCmd = &cobra.Command{ - Use: "delete-run [RUN_ID]", + Use: "delete-run RUN_ID", Short: `Delete a job run.`, Long: `Delete a job run. @@ -291,23 +329,30 @@ var deleteRunCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = deleteRunJson.Unmarshal(&deleteRunReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The canonical identifier of the run for which to retrieve the metadata") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The canonical identifier of the run for which to retrieve the metadata") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the canonical identifier of the run for which to retrieve the metadata") + } + _, err = fmt.Sscan(args[0], &deleteRunReq.RunId) if err != nil { - return err + return fmt.Errorf("invalid RUN_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the canonical identifier of the run for which to retrieve the metadata") - } - _, err = fmt.Sscan(args[0], &deleteRunReq.RunId) - if err != nil { - return fmt.Errorf("invalid RUN_ID: %s", args[0]) } err = w.Jobs.DeleteRun(ctx, deleteRunReq) @@ -321,17 +366,19 @@ var deleteRunCmd = &cobra.Command{ // start export-run command var exportRunReq jobs.ExportRunRequest +var exportRunJson flags.JsonFlag func init() { Cmd.AddCommand(exportRunCmd) // TODO: short flags + exportRunCmd.Flags().Var(&exportRunJson, "json", `either inline JSON string or @path/to/file.json with request body`) exportRunCmd.Flags().Var(&exportRunReq.ViewsToExport, "views-to-export", `Which views to export (CODE, DASHBOARDS, or ALL).`) } var exportRunCmd = &cobra.Command{ - Use: "export-run [RUN_ID]", + Use: "export-run RUN_ID", Short: `Export and retrieve a job run.`, Long: `Export and retrieve a job run. @@ -342,23 +389,30 @@ var exportRunCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = exportRunJson.Unmarshal(&exportRunReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The canonical identifier for the run") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The canonical identifier for the run") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the canonical identifier for the run") + } + _, err = fmt.Sscan(args[0], &exportRunReq.RunId) if err != nil { - return err + return fmt.Errorf("invalid RUN_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the canonical identifier for the run") - } - _, err = fmt.Sscan(args[0], &exportRunReq.RunId) - if err != nil { - return fmt.Errorf("invalid RUN_ID: %s", args[0]) } response, err := w.Jobs.ExportRun(ctx, exportRunReq) @@ -372,15 +426,17 @@ var exportRunCmd = &cobra.Command{ // start get command var getReq jobs.GetJobRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [JOB_ID]", + Use: "get JOB_ID", Short: `Get a single job.`, Long: `Get a single job. @@ -391,23 +447,30 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The canonical identifier of the job to retrieve information about") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The canonical identifier of the job to retrieve information about") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the canonical identifier of the job to retrieve information about") + } + _, err = fmt.Sscan(args[0], &getReq.JobId) if err != nil { - return err + return fmt.Errorf("invalid JOB_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the canonical identifier of the job to retrieve information about") - } - _, err = fmt.Sscan(args[0], &getReq.JobId) - if err != nil { - return fmt.Errorf("invalid JOB_ID: %s", args[0]) } response, err := w.Jobs.Get(ctx, getReq) @@ -421,7 +484,7 @@ var getCmd = &cobra.Command{ // start get-run command var getRunReq jobs.GetRunRequest - +var getRunJson flags.JsonFlag var getRunSkipWait bool var getRunTimeout time.Duration @@ -431,13 +494,14 @@ func init() { getRunCmd.Flags().BoolVar(&getRunSkipWait, "no-wait", getRunSkipWait, `do not wait to reach TERMINATED or SKIPPED state`) getRunCmd.Flags().DurationVar(&getRunTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach TERMINATED or SKIPPED state`) // TODO: short flags + getRunCmd.Flags().Var(&getRunJson, "json", `either inline JSON string or @path/to/file.json with request body`) getRunCmd.Flags().BoolVar(&getRunReq.IncludeHistory, "include-history", getRunReq.IncludeHistory, `Whether to include the repair history in the response.`) } var getRunCmd = &cobra.Command{ - Use: "get-run [RUN_ID]", + Use: "get-run RUN_ID", Short: `Get a single job run.`, Long: `Get a single job run. @@ -448,23 +512,30 @@ var getRunCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = getRunJson.Unmarshal(&getRunReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The canonical identifier of the run for which to retrieve the metadata") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The canonical identifier of the run for which to retrieve the metadata") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the canonical identifier of the run for which to retrieve the metadata") + } + _, err = fmt.Sscan(args[0], &getRunReq.RunId) if err != nil { - return err + return fmt.Errorf("invalid RUN_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the canonical identifier of the run for which to retrieve the metadata") - } - _, err = fmt.Sscan(args[0], &getRunReq.RunId) - if err != nil { - return fmt.Errorf("invalid RUN_ID: %s", args[0]) } response, err := w.Jobs.GetRun(ctx, getRunReq) @@ -478,15 +549,17 @@ var getRunCmd = &cobra.Command{ // start get-run-output command var getRunOutputReq jobs.GetRunOutputRequest +var getRunOutputJson flags.JsonFlag func init() { Cmd.AddCommand(getRunOutputCmd) // TODO: short flags + getRunOutputCmd.Flags().Var(&getRunOutputJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getRunOutputCmd = &cobra.Command{ - Use: "get-run-output [RUN_ID]", + Use: "get-run-output RUN_ID", Short: `Get the output for a single run.`, Long: `Get the output for a single run. @@ -506,23 +579,30 @@ var getRunOutputCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if cmd.Flags().Changed("json") { + err = getRunOutputJson.Unmarshal(&getRunOutputReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The canonical identifier for the run") + } else { + if len(args) == 0 { + names, err := w.Jobs.BaseJobSettingsNameToJobIdMap(ctx, jobs.ListJobsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The canonical identifier for the run") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the canonical identifier for the run") + } + _, err = fmt.Sscan(args[0], &getRunOutputReq.RunId) if err != nil { - return err + return fmt.Errorf("invalid RUN_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the canonical identifier for the run") - } - _, err = fmt.Sscan(args[0], &getRunOutputReq.RunId) - if err != nil { - return fmt.Errorf("invalid RUN_ID: %s", args[0]) } response, err := w.Jobs.GetRunOutput(ctx, getRunOutputReq) @@ -536,10 +616,12 @@ var getRunOutputCmd = &cobra.Command{ // start list command var listReq jobs.ListJobsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().BoolVar(&listReq.ExpandTasks, "expand-tasks", listReq.ExpandTasks, `Whether to include task and cluster details in the response.`) listCmd.Flags().IntVar(&listReq.Limit, "limit", listReq.Limit, `The number of jobs to return.`) @@ -556,10 +638,24 @@ var listCmd = &cobra.Command{ Retrieves a list of jobs.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.Jobs.ListAll(ctx, listReq) if err != nil { @@ -572,10 +668,12 @@ var listCmd = &cobra.Command{ // start list-runs command var listRunsReq jobs.ListRunsRequest +var listRunsJson flags.JsonFlag func init() { Cmd.AddCommand(listRunsCmd) // TODO: short flags + listRunsCmd.Flags().Var(&listRunsJson, "json", `either inline JSON string or @path/to/file.json with request body`) listRunsCmd.Flags().BoolVar(&listRunsReq.ActiveOnly, "active-only", listRunsReq.ActiveOnly, `If active_only is true, only active runs are included in the results; otherwise, lists both active and completed runs.`) listRunsCmd.Flags().BoolVar(&listRunsReq.CompletedOnly, "completed-only", listRunsReq.CompletedOnly, `If completed_only is true, only completed runs are included in the results; otherwise, lists both active and completed runs.`) @@ -597,10 +695,24 @@ var listRunsCmd = &cobra.Command{ List runs in descending order by start time.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listRunsJson.Unmarshal(&listRunsReq) + if err != nil { + return err + } + } else { + } response, err := w.Jobs.ListRunsAll(ctx, listRunsReq) if err != nil { @@ -640,7 +752,7 @@ func init() { } var repairRunCmd = &cobra.Command{ - Use: "repair-run [RUN_ID]", + Use: "repair-run RUN_ID", Short: `Repair a job run.`, Long: `Repair a job run. @@ -677,7 +789,6 @@ var repairRunCmd = &cobra.Command{ if err != nil { return fmt.Errorf("invalid RUN_ID: %s", args[0]) } - } if repairRunSkipWait { @@ -737,17 +848,20 @@ var resetCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = resetJson.Unmarshal(&resetReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &resetReq.JobId) - if err != nil { - return fmt.Errorf("invalid JOB_ID: %s", args[0]) - } - _, err = fmt.Sscan(args[1], &resetReq.NewSettings) - if err != nil { - return fmt.Errorf("invalid NEW_SETTINGS: %s", args[1]) + if cmd.Flags().Changed("json") { + err = resetJson.Unmarshal(&resetReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &resetReq.JobId) + if err != nil { + return fmt.Errorf("invalid JOB_ID: %s", args[0]) + } + _, err = fmt.Sscan(args[1], &resetReq.NewSettings) + if err != nil { + return fmt.Errorf("invalid NEW_SETTINGS: %s", args[1]) + } } err = w.Jobs.Reset(ctx, resetReq) @@ -786,7 +900,7 @@ func init() { } var runNowCmd = &cobra.Command{ - Use: "run-now [JOB_ID]", + Use: "run-now JOB_ID", Short: `Trigger a new job run.`, Long: `Trigger a new job run. @@ -821,7 +935,6 @@ var runNowCmd = &cobra.Command{ if err != nil { return fmt.Errorf("invalid JOB_ID: %s", args[0]) } - } if runNowSkipWait { @@ -893,13 +1006,23 @@ var submitCmd = &cobra.Command{ submitted.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = submitJson.Unmarshal(&submitReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = submitJson.Unmarshal(&submitReq) + if err != nil { + return err + } + } else { } if submitSkipWait { @@ -950,7 +1073,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [JOB_ID]", + Use: "update JOB_ID", Short: `Partially update a job.`, Long: `Partially update a job. @@ -986,7 +1109,6 @@ var updateCmd = &cobra.Command{ if err != nil { return fmt.Errorf("invalid JOB_ID: %s", args[0]) } - } err = w.Jobs.Update(ctx, updateReq) diff --git a/cmd/workspace/libraries/libraries.go b/cmd/workspace/libraries/libraries.go index d7a5004230..368fe37ea7 100755 --- a/cmd/workspace/libraries/libraries.go +++ b/cmd/workspace/libraries/libraries.go @@ -69,10 +69,12 @@ var allClusterStatusesCmd = &cobra.Command{ // start cluster-status command var clusterStatusReq compute.ClusterStatusRequest +var clusterStatusJson flags.JsonFlag func init() { Cmd.AddCommand(clusterStatusCmd) // TODO: short flags + clusterStatusCmd.Flags().Var(&clusterStatusJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -98,12 +100,25 @@ var clusterStatusCmd = &cobra.Command{ guarantee.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - clusterStatusReq.ClusterId = args[0] + if cmd.Flags().Changed("json") { + err = clusterStatusJson.Unmarshal(&clusterStatusReq) + if err != nil { + return err + } + } else { + clusterStatusReq.ClusterId = args[0] + } response, err := w.Libraries.ClusterStatus(ctx, clusterStatusReq) if err != nil { @@ -142,14 +157,17 @@ var installCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = installJson.Unmarshal(&installReq) - if err != nil { - return err - } - installReq.ClusterId = args[0] - _, err = fmt.Sscan(args[1], &installReq.Libraries) - if err != nil { - return fmt.Errorf("invalid LIBRARIES: %s", args[1]) + if cmd.Flags().Changed("json") { + err = installJson.Unmarshal(&installReq) + if err != nil { + return err + } + } else { + installReq.ClusterId = args[0] + _, err = fmt.Sscan(args[1], &installReq.Libraries) + if err != nil { + return fmt.Errorf("invalid LIBRARIES: %s", args[1]) + } } err = w.Libraries.Install(ctx, installReq) @@ -186,14 +204,17 @@ var uninstallCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = uninstallJson.Unmarshal(&uninstallReq) - if err != nil { - return err - } - uninstallReq.ClusterId = args[0] - _, err = fmt.Sscan(args[1], &uninstallReq.Libraries) - if err != nil { - return fmt.Errorf("invalid LIBRARIES: %s", args[1]) + if cmd.Flags().Changed("json") { + err = uninstallJson.Unmarshal(&uninstallReq) + if err != nil { + return err + } + } else { + uninstallReq.ClusterId = args[0] + _, err = fmt.Sscan(args[1], &uninstallReq.Libraries) + if err != nil { + return fmt.Errorf("invalid LIBRARIES: %s", args[1]) + } } err = w.Libraries.Uninstall(ctx, uninstallReq) diff --git a/cmd/workspace/metastores/metastores.go b/cmd/workspace/metastores/metastores.go index 48e5f60683..f1cc68e6d4 100755 --- a/cmd/workspace/metastores/metastores.go +++ b/cmd/workspace/metastores/metastores.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" ) @@ -32,10 +33,12 @@ var Cmd = &cobra.Command{ // start assign command var assignReq catalog.CreateMetastoreAssignment +var assignJson flags.JsonFlag func init() { Cmd.AddCommand(assignCmd) // TODO: short flags + assignCmd.Flags().Var(&assignJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -49,16 +52,29 @@ var assignCmd = &cobra.Command{ and __default_catalog_name__. The caller must be an account admin.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - assignReq.MetastoreId = args[0] - assignReq.DefaultCatalogName = args[1] - _, err = fmt.Sscan(args[2], &assignReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[2]) + if cmd.Flags().Changed("json") { + err = assignJson.Unmarshal(&assignReq) + if err != nil { + return err + } + } else { + assignReq.MetastoreId = args[0] + assignReq.DefaultCatalogName = args[1] + _, err = fmt.Sscan(args[2], &assignReq.WorkspaceId) + if err != nil { + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[2]) + } } err = w.Metastores.Assign(ctx, assignReq) @@ -72,10 +88,12 @@ var assignCmd = &cobra.Command{ // start create command var createReq catalog.CreateMetastore +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().StringVar(&createReq.Region, "region", createReq.Region, `Cloud region which the metastore serves (e.g., us-west-2, westus).`) @@ -89,13 +107,26 @@ var createCmd = &cobra.Command{ Creates a new metastore based on a provided name and storage root path.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - createReq.Name = args[0] - createReq.StorageRoot = args[1] + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + createReq.StorageRoot = args[1] + } response, err := w.Metastores.Create(ctx, createReq) if err != nil { @@ -135,17 +166,19 @@ var currentCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteMetastoreRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) deleteCmd.Flags().BoolVar(&deleteReq.Force, "force", deleteReq.Force, `Force deletion even if the metastore is not empty.`) } var deleteCmd = &cobra.Command{ - Use: "delete [ID]", + Use: "delete ID", Short: `Delete a metastore.`, Long: `Delete a metastore. @@ -156,21 +189,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Metastores.MetastoreInfoNameToMetastoreIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID of the metastore") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Metastores.MetastoreInfoNameToMetastoreIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID of the metastore") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id of the metastore") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id of the metastore") + } + deleteReq.Id = args[0] } - deleteReq.Id = args[0] err = w.Metastores.Delete(ctx, deleteReq) if err != nil { @@ -183,15 +223,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq catalog.GetMetastoreRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [ID]", + Use: "get ID", Short: `Get a metastore.`, Long: `Get a metastore. @@ -203,21 +245,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Metastores.MetastoreInfoNameToMetastoreIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID of the metastore") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Metastores.MetastoreInfoNameToMetastoreIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID of the metastore") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id of the metastore") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id of the metastore") + } + getReq.Id = args[0] } - getReq.Id = args[0] response, err := w.Metastores.Get(ctx, getReq) if err != nil { @@ -259,10 +308,12 @@ var listCmd = &cobra.Command{ // start maintenance command var maintenanceReq catalog.UpdateAutoMaintenance +var maintenanceJson flags.JsonFlag func init() { Cmd.AddCommand(maintenanceCmd) // TODO: short flags + maintenanceCmd.Flags().Var(&maintenanceJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -274,15 +325,28 @@ var maintenanceCmd = &cobra.Command{ Enables or disables auto maintenance on the metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - maintenanceReq.MetastoreId = args[0] - _, err = fmt.Sscan(args[1], &maintenanceReq.Enable) - if err != nil { - return fmt.Errorf("invalid ENABLE: %s", args[1]) + if cmd.Flags().Changed("json") { + err = maintenanceJson.Unmarshal(&maintenanceReq) + if err != nil { + return err + } + } else { + maintenanceReq.MetastoreId = args[0] + _, err = fmt.Sscan(args[1], &maintenanceReq.Enable) + if err != nil { + return fmt.Errorf("invalid ENABLE: %s", args[1]) + } } response, err := w.Metastores.Maintenance(ctx, maintenanceReq) @@ -324,10 +388,12 @@ var summaryCmd = &cobra.Command{ // start unassign command var unassignReq catalog.UnassignRequest +var unassignJson flags.JsonFlag func init() { Cmd.AddCommand(unassignCmd) // TODO: short flags + unassignCmd.Flags().Var(&unassignJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -339,16 +405,29 @@ var unassignCmd = &cobra.Command{ Deletes a metastore assignment. The caller must be an account administrator.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - _, err = fmt.Sscan(args[0], &unassignReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = unassignJson.Unmarshal(&unassignReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &unassignReq.WorkspaceId) + if err != nil { + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + } + unassignReq.MetastoreId = args[1] } - unassignReq.MetastoreId = args[1] err = w.Metastores.Unassign(ctx, unassignReq) if err != nil { @@ -361,10 +440,12 @@ var unassignCmd = &cobra.Command{ // start update command var updateReq catalog.UpdateMetastore +var updateJson flags.JsonFlag func init() { Cmd.AddCommand(updateCmd) // TODO: short flags + updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateCmd.Flags().StringVar(&updateReq.DeltaSharingOrganizationName, "delta-sharing-organization-name", updateReq.DeltaSharingOrganizationName, `The organization name of a Delta Sharing entity, to be used in Databricks-to-Databricks Delta Sharing as the official name.`) updateCmd.Flags().Int64Var(&updateReq.DeltaSharingRecipientTokenLifetimeInSeconds, "delta-sharing-recipient-token-lifetime-in-seconds", updateReq.DeltaSharingRecipientTokenLifetimeInSeconds, `The lifetime of delta sharing recipient token in seconds.`) @@ -377,7 +458,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [ID]", + Use: "update ID", Short: `Update a metastore.`, Long: `Update a metastore. @@ -389,21 +470,28 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Metastores.MetastoreInfoNameToMetastoreIdMap(ctx) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID of the metastore") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Metastores.MetastoreInfoNameToMetastoreIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID of the metastore") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id of the metastore") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id of the metastore") + } + updateReq.Id = args[0] } - updateReq.Id = args[0] response, err := w.Metastores.Update(ctx, updateReq) if err != nil { @@ -416,10 +504,12 @@ var updateCmd = &cobra.Command{ // start update-assignment command var updateAssignmentReq catalog.UpdateMetastoreAssignment +var updateAssignmentJson flags.JsonFlag func init() { Cmd.AddCommand(updateAssignmentCmd) // TODO: short flags + updateAssignmentCmd.Flags().Var(&updateAssignmentJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateAssignmentCmd.Flags().StringVar(&updateAssignmentReq.DefaultCatalogName, "default-catalog-name", updateAssignmentReq.DefaultCatalogName, `The name of the default catalog for the metastore.`) updateAssignmentCmd.Flags().StringVar(&updateAssignmentReq.MetastoreId, "metastore-id", updateAssignmentReq.MetastoreId, `The unique ID of the metastore.`) @@ -427,7 +517,7 @@ func init() { } var updateAssignmentCmd = &cobra.Command{ - Use: "update-assignment [WORKSPACE_ID]", + Use: "update-assignment WORKSPACE_ID", Short: `Update an assignment.`, Long: `Update an assignment. @@ -441,23 +531,30 @@ var updateAssignmentCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Metastores.MetastoreInfoNameToMetastoreIdMap(ctx) + if cmd.Flags().Changed("json") { + err = updateAssignmentJson.Unmarshal(&updateAssignmentReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "A workspace ID") + } else { + if len(args) == 0 { + names, err := w.Metastores.MetastoreInfoNameToMetastoreIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "A workspace ID") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have a workspace id") + } + _, err = fmt.Sscan(args[0], &updateAssignmentReq.WorkspaceId) if err != nil { - return err + return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have a workspace id") - } - _, err = fmt.Sscan(args[0], &updateAssignmentReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) } err = w.Metastores.UpdateAssignment(ctx, updateAssignmentReq) diff --git a/cmd/workspace/model-registry/model-registry.go b/cmd/workspace/model-registry/model-registry.go index 4e69226e38..3bda9e6f0f 100755 --- a/cmd/workspace/model-registry/model-registry.go +++ b/cmd/workspace/model-registry/model-registry.go @@ -22,10 +22,12 @@ var Cmd = &cobra.Command{ // start approve-transition-request command var approveTransitionRequestReq ml.ApproveTransitionRequest +var approveTransitionRequestJson flags.JsonFlag func init() { Cmd.AddCommand(approveTransitionRequestCmd) // TODO: short flags + approveTransitionRequestCmd.Flags().Var(&approveTransitionRequestJson, "json", `either inline JSON string or @path/to/file.json with request body`) approveTransitionRequestCmd.Flags().StringVar(&approveTransitionRequestReq.Comment, "comment", approveTransitionRequestReq.Comment, `User-provided comment on the action.`) @@ -39,20 +41,33 @@ var approveTransitionRequestCmd = &cobra.Command{ Approves a model version stage transition request.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(4), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(4) + 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) - approveTransitionRequestReq.Name = args[0] - approveTransitionRequestReq.Version = args[1] - _, err = fmt.Sscan(args[2], &approveTransitionRequestReq.Stage) - if err != nil { - return fmt.Errorf("invalid STAGE: %s", args[2]) - } - _, err = fmt.Sscan(args[3], &approveTransitionRequestReq.ArchiveExistingVersions) - if err != nil { - return fmt.Errorf("invalid ARCHIVE_EXISTING_VERSIONS: %s", args[3]) + if cmd.Flags().Changed("json") { + err = approveTransitionRequestJson.Unmarshal(&approveTransitionRequestReq) + if err != nil { + return err + } + } else { + approveTransitionRequestReq.Name = args[0] + approveTransitionRequestReq.Version = args[1] + _, err = fmt.Sscan(args[2], &approveTransitionRequestReq.Stage) + if err != nil { + return fmt.Errorf("invalid STAGE: %s", args[2]) + } + _, err = fmt.Sscan(args[3], &approveTransitionRequestReq.ArchiveExistingVersions) + if err != nil { + return fmt.Errorf("invalid ARCHIVE_EXISTING_VERSIONS: %s", args[3]) + } } response, err := w.ModelRegistry.ApproveTransitionRequest(ctx, approveTransitionRequestReq) @@ -66,10 +81,12 @@ var approveTransitionRequestCmd = &cobra.Command{ // start create-comment command var createCommentReq ml.CreateComment +var createCommentJson flags.JsonFlag func init() { Cmd.AddCommand(createCommentCmd) // TODO: short flags + createCommentCmd.Flags().Var(&createCommentJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -83,14 +100,27 @@ var createCommentCmd = &cobra.Command{ example, test results or deployment errors.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - createCommentReq.Name = args[0] - createCommentReq.Version = args[1] - createCommentReq.Comment = args[2] + if cmd.Flags().Changed("json") { + err = createCommentJson.Unmarshal(&createCommentReq) + if err != nil { + return err + } + } else { + createCommentReq.Name = args[0] + createCommentReq.Version = args[1] + createCommentReq.Comment = args[2] + } response, err := w.ModelRegistry.CreateComment(ctx, createCommentReq) if err != nil { @@ -126,16 +156,25 @@ var createModelCmd = &cobra.Command{ exists.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = createModelJson.Unmarshal(&createModelReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createModelJson.Unmarshal(&createModelReq) + if err != nil { + return err + } + } else { + createModelReq.Name = args[0] } - createModelReq.Name = args[0] response, err := w.ModelRegistry.CreateModel(ctx, createModelReq) if err != nil { @@ -170,17 +209,26 @@ var createModelVersionCmd = &cobra.Command{ Creates a model version.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = createModelVersionJson.Unmarshal(&createModelVersionReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createModelVersionJson.Unmarshal(&createModelVersionReq) + if err != nil { + return err + } + } else { + createModelVersionReq.Name = args[0] + createModelVersionReq.Source = args[1] } - createModelVersionReq.Name = args[0] - createModelVersionReq.Source = args[1] response, err := w.ModelRegistry.CreateModelVersion(ctx, createModelVersionReq) if err != nil { @@ -193,10 +241,12 @@ var createModelVersionCmd = &cobra.Command{ // start create-transition-request command var createTransitionRequestReq ml.CreateTransitionRequest +var createTransitionRequestJson flags.JsonFlag func init() { Cmd.AddCommand(createTransitionRequestCmd) // TODO: short flags + createTransitionRequestCmd.Flags().Var(&createTransitionRequestJson, "json", `either inline JSON string or @path/to/file.json with request body`) createTransitionRequestCmd.Flags().StringVar(&createTransitionRequestReq.Comment, "comment", createTransitionRequestReq.Comment, `User-provided comment on the action.`) @@ -210,16 +260,29 @@ var createTransitionRequestCmd = &cobra.Command{ Creates a model version stage transition request.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - createTransitionRequestReq.Name = args[0] - createTransitionRequestReq.Version = args[1] - _, err = fmt.Sscan(args[2], &createTransitionRequestReq.Stage) - if err != nil { - return fmt.Errorf("invalid STAGE: %s", args[2]) + if cmd.Flags().Changed("json") { + err = createTransitionRequestJson.Unmarshal(&createTransitionRequestReq) + if err != nil { + return err + } + } else { + createTransitionRequestReq.Name = args[0] + createTransitionRequestReq.Version = args[1] + _, err = fmt.Sscan(args[2], &createTransitionRequestReq.Stage) + if err != nil { + return fmt.Errorf("invalid STAGE: %s", args[2]) + } } response, err := w.ModelRegistry.CreateTransitionRequest(ctx, createTransitionRequestReq) @@ -262,13 +325,16 @@ var createWebhookCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = createWebhookJson.Unmarshal(&createWebhookReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &createWebhookReq.Events) - if err != nil { - return fmt.Errorf("invalid EVENTS: %s", args[0]) + if cmd.Flags().Changed("json") { + err = createWebhookJson.Unmarshal(&createWebhookReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &createWebhookReq.Events) + if err != nil { + return fmt.Errorf("invalid EVENTS: %s", args[0]) + } } response, err := w.ModelRegistry.CreateWebhook(ctx, createWebhookReq) @@ -282,10 +348,12 @@ var createWebhookCmd = &cobra.Command{ // start delete-comment command var deleteCommentReq ml.DeleteCommentRequest +var deleteCommentJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCommentCmd) // TODO: short flags + deleteCommentCmd.Flags().Var(&deleteCommentJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -297,12 +365,25 @@ var deleteCommentCmd = &cobra.Command{ Deletes a comment on a model version.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteCommentReq.Id = args[0] + if cmd.Flags().Changed("json") { + err = deleteCommentJson.Unmarshal(&deleteCommentReq) + if err != nil { + return err + } + } else { + deleteCommentReq.Id = args[0] + } err = w.ModelRegistry.DeleteComment(ctx, deleteCommentReq) if err != nil { @@ -315,10 +396,12 @@ var deleteCommentCmd = &cobra.Command{ // start delete-model command var deleteModelReq ml.DeleteModelRequest +var deleteModelJson flags.JsonFlag func init() { Cmd.AddCommand(deleteModelCmd) // TODO: short flags + deleteModelCmd.Flags().Var(&deleteModelJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -330,12 +413,25 @@ var deleteModelCmd = &cobra.Command{ Deletes a registered model.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteModelReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = deleteModelJson.Unmarshal(&deleteModelReq) + if err != nil { + return err + } + } else { + deleteModelReq.Name = args[0] + } err = w.ModelRegistry.DeleteModel(ctx, deleteModelReq) if err != nil { @@ -348,10 +444,12 @@ var deleteModelCmd = &cobra.Command{ // start delete-model-tag command var deleteModelTagReq ml.DeleteModelTagRequest +var deleteModelTagJson flags.JsonFlag func init() { Cmd.AddCommand(deleteModelTagCmd) // TODO: short flags + deleteModelTagCmd.Flags().Var(&deleteModelTagJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -363,13 +461,26 @@ var deleteModelTagCmd = &cobra.Command{ Deletes the tag for a registered model.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - deleteModelTagReq.Name = args[0] - deleteModelTagReq.Key = args[1] + if cmd.Flags().Changed("json") { + err = deleteModelTagJson.Unmarshal(&deleteModelTagReq) + if err != nil { + return err + } + } else { + deleteModelTagReq.Name = args[0] + deleteModelTagReq.Key = args[1] + } err = w.ModelRegistry.DeleteModelTag(ctx, deleteModelTagReq) if err != nil { @@ -382,10 +493,12 @@ var deleteModelTagCmd = &cobra.Command{ // start delete-model-version command var deleteModelVersionReq ml.DeleteModelVersionRequest +var deleteModelVersionJson flags.JsonFlag func init() { Cmd.AddCommand(deleteModelVersionCmd) // TODO: short flags + deleteModelVersionCmd.Flags().Var(&deleteModelVersionJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -397,13 +510,26 @@ var deleteModelVersionCmd = &cobra.Command{ Deletes a model version.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - deleteModelVersionReq.Name = args[0] - deleteModelVersionReq.Version = args[1] + if cmd.Flags().Changed("json") { + err = deleteModelVersionJson.Unmarshal(&deleteModelVersionReq) + if err != nil { + return err + } + } else { + deleteModelVersionReq.Name = args[0] + deleteModelVersionReq.Version = args[1] + } err = w.ModelRegistry.DeleteModelVersion(ctx, deleteModelVersionReq) if err != nil { @@ -416,10 +542,12 @@ var deleteModelVersionCmd = &cobra.Command{ // start delete-model-version-tag command var deleteModelVersionTagReq ml.DeleteModelVersionTagRequest +var deleteModelVersionTagJson flags.JsonFlag func init() { Cmd.AddCommand(deleteModelVersionTagCmd) // TODO: short flags + deleteModelVersionTagCmd.Flags().Var(&deleteModelVersionTagJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -431,14 +559,27 @@ var deleteModelVersionTagCmd = &cobra.Command{ Deletes a model version tag.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - deleteModelVersionTagReq.Name = args[0] - deleteModelVersionTagReq.Version = args[1] - deleteModelVersionTagReq.Key = args[2] + if cmd.Flags().Changed("json") { + err = deleteModelVersionTagJson.Unmarshal(&deleteModelVersionTagReq) + if err != nil { + return err + } + } else { + deleteModelVersionTagReq.Name = args[0] + deleteModelVersionTagReq.Version = args[1] + deleteModelVersionTagReq.Key = args[2] + } err = w.ModelRegistry.DeleteModelVersionTag(ctx, deleteModelVersionTagReq) if err != nil { @@ -451,10 +592,12 @@ var deleteModelVersionTagCmd = &cobra.Command{ // start delete-transition-request command var deleteTransitionRequestReq ml.DeleteTransitionRequestRequest +var deleteTransitionRequestJson flags.JsonFlag func init() { Cmd.AddCommand(deleteTransitionRequestCmd) // TODO: short flags + deleteTransitionRequestCmd.Flags().Var(&deleteTransitionRequestJson, "json", `either inline JSON string or @path/to/file.json with request body`) deleteTransitionRequestCmd.Flags().StringVar(&deleteTransitionRequestReq.Comment, "comment", deleteTransitionRequestReq.Comment, `User-provided comment on the action.`) @@ -468,18 +611,31 @@ var deleteTransitionRequestCmd = &cobra.Command{ Cancels a model version stage transition request.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(4), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(4) + 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) - deleteTransitionRequestReq.Name = args[0] - deleteTransitionRequestReq.Version = args[1] - _, err = fmt.Sscan(args[2], &deleteTransitionRequestReq.Stage) - if err != nil { - return fmt.Errorf("invalid STAGE: %s", args[2]) + if cmd.Flags().Changed("json") { + err = deleteTransitionRequestJson.Unmarshal(&deleteTransitionRequestReq) + if err != nil { + return err + } + } else { + deleteTransitionRequestReq.Name = args[0] + deleteTransitionRequestReq.Version = args[1] + _, err = fmt.Sscan(args[2], &deleteTransitionRequestReq.Stage) + if err != nil { + return fmt.Errorf("invalid STAGE: %s", args[2]) + } + deleteTransitionRequestReq.Creator = args[3] } - deleteTransitionRequestReq.Creator = args[3] err = w.ModelRegistry.DeleteTransitionRequest(ctx, deleteTransitionRequestReq) if err != nil { @@ -492,10 +648,12 @@ var deleteTransitionRequestCmd = &cobra.Command{ // start delete-webhook command var deleteWebhookReq ml.DeleteWebhookRequest +var deleteWebhookJson flags.JsonFlag func init() { Cmd.AddCommand(deleteWebhookCmd) // TODO: short flags + deleteWebhookCmd.Flags().Var(&deleteWebhookJson, "json", `either inline JSON string or @path/to/file.json with request body`) deleteWebhookCmd.Flags().StringVar(&deleteWebhookReq.Id, "id", deleteWebhookReq.Id, `Webhook ID required to delete a registry webhook.`) @@ -511,10 +669,24 @@ var deleteWebhookCmd = &cobra.Command{ Deletes a registry webhook.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = deleteWebhookJson.Unmarshal(&deleteWebhookReq) + if err != nil { + return err + } + } else { + } err = w.ModelRegistry.DeleteWebhook(ctx, deleteWebhookReq) if err != nil { @@ -546,16 +718,25 @@ var getLatestVersionsCmd = &cobra.Command{ Gets the latest version of a registered model.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = getLatestVersionsJson.Unmarshal(&getLatestVersionsReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = getLatestVersionsJson.Unmarshal(&getLatestVersionsReq) + if err != nil { + return err + } + } else { + getLatestVersionsReq.Name = args[0] } - getLatestVersionsReq.Name = args[0] response, err := w.ModelRegistry.GetLatestVersionsAll(ctx, getLatestVersionsReq) if err != nil { @@ -568,10 +749,12 @@ var getLatestVersionsCmd = &cobra.Command{ // start get-model command var getModelReq ml.GetModelRequest +var getModelJson flags.JsonFlag func init() { Cmd.AddCommand(getModelCmd) // TODO: short flags + getModelCmd.Flags().Var(&getModelJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -587,12 +770,25 @@ var getModelCmd = &cobra.Command{ [MLflow endpoint]: https://www.mlflow.org/docs/latest/rest-api.html#get-registeredmodel`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getModelReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = getModelJson.Unmarshal(&getModelReq) + if err != nil { + return err + } + } else { + getModelReq.Name = args[0] + } response, err := w.ModelRegistry.GetModel(ctx, getModelReq) if err != nil { @@ -605,10 +801,12 @@ var getModelCmd = &cobra.Command{ // start get-model-version command var getModelVersionReq ml.GetModelVersionRequest +var getModelVersionJson flags.JsonFlag func init() { Cmd.AddCommand(getModelVersionCmd) // TODO: short flags + getModelVersionCmd.Flags().Var(&getModelVersionJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -620,13 +818,26 @@ var getModelVersionCmd = &cobra.Command{ Get a model version.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - getModelVersionReq.Name = args[0] - getModelVersionReq.Version = args[1] + if cmd.Flags().Changed("json") { + err = getModelVersionJson.Unmarshal(&getModelVersionReq) + if err != nil { + return err + } + } else { + getModelVersionReq.Name = args[0] + getModelVersionReq.Version = args[1] + } response, err := w.ModelRegistry.GetModelVersion(ctx, getModelVersionReq) if err != nil { @@ -639,10 +850,12 @@ var getModelVersionCmd = &cobra.Command{ // start get-model-version-download-uri command var getModelVersionDownloadUriReq ml.GetModelVersionDownloadUriRequest +var getModelVersionDownloadUriJson flags.JsonFlag func init() { Cmd.AddCommand(getModelVersionDownloadUriCmd) // TODO: short flags + getModelVersionDownloadUriCmd.Flags().Var(&getModelVersionDownloadUriJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -654,13 +867,26 @@ var getModelVersionDownloadUriCmd = &cobra.Command{ Gets a URI to download the model version.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - getModelVersionDownloadUriReq.Name = args[0] - getModelVersionDownloadUriReq.Version = args[1] + if cmd.Flags().Changed("json") { + err = getModelVersionDownloadUriJson.Unmarshal(&getModelVersionDownloadUriReq) + if err != nil { + return err + } + } else { + getModelVersionDownloadUriReq.Name = args[0] + getModelVersionDownloadUriReq.Version = args[1] + } response, err := w.ModelRegistry.GetModelVersionDownloadUri(ctx, getModelVersionDownloadUriReq) if err != nil { @@ -673,10 +899,12 @@ var getModelVersionDownloadUriCmd = &cobra.Command{ // start list-models command var listModelsReq ml.ListModelsRequest +var listModelsJson flags.JsonFlag func init() { Cmd.AddCommand(listModelsCmd) // TODO: short flags + listModelsCmd.Flags().Var(&listModelsJson, "json", `either inline JSON string or @path/to/file.json with request body`) listModelsCmd.Flags().IntVar(&listModelsReq.MaxResults, "max-results", listModelsReq.MaxResults, `Maximum number of registered models desired.`) listModelsCmd.Flags().StringVar(&listModelsReq.PageToken, "page-token", listModelsReq.PageToken, `Pagination token to go to the next page based on a previous query.`) @@ -692,10 +920,24 @@ var listModelsCmd = &cobra.Command{ __max_results__.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listModelsJson.Unmarshal(&listModelsReq) + if err != nil { + return err + } + } else { + } response, err := w.ModelRegistry.ListModelsAll(ctx, listModelsReq) if err != nil { @@ -708,10 +950,12 @@ var listModelsCmd = &cobra.Command{ // start list-transition-requests command var listTransitionRequestsReq ml.ListTransitionRequestsRequest +var listTransitionRequestsJson flags.JsonFlag func init() { Cmd.AddCommand(listTransitionRequestsCmd) // TODO: short flags + listTransitionRequestsCmd.Flags().Var(&listTransitionRequestsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -723,13 +967,26 @@ var listTransitionRequestsCmd = &cobra.Command{ Gets a list of all open stage transition requests for the model version.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - listTransitionRequestsReq.Name = args[0] - listTransitionRequestsReq.Version = args[1] + if cmd.Flags().Changed("json") { + err = listTransitionRequestsJson.Unmarshal(&listTransitionRequestsReq) + if err != nil { + return err + } + } else { + listTransitionRequestsReq.Name = args[0] + listTransitionRequestsReq.Version = args[1] + } response, err := w.ModelRegistry.ListTransitionRequestsAll(ctx, listTransitionRequestsReq) if err != nil { @@ -765,13 +1022,23 @@ var listWebhooksCmd = &cobra.Command{ Lists all registry webhooks.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = listWebhooksJson.Unmarshal(&listWebhooksReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = listWebhooksJson.Unmarshal(&listWebhooksReq) + if err != nil { + return err + } + } else { } response, err := w.ModelRegistry.ListWebhooksAll(ctx, listWebhooksReq) @@ -785,10 +1052,12 @@ var listWebhooksCmd = &cobra.Command{ // start reject-transition-request command var rejectTransitionRequestReq ml.RejectTransitionRequest +var rejectTransitionRequestJson flags.JsonFlag func init() { Cmd.AddCommand(rejectTransitionRequestCmd) // TODO: short flags + rejectTransitionRequestCmd.Flags().Var(&rejectTransitionRequestJson, "json", `either inline JSON string or @path/to/file.json with request body`) rejectTransitionRequestCmd.Flags().StringVar(&rejectTransitionRequestReq.Comment, "comment", rejectTransitionRequestReq.Comment, `User-provided comment on the action.`) @@ -802,16 +1071,29 @@ var rejectTransitionRequestCmd = &cobra.Command{ Rejects a model version stage transition request.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - rejectTransitionRequestReq.Name = args[0] - rejectTransitionRequestReq.Version = args[1] - _, err = fmt.Sscan(args[2], &rejectTransitionRequestReq.Stage) - if err != nil { - return fmt.Errorf("invalid STAGE: %s", args[2]) + if cmd.Flags().Changed("json") { + err = rejectTransitionRequestJson.Unmarshal(&rejectTransitionRequestReq) + if err != nil { + return err + } + } else { + rejectTransitionRequestReq.Name = args[0] + rejectTransitionRequestReq.Version = args[1] + _, err = fmt.Sscan(args[2], &rejectTransitionRequestReq.Stage) + if err != nil { + return fmt.Errorf("invalid STAGE: %s", args[2]) + } } response, err := w.ModelRegistry.RejectTransitionRequest(ctx, rejectTransitionRequestReq) @@ -825,10 +1107,12 @@ var rejectTransitionRequestCmd = &cobra.Command{ // start rename-model command var renameModelReq ml.RenameModelRequest +var renameModelJson flags.JsonFlag func init() { Cmd.AddCommand(renameModelCmd) // TODO: short flags + renameModelCmd.Flags().Var(&renameModelJson, "json", `either inline JSON string or @path/to/file.json with request body`) renameModelCmd.Flags().StringVar(&renameModelReq.NewName, "new-name", renameModelReq.NewName, `If provided, updates the name for this registered_model.`) @@ -842,12 +1126,25 @@ var renameModelCmd = &cobra.Command{ Renames a registered model.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - renameModelReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = renameModelJson.Unmarshal(&renameModelReq) + if err != nil { + return err + } + } else { + renameModelReq.Name = args[0] + } response, err := w.ModelRegistry.RenameModel(ctx, renameModelReq) if err != nil { @@ -882,13 +1179,23 @@ var searchModelVersionsCmd = &cobra.Command{ Searches for specific model versions based on the supplied __filter__.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = searchModelVersionsJson.Unmarshal(&searchModelVersionsReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = searchModelVersionsJson.Unmarshal(&searchModelVersionsReq) + if err != nil { + return err + } + } else { } response, err := w.ModelRegistry.SearchModelVersionsAll(ctx, searchModelVersionsReq) @@ -924,13 +1231,23 @@ var searchModelsCmd = &cobra.Command{ Search for registered models based on the specified __filter__.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = searchModelsJson.Unmarshal(&searchModelsReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = searchModelsJson.Unmarshal(&searchModelsReq) + if err != nil { + return err + } + } else { } response, err := w.ModelRegistry.SearchModelsAll(ctx, searchModelsReq) @@ -944,10 +1261,12 @@ var searchModelsCmd = &cobra.Command{ // start set-model-tag command var setModelTagReq ml.SetModelTagRequest +var setModelTagJson flags.JsonFlag func init() { Cmd.AddCommand(setModelTagCmd) // TODO: short flags + setModelTagCmd.Flags().Var(&setModelTagJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -959,14 +1278,27 @@ var setModelTagCmd = &cobra.Command{ Sets a tag on a registered model.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - setModelTagReq.Name = args[0] - setModelTagReq.Key = args[1] - setModelTagReq.Value = args[2] + if cmd.Flags().Changed("json") { + err = setModelTagJson.Unmarshal(&setModelTagReq) + if err != nil { + return err + } + } else { + setModelTagReq.Name = args[0] + setModelTagReq.Key = args[1] + setModelTagReq.Value = args[2] + } err = w.ModelRegistry.SetModelTag(ctx, setModelTagReq) if err != nil { @@ -979,10 +1311,12 @@ var setModelTagCmd = &cobra.Command{ // start set-model-version-tag command var setModelVersionTagReq ml.SetModelVersionTagRequest +var setModelVersionTagJson flags.JsonFlag func init() { Cmd.AddCommand(setModelVersionTagCmd) // TODO: short flags + setModelVersionTagCmd.Flags().Var(&setModelVersionTagJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -994,15 +1328,28 @@ var setModelVersionTagCmd = &cobra.Command{ Sets a model version tag.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(4), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(4) + 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) - setModelVersionTagReq.Name = args[0] - setModelVersionTagReq.Version = args[1] - setModelVersionTagReq.Key = args[2] - setModelVersionTagReq.Value = args[3] + if cmd.Flags().Changed("json") { + err = setModelVersionTagJson.Unmarshal(&setModelVersionTagReq) + if err != nil { + return err + } + } else { + setModelVersionTagReq.Name = args[0] + setModelVersionTagReq.Version = args[1] + setModelVersionTagReq.Key = args[2] + setModelVersionTagReq.Value = args[3] + } err = w.ModelRegistry.SetModelVersionTag(ctx, setModelVersionTagReq) if err != nil { @@ -1015,10 +1362,12 @@ var setModelVersionTagCmd = &cobra.Command{ // start test-registry-webhook command var testRegistryWebhookReq ml.TestRegistryWebhookRequest +var testRegistryWebhookJson flags.JsonFlag func init() { Cmd.AddCommand(testRegistryWebhookCmd) // TODO: short flags + testRegistryWebhookCmd.Flags().Var(&testRegistryWebhookJson, "json", `either inline JSON string or @path/to/file.json with request body`) testRegistryWebhookCmd.Flags().Var(&testRegistryWebhookReq.Event, "event", `If event is specified, the test trigger uses the specified event.`) @@ -1034,12 +1383,25 @@ var testRegistryWebhookCmd = &cobra.Command{ Tests a registry webhook.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - testRegistryWebhookReq.Id = args[0] + if cmd.Flags().Changed("json") { + err = testRegistryWebhookJson.Unmarshal(&testRegistryWebhookReq) + if err != nil { + return err + } + } else { + testRegistryWebhookReq.Id = args[0] + } response, err := w.ModelRegistry.TestRegistryWebhook(ctx, testRegistryWebhookReq) if err != nil { @@ -1052,10 +1414,12 @@ var testRegistryWebhookCmd = &cobra.Command{ // start transition-stage command var transitionStageReq ml.TransitionModelVersionStageDatabricks +var transitionStageJson flags.JsonFlag func init() { Cmd.AddCommand(transitionStageCmd) // TODO: short flags + transitionStageCmd.Flags().Var(&transitionStageJson, "json", `either inline JSON string or @path/to/file.json with request body`) transitionStageCmd.Flags().StringVar(&transitionStageReq.Comment, "comment", transitionStageReq.Comment, `User-provided comment on the action.`) @@ -1073,20 +1437,33 @@ var transitionStageCmd = &cobra.Command{ [MLflow endpoint]: https://www.mlflow.org/docs/latest/rest-api.html#transition-modelversion-stage`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(4), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(4) + 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) - transitionStageReq.Name = args[0] - transitionStageReq.Version = args[1] - _, err = fmt.Sscan(args[2], &transitionStageReq.Stage) - if err != nil { - return fmt.Errorf("invalid STAGE: %s", args[2]) - } - _, err = fmt.Sscan(args[3], &transitionStageReq.ArchiveExistingVersions) - if err != nil { - return fmt.Errorf("invalid ARCHIVE_EXISTING_VERSIONS: %s", args[3]) + if cmd.Flags().Changed("json") { + err = transitionStageJson.Unmarshal(&transitionStageReq) + if err != nil { + return err + } + } else { + transitionStageReq.Name = args[0] + transitionStageReq.Version = args[1] + _, err = fmt.Sscan(args[2], &transitionStageReq.Stage) + if err != nil { + return fmt.Errorf("invalid STAGE: %s", args[2]) + } + _, err = fmt.Sscan(args[3], &transitionStageReq.ArchiveExistingVersions) + if err != nil { + return fmt.Errorf("invalid ARCHIVE_EXISTING_VERSIONS: %s", args[3]) + } } response, err := w.ModelRegistry.TransitionStage(ctx, transitionStageReq) @@ -1100,10 +1477,12 @@ var transitionStageCmd = &cobra.Command{ // start update-comment command var updateCommentReq ml.UpdateComment +var updateCommentJson flags.JsonFlag func init() { Cmd.AddCommand(updateCommentCmd) // TODO: short flags + updateCommentCmd.Flags().Var(&updateCommentJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -1115,13 +1494,26 @@ var updateCommentCmd = &cobra.Command{ Post an edit to a comment on a model version.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - updateCommentReq.Id = args[0] - updateCommentReq.Comment = args[1] + if cmd.Flags().Changed("json") { + err = updateCommentJson.Unmarshal(&updateCommentReq) + if err != nil { + return err + } + } else { + updateCommentReq.Id = args[0] + updateCommentReq.Comment = args[1] + } response, err := w.ModelRegistry.UpdateComment(ctx, updateCommentReq) if err != nil { @@ -1134,10 +1526,12 @@ var updateCommentCmd = &cobra.Command{ // start update-model command var updateModelReq ml.UpdateModelRequest +var updateModelJson flags.JsonFlag func init() { Cmd.AddCommand(updateModelCmd) // TODO: short flags + updateModelCmd.Flags().Var(&updateModelJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateModelCmd.Flags().StringVar(&updateModelReq.Description, "description", updateModelReq.Description, `If provided, updates the description for this registered_model.`) @@ -1151,12 +1545,25 @@ var updateModelCmd = &cobra.Command{ Updates a registered model.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - updateModelReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = updateModelJson.Unmarshal(&updateModelReq) + if err != nil { + return err + } + } else { + updateModelReq.Name = args[0] + } err = w.ModelRegistry.UpdateModel(ctx, updateModelReq) if err != nil { @@ -1169,10 +1576,12 @@ var updateModelCmd = &cobra.Command{ // start update-model-version command var updateModelVersionReq ml.UpdateModelVersionRequest +var updateModelVersionJson flags.JsonFlag func init() { Cmd.AddCommand(updateModelVersionCmd) // TODO: short flags + updateModelVersionCmd.Flags().Var(&updateModelVersionJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateModelVersionCmd.Flags().StringVar(&updateModelVersionReq.Description, "description", updateModelVersionReq.Description, `If provided, updates the description for this registered_model.`) @@ -1186,13 +1595,26 @@ var updateModelVersionCmd = &cobra.Command{ Updates the model version.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - updateModelVersionReq.Name = args[0] - updateModelVersionReq.Version = args[1] + if cmd.Flags().Changed("json") { + err = updateModelVersionJson.Unmarshal(&updateModelVersionReq) + if err != nil { + return err + } + } else { + updateModelVersionReq.Name = args[0] + updateModelVersionReq.Version = args[1] + } err = w.ModelRegistry.UpdateModelVersion(ctx, updateModelVersionReq) if err != nil { @@ -1230,16 +1652,25 @@ var updateWebhookCmd = &cobra.Command{ Updates a registry webhook.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = updateWebhookJson.Unmarshal(&updateWebhookReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = updateWebhookJson.Unmarshal(&updateWebhookReq) + if err != nil { + return err + } + } else { + updateWebhookReq.Id = args[0] } - updateWebhookReq.Id = args[0] err = w.ModelRegistry.UpdateWebhook(ctx, updateWebhookReq) if err != nil { diff --git a/cmd/workspace/permissions/permissions.go b/cmd/workspace/permissions/permissions.go index b34832a83b..e130dc3cef 100755 --- a/cmd/workspace/permissions/permissions.go +++ b/cmd/workspace/permissions/permissions.go @@ -20,10 +20,12 @@ var Cmd = &cobra.Command{ // start get command var getReq iam.GetPermissionRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -36,13 +38,26 @@ var getCmd = &cobra.Command{ parent objects or root objects.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - getReq.RequestObjectType = args[0] - getReq.RequestObjectId = args[1] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.RequestObjectType = args[0] + getReq.RequestObjectId = args[1] + } response, err := w.Permissions.Get(ctx, getReq) if err != nil { @@ -55,10 +70,12 @@ var getCmd = &cobra.Command{ // start get-permission-levels command var getPermissionLevelsReq iam.GetPermissionLevelsRequest +var getPermissionLevelsJson flags.JsonFlag func init() { Cmd.AddCommand(getPermissionLevelsCmd) // TODO: short flags + getPermissionLevelsCmd.Flags().Var(&getPermissionLevelsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -70,13 +87,26 @@ var getPermissionLevelsCmd = &cobra.Command{ Gets the permission levels that a user can have on an object.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - getPermissionLevelsReq.RequestObjectType = args[0] - getPermissionLevelsReq.RequestObjectId = args[1] + if cmd.Flags().Changed("json") { + err = getPermissionLevelsJson.Unmarshal(&getPermissionLevelsReq) + if err != nil { + return err + } + } else { + getPermissionLevelsReq.RequestObjectType = args[0] + getPermissionLevelsReq.RequestObjectId = args[1] + } response, err := w.Permissions.GetPermissionLevels(ctx, getPermissionLevelsReq) if err != nil { @@ -109,17 +139,26 @@ var setCmd = &cobra.Command{ objects and root objects.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = setJson.Unmarshal(&setReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = setJson.Unmarshal(&setReq) + if err != nil { + return err + } + } else { + setReq.RequestObjectType = args[0] + setReq.RequestObjectId = args[1] } - setReq.RequestObjectType = args[0] - setReq.RequestObjectId = args[1] err = w.Permissions.Set(ctx, setReq) if err != nil { @@ -151,17 +190,26 @@ var updateCmd = &cobra.Command{ Updates the permissions on an object.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.RequestObjectType = args[0] + updateReq.RequestObjectId = args[1] } - updateReq.RequestObjectType = args[0] - updateReq.RequestObjectId = args[1] err = w.Permissions.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/pipelines/pipelines.go b/cmd/workspace/pipelines/pipelines.go index 52108c7148..b65af4d74b 100755 --- a/cmd/workspace/pipelines/pipelines.go +++ b/cmd/workspace/pipelines/pipelines.go @@ -73,13 +73,23 @@ var createCmd = &cobra.Command{ If successful, this method returns the ID of the new pipeline.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := w.Pipelines.Create(ctx, createReq) @@ -93,15 +103,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq pipelines.DeletePipelineRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [PIPELINE_ID]", + Use: "delete PIPELINE_ID", Short: `Delete a pipeline.`, Long: `Delete a pipeline. @@ -112,21 +124,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + deleteReq.PipelineId = args[0] } - deleteReq.PipelineId = args[0] err = w.Pipelines.Delete(ctx, deleteReq) if err != nil { @@ -139,7 +158,7 @@ var deleteCmd = &cobra.Command{ // start get command var getReq pipelines.GetPipelineRequest - +var getJson flags.JsonFlag var getSkipWait bool var getTimeout time.Duration @@ -149,11 +168,12 @@ func init() { getCmd.Flags().BoolVar(&getSkipWait, "no-wait", getSkipWait, `do not wait to reach RUNNING state`) getCmd.Flags().DurationVar(&getTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach RUNNING state`) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [PIPELINE_ID]", + Use: "get PIPELINE_ID", Short: `Get a pipeline.`, Long: `Get a pipeline.`, @@ -162,21 +182,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + getReq.PipelineId = args[0] } - getReq.PipelineId = args[0] response, err := w.Pipelines.Get(ctx, getReq) if err != nil { @@ -189,10 +216,12 @@ var getCmd = &cobra.Command{ // start get-update command var getUpdateReq pipelines.GetUpdateRequest +var getUpdateJson flags.JsonFlag func init() { Cmd.AddCommand(getUpdateCmd) // TODO: short flags + getUpdateCmd.Flags().Var(&getUpdateJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -204,13 +233,26 @@ var getUpdateCmd = &cobra.Command{ Gets an update from an active pipeline.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - getUpdateReq.PipelineId = args[0] - getUpdateReq.UpdateId = args[1] + if cmd.Flags().Changed("json") { + err = getUpdateJson.Unmarshal(&getUpdateReq) + if err != nil { + return err + } + } else { + getUpdateReq.PipelineId = args[0] + getUpdateReq.UpdateId = args[1] + } response, err := w.Pipelines.GetUpdate(ctx, getUpdateReq) if err != nil { @@ -238,7 +280,7 @@ func init() { } var listPipelineEventsCmd = &cobra.Command{ - Use: "list-pipeline-events [PIPELINE_ID]", + Use: "list-pipeline-events PIPELINE_ID", Short: `List pipeline events.`, Long: `List pipeline events. @@ -270,7 +312,6 @@ var listPipelineEventsCmd = &cobra.Command{ return fmt.Errorf("expected to have ") } listPipelineEventsReq.PipelineId = args[0] - } response, err := w.Pipelines.ListPipelineEventsAll(ctx, listPipelineEventsReq) @@ -306,13 +347,23 @@ var listPipelinesCmd = &cobra.Command{ Lists pipelines defined in the Delta Live Tables system.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = listPipelinesJson.Unmarshal(&listPipelinesReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = listPipelinesJson.Unmarshal(&listPipelinesReq) + if err != nil { + return err + } + } else { } response, err := w.Pipelines.ListPipelinesAll(ctx, listPipelinesReq) @@ -326,10 +377,12 @@ var listPipelinesCmd = &cobra.Command{ // start list-updates command var listUpdatesReq pipelines.ListUpdatesRequest +var listUpdatesJson flags.JsonFlag func init() { Cmd.AddCommand(listUpdatesCmd) // TODO: short flags + listUpdatesCmd.Flags().Var(&listUpdatesJson, "json", `either inline JSON string or @path/to/file.json with request body`) listUpdatesCmd.Flags().IntVar(&listUpdatesReq.MaxResults, "max-results", listUpdatesReq.MaxResults, `Max number of entries to return in a single page.`) listUpdatesCmd.Flags().StringVar(&listUpdatesReq.PageToken, "page-token", listUpdatesReq.PageToken, `Page token returned by previous call.`) @@ -338,7 +391,7 @@ func init() { } var listUpdatesCmd = &cobra.Command{ - Use: "list-updates [PIPELINE_ID]", + Use: "list-updates PIPELINE_ID", Short: `List pipeline updates.`, Long: `List pipeline updates. @@ -349,21 +402,28 @@ var listUpdatesCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if cmd.Flags().Changed("json") { + err = listUpdatesJson.Unmarshal(&listUpdatesReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The pipeline to return updates for") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The pipeline to return updates for") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the pipeline to return updates for") + if len(args) != 1 { + return fmt.Errorf("expected to have the pipeline to return updates for") + } + listUpdatesReq.PipelineId = args[0] } - listUpdatesReq.PipelineId = args[0] response, err := w.Pipelines.ListUpdates(ctx, listUpdatesReq) if err != nil { @@ -376,7 +436,7 @@ var listUpdatesCmd = &cobra.Command{ // start reset command var resetReq pipelines.ResetRequest - +var resetJson flags.JsonFlag var resetSkipWait bool var resetTimeout time.Duration @@ -386,11 +446,12 @@ func init() { resetCmd.Flags().BoolVar(&resetSkipWait, "no-wait", resetSkipWait, `do not wait to reach RUNNING state`) resetCmd.Flags().DurationVar(&resetTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach RUNNING state`) // TODO: short flags + resetCmd.Flags().Var(&resetJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var resetCmd = &cobra.Command{ - Use: "reset [PIPELINE_ID]", + Use: "reset PIPELINE_ID", Short: `Reset a pipeline.`, Long: `Reset a pipeline. @@ -401,21 +462,28 @@ var resetCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if cmd.Flags().Changed("json") { + err = resetJson.Unmarshal(&resetReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + resetReq.PipelineId = args[0] } - resetReq.PipelineId = args[0] if resetSkipWait { err = w.Pipelines.Reset(ctx, resetReq) @@ -460,7 +528,7 @@ func init() { } var startUpdateCmd = &cobra.Command{ - Use: "start-update [PIPELINE_ID]", + Use: "start-update PIPELINE_ID", Short: `Queue a pipeline update.`, Long: `Queue a pipeline update. @@ -492,7 +560,6 @@ var startUpdateCmd = &cobra.Command{ return fmt.Errorf("expected to have ") } startUpdateReq.PipelineId = args[0] - } response, err := w.Pipelines.StartUpdate(ctx, startUpdateReq) @@ -506,7 +573,7 @@ var startUpdateCmd = &cobra.Command{ // start stop command var stopReq pipelines.StopRequest - +var stopJson flags.JsonFlag var stopSkipWait bool var stopTimeout time.Duration @@ -516,11 +583,12 @@ func init() { stopCmd.Flags().BoolVar(&stopSkipWait, "no-wait", stopSkipWait, `do not wait to reach IDLE state`) stopCmd.Flags().DurationVar(&stopTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach IDLE state`) // TODO: short flags + stopCmd.Flags().Var(&stopJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var stopCmd = &cobra.Command{ - Use: "stop [PIPELINE_ID]", + Use: "stop PIPELINE_ID", Short: `Stop a pipeline.`, Long: `Stop a pipeline. @@ -531,21 +599,28 @@ var stopCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if cmd.Flags().Changed("json") { + err = stopJson.Unmarshal(&stopReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Pipelines.PipelineStateInfoNameToPipelineIdMap(ctx, pipelines.ListPipelinesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + stopReq.PipelineId = args[0] } - stopReq.PipelineId = args[0] if stopSkipWait { err = w.Pipelines.Stop(ctx, stopReq) @@ -605,7 +680,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [PIPELINE_ID]", + Use: "update PIPELINE_ID", Short: `Edit a pipeline.`, Long: `Edit a pipeline. @@ -637,7 +712,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have unique identifier for this pipeline") } updateReq.PipelineId = args[0] - } err = w.Pipelines.Update(ctx, updateReq) diff --git a/cmd/workspace/policy-families/policy-families.go b/cmd/workspace/policy-families/policy-families.go index 9dbe471d92..284cfc996e 100755 --- a/cmd/workspace/policy-families/policy-families.go +++ b/cmd/workspace/policy-families/policy-families.go @@ -5,6 +5,7 @@ package policy_families import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/compute" "github.com/spf13/cobra" ) @@ -26,10 +27,12 @@ var Cmd = &cobra.Command{ // start get command var getReq compute.GetPolicyFamilyRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -37,12 +40,25 @@ var getCmd = &cobra.Command{ Use: "get POLICY_FAMILY_ID", Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.PolicyFamilyId = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.PolicyFamilyId = args[0] + } response, err := w.PolicyFamilies.Get(ctx, getReq) if err != nil { @@ -55,10 +71,12 @@ var getCmd = &cobra.Command{ // start list command var listReq compute.ListPolicyFamiliesRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().Int64Var(&listReq.MaxResults, "max-results", listReq.MaxResults, `The max number of policy families to return.`) listCmd.Flags().StringVar(&listReq.PageToken, "page-token", listReq.PageToken, `A token that can be used to get the next page of results.`) @@ -69,10 +87,24 @@ var listCmd = &cobra.Command{ Use: "list", Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.PolicyFamilies.ListAll(ctx, listReq) if err != nil { diff --git a/cmd/workspace/providers/providers.go b/cmd/workspace/providers/providers.go index 86283abf1c..21a85ff132 100755 --- a/cmd/workspace/providers/providers.go +++ b/cmd/workspace/providers/providers.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/sharing" "github.com/spf13/cobra" ) @@ -20,10 +21,12 @@ var Cmd = &cobra.Command{ // start create command var createReq sharing.CreateProvider +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().StringVar(&createReq.Comment, "comment", createReq.Comment, `Description about the provider.`) createCmd.Flags().StringVar(&createReq.RecipientProfileStr, "recipient-profile-str", createReq.RecipientProfileStr, `This field is required when the __authentication_type__ is **TOKEN** or not provided.`) @@ -39,15 +42,28 @@ var createCmd = &cobra.Command{ authentication type. The caller must be an admin on the metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - createReq.Name = args[0] - _, err = fmt.Sscan(args[1], &createReq.AuthenticationType) - if err != nil { - return fmt.Errorf("invalid AUTHENTICATION_TYPE: %s", args[1]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + _, err = fmt.Sscan(args[1], &createReq.AuthenticationType) + if err != nil { + return fmt.Errorf("invalid AUTHENTICATION_TYPE: %s", args[1]) + } } response, err := w.Providers.Create(ctx, createReq) @@ -61,15 +77,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq sharing.DeleteProviderRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [NAME]", + Use: "delete NAME", Short: `Delete a provider.`, Long: `Delete a provider. @@ -81,21 +99,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Providers.ProviderInfoNameToMetastoreIdMap(ctx, sharing.ListProvidersRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Name of the provider") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Providers.ProviderInfoNameToMetastoreIdMap(ctx, sharing.ListProvidersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Name of the provider") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have name of the provider") + if len(args) != 1 { + return fmt.Errorf("expected to have name of the provider") + } + deleteReq.Name = args[0] } - deleteReq.Name = args[0] err = w.Providers.Delete(ctx, deleteReq) if err != nil { @@ -108,15 +133,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq sharing.GetProviderRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [NAME]", + Use: "get NAME", Short: `Get a provider.`, Long: `Get a provider. @@ -129,21 +156,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Providers.ProviderInfoNameToMetastoreIdMap(ctx, sharing.ListProvidersRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Name of the provider") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Providers.ProviderInfoNameToMetastoreIdMap(ctx, sharing.ListProvidersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Name of the provider") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have name of the provider") + if len(args) != 1 { + return fmt.Errorf("expected to have name of the provider") + } + getReq.Name = args[0] } - getReq.Name = args[0] response, err := w.Providers.Get(ctx, getReq) if err != nil { @@ -156,10 +190,12 @@ var getCmd = &cobra.Command{ // start list command var listReq sharing.ListProvidersRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.DataProviderGlobalMetastoreId, "data-provider-global-metastore-id", listReq.DataProviderGlobalMetastoreId, `If not provided, all providers will be returned.`) @@ -176,10 +212,24 @@ var listCmd = &cobra.Command{ 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(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.Providers.ListAll(ctx, listReq) if err != nil { @@ -192,15 +242,17 @@ var listCmd = &cobra.Command{ // start list-shares command var listSharesReq sharing.ListSharesRequest +var listSharesJson flags.JsonFlag func init() { Cmd.AddCommand(listSharesCmd) // TODO: short flags + listSharesCmd.Flags().Var(&listSharesJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var listSharesCmd = &cobra.Command{ - Use: "list-shares [NAME]", + Use: "list-shares NAME", Short: `List shares by Provider.`, Long: `List shares by Provider. @@ -213,21 +265,28 @@ var listSharesCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Providers.ProviderInfoNameToMetastoreIdMap(ctx, sharing.ListProvidersRequest{}) + if cmd.Flags().Changed("json") { + err = listSharesJson.Unmarshal(&listSharesReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Name of the provider in which to list shares") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Providers.ProviderInfoNameToMetastoreIdMap(ctx, sharing.ListProvidersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Name of the provider in which to list shares") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have name of the provider in which to list shares") + if len(args) != 1 { + return fmt.Errorf("expected to have name of the provider in which to list shares") + } + listSharesReq.Name = args[0] } - listSharesReq.Name = args[0] response, err := w.Providers.ListShares(ctx, listSharesReq) if err != nil { @@ -240,10 +299,12 @@ var listSharesCmd = &cobra.Command{ // start update command var updateReq sharing.UpdateProvider +var updateJson flags.JsonFlag func init() { Cmd.AddCommand(updateCmd) // TODO: short flags + updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateCmd.Flags().StringVar(&updateReq.Comment, "comment", updateReq.Comment, `Description about the provider.`) updateCmd.Flags().StringVar(&updateReq.Name, "name", updateReq.Name, `The name of the Provider.`) @@ -253,7 +314,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [NAME]", + Use: "update NAME", Short: `Update a provider.`, Long: `Update a provider. @@ -267,21 +328,28 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Providers.ProviderInfoNameToMetastoreIdMap(ctx, sharing.ListProvidersRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The name of the Provider") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Providers.ProviderInfoNameToMetastoreIdMap(ctx, sharing.ListProvidersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The name of the Provider") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the name of the provider") + if len(args) != 1 { + return fmt.Errorf("expected to have the name of the provider") + } + updateReq.Name = args[0] } - updateReq.Name = args[0] response, err := w.Providers.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/queries/queries.go b/cmd/workspace/queries/queries.go index 26c57ceebb..1c81c704c9 100755 --- a/cmd/workspace/queries/queries.go +++ b/cmd/workspace/queries/queries.go @@ -56,13 +56,23 @@ var createCmd = &cobra.Command{ **Note**: You cannot add a visualization until you create the query.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := w.Queries.Create(ctx, createReq) @@ -76,15 +86,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq sql.DeleteQueryRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [QUERY_ID]", + Use: "delete QUERY_ID", Short: `Delete a query.`, Long: `Delete a query. @@ -97,21 +109,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Queries.QueryNameToIdMap(ctx, sql.ListQueriesRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Queries.QueryNameToIdMap(ctx, sql.ListQueriesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + deleteReq.QueryId = args[0] } - deleteReq.QueryId = args[0] err = w.Queries.Delete(ctx, deleteReq) if err != nil { @@ -124,15 +143,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq sql.GetQueryRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [QUERY_ID]", + Use: "get QUERY_ID", Short: `Get a query definition.`, Long: `Get a query definition. @@ -144,21 +165,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Queries.QueryNameToIdMap(ctx, sql.ListQueriesRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Queries.QueryNameToIdMap(ctx, sql.ListQueriesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + getReq.QueryId = args[0] } - getReq.QueryId = args[0] response, err := w.Queries.Get(ctx, getReq) if err != nil { @@ -171,10 +199,12 @@ var getCmd = &cobra.Command{ // start list command var listReq sql.ListQueriesRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.Order, "order", listReq.Order, `Name of query attribute to order by.`) listCmd.Flags().IntVar(&listReq.Page, "page", listReq.Page, `Page number to retrieve.`) @@ -192,10 +222,24 @@ var listCmd = &cobra.Command{ term.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.Queries.ListAll(ctx, listReq) if err != nil { @@ -208,15 +252,17 @@ var listCmd = &cobra.Command{ // start restore command var restoreReq sql.RestoreQueryRequest +var restoreJson flags.JsonFlag func init() { Cmd.AddCommand(restoreCmd) // TODO: short flags + restoreCmd.Flags().Var(&restoreJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var restoreCmd = &cobra.Command{ - Use: "restore [QUERY_ID]", + Use: "restore QUERY_ID", Short: `Restore a query.`, Long: `Restore a query. @@ -228,21 +274,28 @@ var restoreCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Queries.QueryNameToIdMap(ctx, sql.ListQueriesRequest{}) + if cmd.Flags().Changed("json") { + err = restoreJson.Unmarshal(&restoreReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Queries.QueryNameToIdMap(ctx, sql.ListQueriesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have ") + if len(args) != 1 { + return fmt.Errorf("expected to have ") + } + restoreReq.QueryId = args[0] } - restoreReq.QueryId = args[0] err = w.Queries.Restore(ctx, restoreReq) if err != nil { @@ -271,7 +324,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [QUERY_ID]", + Use: "update QUERY_ID", Short: `Change a query definition.`, Long: `Change a query definition. @@ -305,7 +358,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have ") } updateReq.QueryId = args[0] - } response, err := w.Queries.Update(ctx, updateReq) diff --git a/cmd/workspace/query-history/query-history.go b/cmd/workspace/query-history/query-history.go index 0f8852e5e6..0f1e83dcad 100755 --- a/cmd/workspace/query-history/query-history.go +++ b/cmd/workspace/query-history/query-history.go @@ -43,13 +43,23 @@ var listCmd = &cobra.Command{ You can filter by user ID, warehouse ID, status, and time range.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = listJson.Unmarshal(&listReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { } response, err := w.QueryHistory.ListAll(ctx, listReq) diff --git a/cmd/workspace/recipient-activation/recipient-activation.go b/cmd/workspace/recipient-activation/recipient-activation.go index c56c2791bf..8897a61194 100755 --- a/cmd/workspace/recipient-activation/recipient-activation.go +++ b/cmd/workspace/recipient-activation/recipient-activation.go @@ -5,6 +5,7 @@ package recipient_activation import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/sharing" "github.com/spf13/cobra" ) @@ -18,10 +19,12 @@ var Cmd = &cobra.Command{ // start get-activation-url-info command var getActivationUrlInfoReq sharing.GetActivationUrlInfoRequest +var getActivationUrlInfoJson flags.JsonFlag func init() { Cmd.AddCommand(getActivationUrlInfoCmd) // TODO: short flags + getActivationUrlInfoCmd.Flags().Var(&getActivationUrlInfoJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -33,12 +36,25 @@ var getActivationUrlInfoCmd = &cobra.Command{ Gets an activation URL for a share.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getActivationUrlInfoReq.ActivationUrl = args[0] + if cmd.Flags().Changed("json") { + err = getActivationUrlInfoJson.Unmarshal(&getActivationUrlInfoReq) + if err != nil { + return err + } + } else { + getActivationUrlInfoReq.ActivationUrl = args[0] + } err = w.RecipientActivation.GetActivationUrlInfo(ctx, getActivationUrlInfoReq) if err != nil { @@ -51,10 +67,12 @@ var getActivationUrlInfoCmd = &cobra.Command{ // start retrieve-token command var retrieveTokenReq sharing.RetrieveTokenRequest +var retrieveTokenJson flags.JsonFlag func init() { Cmd.AddCommand(retrieveTokenCmd) // TODO: short flags + retrieveTokenCmd.Flags().Var(&retrieveTokenJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -67,12 +85,25 @@ var retrieveTokenCmd = &cobra.Command{ authentication.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - retrieveTokenReq.ActivationUrl = args[0] + if cmd.Flags().Changed("json") { + err = retrieveTokenJson.Unmarshal(&retrieveTokenReq) + if err != nil { + return err + } + } else { + retrieveTokenReq.ActivationUrl = args[0] + } response, err := w.RecipientActivation.RetrieveToken(ctx, retrieveTokenReq) if err != nil { diff --git a/cmd/workspace/recipients/recipients.go b/cmd/workspace/recipients/recipients.go index 208a98bccb..6ccbece9dd 100755 --- a/cmd/workspace/recipients/recipients.go +++ b/cmd/workspace/recipients/recipients.go @@ -47,19 +47,28 @@ var createCmd = &cobra.Command{ **CREATE_RECIPIENT** privilege on the metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - createReq.Name = args[0] - _, err = fmt.Sscan(args[1], &createReq.AuthenticationType) - if err != nil { - return fmt.Errorf("invalid AUTHENTICATION_TYPE: %s", args[1]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + _, err = fmt.Sscan(args[1], &createReq.AuthenticationType) + if err != nil { + return fmt.Errorf("invalid AUTHENTICATION_TYPE: %s", args[1]) + } } response, err := w.Recipients.Create(ctx, createReq) @@ -73,15 +82,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq sharing.DeleteRecipientRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [NAME]", + Use: "delete NAME", Short: `Delete a share recipient.`, Long: `Delete a share recipient. @@ -93,21 +104,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Recipients.RecipientInfoNameToMetastoreIdMap(ctx, sharing.ListRecipientsRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Name of the recipient") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Recipients.RecipientInfoNameToMetastoreIdMap(ctx, sharing.ListRecipientsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Name of the recipient") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have name of the recipient") + if len(args) != 1 { + return fmt.Errorf("expected to have name of the recipient") + } + deleteReq.Name = args[0] } - deleteReq.Name = args[0] err = w.Recipients.Delete(ctx, deleteReq) if err != nil { @@ -120,15 +138,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq sharing.GetRecipientRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [NAME]", + Use: "get NAME", Short: `Get a share recipient.`, Long: `Get a share recipient. @@ -141,21 +161,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Recipients.RecipientInfoNameToMetastoreIdMap(ctx, sharing.ListRecipientsRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Name of the recipient") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Recipients.RecipientInfoNameToMetastoreIdMap(ctx, sharing.ListRecipientsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Name of the recipient") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have name of the recipient") + if len(args) != 1 { + return fmt.Errorf("expected to have name of the recipient") + } + getReq.Name = args[0] } - getReq.Name = args[0] response, err := w.Recipients.Get(ctx, getReq) if err != nil { @@ -168,10 +195,12 @@ var getCmd = &cobra.Command{ // start list command var listReq sharing.ListRecipientsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.DataRecipientGlobalMetastoreId, "data-recipient-global-metastore-id", listReq.DataRecipientGlobalMetastoreId, `If not provided, all recipients will be returned.`) @@ -188,10 +217,24 @@ var listCmd = &cobra.Command{ 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(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.Recipients.ListAll(ctx, listReq) if err != nil { @@ -204,10 +247,12 @@ var listCmd = &cobra.Command{ // start rotate-token command var rotateTokenReq sharing.RotateRecipientToken +var rotateTokenJson flags.JsonFlag func init() { Cmd.AddCommand(rotateTokenCmd) // TODO: short flags + rotateTokenCmd.Flags().Var(&rotateTokenJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -220,16 +265,29 @@ var rotateTokenCmd = &cobra.Command{ the provided token info. The caller must be the owner of the recipient.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - _, err = fmt.Sscan(args[0], &rotateTokenReq.ExistingTokenExpireInSeconds) - if err != nil { - return fmt.Errorf("invalid EXISTING_TOKEN_EXPIRE_IN_SECONDS: %s", args[0]) + if cmd.Flags().Changed("json") { + err = rotateTokenJson.Unmarshal(&rotateTokenReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &rotateTokenReq.ExistingTokenExpireInSeconds) + if err != nil { + return fmt.Errorf("invalid EXISTING_TOKEN_EXPIRE_IN_SECONDS: %s", args[0]) + } + rotateTokenReq.Name = args[1] } - rotateTokenReq.Name = args[1] response, err := w.Recipients.RotateToken(ctx, rotateTokenReq) if err != nil { @@ -242,15 +300,17 @@ var rotateTokenCmd = &cobra.Command{ // start share-permissions command var sharePermissionsReq sharing.SharePermissionsRequest +var sharePermissionsJson flags.JsonFlag func init() { Cmd.AddCommand(sharePermissionsCmd) // TODO: short flags + sharePermissionsCmd.Flags().Var(&sharePermissionsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var sharePermissionsCmd = &cobra.Command{ - Use: "share-permissions [NAME]", + Use: "share-permissions NAME", Short: `Get recipient share permissions.`, Long: `Get recipient share permissions. @@ -262,21 +322,28 @@ var sharePermissionsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Recipients.RecipientInfoNameToMetastoreIdMap(ctx, sharing.ListRecipientsRequest{}) + if cmd.Flags().Changed("json") { + err = sharePermissionsJson.Unmarshal(&sharePermissionsReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The name of the Recipient") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Recipients.RecipientInfoNameToMetastoreIdMap(ctx, sharing.ListRecipientsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The name of the Recipient") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the name of the recipient") + if len(args) != 1 { + return fmt.Errorf("expected to have the name of the recipient") + } + sharePermissionsReq.Name = args[0] } - sharePermissionsReq.Name = args[0] response, err := w.Recipients.SharePermissions(ctx, sharePermissionsReq) if err != nil { @@ -305,7 +372,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [NAME]", + Use: "update NAME", Short: `Update a share recipient.`, Long: `Update a share recipient. @@ -339,7 +406,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have name of recipient") } updateReq.Name = args[0] - } err = w.Recipients.Update(ctx, updateReq) diff --git a/cmd/workspace/repos/repos.go b/cmd/workspace/repos/repos.go index a0c2878ff1..403c8a9575 100755 --- a/cmd/workspace/repos/repos.go +++ b/cmd/workspace/repos/repos.go @@ -52,17 +52,26 @@ var createCmd = &cobra.Command{ unlike repos created in the browser.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Url = args[0] + createReq.Provider = args[1] } - createReq.Url = args[0] - createReq.Provider = args[1] response, err := w.Repos.Create(ctx, createReq) if err != nil { @@ -75,15 +84,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq workspace.DeleteRepoRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [REPO_ID]", + Use: "delete REPO_ID", Short: `Delete a repo.`, Long: `Delete a repo. @@ -94,23 +105,30 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Repos.RepoInfoPathToIdMap(ctx, workspace.ListReposRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID for the corresponding repo to access") + } else { + if len(args) == 0 { + names, err := w.Repos.RepoInfoPathToIdMap(ctx, workspace.ListReposRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding repo to access") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding repo to access") + } + _, err = fmt.Sscan(args[0], &deleteReq.RepoId) if err != nil { - return err + return fmt.Errorf("invalid REPO_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id for the corresponding repo to access") - } - _, err = fmt.Sscan(args[0], &deleteReq.RepoId) - if err != nil { - return fmt.Errorf("invalid REPO_ID: %s", args[0]) } err = w.Repos.Delete(ctx, deleteReq) @@ -124,15 +142,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq workspace.GetRepoRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [REPO_ID]", + Use: "get REPO_ID", Short: `Get a repo.`, Long: `Get a repo. @@ -143,23 +163,30 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Repos.RepoInfoPathToIdMap(ctx, workspace.ListReposRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID for the corresponding repo to access") + } else { + if len(args) == 0 { + names, err := w.Repos.RepoInfoPathToIdMap(ctx, workspace.ListReposRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID for the corresponding repo to access") + if err != nil { + return err + } + args = append(args, id) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the id for the corresponding repo to access") + } + _, err = fmt.Sscan(args[0], &getReq.RepoId) if err != nil { - return err + return fmt.Errorf("invalid REPO_ID: %s", args[0]) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id for the corresponding repo to access") - } - _, err = fmt.Sscan(args[0], &getReq.RepoId) - if err != nil { - return fmt.Errorf("invalid REPO_ID: %s", args[0]) } response, err := w.Repos.Get(ctx, getReq) @@ -173,10 +200,12 @@ var getCmd = &cobra.Command{ // start list command var listReq workspace.ListReposRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.NextPageToken, "next-page-token", listReq.NextPageToken, `Token used to get the next page of results.`) listCmd.Flags().StringVar(&listReq.PathPrefix, "path-prefix", listReq.PathPrefix, `Filters repos that have paths starting with the given path prefix.`) @@ -192,10 +221,24 @@ var listCmd = &cobra.Command{ paginated with each page containing twenty repos.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.Repos.ListAll(ctx, listReq) if err != nil { @@ -222,7 +265,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [REPO_ID]", + Use: "update REPO_ID", Short: `Update a repo.`, Long: `Update a repo. @@ -258,7 +301,6 @@ var updateCmd = &cobra.Command{ if err != nil { return fmt.Errorf("invalid REPO_ID: %s", args[0]) } - } err = w.Repos.Update(ctx, updateReq) diff --git a/cmd/workspace/schemas/schemas.go b/cmd/workspace/schemas/schemas.go index 635f4e6094..1f62d0cf45 100755 --- a/cmd/workspace/schemas/schemas.go +++ b/cmd/workspace/schemas/schemas.go @@ -48,17 +48,26 @@ var createCmd = &cobra.Command{ catalog.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + createReq.CatalogName = args[1] } - createReq.Name = args[0] - createReq.CatalogName = args[1] response, err := w.Schemas.Create(ctx, createReq) if err != nil { @@ -71,15 +80,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteSchemaRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [FULL_NAME]", + Use: "delete FULL_NAME", Short: `Delete a schema.`, Long: `Delete a schema. @@ -91,21 +102,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Schemas.SchemaInfoNameToFullNameMap(ctx, catalog.ListSchemasRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Full name of the schema") - if err != nil { - 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, "Full name of the schema") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have full name of the schema") + if len(args) != 1 { + return fmt.Errorf("expected to have full name of the schema") + } + deleteReq.FullName = args[0] } - deleteReq.FullName = args[0] err = w.Schemas.Delete(ctx, deleteReq) if err != nil { @@ -118,15 +136,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq catalog.GetSchemaRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [FULL_NAME]", + Use: "get FULL_NAME", Short: `Get a schema.`, Long: `Get a schema. @@ -139,21 +159,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Schemas.SchemaInfoNameToFullNameMap(ctx, catalog.ListSchemasRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Full name of the schema") - if err != nil { - 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, "Full name of the schema") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have full name of the schema") + if len(args) != 1 { + return fmt.Errorf("expected to have full name of the schema") + } + getReq.FullName = args[0] } - getReq.FullName = args[0] response, err := w.Schemas.Get(ctx, getReq) if err != nil { @@ -166,15 +193,17 @@ var getCmd = &cobra.Command{ // start list command var listReq catalog.ListSchemasRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var listCmd = &cobra.Command{ - Use: "list [CATALOG_NAME]", + Use: "list CATALOG_NAME", Short: `List schemas.`, Long: `List schemas. @@ -189,21 +218,28 @@ var listCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Schemas.SchemaInfoNameToFullNameMap(ctx, catalog.ListSchemasRequest{}) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Parent catalog for schemas of interest") - if err != nil { - 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) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have parent catalog for schemas of interest") + if len(args) != 1 { + return fmt.Errorf("expected to have parent catalog for schemas of interest") + } + listReq.CatalogName = args[0] } - listReq.CatalogName = args[0] response, err := w.Schemas.ListAll(ctx, listReq) if err != nil { @@ -231,7 +267,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [FULL_NAME]", + Use: "update FULL_NAME", Short: `Update a schema.`, Long: `Update a schema. @@ -267,7 +303,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have full name of the schema") } updateReq.FullName = args[0] - } response, err := w.Schemas.Update(ctx, updateReq) diff --git a/cmd/workspace/secrets/secrets.go b/cmd/workspace/secrets/secrets.go index 995746c814..a7526a6545 100755 --- a/cmd/workspace/secrets/secrets.go +++ b/cmd/workspace/secrets/secrets.go @@ -55,16 +55,25 @@ var createScopeCmd = &cobra.Command{ in a workspace is 100.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = createScopeJson.Unmarshal(&createScopeReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createScopeJson.Unmarshal(&createScopeReq) + if err != nil { + return err + } + } else { + createScopeReq.Scope = args[0] } - createScopeReq.Scope = args[0] err = w.Secrets.CreateScope(ctx, createScopeReq) if err != nil { @@ -77,10 +86,12 @@ var createScopeCmd = &cobra.Command{ // start delete-acl command var deleteAclReq workspace.DeleteAcl +var deleteAclJson flags.JsonFlag func init() { Cmd.AddCommand(deleteAclCmd) // TODO: short flags + deleteAclCmd.Flags().Var(&deleteAclJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -97,13 +108,26 @@ var deleteAclCmd = &cobra.Command{ API call.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - deleteAclReq.Scope = args[0] - deleteAclReq.Principal = args[1] + if cmd.Flags().Changed("json") { + err = deleteAclJson.Unmarshal(&deleteAclReq) + if err != nil { + return err + } + } else { + deleteAclReq.Scope = args[0] + deleteAclReq.Principal = args[1] + } err = w.Secrets.DeleteAcl(ctx, deleteAclReq) if err != nil { @@ -116,10 +140,12 @@ var deleteAclCmd = &cobra.Command{ // start delete-scope command var deleteScopeReq workspace.DeleteScope +var deleteScopeJson flags.JsonFlag func init() { Cmd.AddCommand(deleteScopeCmd) // TODO: short flags + deleteScopeCmd.Flags().Var(&deleteScopeJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -135,12 +161,25 @@ var deleteScopeCmd = &cobra.Command{ call.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteScopeReq.Scope = args[0] + if cmd.Flags().Changed("json") { + err = deleteScopeJson.Unmarshal(&deleteScopeReq) + if err != nil { + return err + } + } else { + deleteScopeReq.Scope = args[0] + } err = w.Secrets.DeleteScope(ctx, deleteScopeReq) if err != nil { @@ -153,10 +192,12 @@ var deleteScopeCmd = &cobra.Command{ // start delete-secret command var deleteSecretReq workspace.DeleteSecret +var deleteSecretJson flags.JsonFlag func init() { Cmd.AddCommand(deleteSecretCmd) // TODO: short flags + deleteSecretCmd.Flags().Var(&deleteSecretJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -173,13 +214,26 @@ var deleteSecretCmd = &cobra.Command{ API call.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - deleteSecretReq.Scope = args[0] - deleteSecretReq.Key = args[1] + if cmd.Flags().Changed("json") { + err = deleteSecretJson.Unmarshal(&deleteSecretReq) + if err != nil { + return err + } + } else { + deleteSecretReq.Scope = args[0] + deleteSecretReq.Key = args[1] + } err = w.Secrets.DeleteSecret(ctx, deleteSecretReq) if err != nil { @@ -192,10 +246,12 @@ var deleteSecretCmd = &cobra.Command{ // start get-acl command var getAclReq workspace.GetAclRequest +var getAclJson flags.JsonFlag func init() { Cmd.AddCommand(getAclCmd) // TODO: short flags + getAclCmd.Flags().Var(&getAclJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -212,13 +268,26 @@ var getAclCmd = &cobra.Command{ call.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - getAclReq.Scope = args[0] - getAclReq.Principal = args[1] + if cmd.Flags().Changed("json") { + err = getAclJson.Unmarshal(&getAclReq) + if err != nil { + return err + } + } else { + getAclReq.Scope = args[0] + getAclReq.Principal = args[1] + } response, err := w.Secrets.GetAcl(ctx, getAclReq) if err != nil { @@ -231,10 +300,12 @@ var getAclCmd = &cobra.Command{ // start list-acls command var listAclsReq workspace.ListAclsRequest +var listAclsJson flags.JsonFlag func init() { Cmd.AddCommand(listAclsCmd) // TODO: short flags + listAclsCmd.Flags().Var(&listAclsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -251,12 +322,25 @@ var listAclsCmd = &cobra.Command{ call.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - listAclsReq.Scope = args[0] + if cmd.Flags().Changed("json") { + err = listAclsJson.Unmarshal(&listAclsReq) + if err != nil { + return err + } + } else { + listAclsReq.Scope = args[0] + } response, err := w.Secrets.ListAclsAll(ctx, listAclsReq) if err != nil { @@ -299,10 +383,12 @@ var listScopesCmd = &cobra.Command{ // start list-secrets command var listSecretsReq workspace.ListSecretsRequest +var listSecretsJson flags.JsonFlag func init() { Cmd.AddCommand(listSecretsCmd) // TODO: short flags + listSecretsCmd.Flags().Var(&listSecretsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -321,12 +407,25 @@ var listSecretsCmd = &cobra.Command{ call.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - listSecretsReq.Scope = args[0] + if cmd.Flags().Changed("json") { + err = listSecretsJson.Unmarshal(&listSecretsReq) + if err != nil { + return err + } + } else { + listSecretsReq.Scope = args[0] + } response, err := w.Secrets.ListSecretsAll(ctx, listSecretsReq) if err != nil { @@ -339,10 +438,12 @@ var listSecretsCmd = &cobra.Command{ // start put-acl command var putAclReq workspace.PutAcl +var putAclJson flags.JsonFlag func init() { Cmd.AddCommand(putAclCmd) // TODO: short flags + putAclCmd.Flags().Var(&putAclJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -379,16 +480,29 @@ var putAclCmd = &cobra.Command{ call.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - putAclReq.Scope = args[0] - putAclReq.Principal = args[1] - _, err = fmt.Sscan(args[2], &putAclReq.Permission) - if err != nil { - return fmt.Errorf("invalid PERMISSION: %s", args[2]) + if cmd.Flags().Changed("json") { + err = putAclJson.Unmarshal(&putAclReq) + if err != nil { + return err + } + } else { + putAclReq.Scope = args[0] + putAclReq.Principal = args[1] + _, err = fmt.Sscan(args[2], &putAclReq.Permission) + if err != nil { + return fmt.Errorf("invalid PERMISSION: %s", args[2]) + } } err = w.Secrets.PutAcl(ctx, putAclReq) @@ -402,10 +516,12 @@ var putAclCmd = &cobra.Command{ // start put-secret command var putSecretReq workspace.PutSecret +var putSecretJson flags.JsonFlag func init() { Cmd.AddCommand(putSecretCmd) // TODO: short flags + putSecretCmd.Flags().Var(&putSecretJson, "json", `either inline JSON string or @path/to/file.json with request body`) putSecretCmd.Flags().StringVar(&putSecretReq.BytesValue, "bytes-value", putSecretReq.BytesValue, `If specified, value will be stored as bytes.`) putSecretCmd.Flags().StringVar(&putSecretReq.StringValue, "string-value", putSecretReq.StringValue, `If specified, note that the value will be stored in UTF-8 (MB4) form.`) @@ -438,13 +554,26 @@ var putSecretCmd = &cobra.Command{ API call.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - putSecretReq.Scope = args[0] - putSecretReq.Key = args[1] + if cmd.Flags().Changed("json") { + err = putSecretJson.Unmarshal(&putSecretReq) + if err != nil { + return err + } + } else { + putSecretReq.Scope = args[0] + putSecretReq.Key = args[1] + } err = w.Secrets.PutSecret(ctx, putSecretReq) if err != nil { diff --git a/cmd/workspace/service-principals/service-principals.go b/cmd/workspace/service-principals/service-principals.go index dc1abde08e..675ce89914 100755 --- a/cmd/workspace/service-principals/service-principals.go +++ b/cmd/workspace/service-principals/service-principals.go @@ -52,13 +52,23 @@ var createCmd = &cobra.Command{ Creates a new service principal in the Databricks workspace.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := w.ServicePrincipals.Create(ctx, createReq) @@ -72,15 +82,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq iam.DeleteServicePrincipalRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [ID]", + Use: "delete ID", Short: `Delete a service principal.`, Long: `Delete a service principal. @@ -91,21 +103,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks workspace") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks workspace") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a service principal in the databricks workspace") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a service principal in the databricks workspace") + } + deleteReq.Id = args[0] } - deleteReq.Id = args[0] err = w.ServicePrincipals.Delete(ctx, deleteReq) if err != nil { @@ -118,15 +137,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq iam.GetServicePrincipalRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [ID]", + Use: "get ID", Short: `Get service principal details.`, Long: `Get service principal details. @@ -138,21 +159,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks workspace") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.ServicePrincipals.ServicePrincipalDisplayNameToIdMap(ctx, iam.ListServicePrincipalsRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a service principal in the Databricks workspace") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a service principal in the databricks workspace") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a service principal in the databricks workspace") + } + getReq.Id = args[0] } - getReq.Id = args[0] response, err := w.ServicePrincipals.Get(ctx, getReq) if err != nil { @@ -165,10 +193,12 @@ var getCmd = &cobra.Command{ // start list command var listReq iam.ListServicePrincipalsRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.Attributes, "attributes", listReq.Attributes, `Comma-separated list of attributes to return in response.`) listCmd.Flags().IntVar(&listReq.Count, "count", listReq.Count, `Desired number of results per page.`) @@ -188,10 +218,24 @@ var listCmd = &cobra.Command{ Gets the set of service principals associated with a Databricks workspace.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.ServicePrincipals.ListAll(ctx, listReq) if err != nil { @@ -216,7 +260,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch [ID]", + Use: "patch ID", Short: `Update service principal details.`, Long: `Update service principal details. @@ -249,7 +293,6 @@ var patchCmd = &cobra.Command{ return fmt.Errorf("expected to have unique id for a service principal in the databricks workspace") } patchReq.Id = args[0] - } err = w.ServicePrincipals.Patch(ctx, patchReq) @@ -282,7 +325,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [ID]", + Use: "update ID", Short: `Replace service principal.`, Long: `Replace service principal. @@ -316,7 +359,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have databricks service principal id") } updateReq.Id = args[0] - } err = w.ServicePrincipals.Update(ctx, updateReq) diff --git a/cmd/workspace/serving-endpoints/serving-endpoints.go b/cmd/workspace/serving-endpoints/serving-endpoints.go index 059e1dfd44..72dbd94c51 100755 --- a/cmd/workspace/serving-endpoints/serving-endpoints.go +++ b/cmd/workspace/serving-endpoints/serving-endpoints.go @@ -35,10 +35,12 @@ var Cmd = &cobra.Command{ // start build-logs command var buildLogsReq serving.BuildLogsRequest +var buildLogsJson flags.JsonFlag func init() { Cmd.AddCommand(buildLogsCmd) // TODO: short flags + buildLogsCmd.Flags().Var(&buildLogsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -51,13 +53,26 @@ var buildLogsCmd = &cobra.Command{ Retrieves the build logs associated with the provided served model.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - buildLogsReq.Name = args[0] - buildLogsReq.ServedModelName = args[1] + if cmd.Flags().Changed("json") { + err = buildLogsJson.Unmarshal(&buildLogsReq) + if err != nil { + return err + } + } else { + buildLogsReq.Name = args[0] + buildLogsReq.ServedModelName = args[1] + } response, err := w.ServingEndpoints.BuildLogs(ctx, buildLogsReq) if err != nil { @@ -94,14 +109,17 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - createReq.Name = args[0] - _, err = fmt.Sscan(args[1], &createReq.Config) - if err != nil { - return fmt.Errorf("invalid CONFIG: %s", args[1]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + _, err = fmt.Sscan(args[1], &createReq.Config) + if err != nil { + return fmt.Errorf("invalid CONFIG: %s", args[1]) + } } if createSkipWait { @@ -133,10 +151,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq serving.DeleteServingEndpointRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -146,12 +166,25 @@ var deleteCmd = &cobra.Command{ Long: `Delete a serving endpoint.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.Name = args[0] + } err = w.ServingEndpoints.Delete(ctx, deleteReq) if err != nil { @@ -164,10 +197,12 @@ var deleteCmd = &cobra.Command{ // start export-metrics command var exportMetricsReq serving.ExportMetricsRequest +var exportMetricsJson flags.JsonFlag func init() { Cmd.AddCommand(exportMetricsCmd) // TODO: short flags + exportMetricsCmd.Flags().Var(&exportMetricsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -181,12 +216,25 @@ var exportMetricsCmd = &cobra.Command{ Prometheus or OpenMetrics exposition format.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - exportMetricsReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = exportMetricsJson.Unmarshal(&exportMetricsReq) + if err != nil { + return err + } + } else { + exportMetricsReq.Name = args[0] + } err = w.ServingEndpoints.ExportMetrics(ctx, exportMetricsReq) if err != nil { @@ -199,10 +247,12 @@ var exportMetricsCmd = &cobra.Command{ // start get command var getReq serving.GetServingEndpointRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -214,12 +264,25 @@ var getCmd = &cobra.Command{ Retrieves the details for a single serving endpoint.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.Name = args[0] + } response, err := w.ServingEndpoints.Get(ctx, getReq) if err != nil { @@ -257,10 +320,12 @@ var listCmd = &cobra.Command{ // start logs command var logsReq serving.LogsRequest +var logsJson flags.JsonFlag func init() { Cmd.AddCommand(logsCmd) // TODO: short flags + logsCmd.Flags().Var(&logsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -273,13 +338,26 @@ var logsCmd = &cobra.Command{ Retrieves the service logs associated with the provided served model.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - logsReq.Name = args[0] - logsReq.ServedModelName = args[1] + if cmd.Flags().Changed("json") { + err = logsJson.Unmarshal(&logsReq) + if err != nil { + return err + } + } else { + logsReq.Name = args[0] + logsReq.ServedModelName = args[1] + } response, err := w.ServingEndpoints.Logs(ctx, logsReq) if err != nil { @@ -292,10 +370,12 @@ var logsCmd = &cobra.Command{ // start query command var queryReq serving.QueryRequest +var queryJson flags.JsonFlag func init() { Cmd.AddCommand(queryCmd) // TODO: short flags + queryCmd.Flags().Var(&queryJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -305,12 +385,25 @@ var queryCmd = &cobra.Command{ Long: `Query a serving endpoint with provided model input.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - queryReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = queryJson.Unmarshal(&queryReq) + if err != nil { + return err + } + } else { + queryReq.Name = args[0] + } response, err := w.ServingEndpoints.Query(ctx, queryReq) if err != nil { @@ -354,15 +447,18 @@ var updateConfigCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = updateConfigJson.Unmarshal(&updateConfigReq) - if err != nil { - return err - } - _, err = fmt.Sscan(args[0], &updateConfigReq.ServedModels) - if err != nil { - return fmt.Errorf("invalid SERVED_MODELS: %s", args[0]) + if cmd.Flags().Changed("json") { + err = updateConfigJson.Unmarshal(&updateConfigReq) + if err != nil { + return err + } + } else { + _, err = fmt.Sscan(args[0], &updateConfigReq.ServedModels) + if err != nil { + return fmt.Errorf("invalid SERVED_MODELS: %s", args[0]) + } + updateConfigReq.Name = args[1] } - updateConfigReq.Name = args[1] if updateConfigSkipWait { response, err := w.ServingEndpoints.UpdateConfig(ctx, updateConfigReq) diff --git a/cmd/workspace/shares/shares.go b/cmd/workspace/shares/shares.go index ff52dde3f1..49d12ff25c 100755 --- a/cmd/workspace/shares/shares.go +++ b/cmd/workspace/shares/shares.go @@ -19,10 +19,12 @@ var Cmd = &cobra.Command{ // start create command var createReq sharing.CreateShare +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().StringVar(&createReq.Comment, "comment", createReq.Comment, `User-provided free-form text description.`) @@ -38,12 +40,25 @@ var createCmd = &cobra.Command{ **CREATE_SHARE** privilege on the metastore.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - createReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.Name = args[0] + } response, err := w.Shares.Create(ctx, createReq) if err != nil { @@ -56,10 +71,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq sharing.DeleteShareRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -72,12 +89,25 @@ var deleteCmd = &cobra.Command{ the share.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - deleteReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.Name = args[0] + } err = w.Shares.Delete(ctx, deleteReq) if err != nil { @@ -90,10 +120,12 @@ var deleteCmd = &cobra.Command{ // start get command var getReq sharing.GetShareRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) getCmd.Flags().BoolVar(&getReq.IncludeSharedData, "include-shared-data", getReq.IncludeSharedData, `Query for data to include in the share.`) @@ -108,12 +140,25 @@ var getCmd = &cobra.Command{ admin or the owner of the share.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.Name = args[0] + } response, err := w.Shares.Get(ctx, getReq) if err != nil { @@ -155,10 +200,12 @@ var listCmd = &cobra.Command{ // start share-permissions command var sharePermissionsReq sharing.SharePermissionsRequest +var sharePermissionsJson flags.JsonFlag func init() { Cmd.AddCommand(sharePermissionsCmd) // TODO: short flags + sharePermissionsCmd.Flags().Var(&sharePermissionsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -171,12 +218,25 @@ var sharePermissionsCmd = &cobra.Command{ metastore admin or the owner of the share.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - sharePermissionsReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = sharePermissionsJson.Unmarshal(&sharePermissionsReq) + if err != nil { + return err + } + } else { + sharePermissionsReq.Name = args[0] + } response, err := w.Shares.SharePermissions(ctx, sharePermissionsReq) if err != nil { @@ -224,16 +284,25 @@ var updateCmd = &cobra.Command{ Table removals through **update** do not require additional privileges.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.Name = args[0] } - updateReq.Name = args[0] response, err := w.Shares.Update(ctx, updateReq) if err != nil { @@ -269,16 +338,25 @@ var updatePermissionsCmd = &cobra.Command{ recipient revocations do not require additional privileges.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = updatePermissionsJson.Unmarshal(&updatePermissionsReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = updatePermissionsJson.Unmarshal(&updatePermissionsReq) + if err != nil { + return err + } + } else { + updatePermissionsReq.Name = args[0] } - updatePermissionsReq.Name = args[0] err = w.Shares.UpdatePermissions(ctx, updatePermissionsReq) if err != nil { diff --git a/cmd/workspace/storage-credentials/storage-credentials.go b/cmd/workspace/storage-credentials/storage-credentials.go index bacbfef134..4ff2b1a3e0 100755 --- a/cmd/workspace/storage-credentials/storage-credentials.go +++ b/cmd/workspace/storage-credentials/storage-credentials.go @@ -50,7 +50,7 @@ func init() { } var createCmd = &cobra.Command{ - Use: "create [NAME]", + Use: "create NAME", Short: `Create a storage credential.`, Long: `Create a storage credential. @@ -88,7 +88,6 @@ var createCmd = &cobra.Command{ return fmt.Errorf("expected to have the credential name") } createReq.Name = args[0] - } response, err := w.StorageCredentials.Create(ctx, createReq) @@ -102,17 +101,19 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteStorageCredentialRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) deleteCmd.Flags().BoolVar(&deleteReq.Force, "force", deleteReq.Force, `Force deletion even if there are dependent external locations or external tables.`) } var deleteCmd = &cobra.Command{ - Use: "delete [NAME]", + Use: "delete NAME", Short: `Delete a credential.`, Long: `Delete a credential. @@ -124,21 +125,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.StorageCredentials.StorageCredentialInfoNameToIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Name of the storage credential") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.StorageCredentials.StorageCredentialInfoNameToIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Name of the storage credential") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have name of the storage credential") + if len(args) != 1 { + return fmt.Errorf("expected to have name of the storage credential") + } + deleteReq.Name = args[0] } - deleteReq.Name = args[0] err = w.StorageCredentials.Delete(ctx, deleteReq) if err != nil { @@ -151,15 +159,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq catalog.GetStorageCredentialRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [NAME]", + Use: "get NAME", Short: `Get a credential.`, Long: `Get a credential. @@ -172,21 +182,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.StorageCredentials.StorageCredentialInfoNameToIdMap(ctx) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Name of the storage credential") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.StorageCredentials.StorageCredentialInfoNameToIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Name of the storage credential") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have name of the storage credential") + if len(args) != 1 { + return fmt.Errorf("expected to have name of the storage credential") + } + getReq.Name = args[0] } - getReq.Name = args[0] response, err := w.StorageCredentials.Get(ctx, getReq) if err != nil { @@ -250,7 +267,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [NAME]", + Use: "update NAME", Short: `Update a credential.`, Long: `Update a credential. @@ -284,7 +301,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have the credential name") } updateReq.Name = args[0] - } response, err := w.StorageCredentials.Update(ctx, updateReq) @@ -334,13 +350,23 @@ var validateCmd = &cobra.Command{ credential.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = validateJson.Unmarshal(&validateReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = validateJson.Unmarshal(&validateReq) + if err != nil { + return err + } + } else { } response, err := w.StorageCredentials.Validate(ctx, validateReq) diff --git a/cmd/workspace/table-constraints/table-constraints.go b/cmd/workspace/table-constraints/table-constraints.go index 53a0e4f0c4..2ff19ffd21 100755 --- a/cmd/workspace/table-constraints/table-constraints.go +++ b/cmd/workspace/table-constraints/table-constraints.go @@ -63,14 +63,17 @@ var createCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err - } - createReq.FullNameArg = args[0] - _, err = fmt.Sscan(args[1], &createReq.Constraint) - if err != nil { - return fmt.Errorf("invalid CONSTRAINT: %s", args[1]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.FullNameArg = args[0] + _, err = fmt.Sscan(args[1], &createReq.Constraint) + if err != nil { + return fmt.Errorf("invalid CONSTRAINT: %s", args[1]) + } } response, err := w.TableConstraints.Create(ctx, createReq) @@ -84,10 +87,12 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteTableConstraintRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -107,16 +112,29 @@ var deleteCmd = &cobra.Command{ on the table's schema, and be the owner of the table.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(3), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(3) + 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) - deleteReq.FullName = args[0] - deleteReq.ConstraintName = args[1] - _, err = fmt.Sscan(args[2], &deleteReq.Cascade) - if err != nil { - return fmt.Errorf("invalid CASCADE: %s", args[2]) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) + if err != nil { + return err + } + } else { + deleteReq.FullName = args[0] + deleteReq.ConstraintName = args[1] + _, err = fmt.Sscan(args[2], &deleteReq.Cascade) + if err != nil { + return fmt.Errorf("invalid CASCADE: %s", args[2]) + } } err = w.TableConstraints.Delete(ctx, deleteReq) diff --git a/cmd/workspace/tables/tables.go b/cmd/workspace/tables/tables.go index c832d2f16b..622cbe8a96 100755 --- a/cmd/workspace/tables/tables.go +++ b/cmd/workspace/tables/tables.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" ) @@ -28,15 +29,17 @@ var Cmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteTableRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [FULL_NAME]", + Use: "delete FULL_NAME", Short: `Delete a table.`, Long: `Delete a table. @@ -51,21 +54,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Tables.TableInfoNameToTableIdMap(ctx, catalog.ListTablesRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Full name of the table") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Tables.TableInfoNameToTableIdMap(ctx, catalog.ListTablesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Full name of the table") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have full name of the table") + if len(args) != 1 { + return fmt.Errorf("expected to have full name of the table") + } + deleteReq.FullName = args[0] } - deleteReq.FullName = args[0] err = w.Tables.Delete(ctx, deleteReq) if err != nil { @@ -78,17 +88,19 @@ var deleteCmd = &cobra.Command{ // start get command var getReq catalog.GetTableRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) getCmd.Flags().BoolVar(&getReq.IncludeDeltaMetadata, "include-delta-metadata", getReq.IncludeDeltaMetadata, `Whether delta metadata should be included in the response.`) } var getCmd = &cobra.Command{ - Use: "get [FULL_NAME]", + Use: "get FULL_NAME", Short: `Get a table.`, Long: `Get a table. @@ -103,21 +115,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Tables.TableInfoNameToTableIdMap(ctx, catalog.ListTablesRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Full name of the table") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Tables.TableInfoNameToTableIdMap(ctx, catalog.ListTablesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Full name of the table") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have full name of the table") + if len(args) != 1 { + return fmt.Errorf("expected to have full name of the table") + } + getReq.FullName = args[0] } - getReq.FullName = args[0] response, err := w.Tables.Get(ctx, getReq) if err != nil { @@ -130,10 +149,12 @@ var getCmd = &cobra.Command{ // start list command var listReq catalog.ListTablesRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().BoolVar(&listReq.IncludeDeltaMetadata, "include-delta-metadata", listReq.IncludeDeltaMetadata, `Whether delta metadata should be included in the response.`) listCmd.Flags().IntVar(&listReq.MaxResults, "max-results", listReq.MaxResults, `Maximum number of tables to return (page length).`) @@ -154,13 +175,26 @@ var listCmd = &cobra.Command{ specific ordering of the elements in the array.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - listReq.CatalogName = args[0] - listReq.SchemaName = args[1] + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + listReq.CatalogName = args[0] + listReq.SchemaName = args[1] + } response, err := w.Tables.ListAll(ctx, listReq) if err != nil { @@ -173,10 +207,12 @@ var listCmd = &cobra.Command{ // start list-summaries command var listSummariesReq catalog.ListSummariesRequest +var listSummariesJson flags.JsonFlag func init() { Cmd.AddCommand(listSummariesCmd) // TODO: short flags + listSummariesCmd.Flags().Var(&listSummariesJson, "json", `either inline JSON string or @path/to/file.json with request body`) listSummariesCmd.Flags().IntVar(&listSummariesReq.MaxResults, "max-results", listSummariesReq.MaxResults, `Maximum number of tables to return (page length).`) listSummariesCmd.Flags().StringVar(&listSummariesReq.PageToken, "page-token", listSummariesReq.PageToken, `Opaque token to send for the next page of results (pagination).`) @@ -186,7 +222,7 @@ func init() { } var listSummariesCmd = &cobra.Command{ - Use: "list-summaries [CATALOG_NAME]", + Use: "list-summaries CATALOG_NAME", Short: `List table summaries.`, Long: `List table summaries. @@ -207,21 +243,28 @@ var listSummariesCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Tables.TableInfoNameToTableIdMap(ctx, catalog.ListTablesRequest{}) + if cmd.Flags().Changed("json") { + err = listSummariesJson.Unmarshal(&listSummariesReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Name of parent catalog for tables of interest") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Tables.TableInfoNameToTableIdMap(ctx, catalog.ListTablesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Name of parent catalog for tables of interest") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have name of parent catalog for tables of interest") + if len(args) != 1 { + return fmt.Errorf("expected to have name of parent catalog for tables of interest") + } + listSummariesReq.CatalogName = args[0] } - listSummariesReq.CatalogName = args[0] response, err := w.Tables.ListSummaries(ctx, listSummariesReq) if err != nil { diff --git a/cmd/workspace/token-management/token-management.go b/cmd/workspace/token-management/token-management.go index 74a43b8d64..9e7f561433 100755 --- a/cmd/workspace/token-management/token-management.go +++ b/cmd/workspace/token-management/token-management.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/settings" "github.com/spf13/cobra" ) @@ -22,10 +23,12 @@ var Cmd = &cobra.Command{ // start create-obo-token command var createOboTokenReq settings.CreateOboTokenRequest +var createOboTokenJson flags.JsonFlag func init() { Cmd.AddCommand(createOboTokenCmd) // TODO: short flags + createOboTokenCmd.Flags().Var(&createOboTokenJson, "json", `either inline JSON string or @path/to/file.json with request body`) createOboTokenCmd.Flags().StringVar(&createOboTokenReq.Comment, "comment", createOboTokenReq.Comment, `Comment that describes the purpose of the token.`) @@ -39,15 +42,28 @@ var createOboTokenCmd = &cobra.Command{ Creates a token on behalf of a service principal.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - createOboTokenReq.ApplicationId = args[0] - _, err = fmt.Sscan(args[1], &createOboTokenReq.LifetimeSeconds) - if err != nil { - return fmt.Errorf("invalid LIFETIME_SECONDS: %s", args[1]) + if cmd.Flags().Changed("json") { + err = createOboTokenJson.Unmarshal(&createOboTokenReq) + if err != nil { + return err + } + } else { + createOboTokenReq.ApplicationId = args[0] + _, err = fmt.Sscan(args[1], &createOboTokenReq.LifetimeSeconds) + if err != nil { + return fmt.Errorf("invalid LIFETIME_SECONDS: %s", args[1]) + } } response, err := w.TokenManagement.CreateOboToken(ctx, createOboTokenReq) @@ -61,15 +77,17 @@ var createOboTokenCmd = &cobra.Command{ // start delete command var deleteReq settings.DeleteTokenManagementRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [TOKEN_ID]", + Use: "delete TOKEN_ID", Short: `Delete a token.`, Long: `Delete a token. @@ -80,21 +98,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.TokenManagement.TokenInfoCommentToTokenIdMap(ctx, settings.ListTokenManagementRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID of the token to get") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.TokenManagement.TokenInfoCommentToTokenIdMap(ctx, settings.ListTokenManagementRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID of the token to get") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id of the token to get") + if len(args) != 1 { + return fmt.Errorf("expected to have the id of the token to get") + } + deleteReq.TokenId = args[0] } - deleteReq.TokenId = args[0] err = w.TokenManagement.Delete(ctx, deleteReq) if err != nil { @@ -107,15 +132,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq settings.GetTokenManagementRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [TOKEN_ID]", + Use: "get TOKEN_ID", Short: `Get token info.`, Long: `Get token info. @@ -126,21 +153,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.TokenManagement.TokenInfoCommentToTokenIdMap(ctx, settings.ListTokenManagementRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID of the token to get") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.TokenManagement.TokenInfoCommentToTokenIdMap(ctx, settings.ListTokenManagementRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID of the token to get") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id of the token to get") + if len(args) != 1 { + return fmt.Errorf("expected to have the id of the token to get") + } + getReq.TokenId = args[0] } - getReq.TokenId = args[0] response, err := w.TokenManagement.Get(ctx, getReq) if err != nil { @@ -153,10 +187,12 @@ var getCmd = &cobra.Command{ // start list command var listReq settings.ListTokenManagementRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.CreatedById, "created-by-id", listReq.CreatedById, `User ID of the user that created the token.`) listCmd.Flags().StringVar(&listReq.CreatedByUsername, "created-by-username", listReq.CreatedByUsername, `Username of the user that created the token.`) @@ -171,10 +207,24 @@ var listCmd = &cobra.Command{ Lists all tokens associated with the specified workspace or user.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.TokenManagement.ListAll(ctx, listReq) if err != nil { diff --git a/cmd/workspace/tokens/tokens.go b/cmd/workspace/tokens/tokens.go index 37880360f8..ad829a6a64 100755 --- a/cmd/workspace/tokens/tokens.go +++ b/cmd/workspace/tokens/tokens.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/settings" "github.com/spf13/cobra" ) @@ -21,10 +22,12 @@ var Cmd = &cobra.Command{ // start create command var createReq settings.CreateTokenRequest +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().StringVar(&createReq.Comment, "comment", createReq.Comment, `Optional description to attach to the token.`) createCmd.Flags().Int64Var(&createReq.LifetimeSeconds, "lifetime-seconds", createReq.LifetimeSeconds, `The lifetime of the token, in seconds.`) @@ -42,10 +45,24 @@ var createCmd = &cobra.Command{ an error **QUOTA_EXCEEDED**.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + } response, err := w.Tokens.Create(ctx, createReq) if err != nil { @@ -58,15 +75,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq settings.RevokeTokenRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [TOKEN_ID]", + Use: "delete TOKEN_ID", Short: `Revoke token.`, Long: `Revoke token. @@ -80,21 +99,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Tokens.TokenInfoCommentToTokenIdMap(ctx) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The ID of the token to be revoked") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Tokens.TokenInfoCommentToTokenIdMap(ctx) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The ID of the token to be revoked") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the id of the token to be revoked") + if len(args) != 1 { + return fmt.Errorf("expected to have the id of the token to be revoked") + } + deleteReq.TokenId = args[0] } - deleteReq.TokenId = args[0] err = w.Tokens.Delete(ctx, deleteReq) if err != nil { diff --git a/cmd/workspace/users/users.go b/cmd/workspace/users/users.go index 8b79531fae..fcafde2da8 100755 --- a/cmd/workspace/users/users.go +++ b/cmd/workspace/users/users.go @@ -60,13 +60,23 @@ var createCmd = &cobra.Command{ added to the Databricks account.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } response, err := w.Users.Create(ctx, createReq) @@ -80,15 +90,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq iam.DeleteUserRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [ID]", + Use: "delete ID", Short: `Delete a user.`, Long: `Delete a user. @@ -100,21 +112,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks workspace") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks workspace") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a user in the databricks workspace") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a user in the databricks workspace") + } + deleteReq.Id = args[0] } - deleteReq.Id = args[0] err = w.Users.Delete(ctx, deleteReq) if err != nil { @@ -127,15 +146,17 @@ var deleteCmd = &cobra.Command{ // start get command var getReq iam.GetUserRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [ID]", + Use: "get ID", Short: `Get user details.`, Long: `Get user details. @@ -146,21 +167,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks workspace") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Users.UserUserNameToIdMap(ctx, iam.ListUsersRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Unique ID for a user in the Databricks workspace") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have unique id for a user in the databricks workspace") + if len(args) != 1 { + return fmt.Errorf("expected to have unique id for a user in the databricks workspace") + } + getReq.Id = args[0] } - getReq.Id = args[0] response, err := w.Users.Get(ctx, getReq) if err != nil { @@ -173,10 +201,12 @@ var getCmd = &cobra.Command{ // start list command var listReq iam.ListUsersRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().StringVar(&listReq.Attributes, "attributes", listReq.Attributes, `Comma-separated list of attributes to return in response.`) listCmd.Flags().IntVar(&listReq.Count, "count", listReq.Count, `Desired number of results per page.`) @@ -196,10 +226,24 @@ var listCmd = &cobra.Command{ Gets details for all the users associated with a Databricks workspace.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.Users.ListAll(ctx, listReq) if err != nil { @@ -224,7 +268,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch [ID]", + Use: "patch ID", Short: `Update user details.`, Long: `Update user details. @@ -257,7 +301,6 @@ var patchCmd = &cobra.Command{ return fmt.Errorf("expected to have unique id for a user in the databricks workspace") } patchReq.Id = args[0] - } err = w.Users.Patch(ctx, patchReq) @@ -292,7 +335,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [ID]", + Use: "update ID", Short: `Replace a user.`, Long: `Replace a user. @@ -324,7 +367,6 @@ var updateCmd = &cobra.Command{ return fmt.Errorf("expected to have databricks user id") } updateReq.Id = args[0] - } err = w.Users.Update(ctx, updateReq) diff --git a/cmd/workspace/volumes/volumes.go b/cmd/workspace/volumes/volumes.go index 63e0e211a0..3484e227dc 100755 --- a/cmd/workspace/volumes/volumes.go +++ b/cmd/workspace/volumes/volumes.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" ) @@ -27,10 +28,12 @@ var Cmd = &cobra.Command{ // start create command var createReq catalog.CreateVolumeRequestContent +var createJson flags.JsonFlag func init() { Cmd.AddCommand(createCmd) // TODO: short flags + createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`) createCmd.Flags().StringVar(&createReq.Comment, "comment", createReq.Comment, `The comment attached to the volume.`) createCmd.Flags().StringVar(&createReq.StorageLocation, "storage-location", createReq.StorageLocation, `The storage location on the cloud.`) @@ -62,17 +65,30 @@ var createCmd = &cobra.Command{ tables, nor volumes, or catalogs or schemas.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(4), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(4) + 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) - createReq.CatalogName = args[0] - createReq.Name = args[1] - createReq.SchemaName = args[2] - _, err = fmt.Sscan(args[3], &createReq.VolumeType) - if err != nil { - return fmt.Errorf("invalid VOLUME_TYPE: %s", args[3]) + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { + createReq.CatalogName = args[0] + createReq.Name = args[1] + createReq.SchemaName = args[2] + _, err = fmt.Sscan(args[3], &createReq.VolumeType) + if err != nil { + return fmt.Errorf("invalid VOLUME_TYPE: %s", args[3]) + } } response, err := w.Volumes.Create(ctx, createReq) @@ -86,15 +102,17 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq catalog.DeleteVolumeRequest +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [FULL_NAME_ARG]", + Use: "delete FULL_NAME_ARG", Short: `Delete a Volume.`, Long: `Delete a Volume. @@ -109,21 +127,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Volumes.VolumeInfoNameToVolumeIdMap(ctx, catalog.ListVolumesRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The three-level (fully qualified) name of the volume") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Volumes.VolumeInfoNameToVolumeIdMap(ctx, catalog.ListVolumesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The three-level (fully qualified) name of the volume") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the three-level (fully qualified) name of the volume") + if len(args) != 1 { + return fmt.Errorf("expected to have the three-level (fully qualified) name of the volume") + } + deleteReq.FullNameArg = args[0] } - deleteReq.FullNameArg = args[0] err = w.Volumes.Delete(ctx, deleteReq) if err != nil { @@ -136,10 +161,12 @@ var deleteCmd = &cobra.Command{ // start list command var listReq catalog.ListVolumesRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -161,13 +188,26 @@ var listCmd = &cobra.Command{ There is no guarantee of a specific ordering of the elements in the array.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(2), - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(2) + 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) - listReq.CatalogName = args[0] - listReq.SchemaName = args[1] + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + listReq.CatalogName = args[0] + listReq.SchemaName = args[1] + } response, err := w.Volumes.ListAll(ctx, listReq) if err != nil { @@ -180,15 +220,17 @@ var listCmd = &cobra.Command{ // start read command var readReq catalog.ReadVolumeRequest +var readJson flags.JsonFlag func init() { Cmd.AddCommand(readCmd) // TODO: short flags + readCmd.Flags().Var(&readJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var readCmd = &cobra.Command{ - Use: "read [FULL_NAME_ARG]", + Use: "read FULL_NAME_ARG", Short: `Get a Volume.`, Long: `Get a Volume. @@ -204,21 +246,28 @@ var readCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Volumes.VolumeInfoNameToVolumeIdMap(ctx, catalog.ListVolumesRequest{}) + if cmd.Flags().Changed("json") { + err = readJson.Unmarshal(&readReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The three-level (fully qualified) name of the volume") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Volumes.VolumeInfoNameToVolumeIdMap(ctx, catalog.ListVolumesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The three-level (fully qualified) name of the volume") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the three-level (fully qualified) name of the volume") + if len(args) != 1 { + return fmt.Errorf("expected to have the three-level (fully qualified) name of the volume") + } + readReq.FullNameArg = args[0] } - readReq.FullNameArg = args[0] response, err := w.Volumes.Read(ctx, readReq) if err != nil { @@ -231,10 +280,12 @@ var readCmd = &cobra.Command{ // start update command var updateReq catalog.UpdateVolumeRequestContent +var updateJson flags.JsonFlag func init() { Cmd.AddCommand(updateCmd) // TODO: short flags + updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`) updateCmd.Flags().StringVar(&updateReq.Comment, "comment", updateReq.Comment, `The comment attached to the volume.`) updateCmd.Flags().StringVar(&updateReq.Name, "name", updateReq.Name, `The name of the volume.`) @@ -243,7 +294,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update [FULL_NAME_ARG]", + Use: "update FULL_NAME_ARG", Short: `Update a Volume.`, Long: `Update a Volume. @@ -261,21 +312,28 @@ var updateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Volumes.VolumeInfoNameToVolumeIdMap(ctx, catalog.ListVolumesRequest{}) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The three-level (fully qualified) name of the volume") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Volumes.VolumeInfoNameToVolumeIdMap(ctx, catalog.ListVolumesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "The three-level (fully qualified) name of the volume") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the three-level (fully qualified) name of the volume") + if len(args) != 1 { + return fmt.Errorf("expected to have the three-level (fully qualified) name of the volume") + } + updateReq.FullNameArg = args[0] } - updateReq.FullNameArg = args[0] response, err := w.Volumes.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/warehouses/warehouses.go b/cmd/workspace/warehouses/warehouses.go index 858bfed711..bc8621013d 100755 --- a/cmd/workspace/warehouses/warehouses.go +++ b/cmd/workspace/warehouses/warehouses.go @@ -61,13 +61,23 @@ var createCmd = &cobra.Command{ Creates a new SQL warehouse.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = createJson.Unmarshal(&createReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + if err != nil { + return err + } + } else { } if createSkipWait { @@ -105,7 +115,7 @@ var createCmd = &cobra.Command{ // start delete command var deleteReq sql.DeleteWarehouseRequest - +var deleteJson flags.JsonFlag var deleteSkipWait bool var deleteTimeout time.Duration @@ -115,11 +125,12 @@ func init() { deleteCmd.Flags().BoolVar(&deleteSkipWait, "no-wait", deleteSkipWait, `do not wait to reach DELETED state`) deleteCmd.Flags().DurationVar(&deleteTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach DELETED state`) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var deleteCmd = &cobra.Command{ - Use: "delete [ID]", + Use: "delete ID", Short: `Delete a warehouse.`, Long: `Delete a warehouse. @@ -130,21 +141,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Required") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Required") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have required") + if len(args) != 1 { + return fmt.Errorf("expected to have required") + } + deleteReq.Id = args[0] } - deleteReq.Id = args[0] if deleteSkipWait { err = w.Warehouses.Delete(ctx, deleteReq) @@ -210,7 +228,7 @@ func init() { } var editCmd = &cobra.Command{ - Use: "edit [ID]", + Use: "edit ID", Short: `Update a warehouse.`, Long: `Update a warehouse. @@ -242,7 +260,6 @@ var editCmd = &cobra.Command{ return fmt.Errorf("expected to have required") } editReq.Id = args[0] - } if editSkipWait { @@ -280,7 +297,7 @@ var editCmd = &cobra.Command{ // start get command var getReq sql.GetWarehouseRequest - +var getJson flags.JsonFlag var getSkipWait bool var getTimeout time.Duration @@ -290,11 +307,12 @@ func init() { getCmd.Flags().BoolVar(&getSkipWait, "no-wait", getSkipWait, `do not wait to reach RUNNING state`) getCmd.Flags().DurationVar(&getTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach RUNNING state`) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getCmd = &cobra.Command{ - Use: "get [ID]", + Use: "get ID", Short: `Get warehouse info.`, Long: `Get warehouse info. @@ -305,21 +323,28 @@ var getCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Required") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Required") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have required") + if len(args) != 1 { + return fmt.Errorf("expected to have required") + } + getReq.Id = args[0] } - getReq.Id = args[0] response, err := w.Warehouses.Get(ctx, getReq) if err != nil { @@ -360,10 +385,12 @@ var getWorkspaceWarehouseConfigCmd = &cobra.Command{ // start list command var listReq sql.ListWarehousesRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().IntVar(&listReq.RunAsUserId, "run-as-user-id", listReq.RunAsUserId, `Service Principal which will be used to fetch the list of warehouses.`) @@ -377,10 +404,24 @@ var listCmd = &cobra.Command{ Lists all SQL warehouses that a user has manager permissions on.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) + if err != nil { + return err + } + } else { + } response, err := w.Warehouses.ListAll(ctx, listReq) if err != nil { @@ -421,13 +462,23 @@ var setWorkspaceWarehouseConfigCmd = &cobra.Command{ a workspace.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) - err = setWorkspaceWarehouseConfigJson.Unmarshal(&setWorkspaceWarehouseConfigReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = setWorkspaceWarehouseConfigJson.Unmarshal(&setWorkspaceWarehouseConfigReq) + if err != nil { + return err + } + } else { } err = w.Warehouses.SetWorkspaceWarehouseConfig(ctx, setWorkspaceWarehouseConfigReq) @@ -441,7 +492,7 @@ var setWorkspaceWarehouseConfigCmd = &cobra.Command{ // start start command var startReq sql.StartRequest - +var startJson flags.JsonFlag var startSkipWait bool var startTimeout time.Duration @@ -451,11 +502,12 @@ func init() { startCmd.Flags().BoolVar(&startSkipWait, "no-wait", startSkipWait, `do not wait to reach RUNNING state`) startCmd.Flags().DurationVar(&startTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach RUNNING state`) // TODO: short flags + startCmd.Flags().Var(&startJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var startCmd = &cobra.Command{ - Use: "start [ID]", + Use: "start ID", Short: `Start a warehouse.`, Long: `Start a warehouse. @@ -466,21 +518,28 @@ var startCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if cmd.Flags().Changed("json") { + err = startJson.Unmarshal(&startReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Required") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Required") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have required") + if len(args) != 1 { + return fmt.Errorf("expected to have required") + } + startReq.Id = args[0] } - startReq.Id = args[0] if startSkipWait { err = w.Warehouses.Start(ctx, startReq) @@ -517,7 +576,7 @@ var startCmd = &cobra.Command{ // start stop command var stopReq sql.StopRequest - +var stopJson flags.JsonFlag var stopSkipWait bool var stopTimeout time.Duration @@ -527,11 +586,12 @@ func init() { stopCmd.Flags().BoolVar(&stopSkipWait, "no-wait", stopSkipWait, `do not wait to reach STOPPED state`) stopCmd.Flags().DurationVar(&stopTimeout, "timeout", 20*time.Minute, `maximum amount of time to reach STOPPED state`) // TODO: short flags + stopCmd.Flags().Var(&stopJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var stopCmd = &cobra.Command{ - Use: "stop [ID]", + Use: "stop ID", Short: `Stop a warehouse.`, Long: `Stop a warehouse. @@ -542,21 +602,28 @@ var stopCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if cmd.Flags().Changed("json") { + err = stopJson.Unmarshal(&stopReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "Required") - if err != nil { - return err + } else { + if len(args) == 0 { + names, err := w.Warehouses.EndpointInfoNameToIdMap(ctx, sql.ListWarehousesRequest{}) + if err != nil { + return err + } + id, err := cmdio.Select(ctx, names, "Required") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have required") + if len(args) != 1 { + return fmt.Errorf("expected to have required") + } + stopReq.Id = args[0] } - stopReq.Id = args[0] if stopSkipWait { err = w.Warehouses.Stop(ctx, stopReq) diff --git a/cmd/workspace/workspace-bindings/workspace-bindings.go b/cmd/workspace/workspace-bindings/workspace-bindings.go index d01aea92d8..75ecb396d2 100755 --- a/cmd/workspace/workspace-bindings/workspace-bindings.go +++ b/cmd/workspace/workspace-bindings/workspace-bindings.go @@ -24,10 +24,12 @@ var Cmd = &cobra.Command{ // start get command var getReq catalog.GetWorkspaceBindingRequest +var getJson flags.JsonFlag func init() { Cmd.AddCommand(getCmd) // TODO: short flags + getCmd.Flags().Var(&getJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -40,12 +42,25 @@ var getCmd = &cobra.Command{ or an owner of the catalog.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getReq.Name = args[0] + if cmd.Flags().Changed("json") { + err = getJson.Unmarshal(&getReq) + if err != nil { + return err + } + } else { + getReq.Name = args[0] + } response, err := w.WorkspaceBindings.Get(ctx, getReq) if err != nil { @@ -79,16 +94,25 @@ var updateCmd = &cobra.Command{ admin or an owner of the catalog.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - err = updateJson.Unmarshal(&updateReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } else { + updateReq.Name = args[0] } - updateReq.Name = args[0] response, err := w.WorkspaceBindings.Update(ctx, updateReq) if err != nil { diff --git a/cmd/workspace/workspace-conf/workspace-conf.go b/cmd/workspace/workspace-conf/workspace-conf.go index 4d6d649e86..a9a78f6371 100755 --- a/cmd/workspace/workspace-conf/workspace-conf.go +++ b/cmd/workspace/workspace-conf/workspace-conf.go @@ -5,6 +5,7 @@ package workspace_conf import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/settings" "github.com/spf13/cobra" ) @@ -18,10 +19,12 @@ var Cmd = &cobra.Command{ // start get-status command var getStatusReq settings.GetStatusRequest +var getStatusJson flags.JsonFlag func init() { Cmd.AddCommand(getStatusCmd) // TODO: short flags + getStatusCmd.Flags().Var(&getStatusJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -33,12 +36,25 @@ var getStatusCmd = &cobra.Command{ Gets the configuration status for a workspace.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(1), - 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) - getStatusReq.Keys = args[0] + if cmd.Flags().Changed("json") { + err = getStatusJson.Unmarshal(&getStatusReq) + if err != nil { + return err + } + } else { + getStatusReq.Keys = args[0] + } response, err := w.WorkspaceConf.GetStatus(ctx, getStatusReq) if err != nil { @@ -51,10 +67,12 @@ var getStatusCmd = &cobra.Command{ // start set-status command var setStatusReq settings.WorkspaceConf +var setStatusJson flags.JsonFlag func init() { Cmd.AddCommand(setStatusCmd) // TODO: short flags + setStatusCmd.Flags().Var(&setStatusJson, "json", `either inline JSON string or @path/to/file.json with request body`) } @@ -67,10 +85,24 @@ var setStatusCmd = &cobra.Command{ it.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(0) + 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) + if cmd.Flags().Changed("json") { + err = setStatusJson.Unmarshal(&setStatusReq) + if err != nil { + return err + } + } else { + } err = w.WorkspaceConf.SetStatus(ctx, setStatusReq) if err != nil { diff --git a/cmd/workspace/workspace/workspace.go b/cmd/workspace/workspace/workspace.go index e263f7fc42..7cfe39dca4 100755 --- a/cmd/workspace/workspace/workspace.go +++ b/cmd/workspace/workspace/workspace.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/service/workspace" "github.com/spf13/cobra" ) @@ -24,17 +25,19 @@ var Cmd = &cobra.Command{ // start delete command var deleteReq workspace.Delete +var deleteJson flags.JsonFlag func init() { Cmd.AddCommand(deleteCmd) // TODO: short flags + deleteCmd.Flags().Var(&deleteJson, "json", `either inline JSON string or @path/to/file.json with request body`) deleteCmd.Flags().BoolVar(&deleteReq.Recursive, "recursive", deleteReq.Recursive, `The flag that specifies whether to delete the object recursively.`) } var deleteCmd = &cobra.Command{ - Use: "delete [PATH]", + Use: "delete PATH", Short: `Delete a workspace object.`, Long: `Delete a workspace object. @@ -52,21 +55,28 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Workspace.ObjectInfoPathToObjectIdMap(ctx, workspace.ListWorkspaceRequest{}) + if cmd.Flags().Changed("json") { + err = deleteJson.Unmarshal(&deleteReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The absolute path of the notebook or directory") - if err != nil { - 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) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the absolute path of the notebook or directory") + if len(args) != 1 { + return fmt.Errorf("expected to have the absolute path of the notebook or directory") + } + deleteReq.Path = args[0] } - deleteReq.Path = args[0] err = w.Workspace.Delete(ctx, deleteReq) if err != nil { @@ -79,10 +89,12 @@ var deleteCmd = &cobra.Command{ // start export command var exportReq workspace.ExportRequest +var exportJson flags.JsonFlag func init() { Cmd.AddCommand(exportCmd) // TODO: short flags + exportCmd.Flags().Var(&exportJson, "json", `either inline JSON string or @path/to/file.json with request body`) exportCmd.Flags().BoolVar(&exportReq.DirectDownload, "direct-download", exportReq.DirectDownload, `Flag to enable direct download.`) exportCmd.Flags().Var(&exportReq.Format, "format", `This specifies the format of the exported file.`) @@ -90,7 +102,7 @@ func init() { } var exportCmd = &cobra.Command{ - Use: "export [PATH]", + Use: "export PATH", Short: `Export a workspace object.`, Long: `Export a workspace object. @@ -108,21 +120,28 @@ var exportCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Workspace.ObjectInfoPathToObjectIdMap(ctx, workspace.ListWorkspaceRequest{}) + if cmd.Flags().Changed("json") { + err = exportJson.Unmarshal(&exportReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The absolute path of the object or directory") - if err != nil { - 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 object or directory") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the absolute path of the object or directory") + if len(args) != 1 { + return fmt.Errorf("expected to have the absolute path of the object or directory") + } + exportReq.Path = args[0] } - exportReq.Path = args[0] response, err := w.Workspace.Export(ctx, exportReq) if err != nil { @@ -135,15 +154,17 @@ var exportCmd = &cobra.Command{ // start get-status command var getStatusReq workspace.GetStatusRequest +var getStatusJson flags.JsonFlag func init() { Cmd.AddCommand(getStatusCmd) // TODO: short flags + getStatusCmd.Flags().Var(&getStatusJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var getStatusCmd = &cobra.Command{ - Use: "get-status [PATH]", + Use: "get-status PATH", Short: `Get status.`, Long: `Get status. @@ -155,21 +176,28 @@ var getStatusCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Workspace.ObjectInfoPathToObjectIdMap(ctx, workspace.ListWorkspaceRequest{}) + if cmd.Flags().Changed("json") { + err = getStatusJson.Unmarshal(&getStatusReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The absolute path of the notebook or directory") - if err != nil { - 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) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the absolute path of the notebook or directory") + if len(args) != 1 { + return fmt.Errorf("expected to have the absolute path of the notebook or directory") + } + getStatusReq.Path = args[0] } - getStatusReq.Path = args[0] response, err := w.Workspace.GetStatus(ctx, getStatusReq) if err != nil { @@ -182,10 +210,12 @@ var getStatusCmd = &cobra.Command{ // start import command var importReq workspace.Import +var importJson flags.JsonFlag func init() { Cmd.AddCommand(importCmd) // TODO: short flags + importCmd.Flags().Var(&importJson, "json", `either inline JSON string or @path/to/file.json with request body`) importCmd.Flags().StringVar(&importReq.Content, "content", importReq.Content, `The base64-encoded content.`) importCmd.Flags().Var(&importReq.Format, "format", `This specifies the format of the file to be imported.`) @@ -195,7 +225,7 @@ func init() { } var importCmd = &cobra.Command{ - Use: "import [PATH]", + Use: "import PATH", Short: `Import a workspace object.`, Long: `Import a workspace object. @@ -209,21 +239,28 @@ var importCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Workspace.ObjectInfoPathToObjectIdMap(ctx, workspace.ListWorkspaceRequest{}) + if cmd.Flags().Changed("json") { + err = importJson.Unmarshal(&importReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The absolute path of the object or directory") - if err != nil { - 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 object or directory") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the absolute path of the object or directory") + if len(args) != 1 { + return fmt.Errorf("expected to have the absolute path of the object or directory") + } + importReq.Path = args[0] } - importReq.Path = args[0] err = w.Workspace.Import(ctx, importReq) if err != nil { @@ -236,17 +273,19 @@ var importCmd = &cobra.Command{ // start list command var listReq workspace.ListWorkspaceRequest +var listJson flags.JsonFlag func init() { Cmd.AddCommand(listCmd) // TODO: short flags + listCmd.Flags().Var(&listJson, "json", `either inline JSON string or @path/to/file.json with request body`) listCmd.Flags().IntVar(&listReq.NotebooksModifiedAfter, "notebooks-modified-after", listReq.NotebooksModifiedAfter, `.`) } var listCmd = &cobra.Command{ - Use: "list [PATH]", + Use: "list PATH", Short: `List contents.`, Long: `List contents. @@ -259,21 +298,28 @@ var listCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Workspace.ObjectInfoPathToObjectIdMap(ctx, workspace.ListWorkspaceRequest{}) + if cmd.Flags().Changed("json") { + err = listJson.Unmarshal(&listReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The absolute path of the notebook or directory") - if err != nil { - 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) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the absolute path of the notebook or directory") + if len(args) != 1 { + return fmt.Errorf("expected to have the absolute path of the notebook or directory") + } + listReq.Path = args[0] } - listReq.Path = args[0] response, err := w.Workspace.ListAll(ctx, listReq) if err != nil { @@ -286,15 +332,17 @@ var listCmd = &cobra.Command{ // start mkdirs command var mkdirsReq workspace.Mkdirs +var mkdirsJson flags.JsonFlag func init() { Cmd.AddCommand(mkdirsCmd) // TODO: short flags + mkdirsCmd.Flags().Var(&mkdirsJson, "json", `either inline JSON string or @path/to/file.json with request body`) } var mkdirsCmd = &cobra.Command{ - Use: "mkdirs [PATH]", + Use: "mkdirs PATH", Short: `Create a directory.`, Long: `Create a directory. @@ -310,21 +358,28 @@ var mkdirsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - if len(args) == 0 { - names, err := w.Workspace.ObjectInfoPathToObjectIdMap(ctx, workspace.ListWorkspaceRequest{}) + if cmd.Flags().Changed("json") { + err = mkdirsJson.Unmarshal(&mkdirsReq) if err != nil { return err } - id, err := cmdio.Select(ctx, names, "The absolute path of the directory") - if err != nil { - 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 directory") + if err != nil { + return err + } + args = append(args, id) } - args = append(args, id) - } - if len(args) != 1 { - return fmt.Errorf("expected to have the absolute path of the directory") + if len(args) != 1 { + return fmt.Errorf("expected to have the absolute path of the directory") + } + mkdirsReq.Path = args[0] } - mkdirsReq.Path = args[0] err = w.Workspace.Mkdirs(ctx, mkdirsReq) if err != nil { From 1c91e730e0b4023d126a34540d87829eb91a1153 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Fri, 26 May 2023 14:03:05 +0200 Subject: [PATCH 5/5] Added integration tests --- internal/clusters_test.go | 38 ++++++++++++++++++++++++++++++++++++++ internal/helpers.go | 7 +++++++ internal/secrets_test.go | 14 ++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 internal/clusters_test.go create mode 100644 internal/secrets_test.go diff --git a/internal/clusters_test.go b/internal/clusters_test.go new file mode 100644 index 0000000000..827015d2e0 --- /dev/null +++ b/internal/clusters_test.go @@ -0,0 +1,38 @@ +package internal + +import ( + "fmt" + "regexp" + "testing" + + "github.com/stretchr/testify/assert" + + _ "github.com/databricks/cli/cmd/workspace" +) + +var clusterId string + +func TestAccClustersList(t *testing.T) { + t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV")) + + stdout, stderr := RequireSuccessfulRun(t, "clusters", "list") + outStr := stdout.String() + assert.Contains(t, outStr, "ID") + assert.Contains(t, outStr, "Name") + assert.Contains(t, outStr, "State") + assert.Equal(t, "", stderr.String()) + + idRegExp := regexp.MustCompile(`[0-9]{4}\-[0-9]{6}-[a-z0-9]{8}`) + clusterId = idRegExp.FindString(outStr) + fmt.Println(clusterId) + assert.NotEmpty(t, clusterId) +} + +func TestAccClustersGet(t *testing.T) { + t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV")) + + stdout, stderr := RequireSuccessfulRun(t, "clusters", "get", clusterId) + outStr := stdout.String() + assert.Contains(t, outStr, fmt.Sprintf(`"cluster_id":"%s"`, clusterId)) + assert.Equal(t, "", stderr.String()) +} diff --git a/internal/helpers.go b/internal/helpers.go index 42132a5497..135ebbdbea 100644 --- a/internal/helpers.go +++ b/internal/helpers.go @@ -159,6 +159,13 @@ func RequireSuccessfulRun(t *testing.T, args ...string) (bytes.Buffer, bytes.Buf return stdout, stderr } +func RequireErrorRun(t *testing.T, args ...string) (bytes.Buffer, bytes.Buffer, error) { + c := NewCobraTestRunner(t, args...) + stdout, stderr, err := c.Run() + require.Error(t, err) + return stdout, stderr, err +} + func writeFile(t *testing.T, name string, body string) string { f, err := os.Create(filepath.Join(t.TempDir(), name)) require.NoError(t, err) diff --git a/internal/secrets_test.go b/internal/secrets_test.go new file mode 100644 index 0000000000..69423d1753 --- /dev/null +++ b/internal/secrets_test.go @@ -0,0 +1,14 @@ +package internal + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + _ "github.com/databricks/cli/cmd/workspace" +) + +func TestSecretsCreateScopeErrWhenNoArguments(t *testing.T) { + _, _, err := RequireErrorRun(t, "secrets", "create-scope") + assert.Equal(t, "accepts 1 arg(s), received 0", err.Error()) +}