-
Notifications
You must be signed in to change notification settings - Fork 57
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
Improve bundle validate
output
#1532
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d63d350
Improve `bundle validate` output
kanterov 112beac
Add more tests for whitespace
kanterov 9de21bb
Fix more whitespace problems
kanterov e0a74d6
Move cmd/bundle/render -> bundle/render
kanterov a461d93
Fix build
kanterov 7b2fb10
Fix lint
kanterov 7e964f7
Fix tests
kanterov 04caf10
Fix fmt
kanterov 6282a4e
Address CR comments
kanterov 76b76a1
Don't panic
kanterov 9c2e7f9
Merge branch 'main' into improve-validate-output
kanterov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package render | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/databricks/cli/bundle" | ||
"github.com/databricks/cli/libs/diag" | ||
"github.com/databricks/databricks-sdk-go/service/iam" | ||
"github.com/fatih/color" | ||
) | ||
|
||
var renderFuncMap = template.FuncMap{ | ||
"red": color.RedString, | ||
"green": color.GreenString, | ||
"blue": color.BlueString, | ||
"yellow": color.YellowString, | ||
"magenta": color.MagentaString, | ||
"cyan": color.CyanString, | ||
"bold": func(format string, a ...interface{}) string { | ||
return color.New(color.Bold).Sprintf(format, a...) | ||
}, | ||
"italic": func(format string, a ...interface{}) string { | ||
return color.New(color.Italic).Sprintf(format, a...) | ||
}, | ||
} | ||
|
||
const errorTemplate = `{{ "Error" | red }}: {{ .Summary }} | ||
{{- if .Path.String }} | ||
{{ "at " }}{{ .Path.String | green }} | ||
{{- end }} | ||
{{- if .Location.File }} | ||
{{ "in " }}{{ .Location.String | cyan }} | ||
{{- end }} | ||
{{- if .Detail }} | ||
|
||
{{ .Detail }} | ||
{{- end }} | ||
|
||
` | ||
|
||
const warningTemplate = `{{ "Warning" | yellow }}: {{ .Summary }} | ||
{{- if .Path.String }} | ||
{{ "at " }}{{ .Path.String | green }} | ||
{{- end }} | ||
{{- if .Location.File }} | ||
{{ "in " }}{{ .Location.String | cyan }} | ||
{{- end }} | ||
{{- if .Detail }} | ||
|
||
{{ .Detail }} | ||
{{- end }} | ||
|
||
` | ||
|
||
const summaryTemplate = `{{- if .Name -}} | ||
Name: {{ .Name | bold }} | ||
{{- if .Target }} | ||
Target: {{ .Target | bold }} | ||
{{- end }} | ||
{{- if or .User .Host .Path }} | ||
Workspace: | ||
{{- if .Host }} | ||
Host: {{ .Host | bold }} | ||
{{- end }} | ||
{{- if .User }} | ||
User: {{ .User | bold }} | ||
{{- end }} | ||
{{- if .Path }} | ||
Path: {{ .Path | bold }} | ||
{{- end }} | ||
{{- end }} | ||
|
||
{{ end -}} | ||
|
||
{{ .Trailer }} | ||
` | ||
|
||
func pluralize(n int, singular, plural string) string { | ||
if n == 1 { | ||
return fmt.Sprintf("%d %s", n, singular) | ||
} | ||
return fmt.Sprintf("%d %s", n, plural) | ||
} | ||
|
||
func buildTrailer(diags diag.Diagnostics) string { | ||
parts := []string{} | ||
if errors := len(diags.Filter(diag.Error)); errors > 0 { | ||
parts = append(parts, color.RedString(pluralize(errors, "error", "errors"))) | ||
} | ||
if warnings := len(diags.Filter(diag.Warning)); warnings > 0 { | ||
parts = append(parts, color.YellowString(pluralize(warnings, "warning", "warnings"))) | ||
} | ||
if len(parts) > 0 { | ||
return fmt.Sprintf("Found %s", strings.Join(parts, " and ")) | ||
} else { | ||
return color.GreenString("Validation OK!") | ||
} | ||
} | ||
|
||
func renderSummaryTemplate(out io.Writer, b *bundle.Bundle, diags diag.Diagnostics) error { | ||
if b == nil { | ||
return renderSummaryTemplate(out, &bundle.Bundle{}, diags) | ||
} | ||
|
||
var currentUser = &iam.User{} | ||
|
||
if b.Config.Workspace.CurrentUser != nil { | ||
if b.Config.Workspace.CurrentUser.User != nil { | ||
currentUser = b.Config.Workspace.CurrentUser.User | ||
} | ||
} | ||
|
||
t := template.Must(template.New("summary").Funcs(renderFuncMap).Parse(summaryTemplate)) | ||
err := t.Execute(out, map[string]any{ | ||
"Name": b.Config.Bundle.Name, | ||
"Target": b.Config.Bundle.Target, | ||
"User": currentUser.UserName, | ||
"Path": b.Config.Workspace.RootPath, | ||
"Host": b.Config.Workspace.Host, | ||
"Trailer": buildTrailer(diags), | ||
}) | ||
|
||
return err | ||
} | ||
|
||
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 | ||
} | ||
|
||
// Make file relative to bundle root | ||
if d.Location.File != "" { | ||
out, err := filepath.Rel(b.RootPath, d.Location.File) | ||
// if we can't relativize the path, just use path as-is | ||
if err == nil { | ||
d.Location.File = out | ||
} | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
// RenderTextOutput renders the diagnostics in a human-readable format. | ||
func RenderTextOutput(out io.Writer, b *bundle.Bundle, diags diag.Diagnostics) error { | ||
err := renderDiagnostics(out, b, diags) | ||
if err != nil { | ||
return fmt.Errorf("failed to render diagnostics: %w", err) | ||
} | ||
|
||
err = renderSummaryTemplate(out, b, diags) | ||
if err != nil { | ||
return fmt.Errorf("failed to render summary: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No detail for warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add it for consistency, but it wasn't there before, and there is no use case for it right now. We simply put the whole body into "summary". I will add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added detail + unit test for it