Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9c5f503ef6 | |||
| 2586386cc3 |
229
filesystem.go
229
filesystem.go
@@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,6 +19,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 +40,96 @@ 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) resultLabelPlain() string {
|
||||||
target := FormatTargetPath(r.target)
|
if r.err != nil {
|
||||||
|
return "FAIL"
|
||||||
|
}
|
||||||
|
if r.dryRun {
|
||||||
|
return "DRY-RUN"
|
||||||
|
}
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("%s %s → %s (%s)", kindLabel, source, target, status), true
|
func (r operationRecord) formattedSource() string {
|
||||||
|
if r.source == "" {
|
||||||
|
return "-"
|
||||||
|
}
|
||||||
|
return FormatSourcePath(r.source)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r operationRecord) plainSource() string {
|
||||||
|
if r.source == "" {
|
||||||
|
return "-"
|
||||||
|
}
|
||||||
|
return r.source
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r operationRecord) formattedTarget() string {
|
||||||
|
if r.target == "" {
|
||||||
|
return "-"
|
||||||
|
}
|
||||||
|
return FormatTargetPath(r.target)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r operationRecord) plainTarget() string {
|
||||||
|
if r.target == "" {
|
||||||
|
return "-"
|
||||||
|
}
|
||||||
|
return 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 +214,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 +264,132 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildSummaryLines(records []operationRecord) []string {
|
||||||
|
if len(records) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
sorted := make([]operationRecord, len(records))
|
||||||
|
copy(sorted, records)
|
||||||
|
|
||||||
|
opOrder := map[string]int{
|
||||||
|
opRemove: 0,
|
||||||
|
opRemoveAll: 1,
|
||||||
|
opMkdirAll: 2,
|
||||||
|
opSymlink: 3,
|
||||||
|
opHardlink: 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.SliceStable(sorted, func(i, j int) bool {
|
||||||
|
ti := strings.ToLower(sorted[i].plainTarget())
|
||||||
|
tj := strings.ToLower(sorted[j].plainTarget())
|
||||||
|
if ti != tj {
|
||||||
|
return ti < tj
|
||||||
|
}
|
||||||
|
|
||||||
|
si := strings.ToLower(sorted[i].plainSource())
|
||||||
|
sj := strings.ToLower(sorted[j].plainSource())
|
||||||
|
if si != sj {
|
||||||
|
return si < sj
|
||||||
|
}
|
||||||
|
|
||||||
|
oi := opOrderValue(sorted[i].kind, opOrder)
|
||||||
|
oj := opOrderValue(sorted[j].kind, opOrder)
|
||||||
|
if oi != oj {
|
||||||
|
return oi < oj
|
||||||
|
}
|
||||||
|
|
||||||
|
return sorted[i].detail() < sorted[j].detail()
|
||||||
|
})
|
||||||
|
|
||||||
|
header := []string{"RESULT", "OPERATION", "SOURCE", "TARGET", "DETAIL"}
|
||||||
|
widths := make([]int, len(header))
|
||||||
|
for i, h := range header {
|
||||||
|
widths[i] = len(h)
|
||||||
|
}
|
||||||
|
|
||||||
|
plainRows := make([][]string, len(sorted))
|
||||||
|
coloredRows := make([][]string, len(sorted))
|
||||||
|
for i, record := range sorted {
|
||||||
|
detail := record.detail()
|
||||||
|
if detail == "" {
|
||||||
|
detail = "-"
|
||||||
|
}
|
||||||
|
|
||||||
|
plain := []string{
|
||||||
|
record.resultLabelPlain(),
|
||||||
|
record.kindLabel(),
|
||||||
|
record.plainSource(),
|
||||||
|
record.plainTarget(),
|
||||||
|
detail,
|
||||||
|
}
|
||||||
|
colored := []string{
|
||||||
|
record.resultLabel(),
|
||||||
|
record.kindLabel(),
|
||||||
|
record.formattedSource(),
|
||||||
|
record.formattedTarget(),
|
||||||
|
detail,
|
||||||
|
}
|
||||||
|
|
||||||
|
plainRows[i] = plain
|
||||||
|
coloredRows[i] = colored
|
||||||
|
|
||||||
|
for j, val := range plain {
|
||||||
|
if val == "" {
|
||||||
|
val = "-"
|
||||||
|
}
|
||||||
|
if len(val) > widths[j] {
|
||||||
|
widths[j] = len(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := make([]string, 0, len(sorted)+1)
|
||||||
|
lines = append(lines, formatSummaryRow(header, header, widths))
|
||||||
|
for i := range coloredRows {
|
||||||
|
lines = append(lines, formatSummaryRow(coloredRows[i], plainRows[i], widths))
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatSummaryRow(colored, plain []string, widths []int) string {
|
||||||
|
var b strings.Builder
|
||||||
|
for i := range colored {
|
||||||
|
p := plain[i]
|
||||||
|
if p == "" {
|
||||||
|
p = "-"
|
||||||
|
}
|
||||||
|
col := colored[i]
|
||||||
|
if col == "" {
|
||||||
|
col = p
|
||||||
|
}
|
||||||
|
pad := widths[i] - len(p)
|
||||||
|
if pad < 0 {
|
||||||
|
pad = 0
|
||||||
|
}
|
||||||
|
b.WriteString(col)
|
||||||
|
if pad > 0 {
|
||||||
|
b.WriteString(strings.Repeat(" ", pad))
|
||||||
|
}
|
||||||
|
if i < len(colored)-1 {
|
||||||
|
b.WriteString(" ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func opOrderValue(kind string, order map[string]int) int {
|
||||||
|
if v, ok := order[kind]; ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return len(order)
|
||||||
|
}
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -185,10 +185,7 @@ func processInstructions(instructions chan *LinkInstruction) int32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func printSummary(fs FileSystem) {
|
func printSummary(fs FileSystem) {
|
||||||
lines := fs.SummaryLines()
|
lines := BuildSummaryLines(fs.SummaryRecords())
|
||||||
if len(lines) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
LogInfo("Summary:")
|
LogInfo("Summary:")
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
LogInfo("%s", line)
|
LogInfo("%s", line)
|
||||||
|
|||||||
Reference in New Issue
Block a user