Fix up the summary to log as a table

This commit is contained in:
2025-11-20 15:59:14 +01:00
parent cb53596c46
commit 2586386cc3
2 changed files with 136 additions and 16 deletions

View File

@@ -17,6 +17,7 @@ type FileSystem interface {
Link(source, target string) error Link(source, target string) error
RecordLinkAttempt(kind string, source, target string, err error, dryRun bool) RecordLinkAttempt(kind string, source, target string, err error, dryRun bool)
SummaryLines() []string SummaryLines() []string
SummaryRecords() []operationRecord
IsDryRun() bool IsDryRun() bool
} }
@@ -37,26 +38,72 @@ type operationRecord struct {
} }
func (r operationRecord) summaryLine() (string, bool) { func (r operationRecord) summaryLine() (string, bool) {
if r.kind != opSymlink && r.kind != opHardlink { detail := r.detail()
return "", false if detail != "" {
return fmt.Sprintf("%s %s → %s (%s) %s",
r.kindLabel(),
r.formattedSource(),
r.formattedTarget(),
r.resultLabel(),
detail), true
} }
kindLabel := "Symlink" return fmt.Sprintf("%s %s → %s (%s)",
if r.kind == opHardlink { r.kindLabel(),
kindLabel = "Hardlink" r.formattedSource(),
} r.formattedTarget(),
r.resultLabel()), true
}
status := "OK" func (r operationRecord) kindLabel() string {
switch r.kind {
case opSymlink:
return "Symlink"
case opHardlink:
return "Hardlink"
case opRemove:
return "Remove"
case opRemoveAll:
return "RemoveAll"
case opMkdirAll:
return "MkdirAll"
default:
return r.kind
}
}
func (r operationRecord) resultLabel() string {
if r.err != nil { if r.err != nil {
status = fmt.Sprintf("%sFAIL%s: %v", BRed, Reset, r.err) return fmt.Sprintf("%sFAIL%s", BRed, Reset)
} else if r.dryRun {
status = "DRY-RUN"
} }
if r.dryRun {
return fmt.Sprintf("%sDRY-RUN%s", BCyan, Reset)
}
return fmt.Sprintf("%sOK%s", BGreen, Reset)
}
source := FormatSourcePath(r.source) func (r operationRecord) formattedSource() string {
target := FormatTargetPath(r.target) if r.source == "" {
return "-"
}
return FormatSourcePath(r.source)
}
return fmt.Sprintf("%s %s → %s (%s)", kindLabel, source, target, status), true func (r operationRecord) formattedTarget() string {
if r.target == "" {
return "-"
}
return FormatTargetPath(r.target)
}
func (r operationRecord) detail() string {
if r.err != nil {
return r.err.Error()
}
if r.dryRun {
return "dry-run"
}
return ""
} }
type baseFileSystem struct { type baseFileSystem struct {
@@ -141,6 +188,10 @@ func (fs *realFileSystem) SummaryLines() []string {
return summarizeOperations(fs.snapshot()) return summarizeOperations(fs.snapshot())
} }
func (fs *realFileSystem) SummaryRecords() []operationRecord {
return fs.snapshot()
}
func (fs *realFileSystem) IsDryRun() bool { func (fs *realFileSystem) IsDryRun() bool {
return false return false
} }
@@ -187,6 +238,10 @@ func (fs *dryRunFileSystem) SummaryLines() []string {
return summarizeOperations(fs.snapshot()) return summarizeOperations(fs.snapshot())
} }
func (fs *dryRunFileSystem) SummaryRecords() []operationRecord {
return fs.snapshot()
}
func (fs *dryRunFileSystem) IsDryRun() bool { func (fs *dryRunFileSystem) IsDryRun() bool {
return true return true
} }

71
main.go
View File

@@ -8,7 +8,9 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"runtime/debug" "runtime/debug"
"strings"
"sync/atomic" "sync/atomic"
utils "git.site.quack-lab.dev/dave/cyutils" utils "git.site.quack-lab.dev/dave/cyutils"
@@ -185,14 +187,77 @@ func processInstructions(instructions chan *LinkInstruction) int32 {
} }
func printSummary(fs FileSystem) { func printSummary(fs FileSystem) {
lines := fs.SummaryLines() records := fs.SummaryRecords()
if len(lines) == 0 { if len(records) == 0 {
return return
} }
header := []string{"RESULT", "OPERATION", "SOURCE", "TARGET", "DETAIL"}
widths := make([]int, len(header))
for i, h := range header {
widths[i] = len(h)
}
type row struct {
values []string
err error
}
rows := make([]row, len(records))
for i, record := range records {
values := []string{
record.resultLabel(),
record.kindLabel(),
FormatSourcePath(record.source),
FormatTargetPath(record.target),
record.detail(),
}
rows[i] = row{values: values, err: record.err}
for j, val := range values {
if l := visibleLength(val); l > widths[j] {
widths[j] = l
}
}
}
LogInfo("Summary:") LogInfo("Summary:")
for _, line := range lines { var lineBuilder strings.Builder
writeSummaryRow(&lineBuilder, header, widths)
for _, line := range strings.Split(strings.TrimRight(lineBuilder.String(), "\n"), "\n") {
LogInfo("%s", line) LogInfo("%s", line)
} }
for _, r := range rows {
lineBuilder.Reset()
writeSummaryRow(&lineBuilder, r.values, widths)
line := strings.TrimRight(lineBuilder.String(), "\n")
LogInfo("%s", line)
}
}
func writeSummaryRow(b *strings.Builder, cols []string, widths []int) {
for i, val := range cols {
if val == "" {
val = "-"
}
b.WriteString(val)
pad := widths[i] - visibleLength(val)
if pad < 0 {
pad = 0
}
if i < len(cols)-1 {
b.WriteString(strings.Repeat(" ", pad+2))
}
}
b.WriteByte('\n')
}
var ansiRegexp = regexp.MustCompile(`\x1b\[[0-9;]*m`)
func visibleLength(s string) int {
return len(ansiRegexp.ReplaceAllString(s, ""))
} }
func IsPipeInput() bool { func IsPipeInput() bool {