-
Notifications
You must be signed in to change notification settings - Fork 0
/
treecase.go
97 lines (84 loc) · 1.57 KB
/
treecase.go
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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
func main() {
var (
output = flag.String("o", "", "output format")
)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] <file>\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
var root string
if len(flag.Args()) > 1 {
root = flag.Args()[1]
} else {
root = "."
}
res, err := check(root)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch *output {
case "json":
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
err := enc.Encode(res)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
default:
if len(res) > 0 {
fmt.Fprintf(os.Stderr, "Conflicts found: count %d\n", len(res))
}
for _, v := range res {
for _, p := range v.Paths {
fmt.Printf("%s\n", p)
}
fmt.Println()
}
}
}
type result struct {
Paths []string `json:"paths"`
}
func check(root string) ([]result, error) {
d, err := os.ReadDir(root)
if err != nil {
return nil, err
}
var res []result
seen := map[string]*result{} // normalized path -> real paths
for _, f := range d {
real := filepath.Join(root, f.Name())
norm := strings.ToLower(real)
if f.IsDir() {
r, err := check(real)
if err != nil {
return nil, err
}
res = append(res, r...)
}
v, exists := seen[norm]
if exists {
v.Paths = append(v.Paths, real)
continue
}
seen[norm] = &result{Paths: []string{real}}
}
for _, v := range seen {
if len(v.Paths) > 1 {
res = append(res, *v)
}
}
return res, nil
}