-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (79 loc) · 1.92 KB
/
main.go
File metadata and controls
92 lines (79 loc) · 1.92 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
package main
import (
"flag"
"fmt"
"os"
)
type options struct {
write bool
diff bool
list bool
tabLength int
lineLength int
}
func main() {
flag.Usage = func() {
fmt.Println("usage: gocomments [flags] [paths...]")
fmt.Println(" gocomments reformat's comments in Go files. By default, it only prints")
fmt.Println(" the reformatted contents of each file.")
fmt.Println()
fmt.Println("flags:")
flag.PrintDefaults()
}
lineLength := flag.Int("llen", 78, "maximum line length for comments")
tabLength := flag.Int("tlen", 4, "number of spaces that tabs count for")
write := flag.Bool("w", false, "write changes back to source files instead of stdout")
diff := flag.Bool("d", false, "display unified diffs of changes rather than whole files")
list := flag.Bool("l", false, "list files whose formatting would change")
flag.Parse()
paths := flag.Args()
// Use defaults for invalid values
tlen := *tabLength
if tlen <= 0 {
tlen = 4
}
llen := *lineLength
if llen <= 0 {
llen = 78
}
opts := options{
write: *write,
diff: *diff,
list: *list,
tabLength: tlen,
lineLength: llen,
}
// If no paths provided, check if stdin has data
if len(paths) == 0 {
stat, err := os.Stdin.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "error checking stdin: %v\n", err)
os.Exit(1)
}
// If stdin is a pipe or file (not a terminal), read from it
if (stat.Mode() & os.ModeCharDevice) == 0 {
if err := formatStdin(opts); err != nil {
fmt.Fprintf(os.Stderr, "error processing stdin: %v\n", err)
os.Exit(1)
}
return
}
// No paths and no stdin data, show usage
flag.Usage()
os.Exit(1)
}
hasChanges := false
for _, path := range paths {
changed, err := walkPath(path, opts)
if err != nil {
fmt.Fprintf(os.Stderr, "error walking %s: %v\n", path, err)
os.Exit(1)
}
if changed {
hasChanges = true
}
}
if hasChanges {
os.Exit(1)
}
}