Render configuration explanations from acquired reports
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package ckconfig
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/template"
|
||||
@@ -18,6 +17,7 @@ type explanationReport struct {
|
||||
History []explanationChangeDTO `json:"history"`
|
||||
SecretReference *secretReferenceDTO `json:"secret_reference"`
|
||||
Metadata *explanationMetadata `json:"metadata,omitempty"`
|
||||
Sources []validationSource `json:"sources,omitempty"`
|
||||
}
|
||||
|
||||
type explanationMetadata struct {
|
||||
@@ -48,6 +48,29 @@ type pathMetadata struct {
|
||||
|
||||
var pathDiscoveryTemplate = template.Must(commandkit.ParseTextTemplate("paths", "{{range .Paths}}{{printf \"%q\" .Path}} {{if .Present}}{{if .Metadata.Explicit}}explicit{{else}}non-explicit{{end}}{{else}}historical{{end}}\n{{end}}", nil))
|
||||
|
||||
var explanationTemplate = template.Must(commandkit.ParseTextTemplate("explanation", `Scope: {{.Scope}}
|
||||
Path: {{printf "%q" .Path}}
|
||||
Effective: {{template "snapshot" .Effective}}
|
||||
Origin: {{template "origin" .Origin}}
|
||||
History:
|
||||
{{range .History}} Before: {{template "snapshot" .Before}}
|
||||
After: {{template "snapshot" .After}}
|
||||
Origin: {{template "origin" .Origin}}
|
||||
Reasons: {{range .Reasons}}{{printf "%q " .}}{{end}}
|
||||
Dependencies: {{range .Dependencies}}{{printf "%q " .}}{{end}}
|
||||
{{end}}{{with .SecretReference}}Secret reference: {{.Name}}
|
||||
{{end}}{{with .Metadata}}{{if .Explicit}}Explicit: {{.Explicit}}
|
||||
Null: {{.Null}}
|
||||
{{end}}{{range .History}}Metadata history: {{printf "%q" .Path}}
|
||||
{{range .Previous}} Previous origin: {{template "origin" .}}
|
||||
{{end}}{{end}}{{end}}{{with .Sources}}Sources:
|
||||
{{range .}} {{printf "%q" .ID}}: label={{printf "%q" .Label}} path={{printf "%q" .Path}}
|
||||
{{end}}{{end}}{{define "snapshot"}}present={{.Present}} redacted={{.Redacted}} yaml={{printf "%q" .YAML}}{{end}}
|
||||
{{- define "locations"}}{{range .}} {{printf "%q" .SourceID}}:{{.Line}}:{{.Column}}{{end}}{{end}}
|
||||
{{- define "origin"}}layer={{printf "%q" .Layer}} operation={{printf "%q" .Operation}} locations:{{template "locations" .Locations}}
|
||||
{{range .Inheritance}} Inheritance: from={{printf "%q" .From}} to={{printf "%q" .To}} reference={{printf "%q" .Reference}} locations:{{template "locations" .Locations}}
|
||||
{{end}}{{end}}`, nil))
|
||||
|
||||
type snapshotDTO struct {
|
||||
Present bool `json:"present"`
|
||||
Redacted bool `json:"redacted"`
|
||||
@@ -157,17 +180,12 @@ func runConfigExplanation[I, S any](invocation commandkit.Invocation, plan *conf
|
||||
report.Metadata = metadata
|
||||
}
|
||||
}
|
||||
text := template.Must(commandkit.ParseTextTemplate("explanation", "{{render .}}", template.FuncMap{
|
||||
"render": func(report explanationReport) (string, error) {
|
||||
var output bytes.Buffer
|
||||
err := acquired.Value.Explanation.WriteExplanation(&output, plan.path)
|
||||
if err == nil && report.Metadata != nil && report.Metadata.Explicit != nil {
|
||||
_, err = fmt.Fprintf(&output, "Explicit: %t\nNull: %t\n", *report.Metadata.Explicit, *report.Metadata.Null)
|
||||
}
|
||||
return output.String(), err
|
||||
},
|
||||
}))
|
||||
renderErr := reporter.Report(commandkit.Report{Value: report, TextTemplate: text})
|
||||
if acquired.Value.Sources != nil {
|
||||
for _, source := range acquired.Value.Sources.Sources() {
|
||||
report.Sources = append(report.Sources, validationSource{ID: source.ID, Label: source.Label, Path: source.Path})
|
||||
}
|
||||
}
|
||||
renderErr := reporter.Report(commandkit.Report{Value: report, TextTemplate: explanationTemplate})
|
||||
if renderErr != nil && invocation.Context.Err() != nil {
|
||||
return configFailure("Configuration explanation was canceled.", acquired.Explanation, invocation.Context.Err())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user