118 lines
4.0 KiB
Go
118 lines
4.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
"go.maximumdirect.net/ckconfig"
|
|
"go.maximumdirect.net/ckprompt"
|
|
commandkit "go.maximumdirect.net/commandkit"
|
|
ck "go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
type bootstrapReporterKey struct{}
|
|
|
|
func bootstrapFeature() commandkit.InvocationFeature {
|
|
return commandkit.NewInvocationFeature(commandkit.InvocationFeatureSpec[struct{}]{
|
|
Start: func(inv commandkit.Invocation, _ struct{}) (context.Context, func() commandkit.Report) {
|
|
return context.WithValue(inv.Context, bootstrapReporterKey{}, inv.Reporter()), nil
|
|
},
|
|
})
|
|
}
|
|
|
|
type bootstrapError struct {
|
|
cause error
|
|
report *ck.SecretsReport
|
|
}
|
|
|
|
func (e *bootstrapError) Error() string {
|
|
var output bytes.Buffer
|
|
fmt.Fprintln(&output, e.cause)
|
|
var native *ck.SecretsError
|
|
if errors.As(e.cause, &native) {
|
|
for _, diagnostic := range native.Diagnostics() {
|
|
fmt.Fprintf(&output, "%s: %s\n", diagnostic.Code, diagnostic.Message)
|
|
}
|
|
}
|
|
if e.report != nil {
|
|
_, _ = e.report.WriteTo(&output)
|
|
}
|
|
return output.String()
|
|
}
|
|
|
|
func (e *bootstrapError) Unwrap() error { return e.cause }
|
|
|
|
func reportBootstrap(ctx context.Context, report *ck.SecretsReport, err error) error {
|
|
var native *ck.SecretsError
|
|
if errors.As(err, &native) {
|
|
return &bootstrapError{cause: err, report: report}
|
|
}
|
|
if report != nil && len(report.Entries()) > 0 {
|
|
if reporter, ok := ctx.Value(bootstrapReporterKey{}).(*commandkit.Reporter); ok {
|
|
_ = reporter.Info("Secrets bootstrap", func(w io.Writer) error {
|
|
_, writeErr := report.WriteTo(w)
|
|
return writeErr
|
|
})
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// withBootstrapContext retains safe bootstrap evidence in configuration outcomes.
|
|
func withBootstrapContext[I, S any](scope ckconfig.Scope[I, S]) ckconfig.Scope[I, S] {
|
|
acquire := scope.Acquire
|
|
scope.Acquire = func(ctx context.Context, request ckconfig.Request, input I, state S) (commandkit.Acquired[ckconfig.View], error) {
|
|
result, err := acquire(ctx, request, input, state)
|
|
result.Explanation = bootstrapExplanation(result.Explanation, err)
|
|
return result, err
|
|
}
|
|
compare := scope.Compare
|
|
scope.Compare = func(ctx context.Context, request ckconfig.ComparisonRequest[I], state S) (commandkit.Acquired[[]ck.Difference], error) {
|
|
result, err := compare(ctx, request, state)
|
|
result.Explanation = bootstrapExplanation(result.Explanation, err)
|
|
return result, err
|
|
}
|
|
return scope
|
|
}
|
|
|
|
func bootstrapExplanation(explanation commandkit.Explanation, err error) commandkit.Explanation {
|
|
var failure *bootstrapError
|
|
if errors.As(err, &failure) {
|
|
explanation.Details = append(explanation.Details, func(w io.Writer) error {
|
|
_, writeErr := io.WriteString(w, failure.Error())
|
|
return writeErr
|
|
})
|
|
}
|
|
return explanation
|
|
}
|
|
|
|
func withBootstrapAcquisition[R, I, S, T any](load func(context.Context, R, I, S) (commandkit.Acquired[T], error)) func(context.Context, R, I, S) (commandkit.Acquired[T], error) {
|
|
if load == nil {
|
|
return nil
|
|
}
|
|
return func(ctx context.Context, request R, input I, state S) (commandkit.Acquired[T], error) {
|
|
result, err := load(ctx, request, input, state)
|
|
result.Explanation = bootstrapExplanation(result.Explanation, err)
|
|
return result, err
|
|
}
|
|
}
|
|
|
|
func withPromptBootstrapContext[I, S any](suite ckprompt.Suite[I, S]) ckprompt.Suite[I, S] {
|
|
suite.InspectPrompt = withBootstrapAcquisition(suite.InspectPrompt)
|
|
suite.InspectProfile = withBootstrapAcquisition(suite.InspectProfile)
|
|
suite.InspectBackend = withBootstrapAcquisition(suite.InspectBackend)
|
|
suite.InspectRequirements = withBootstrapAcquisition(suite.InspectRequirements)
|
|
suite.ValidateRequest = withBootstrapAcquisition(suite.ValidateRequest)
|
|
suite.PrepareRequest = withBootstrapAcquisition(suite.PrepareRequest)
|
|
suite.SmokeTest = withBootstrapAcquisition(suite.SmokeTest)
|
|
suite.ListPrompts = withBootstrapAcquisition(suite.ListPrompts)
|
|
suite.ListProfiles = withBootstrapAcquisition(suite.ListProfiles)
|
|
suite.ListBackends = withBootstrapAcquisition(suite.ListBackends)
|
|
suite.ListNamespaces = withBootstrapAcquisition(suite.ListNamespaces)
|
|
suite.ListCollections = withBootstrapAcquisition(suite.ListCollections)
|
|
return suite
|
|
}
|