Expose managed output pruning

This commit is contained in:
2026-06-08 19:38:49 +00:00
parent 6daddad543
commit ea562c1c3a
12 changed files with 481 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package app
import (
"context"
"fmt"
"io"
"time"
"gitea.maximumdirect.net/eric/distributor/internal/config"
@@ -16,6 +17,8 @@ type PruneOptions struct {
DestinationID string
DryRun bool
Now time.Time
Stdout io.Writer
OutputFormat OutputFormat
}
type PrunePlanOptions struct {
@@ -63,6 +66,9 @@ type PruneOutputRecord struct {
}
func Prune(ctx context.Context, options PruneOptions) (PruneReport, error) {
if err := ValidateOutputFormat(options.OutputFormat); err != nil {
return PruneReport{}, err
}
if err := ctx.Err(); err != nil {
return PruneReport{}, err
}
@@ -106,7 +112,14 @@ func pruneSetupWithBackendFactory(ctx context.Context, setup runtimeSetup, optio
}
defer closeBackend(destinationBackend)
return executePrune(ctx, destinationBackend, pipeline, destination, options)
report, err := executePrune(ctx, destinationBackend, pipeline, destination, options)
if err != nil {
return report, err
}
if err := WritePruneReport(options.Stdout, options.OutputFormat, report); err != nil {
return PruneReport{}, err
}
return report, nil
}
func requirePruneScope(options PruneOptions) error {
@@ -292,3 +305,35 @@ func pruneOutputRecords(candidates []state.PruneCandidate) []PruneOutputRecord {
}
return records
}
func WritePruneReport(w io.Writer, format OutputFormat, report PruneReport) error {
if IsJSONOutput(format) {
return WriteJSONEnvelope(w, "prune", true, nil, report, nil)
}
return writePruneReportText(w, report)
}
func writePruneReportText(w io.Writer, report PruneReport) error {
if w == nil {
return nil
}
status := "unchanged"
if report.StateChanged {
status = "changed"
} else if report.WouldChange {
status = "would_change"
}
_, err := fmt.Fprintf(w, "Prune: pipeline=%s destination=%s backend=%s root=%s status=%s checked=%d planned=%d deleted=%d preserved=%d dry_run=%t\n",
report.PipelineID,
report.DestinationID,
report.Backend,
report.RootPath,
status,
report.CheckedCount,
len(report.PlannedOutputs),
len(report.DeletedOutputs),
len(report.PreservedOutputs),
report.DryRun,
)
return err
}