Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JSON input validation for CLI commands #1771

Merged
merged 15 commits into from
Oct 11, 2024
19 changes: 9 additions & 10 deletions .codegen/service.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package {{(.TrimPrefix "account").SnakeName}}
import (
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/databricks-sdk-go/service/{{.Package.Name}}"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -227,14 +228,12 @@ func new{{.PascalName}}() *cobra.Command {
cmd.PreRunE = root.Must{{if .Service.IsAccounts}}Account{{else}}Workspace{{end}}Client
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
var diags diag.Diagnostics
{{if .Service.IsAccounts}}a := root.AccountClient(ctx){{else}}w := root.WorkspaceClient(ctx){{end}}
{{- if .Request }}
{{ if .CanUseJson }}
if cmd.Flags().Changed("json") {
err = {{.CamelName}}Json.Unmarshal(&{{.CamelName}}Req)
if err != nil {
return err
}
diags = {{.CamelName}}Json.Unmarshal(&{{.CamelName}}Req)
}{{end}}{{ if .MustUseJson }}else {
return fmt.Errorf("please provide command input in JSON format by specifying the --json flag")
}{{- end}}
Expand Down Expand Up @@ -278,9 +277,9 @@ func new{{.PascalName}}() *cobra.Command {
}
if {{.CamelName}}SkipWait {
{{if not .Response.IsEmpty -}}
return cmdio.Render(ctx, wait.Response)
return cmdio.RenderWithDiagnostics(ctx, wait.Response, diags)
{{- else -}}
return nil
return diags.Error()
{{- end}}
}
spinner := cmdio.Spinner(ctx)
Expand Down Expand Up @@ -308,7 +307,7 @@ func new{{.PascalName}}() *cobra.Command {
if err != nil {
return err
}
return cmdio.Render(ctx, info)
return cmdio.RenderWithDiagnostics(ctx, info, diags)
{{- else -}}
{{template "method-call" .}}
{{end -}}
Expand Down Expand Up @@ -351,12 +350,12 @@ func new{{.PascalName}}() *cobra.Command {
{{ if not .Response.IsEmpty -}}
{{- if .IsResponseByteStream -}}
defer response.{{.ResponseBodyField.PascalName}}.Close()
return cmdio.Render{{ if .Pagination}}Iterator{{end}}(ctx, response.{{.ResponseBodyField.PascalName}})
return cmdio.Render{{ if .Pagination}}Iterator{{end}}WithDiagnostics(ctx, response.{{.ResponseBodyField.PascalName}}, diags)
{{- else -}}
return cmdio.Render{{ if .Pagination}}Iterator{{end}}(ctx, response)
return cmdio.Render{{ if .Pagination}}Iterator{{end}}WithDiagnostics(ctx, response, diags)
{{- end -}}
{{ else -}}
return nil
return diags.Error()
{{- end -}}
{{- end -}}

Expand Down
49 changes: 2 additions & 47 deletions bundle/render/render_text_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"text/template"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/fatih/color"
Expand All @@ -28,34 +29,6 @@ var renderFuncMap = template.FuncMap{
},
}

const errorTemplate = `{{ "Error" | red }}: {{ .Summary }}
{{- range $index, $element := .Paths }}
{{ if eq $index 0 }}at {{else}} {{ end}}{{ $element.String | green }}
{{- end }}
{{- range $index, $element := .Locations }}
{{ if eq $index 0 }}in {{else}} {{ end}}{{ $element.String | cyan }}
{{- end }}
{{- if .Detail }}

{{ .Detail }}
{{- end }}

`

const warningTemplate = `{{ "Warning" | yellow }}: {{ .Summary }}
{{- range $index, $element := .Paths }}
{{ if eq $index 0 }}at {{else}} {{ end}}{{ $element.String | green }}
{{- end }}
{{- range $index, $element := .Locations }}
{{ if eq $index 0 }}in {{else}} {{ end}}{{ $element.String | cyan }}
{{- end }}
{{- if .Detail }}

{{ .Detail }}
{{- end }}

`

const summaryTemplate = `{{- if .Name -}}
Name: {{ .Name | bold }}
{{- if .Target }}
Expand Down Expand Up @@ -128,19 +101,7 @@ func renderSummaryTemplate(out io.Writer, b *bundle.Bundle, diags diag.Diagnosti
}

func renderDiagnostics(out io.Writer, b *bundle.Bundle, diags diag.Diagnostics) error {
errorT := template.Must(template.New("error").Funcs(renderFuncMap).Parse(errorTemplate))
warningT := template.Must(template.New("warning").Funcs(renderFuncMap).Parse(warningTemplate))

// Print errors and warnings.
for _, d := range diags {
var t *template.Template
switch d.Severity {
case diag.Error:
t = errorT
case diag.Warning:
t = warningT
}

for i := range d.Locations {
if b == nil {
break
Expand All @@ -155,15 +116,9 @@ func renderDiagnostics(out io.Writer, b *bundle.Bundle, diags diag.Diagnostics)
}
}
}

// Render the diagnostic with the appropriate template.
err := t.Execute(out, d)
if err != nil {
return fmt.Errorf("failed to render template: %w", err)
}
}

return nil
return cmdio.RenderDiagnostics(out, diags)
}

// RenderOptions contains options for rendering diagnostics.
Expand Down
15 changes: 8 additions & 7 deletions cmd/account/access-control/access-control.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion cmd/account/billable-usage/billable-usage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions cmd/account/budgets/budgets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading