2026-11-21 04:06:45 +00:00
|
|
|
package engine
|
|
|
|
|
|
2026-11-21 04:07:57 +00:00
|
|
|
import (
|
2026-11-21 04:09:41 +00:00
|
|
|
"errors"
|
2026-11-21 04:07:57 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
|
|
yamladapter "go.maximumdirect.net/configkit/internal/yaml"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-21 04:06:45 +00:00
|
|
|
// InspectionView borrows an inspection and selects a canonical reporting scope.
|
|
|
|
|
type InspectionView struct {
|
|
|
|
|
inspection *Inspection
|
|
|
|
|
scope Path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewInspectionView(inspection *Inspection, scope Path) (InspectionView, bool) {
|
|
|
|
|
if inspection == nil || !scope.valid() {
|
|
|
|
|
return InspectionView{}, false
|
|
|
|
|
}
|
|
|
|
|
normalized, err := ParsePath(scope.String())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return InspectionView{}, false
|
|
|
|
|
}
|
|
|
|
|
return InspectionView{inspection: inspection, scope: normalized}, true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (v InspectionView) Inspection() *Inspection { return v.inspection }
|
|
|
|
|
|
2026-11-21 04:09:41 +00:00
|
|
|
func (v InspectionView) Diff(other InspectionView) ([]Difference, error) {
|
|
|
|
|
if v.inspection == nil || other.inspection == nil || v.scope.String() != other.scope.String() {
|
|
|
|
|
return nil, errors.New("configkit: diff inspection scope unavailable or mismatched")
|
|
|
|
|
}
|
|
|
|
|
values, err := Diff(v.inspection, other.inspection)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var result []Difference
|
|
|
|
|
for _, value := range values {
|
|
|
|
|
if pathContains(v.scope, value.Path) || pathContains(value.Path, v.scope) {
|
|
|
|
|
result = append(result, value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 04:07:57 +00:00
|
|
|
func (v InspectionView) WriteEffective(output io.Writer) error {
|
|
|
|
|
if v.inspection == nil || v.inspection.final == nil || output == nil {
|
|
|
|
|
return errEffectiveUnavailable
|
|
|
|
|
}
|
|
|
|
|
node, ok := documentAtPathWithLookup(v.inspection.final, v.scope, v.inspection.documentLookup())
|
|
|
|
|
if !ok {
|
|
|
|
|
return errEffectiveUnavailable
|
|
|
|
|
}
|
|
|
|
|
return yamladapter.Write(output, redactedDocument(node, v.inspection.secrets, v.scope))
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 04:06:45 +00:00
|
|
|
func (v InspectionView) HistoricalPaths() []Path {
|
|
|
|
|
if v.inspection == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var paths []Path
|
|
|
|
|
for _, path := range v.inspection.Metadata().HistoricalPaths() {
|
|
|
|
|
if pathContains(v.scope, path) {
|
|
|
|
|
paths = append(paths, path)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return paths
|
|
|
|
|
}
|