Use native configuration diff and source evidence
This commit is contained in:
@@ -2,14 +2,16 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type configDiffFlags struct {
|
||||
@@ -30,13 +32,6 @@ type configDiffResolved struct {
|
||||
right *inspectionConfig
|
||||
}
|
||||
|
||||
type configDiffRecord struct {
|
||||
Kind string
|
||||
Path string
|
||||
Left string
|
||||
Right string
|
||||
}
|
||||
|
||||
// ConfigDiff compares two explicitly selected overlays through the same
|
||||
// read-only configuration resolution boundary used by config validate, show,
|
||||
// and sources. Differences describe normalized effective values rather than
|
||||
@@ -57,43 +52,42 @@ func configDiffWithDependencies(_ context.Context, args []string, out io.Writer,
|
||||
if err != nil {
|
||||
return fmt.Errorf("config diff: %w", err)
|
||||
}
|
||||
leftValues, err := config.EffectivePipelineValues(resolved.left.Pipeline)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config diff: project left effective configuration: %w", err)
|
||||
}
|
||||
rightValues, err := config.EffectivePipelineValues(resolved.right.Pipeline)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config diff: project right effective configuration: %w", err)
|
||||
}
|
||||
records, err := diffEffectivePipelineValues(leftValues, rightValues)
|
||||
differences, err := config.DiffEffectivePipelines(resolved.left.Pipeline, resolved.right.Pipeline)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config diff: %w", err)
|
||||
}
|
||||
leftDigest := config.EffectivePipelineDigest(resolved.left.Pipeline)
|
||||
rightDigest := config.EffectivePipelineDigest(resolved.right.Pipeline)
|
||||
if len(records) == 0 {
|
||||
if leftDigest != rightDigest {
|
||||
return fmt.Errorf("internal error: effective pipeline digests differ without a semantic difference")
|
||||
}
|
||||
if len(differences) == 0 {
|
||||
_, err := fmt.Fprintln(out, "no differences")
|
||||
return err
|
||||
}
|
||||
for _, record := range records {
|
||||
switch record.Kind {
|
||||
case "added":
|
||||
if _, err := fmt.Fprintf(out, "added\t%s\t%s\n", record.Path, record.Right); err != nil {
|
||||
for _, difference := range differences {
|
||||
kind := "changed"
|
||||
if !difference.Left.Present {
|
||||
kind = "added"
|
||||
}
|
||||
if !difference.Right.Present {
|
||||
kind = "removed"
|
||||
}
|
||||
fields := []string{kind, difference.Path.String()}
|
||||
if difference.Left.Present {
|
||||
value, err := compactSnapshot(difference.Left)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case "removed":
|
||||
if _, err := fmt.Fprintf(out, "removed\t%s\t%s\n", record.Path, record.Left); err != nil {
|
||||
fields = append(fields, value)
|
||||
}
|
||||
if difference.Right.Present {
|
||||
value, err := compactSnapshot(difference.Right)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case "changed":
|
||||
if _, err := fmt.Fprintf(out, "changed\t%s\t%s\t%s\n", record.Path, record.Left, record.Right); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("internal error: unsupported difference kind %q", record.Kind)
|
||||
fields = append(fields, value)
|
||||
}
|
||||
if difference.HiddenChanged {
|
||||
fields = append(fields, "hidden_changed=true")
|
||||
}
|
||||
if _, err := fmt.Fprintln(out, strings.Join(fields, "\t")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -264,50 +258,14 @@ func resolveSharedDiffCampaignPath(left, right *config.PipelineConfig, flags con
|
||||
return leftPath, nil
|
||||
}
|
||||
|
||||
func diffEffectivePipelineValues(left, right []config.EffectivePipelineValueRecord) ([]configDiffRecord, error) {
|
||||
leftByPath, err := effectivePipelineValueMap("left", left)
|
||||
func compactSnapshot(snapshot configkit.ValueSnapshot) (string, error) {
|
||||
var value any
|
||||
if err := yaml.Unmarshal([]byte(snapshot.YAML), &value); err != nil {
|
||||
return "", fmt.Errorf("decode safe diff value: %w", err)
|
||||
}
|
||||
encoded, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", fmt.Errorf("encode safe diff value: %w", err)
|
||||
}
|
||||
rightByPath, err := effectivePipelineValueMap("right", right)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paths := make([]string, 0, len(leftByPath)+len(rightByPath))
|
||||
seen := make(map[string]struct{}, len(leftByPath)+len(rightByPath))
|
||||
for path := range leftByPath {
|
||||
seen[path] = struct{}{}
|
||||
paths = append(paths, path)
|
||||
}
|
||||
for path := range rightByPath {
|
||||
if _, exists := seen[path]; !exists {
|
||||
paths = append(paths, path)
|
||||
}
|
||||
}
|
||||
sort.Strings(paths)
|
||||
records := make([]configDiffRecord, 0)
|
||||
for _, path := range paths {
|
||||
leftValue, leftExists := leftByPath[path]
|
||||
rightValue, rightExists := rightByPath[path]
|
||||
switch {
|
||||
case !leftExists:
|
||||
records = append(records, configDiffRecord{Kind: "added", Path: path, Right: rightValue})
|
||||
case !rightExists:
|
||||
records = append(records, configDiffRecord{Kind: "removed", Path: path, Left: leftValue})
|
||||
case leftValue != rightValue:
|
||||
records = append(records, configDiffRecord{Kind: "changed", Path: path, Left: leftValue, Right: rightValue})
|
||||
}
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func effectivePipelineValueMap(side string, records []config.EffectivePipelineValueRecord) (map[string]string, error) {
|
||||
values := make(map[string]string, len(records))
|
||||
for _, record := range records {
|
||||
if _, exists := values[record.Path]; exists {
|
||||
return nil, fmt.Errorf("internal error: %s effective configuration has duplicate path %q", side, record.Path)
|
||||
}
|
||||
values[record.Path] = record.Value
|
||||
}
|
||||
return values, nil
|
||||
return string(encoded), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user