code.pablomurad.com

patchwork

Publishes the revisions of your pages as a feed, with notes and readable diffs, written in Go

cmd/patchwork/scan.go raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main

import (
	"context"
	"errors"
	"fmt"
	"os"
	"time"

	"git.pablomurad.com/pablo/patchwork/internal/engine"
	"git.pablomurad.com/pablo/patchwork/internal/source/folder"
)

func cmdScan(ctx context.Context, args []string) error {
	fs := newFlags("scan", "[-dry-run] [-all]")
	cfgPath := configFlag(fs)
	dry := fs.Bool("dry-run", false, "show what would happen without writing anything")
	all := fs.Bool("all", false, "also list unchanged files")
	if err := fs.Parse(args); err != nil {
		return err
	}
	a, err := openApp(*cfgPath)
	if err != nil {
		return err
	}
	defer a.Close()

	r := a.runner()
	// A one-off scan reads files right away; waiting for an upload to
	// settle is the watcher's job.
	r.Settle = 0
	rep, err := r.Scan(ctx, *dry)
	if err != nil {
		return err
	}
	printReport(rep, *all)
	if *dry {
		fmt.Fprintln(stdout, "(dry run: nothing was written)")
	}
	if len(rep.Errors) > 0 {
		return errProblems
	}
	return nil
}

func cmdWatch(ctx context.Context, args []string) error {
	fs := newFlags("watch", "[-poll-only]")
	cfgPath := configFlag(fs)
	pollOnly := fs.Bool("poll-only", false, "don't use file events (for network or bind mounts where they never arrive)")
	if err := fs.Parse(args); err != nil {
		return err
	}
	a, err := openApp(*cfgPath)
	if err != nil {
		return err
	}
	defer a.Close()

	r := a.runner()
	r.PollOnly = *pollOnly
	a.log.Info("watching", "source", a.cfg.Source.Path, "db", a.cfg.DB)
	err = r.Watch(ctx, a.cfg.Watch.Debounce.Duration, a.cfg.Watch.Poll.Duration, func(rep folder.Report, err error) {
		if err != nil {
			a.log.Error("scan failed", "err", err)
			return
		}
		printReport(rep, false)
	})
	if errors.Is(err, context.Canceled) {
		return nil
	}
	return err
}

func printReport(rep folder.Report, all bool) {
	for _, res := range rep.Results {
		if res.Action == engine.Unchanged && !res.Restored && !all {
			continue
		}
		fmt.Fprintln(stdout, formatResult(res))
		for _, w := range res.Warnings {
			fmt.Fprintf(os.Stderr, "  warning: %s: %s\n", res.Path, w)
		}
	}
	for _, p := range rep.Pending {
		fmt.Fprintf(stdout, "%-10s %s (still being written; will retry)\n", "pending", p)
	}
	for _, err := range rep.Errors {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
	}
}

func formatResult(res engine.Result) string {
	action := string(res.Action)
	if res.Restored {
		action = "restored"
	}
	s := fmt.Sprintf("%-10s %s", action, res.Path)
	switch res.Action {
	case engine.Published, engine.Amended, engine.Folded:
		s += fmt.Sprintf("  #%d  %q", res.Number, res.Note)
	}
	return s
}

func shortTime(t time.Time) string { return t.Local().Format("2006-01-02 15:04") }