diff --git a/.codegen/service.go.tmpl b/.codegen/service.go.tmpl index 49dce3068f..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 -}} @@ -57,42 +57,52 @@ func init() { {{- end}} {{end}} } -{{ $hasPosArgs := and .Request (or .Request.IsOnlyPrimitiveFields (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"))) -}} +{{- $hasRequiredArgs := and (not $hasIdPrompt) $hasPosArgs -}} var {{.CamelName}}Cmd = &cobra.Command{ 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 and (not $hasIdPrompt) $hasPosArgs }} - Args: cobra.ExactArgs({{len .Request.RequiredFields}}),{{end}} + Annotations: map[string]string{},{{if $hasRequiredArgs }} + 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 $hasIdPrompt}} + {{- if .Request }} + if cmd.Flags().Changed("json") { + err = {{.CamelName}}Json.Unmarshal(&{{.CamelName}}Req) + if err != nil { + return err + } + } 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 { - return err - } - id, err := cmdio.Select(ctx, names, "{{range .Request.RequiredFields}}{{.Summary | trimSuffix "."}}{{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) } - 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 := .}} @@ -104,6 +114,7 @@ var {{.CamelName}}Cmd = &cobra.Command{ }{{else -}} {{$method.CamelName}}Req.{{$field.PascalName}} = args[{{$arg}}] {{- end -}}{{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 05ef4ac47f..dedcdd802c 100755 --- a/cmd/account/budgets/budgets.go +++ b/cmd/account/budgets/budgets.go @@ -43,15 +43,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 - } - _, 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]) + } } - createReq.BudgetId = args[1] response, err := a.Budgets.Create(ctx, createReq) if err != nil { @@ -64,10 +66,12 @@ 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`) } @@ -83,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 { @@ -110,10 +121,12 @@ 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`) } @@ -130,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 { @@ -207,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 3ff0fe2180..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,10 +83,12 @@ 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`) } @@ -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,10 +140,12 @@ 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`) } @@ -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 ca2fe9136a..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 { @@ -183,7 +216,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,15 +224,25 @@ var updateCmd = &cobra.Command{ oauth app integration via :method:CustomAppIntegration/get.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - 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 fb3da6f7dd..f76711d008 100755 --- a/cmd/account/groups/groups.go +++ b/cmd/account/groups/groups.go @@ -53,15 +53,24 @@ 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 { } - createReq.Id = args[0] response, err := a.Groups.Create(ctx, createReq) if err != nil { @@ -74,10 +83,12 @@ 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`) } @@ -93,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 { @@ -120,10 +138,12 @@ 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`) } @@ -139,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 { @@ -166,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.`) @@ -189,11 +218,24 @@ 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, + 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 { @@ -218,7 +260,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update group details.`, Long: `Update group details. @@ -229,11 +271,28 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) + 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) + } + 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 { @@ -264,7 +323,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace a group.`, Long: `Replace a group. @@ -275,11 +334,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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..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,10 +104,12 @@ 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`) } @@ -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,10 +159,12 @@ 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`) } @@ -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 2e91b218a8..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,10 +154,12 @@ 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`) } @@ -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,11 +232,24 @@ var listCmd = &cobra.Command{ specified by ID.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -228,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`) } @@ -247,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 89fb5ea391..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.`) @@ -164,20 +212,32 @@ 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), - 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] - updateReq.Id = args[1] + 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 34584d6f21..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", + Use: "create NETWORK_NAME", Short: `Create network configuration.`, Long: `Create network configuration. @@ -51,11 +51,28 @@ 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 + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + 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) + } + 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 { @@ -68,10 +85,12 @@ 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`) } @@ -92,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 { @@ -119,10 +145,12 @@ 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`) } @@ -139,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 2003fb89b9..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,11 +50,24 @@ var createCmd = &cobra.Command{ status via API :method:OAuthEnrollment/get.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 34092ac9d0..e7235f37c1 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", Short: `Create private access settings.`, Long: `Create private access settings. @@ -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{}, - 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] - createReq.PrivateAccessSettingsId = args[2] response, err := a.PrivateAccess.Create(ctx, createReq) if err != nil { @@ -79,10 +88,12 @@ 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`) } @@ -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,10 +150,12 @@ 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`) } @@ -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 { @@ -227,7 +254,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,17 +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{}, - 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 9931f3dcf8..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 { @@ -175,7 +215,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,15 +223,25 @@ var updateCmd = &cobra.Command{ published oauth app integration via :method:PublishedAppIntegration/get.`, Annotations: map[string]string{}, - PreRunE: root.MustAccountClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustAccountClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - 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 695d497b34..6432ed2d58 100755 --- a/cmd/account/service-principals/service-principals.go +++ b/cmd/account/service-principals/service-principals.go @@ -52,15 +52,24 @@ 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 { } - createReq.Id = args[0] response, err := a.ServicePrincipals.Create(ctx, createReq) if err != nil { @@ -73,10 +82,12 @@ 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`) } @@ -92,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 { @@ -119,10 +137,12 @@ 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`) } @@ -139,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 { @@ -166,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.`) @@ -189,11 +218,24 @@ 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, + 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 { @@ -218,7 +260,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update service principal details.`, Long: `Update service principal details. @@ -230,11 +272,28 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) + 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) + } + 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 { @@ -266,7 +325,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace service principal.`, Long: `Replace service principal. @@ -279,11 +338,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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-credentials/storage-credentials.go b/cmd/account/storage-credentials/storage-credentials.go index 2dc3d497a8..8c633e24fb 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,16 +49,26 @@ var createCmd = &cobra.Command{ **CREATE_STORAGE_CREDENTIAL** privilege on the metastore.`, Annotations: map[string]string{}, - 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 { @@ -71,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`) } @@ -87,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 { @@ -106,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`) } @@ -123,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 { @@ -142,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`) } @@ -158,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 { @@ -196,7 +251,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,16 +260,26 @@ var updateCmd = &cobra.Command{ credential can be changed.`, Annotations: map[string]string{}, - 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 37d99f2fbb..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,10 +80,12 @@ 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`) } @@ -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,10 +136,12 @@ 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`) } @@ -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 d4faca2044..6ccb39f53e 100755 --- a/cmd/account/users/users.go +++ b/cmd/account/users/users.go @@ -60,15 +60,24 @@ 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 { } - createReq.Id = args[0] response, err := a.Users.Create(ctx, createReq) if err != nil { @@ -81,10 +90,12 @@ 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`) } @@ -101,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 { @@ -128,10 +146,12 @@ 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`) } @@ -147,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 { @@ -174,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.`) @@ -197,11 +226,24 @@ 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, + 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 { @@ -226,7 +268,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update user details.`, Long: `Update user details. @@ -238,11 +280,28 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() a := root.AccountClient(ctx) - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) + 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) + } + 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 { @@ -276,7 +335,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace a user.`, Long: `Replace a user. @@ -287,11 +346,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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 d4cd591144..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", + Use: "create VPC_ENDPOINT_NAME", Short: `Create VPC endpoint configuration.`, Long: `Create VPC endpoint configuration. @@ -58,11 +58,28 @@ 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 + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + 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) + } + 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 { @@ -75,10 +92,12 @@ 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`) } @@ -102,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 { @@ -129,10 +155,12 @@ 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`) } @@ -152,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 7919eaa096..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", + Use: "create WORKSPACE_NAME", Short: `Create a new workspace.`, Long: `Create a new workspace. @@ -77,11 +77,28 @@ 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 + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + 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) + } + 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) @@ -111,10 +128,12 @@ 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`) } @@ -137,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) @@ -167,10 +193,12 @@ 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`) } @@ -199,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) @@ -260,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 @@ -270,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.`) @@ -402,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 5c969880ec..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,10 +76,12 @@ 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`) } @@ -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,10 +133,12 @@ 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`) } @@ -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 025f318c78..4e8aa9d717 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,15 +50,25 @@ var createCmd = &cobra.Command{ metastore admin or has the **CREATE_CATALOG** privilege.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - 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 { @@ -71,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.`) @@ -89,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 { @@ -107,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`) } @@ -124,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 { @@ -189,7 +229,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update NAME", Short: `Update a catalog.`, Long: `Update a catalog. @@ -198,15 +238,25 @@ var updateCmd = &cobra.Command{ of the catalog).`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - 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 b56da9e2f6..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.`) @@ -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,10 +105,12 @@ 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`) } @@ -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,10 +217,12 @@ 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`) } @@ -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,11 +292,24 @@ 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, + 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 3a530f8a6e..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", + Use: "create SPARK_VERSION", Short: `Create new cluster.`, Long: `Create new cluster. @@ -147,11 +162,28 @@ 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 + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + 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) + } + 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) @@ -181,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 @@ -191,6 +223,7 @@ 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`) } @@ -209,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) @@ -293,7 +333,7 @@ func init() { } var editCmd = &cobra.Command{ - Use: "edit", + Use: "edit CLUSTER_ID SPARK_VERSION", Short: `Update cluster configuration.`, Long: `Update cluster configuration. @@ -311,16 +351,26 @@ var editCmd = &cobra.Command{ Clusters created by the Databricks Jobs service cannot be edited.`, Annotations: map[string]string{}, - 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) @@ -367,7 +417,7 @@ func init() { } var eventsCmd = &cobra.Command{ - Use: "events", + Use: "events CLUSTER_ID", Short: `List cluster activity events.`, Long: `List cluster activity events. @@ -380,11 +430,28 @@ var eventsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = eventsJson.Unmarshal(&eventsReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = eventsJson.Unmarshal(&eventsReq) + 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) + } + 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 { @@ -397,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 @@ -407,6 +474,7 @@ 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`) } @@ -423,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 { @@ -450,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.`) @@ -475,11 +552,24 @@ var listCmd = &cobra.Command{ terminated job clusters.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -548,10 +638,12 @@ 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`) } @@ -572,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 { @@ -599,10 +698,12 @@ 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`) } @@ -620,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 { @@ -665,7 +773,7 @@ func init() { } var resizeCmd = &cobra.Command{ - Use: "resize", + Use: "resize CLUSTER_ID", Short: `Resize cluster.`, Long: `Resize cluster. @@ -677,11 +785,28 @@ var resizeCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = resizeJson.Unmarshal(&resizeReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = resizeJson.Unmarshal(&resizeReq) + 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) + } + 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) @@ -711,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 @@ -721,6 +846,7 @@ 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, `.`) @@ -739,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) @@ -811,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 @@ -821,6 +954,7 @@ 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`) } @@ -843,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) @@ -887,10 +1028,12 @@ 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`) } @@ -908,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 c64d3935bf..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,10 +76,12 @@ 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`) } @@ -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,10 +132,12 @@ 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`) } @@ -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,11 +210,24 @@ var listCmd = &cobra.Command{ Fetch a paginated list of dashboard objects.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -197,10 +240,12 @@ 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`) } @@ -216,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 8d4e22d730..97b05c1337 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,15 +54,25 @@ var createExperimentCmd = &cobra.Command{ Throws RESOURCE_ALREADY_EXISTS if a experiment with the given name exists.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - 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 { @@ -90,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) @@ -110,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`) } @@ -127,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 { @@ -145,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`) } @@ -160,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 { @@ -178,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`) } @@ -194,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 { @@ -213,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`) } @@ -236,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 { @@ -254,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`) } @@ -269,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 { @@ -287,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.`) @@ -307,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 { @@ -325,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.`) @@ -347,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 { @@ -365,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).`) @@ -386,11 +523,24 @@ 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, + 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 { @@ -403,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.`) @@ -422,11 +574,24 @@ var listExperimentsCmd = &cobra.Command{ Gets a list of all experiments.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -485,7 +650,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 @@ -497,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) @@ -538,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) @@ -558,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.`) @@ -579,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) @@ -605,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.`) @@ -624,11 +826,24 @@ var logModelCmd = &cobra.Command{ without warning.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -641,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.`) @@ -662,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 { @@ -681,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`) } @@ -701,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 { @@ -719,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`) } @@ -734,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 { @@ -775,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) @@ -821,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) @@ -841,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`) } @@ -856,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 { @@ -876,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.`) @@ -895,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 { @@ -914,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.`) @@ -931,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 { @@ -949,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.`) @@ -969,11 +1296,24 @@ var updateRunCmd = &cobra.Command{ Updates run metadata.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 1705f99947..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.`) @@ -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,10 +87,12 @@ 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`) } @@ -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,10 +145,12 @@ 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`) } @@ -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.`) @@ -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 d0f562ad55..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,10 +81,12 @@ 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`) } @@ -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,10 +136,12 @@ 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`) } @@ -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 4453e9d0ae..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 { @@ -121,26 +151,36 @@ 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{}, - 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 d05e086308..3bfc83dba3 100755 --- a/cmd/workspace/groups/groups.go +++ b/cmd/workspace/groups/groups.go @@ -53,15 +53,24 @@ 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 { } - createReq.Id = args[0] response, err := w.Groups.Create(ctx, createReq) if err != nil { @@ -74,10 +83,12 @@ 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`) } @@ -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.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 { @@ -120,10 +138,12 @@ 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`) } @@ -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.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 { @@ -166,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.`) @@ -189,11 +218,24 @@ 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, + 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 { @@ -218,7 +260,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update group details.`, Long: `Update group details. @@ -229,11 +271,28 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) + 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) + } + 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 { @@ -264,7 +323,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace a group.`, Long: `Replace a group. @@ -275,11 +334,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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 e98962e53b..7d39cfd805 100755 --- a/cmd/workspace/instance-pools/instance-pools.go +++ b/cmd/workspace/instance-pools/instance-pools.go @@ -60,23 +60,33 @@ 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{}, - 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 { @@ -89,10 +99,12 @@ 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`) } @@ -109,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 { @@ -158,24 +177,34 @@ 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{}, - 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 { @@ -188,10 +217,12 @@ 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`) } @@ -207,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 381fc262bd..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,10 +105,12 @@ 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`) } @@ -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,10 +160,12 @@ 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`) } @@ -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 cbad973170..2d8dbd0de1 100755 --- a/cmd/workspace/jobs/jobs.go +++ b/cmd/workspace/jobs/jobs.go @@ -40,10 +40,12 @@ 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`) } @@ -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,6 +109,7 @@ 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`) } @@ -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,10 +250,12 @@ 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`) } @@ -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,10 +308,12 @@ 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`) } @@ -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,10 +366,12 @@ 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).`) @@ -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,10 +426,12 @@ 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`) } @@ -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,6 +494,7 @@ 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.`) @@ -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,10 +549,12 @@ 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`) } @@ -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,11 +638,24 @@ var listCmd = &cobra.Command{ Retrieves a list of jobs.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -573,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.`) @@ -598,11 +695,24 @@ var listRunsCmd = &cobra.Command{ List runs in descending order by start time.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -642,7 +752,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,13 +765,30 @@ var repairRunCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - 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 cmd.Flags().Changed("json") { + err = repairRunJson.Unmarshal(&repairRunReq) + if err != nil { + return err + } + } 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 fmt.Errorf("invalid RUN_ID: %s", args[0]) + } } if repairRunSkipWait { @@ -721,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) @@ -781,27 +911,30 @@ 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 { @@ -873,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 { @@ -930,7 +1073,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update JOB_ID", Short: `Partially update a job.`, Long: `Partially update a job. @@ -942,13 +1085,30 @@ 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 - } - _, err = fmt.Sscan(args[0], &updateReq.JobId) - if err != nil { - return fmt.Errorf("invalid JOB_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } 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 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 0d3003790c..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,10 +166,12 @@ 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.`) @@ -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,10 +223,12 @@ 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`) } @@ -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 METASTORE_ID ID", + Use: "update ID", Short: `Update a metastore.`, Long: `Update a metastore. @@ -385,13 +466,32 @@ 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 cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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 { @@ -404,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.`) @@ -415,7 +517,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 +527,35 @@ 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) - _, err = fmt.Sscan(args[0], &updateAssignmentReq.WorkspaceId) - if err != nil { - return fmt.Errorf("invalid WORKSPACE_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = updateAssignmentJson.Unmarshal(&updateAssignmentReq) + 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, "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/model-registry/model-registry.go b/cmd/workspace/model-registry/model-registry.go index ea7b9efad1..3bda9e6f0f 100755 --- a/cmd/workspace/model-registry/model-registry.go +++ b/cmd/workspace/model-registry/model-registry.go @@ -13,16 +13,21 @@ 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 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.`) @@ -36,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) @@ -63,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`) } @@ -80,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 { @@ -113,7 +146,7 @@ func init() { } var createModelCmd = &cobra.Command{ - Use: "create-model", + Use: "create-model NAME", Short: `Create a model.`, Long: `Create a model. @@ -123,15 +156,25 @@ var createModelCmd = &cobra.Command{ exists.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - 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 { @@ -159,23 +202,33 @@ 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{}, - 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 { @@ -188,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.`) @@ -205,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) @@ -257,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) @@ -277,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`) } @@ -292,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 { @@ -310,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`) } @@ -325,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 { @@ -343,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`) } @@ -358,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 { @@ -377,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`) } @@ -392,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 { @@ -411,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`) } @@ -426,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 { @@ -446,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.`) @@ -463,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 { @@ -487,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.`) @@ -506,11 +669,24 @@ var deleteWebhookCmd = &cobra.Command{ Deletes a registry webhook.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -535,22 +711,32 @@ 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{}, - 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 { @@ -563,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`) } @@ -582,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 { @@ -600,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`) } @@ -615,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 { @@ -634,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`) } @@ -649,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 { @@ -668,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.`) @@ -687,11 +920,24 @@ var listModelsCmd = &cobra.Command{ __max_results__.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -704,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`) } @@ -719,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 { @@ -761,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) @@ -781,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.`) @@ -798,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) @@ -821,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.`) @@ -838,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 { @@ -878,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) @@ -920,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) @@ -940,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`) } @@ -955,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 { @@ -975,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`) } @@ -990,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 { @@ -1011,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.`) @@ -1030,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 { @@ -1048,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.`) @@ -1069,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) @@ -1096,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`) } @@ -1111,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 { @@ -1130,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.`) @@ -1147,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 { @@ -1165,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.`) @@ -1182,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 { @@ -1217,7 +1643,7 @@ func init() { } var updateWebhookCmd = &cobra.Command{ - Use: "update-webhook", + Use: "update-webhook ID", Short: `Update a webhook.`, Long: `Update a webhook. @@ -1226,15 +1652,25 @@ var updateWebhookCmd = &cobra.Command{ Updates a registry webhook.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - 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 82d03ca701..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 { @@ -101,7 +131,7 @@ func init() { } var setCmd = &cobra.Command{ - Use: "set", + Use: "set REQUEST_OBJECT_TYPE REQUEST_OBJECT_ID", Short: `Set permissions.`, Long: `Set permissions. @@ -109,16 +139,26 @@ var setCmd = &cobra.Command{ objects and root objects.`, Annotations: map[string]string{}, - 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 { @@ -143,23 +183,33 @@ 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{}, - 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 94d24eb385..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,10 +103,12 @@ 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`) } @@ -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,6 +168,7 @@ 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`) } @@ -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", + Use: "list-pipeline-events PIPELINE_ID", Short: `List pipeline events.`, Long: `List pipeline events. @@ -249,11 +291,28 @@ var listPipelineEventsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = listPipelineEventsJson.Unmarshal(&listPipelineEventsReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = listPipelineEventsJson.Unmarshal(&listPipelineEventsReq) + 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) + } + 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 { @@ -288,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) @@ -308,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.`) @@ -331,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 { @@ -358,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 @@ -368,6 +446,7 @@ 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`) } @@ -383,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) @@ -442,7 +528,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,11 +539,28 @@ var startUpdateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = startUpdateJson.Unmarshal(&startUpdateReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = startUpdateJson.Unmarshal(&startUpdateReq) + 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) + } + 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 { @@ -470,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 @@ -480,6 +583,7 @@ 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`) } @@ -495,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) @@ -569,7 +680,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update PIPELINE_ID", Short: `Edit a pipeline.`, Long: `Edit a pipeline. @@ -580,11 +691,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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/policy-families/policy-families.go b/cmd/workspace/policy-families/policy-families.go index 5c65af5ff3..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,11 +87,24 @@ var listCmd = &cobra.Command{ Use: "list", Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 50d13e75c2..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,10 +77,12 @@ 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`) } @@ -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,10 +133,12 @@ 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`) } @@ -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,11 +212,24 @@ var listCmd = &cobra.Command{ ordering of the elements in the array.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -193,10 +242,12 @@ 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`) } @@ -214,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 { @@ -241,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.`) @@ -268,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 5f4c948ff4..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,10 +86,12 @@ 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`) } @@ -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,10 +143,12 @@ 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`) } @@ -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,11 +222,24 @@ var listCmd = &cobra.Command{ term.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -209,10 +252,12 @@ 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`) } @@ -229,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 { @@ -272,7 +324,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update QUERY_ID", Short: `Change a query definition.`, Long: `Change a query definition. @@ -285,11 +337,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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/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 d90f24a9a5..6ccbece9dd 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,18 +47,28 @@ var createCmd = &cobra.Command{ **CREATE_RECIPIENT** privilege on the metastore.`, Annotations: map[string]string{}, - 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) @@ -72,10 +82,12 @@ 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`) } @@ -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.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 { @@ -119,10 +138,12 @@ 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`) } @@ -140,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 { @@ -167,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.`) @@ -187,11 +217,24 @@ 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, + 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,10 +300,12 @@ 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`) } @@ -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", + Use: "update NAME", Short: `Update a share recipient.`, Long: `Update a share recipient. @@ -318,11 +385,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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 a46f0b81d1..403c8a9575 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,16 +52,26 @@ var createCmd = &cobra.Command{ unlike repos created in the browser.`, Annotations: map[string]string{}, - 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 { @@ -74,10 +84,12 @@ 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`) } @@ -93,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) @@ -123,10 +142,12 @@ 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`) } @@ -142,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) @@ -172,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.`) @@ -191,11 +221,24 @@ var listCmd = &cobra.Command{ paginated with each page containing twenty repos.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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", + Use: "update REPO_ID", Short: `Update a repo.`, Long: `Update a repo. @@ -234,13 +277,30 @@ 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 - } - _, err = fmt.Sscan(args[0], &updateReq.RepoId) - if err != nil { - return fmt.Errorf("invalid REPO_ID: %s", args[0]) + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + if err != nil { + return err + } + } 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 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 ccaa494e2c..1f62d0cf45 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,16 +48,26 @@ var createCmd = &cobra.Command{ catalog.`, Annotations: map[string]string{}, - 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 { @@ -70,10 +80,12 @@ 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`) } @@ -90,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 { @@ -117,10 +136,12 @@ 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`) } @@ -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.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 { @@ -165,10 +193,12 @@ 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`) } @@ -188,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 { @@ -230,7 +267,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update FULL_NAME", Short: `Update a schema.`, Long: `Update a schema. @@ -245,11 +282,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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/secrets/secrets.go b/cmd/workspace/secrets/secrets.go index 32fbe9d312..a7526a6545 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,15 +55,25 @@ var createScopeCmd = &cobra.Command{ in a workspace is 100.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - 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 { @@ -76,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`) } @@ -96,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 { @@ -115,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`) } @@ -134,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 { @@ -152,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`) } @@ -172,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 { @@ -191,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`) } @@ -211,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 { @@ -230,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`) } @@ -250,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 { @@ -298,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`) } @@ -320,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 { @@ -338,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`) } @@ -378,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) @@ -401,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.`) @@ -437,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 7fb0640eb5..675ce89914 100755 --- a/cmd/workspace/service-principals/service-principals.go +++ b/cmd/workspace/service-principals/service-principals.go @@ -52,15 +52,24 @@ 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 { } - createReq.Id = args[0] response, err := w.ServicePrincipals.Create(ctx, createReq) if err != nil { @@ -73,10 +82,12 @@ 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`) } @@ -92,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 { @@ -119,10 +137,12 @@ 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`) } @@ -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.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 { @@ -166,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.`) @@ -189,11 +218,24 @@ 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, + 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 { @@ -218,7 +260,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update service principal details.`, Long: `Update service principal details. @@ -230,11 +272,28 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) + 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) + } + 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 { @@ -266,7 +325,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace service principal.`, Long: `Replace service principal. @@ -279,11 +338,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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/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 b937160117..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 { @@ -204,7 +264,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update NAME", Short: `Update a share.`, Long: `Update a share. @@ -224,15 +284,25 @@ var updateCmd = &cobra.Command{ Table removals through **update** do not require additional privileges.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - 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 { @@ -257,7 +327,7 @@ func init() { } var updatePermissionsCmd = &cobra.Command{ - Use: "update-permissions", + Use: "update-permissions NAME", Short: `Update permissions.`, Long: `Update permissions. @@ -268,15 +338,25 @@ var updatePermissionsCmd = &cobra.Command{ recipient revocations do not require additional privileges.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - 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 512ede412c..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", + Use: "create NAME", Short: `Create a storage credential.`, Long: `Create a storage credential. @@ -67,12 +67,28 @@ 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 + if cmd.Flags().Changed("json") { + err = createJson.Unmarshal(&createReq) + 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) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the credential name") + } + createReq.Name = args[0] } - createReq.Name = args[0] - createReq.MetastoreId = args[1] response, err := w.StorageCredentials.Create(ctx, createReq) if err != nil { @@ -85,10 +101,12 @@ 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.`) @@ -107,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 { @@ -134,10 +159,12 @@ 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`) } @@ -155,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 { @@ -233,7 +267,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update NAME", Short: `Update a credential.`, Long: `Update a credential. @@ -246,12 +280,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + if len(args) != 1 { + return fmt.Errorf("expected to have the credential name") + } + updateReq.Name = args[0] } - updateReq.MetastoreId = args[0] - updateReq.Name = args[1] response, err := w.StorageCredentials.Update(ctx, updateReq) if err != nil { @@ -300,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 c5afc67221..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,10 +29,12 @@ 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`) } @@ -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,10 +88,12 @@ 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.`) @@ -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).`) @@ -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 e9a8db47e0..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,10 +77,12 @@ 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`) } @@ -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,10 +132,12 @@ 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`) } @@ -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,11 +207,24 @@ 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, + 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 94435d8e82..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,11 +45,24 @@ var createCmd = &cobra.Command{ an error **QUOTA_EXCEEDED**.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 { @@ -59,10 +75,12 @@ 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`) } @@ -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.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 1c509ef2c1..fcafde2da8 100755 --- a/cmd/workspace/users/users.go +++ b/cmd/workspace/users/users.go @@ -60,15 +60,24 @@ 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 { } - createReq.Id = args[0] response, err := w.Users.Create(ctx, createReq) if err != nil { @@ -81,10 +90,12 @@ 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`) } @@ -101,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 { @@ -128,10 +146,12 @@ 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`) } @@ -147,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 { @@ -174,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.`) @@ -197,11 +226,24 @@ 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, + 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 { @@ -226,7 +268,7 @@ func init() { } var patchCmd = &cobra.Command{ - Use: "patch", + Use: "patch ID", Short: `Update user details.`, Long: `Update user details. @@ -238,11 +280,28 @@ var patchCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - err = patchJson.Unmarshal(&patchReq) - if err != nil { - return err + if cmd.Flags().Changed("json") { + err = patchJson.Unmarshal(&patchReq) + 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) + } + 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 { @@ -276,7 +335,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update ID", Short: `Replace a user.`, Long: `Replace a user. @@ -287,11 +346,28 @@ 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 + if cmd.Flags().Changed("json") { + err = updateJson.Unmarshal(&updateReq) + 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) + } + 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..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,10 +102,12 @@ 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`) } @@ -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,10 +220,12 @@ 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`) } @@ -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.`) @@ -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 b66445d75f..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,6 +125,7 @@ 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`) } @@ -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", + Use: "edit ID", Short: `Update a warehouse.`, Long: `Update a warehouse. @@ -221,11 +239,28 @@ var editCmd = &cobra.Command{ 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 { + 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") + } + editReq.Id = args[0] } - editReq.Id = args[0] if editSkipWait { err = w.Warehouses.Edit(ctx, editReq) @@ -262,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 @@ -272,6 +307,7 @@ 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`) } @@ -287,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 { @@ -342,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.`) @@ -359,11 +404,24 @@ 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, + 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 { @@ -404,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) @@ -424,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 @@ -434,6 +502,7 @@ 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`) } @@ -449,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) @@ -500,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 @@ -510,6 +586,7 @@ 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`) } @@ -525,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 2874269d8b..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 { @@ -71,7 +86,7 @@ func init() { } var updateCmd = &cobra.Command{ - Use: "update", + Use: "update NAME", Short: `Update catalog workspace bindings.`, Long: `Update catalog workspace bindings. @@ -79,15 +94,25 @@ var updateCmd = &cobra.Command{ admin or an owner of the catalog.`, Annotations: map[string]string{}, - PreRunE: root.MustWorkspaceClient, + Args: func(cmd *cobra.Command, args []string) error { + check := cobra.ExactArgs(1) + if cmd.Flags().Changed("json") { + check = cobra.ExactArgs(0) + } + return check(cmd, args) + }, + PreRunE: root.MustWorkspaceClient, RunE: func(cmd *cobra.Command, args []string) (err error) { ctx := cmd.Context() w := root.WorkspaceClient(ctx) - 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 d37f58f356..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,11 +85,24 @@ var setStatusCmd = &cobra.Command{ it.`, Annotations: map[string]string{}, - Args: cobra.ExactArgs(0), - 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 2b4bff9058..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,10 +25,12 @@ 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.`) @@ -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.`) @@ -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,10 +154,12 @@ 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`) } @@ -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.`) @@ -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,10 +273,12 @@ 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, `.`) @@ -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,10 +332,12 @@ 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`) } @@ -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 { 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()) +}