-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (115 loc) · 3.09 KB
/
main.go
File metadata and controls
136 lines (115 loc) · 3.09 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
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
var useDate bool
var useHashes bool
var verbose bool
func main() {
cfg := loadConfig()
pathA, suffix, err := parseArgs(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(2)
}
pathB, err := computeMirrorPath(pathA, suffix)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(2)
}
if _, err := os.Stat(pathB); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error: mirror path does not exist: %s\n", pathB)
os.Exit(2)
}
ignorer := newIgnorer(cfg.AlwaysExclude, pathA)
listA, err := walkTree(pathA, ignorer, "A")
if err != nil {
fmt.Fprintf(os.Stderr, "Error walking %s: %s\n", pathA, err)
os.Exit(1)
}
listB, err := walkTree(pathB, ignorer, "B")
if err != nil {
fmt.Fprintf(os.Stderr, "Error walking %s: %s\n", pathB, err)
os.Exit(1)
}
diffs := computeDiff(listA, listB, pathA, pathB)
if len(diffs) == 0 {
fmt.Println("No differences found.")
os.Exit(0)
}
syncDocxNotText(diffs, pathA, pathB)
syncModes(diffs, pathA, pathB)
printDiffs(diffs)
os.Exit(1)
}
func parseArgs(args []string) (pathA string, suffix int, err error) {
suffix = 2
var positional []string
for _, arg := range args {
if arg == "--use-date" {
useDate = true
} else if arg == "--hashes" {
useHashes = true
} else if arg == "--verbose" {
verbose = true
} else {
positional = append(positional, arg)
}
}
args = positional
switch len(args) {
case 0:
pathA, err = os.Getwd()
if err != nil {
return "", 0, fmt.Errorf("cannot get working directory: %w", err)
}
case 1:
pathA, err = filepath.Abs(args[0])
if err != nil {
return "", 0, fmt.Errorf("cannot resolve path %q: %w", args[0], err)
}
case 2:
pathA, err = filepath.Abs(args[0])
if err != nil {
return "", 0, fmt.Errorf("cannot resolve path %q: %w", args[0], err)
}
suffix, err = strconv.Atoi(args[1])
if err != nil {
return "", 0, fmt.Errorf("second argument must be a number, got %q", args[1])
}
if suffix < 1 {
return "", 0, fmt.Errorf("suffix must be a positive number, got %d", suffix)
}
default:
return "", 0, fmt.Errorf("usage: differ [path] [number]")
}
if _, statErr := os.Stat(pathA); os.IsNotExist(statErr) {
return "", 0, fmt.Errorf("path does not exist: %s", pathA)
}
return pathA, suffix, 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
}