-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
144 lines (122 loc) · 3.34 KB
/
main.go
File metadata and controls
144 lines (122 loc) · 3.34 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
const (
exitOK = 0
exitDiff = 1
exitUsageErr = 2
)
type options struct {
useDate bool
useHashes bool
verbose bool
}
func main() {
cfg := loadConfig()
pathA, suffix, opts, err := parseArgs(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(exitUsageErr)
}
pathB, err := computeMirrorPath(pathA, suffix)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(exitUsageErr)
}
if _, err := os.Stat(pathB); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error: mirror path does not exist: %s\n", pathB)
os.Exit(exitUsageErr)
}
ignorer := newIgnorer(cfg.AlwaysExclude, pathA)
listA, err := walkTree(pathA, ignorer, "A", opts)
if err != nil {
fmt.Fprintf(os.Stderr, "Error walking %s: %s\n", pathA, err)
os.Exit(exitDiff)
}
listB, err := walkTree(pathB, ignorer, "B", opts)
if err != nil {
fmt.Fprintf(os.Stderr, "Error walking %s: %s\n", pathB, err)
os.Exit(exitDiff)
}
diffs := computeDiff(listA, listB, pathA, pathB, opts)
if len(diffs) == 0 {
fmt.Println("No differences found.")
os.Exit(exitOK)
}
syncDocxNotText(diffs, pathA, pathB)
syncModes(diffs, pathA, pathB)
printDiffs(diffs, opts)
os.Exit(exitDiff)
}
func parseArgs(args []string) (pathA string, suffix int, opts options, err error) {
suffix = 2
var positional []string
for _, arg := range args {
if arg == "--use-date" {
opts.useDate = true
} else if arg == "--hashes" {
opts.useHashes = true
} else if arg == "--verbose" {
opts.verbose = true
} else {
positional = append(positional, arg)
}
}
args = positional
switch len(args) {
case 0:
pathA, err = os.Getwd()
if err != nil {
return "", 0, opts, fmt.Errorf("cannot get working directory: %w", err)
}
case 1:
pathA, err = filepath.Abs(args[0])
if err != nil {
return "", 0, opts, fmt.Errorf("cannot resolve path %q: %w", args[0], err)
}
case 2:
pathA, err = filepath.Abs(args[0])
if err != nil {
return "", 0, opts, fmt.Errorf("cannot resolve path %q: %w", args[0], err)
}
suffix, err = strconv.Atoi(args[1])
if err != nil {
return "", 0, opts, fmt.Errorf("second argument must be a number, got %q", args[1])
}
if suffix < 1 {
return "", 0, opts, fmt.Errorf("suffix must be a positive number, got %d", suffix)
}
default:
return "", 0, opts, fmt.Errorf("usage: differ [path] [number]")
}
if _, statErr := os.Stat(pathA); os.IsNotExist(statErr) {
return "", 0, opts, fmt.Errorf("path does not exist: %s", pathA)
}
return pathA, suffix, opts, nil
}
func computeMirrorPath(pathA string, suffix int) (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("cannot determine home directory: %w", err)
}
if !strings.HasPrefix(pathA, home+string(os.PathSeparator)) {
return "", fmt.Errorf("path %q is not under home directory %q", pathA, home)
}
rel := strings.TrimPrefix(pathA, home+string(os.PathSeparator))
parts := strings.SplitN(rel, string(os.PathSeparator), 2)
component := parts[0]
if component == "" {
return "", fmt.Errorf("path %q has no directory component after home", pathA)
}
newComponent := fmt.Sprintf("%s.%d", component, suffix)
result := filepath.Join(home, newComponent)
if len(parts) > 1 {
result = filepath.Join(result, parts[1])
}
return result, nil
}