Report configured bootstrap evidence at invocation completion
This commit is contained in:
55
internal/app/bootstrap_reporting.go
Normal file
55
internal/app/bootstrap_reporting.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"go.maximumdirect.net/commandkit"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
type bootstrapEvidenceKey struct{}
|
||||
type bootstrapEvidence struct {
|
||||
reports []*configkit.SecretsReport
|
||||
diagnostics [][]configkit.Diagnostic
|
||||
}
|
||||
|
||||
func (e *bootstrapEvidence) Text() (string, error) {
|
||||
var output strings.Builder
|
||||
for _, report := range e.reports {
|
||||
if _, err := report.WriteTo(&output); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
for _, diagnostics := range e.diagnostics {
|
||||
if err := configkit.WriteDiagnostics(&output, diagnostics); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return output.String(), nil
|
||||
}
|
||||
|
||||
var bootstrapTemplate = template.Must(commandkit.ParseTextTemplate("bootstrap", "{{.Text}}", nil))
|
||||
|
||||
func bootstrapFeature() commandkit.InvocationFeature {
|
||||
return commandkit.NewInvocationFeature(commandkit.InvocationFeatureSpec[struct{}]{Start: func(inv commandkit.Invocation, _ struct{}) (context.Context, func() commandkit.Report) {
|
||||
evidence := &bootstrapEvidence{}
|
||||
return context.WithValue(inv.Context, bootstrapEvidenceKey{}, evidence), func() commandkit.Report { return commandkit.Report{Value: evidence, TextTemplate: bootstrapTemplate} }
|
||||
}})
|
||||
}
|
||||
|
||||
func recordBootstrapEvidence(ctx context.Context, report *configkit.SecretsReport, err error) {
|
||||
evidence, _ := ctx.Value(bootstrapEvidenceKey{}).(*bootstrapEvidence)
|
||||
if evidence == nil {
|
||||
return
|
||||
}
|
||||
if report != nil {
|
||||
evidence.reports = append(evidence.reports, report)
|
||||
}
|
||||
var secretsError *configkit.SecretsError
|
||||
if errors.As(err, &secretsError) {
|
||||
evidence.diagnostics = append(evidence.diagnostics, secretsError.Diagnostics())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user