-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
189 lines (163 loc) · 3.85 KB
/
main.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// `tryhard` is a simple tool to list and rewrite `try` candidate statements.
// See README.md for details.
package main
import (
"bytes"
"flag"
"fmt"
"go/format"
"go/parser"
"go/scanner"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
)
var (
// main operation modes
list = flag.Bool("l", false, "list positions of potential `try` candidate statements")
rewrite = flag.Bool("r", false, "rewrite potential `try` candidate statements to use `try`")
// customization
varname = flag.String("err", "", `name of error variable; using "" permits any name`)
filter = flag.String("ignore", "vendor", "ignore files with paths matching this (regexp) pattern")
)
var (
fset = token.NewFileSet()
exitCode int
fileCount int
filterRx *regexp.Regexp
)
func report(err error) {
scanner.PrintError(os.Stderr, err)
exitCode = 2
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: tryhard [flags] [path ...]\n")
flag.PrintDefaults()
}
func main() {
flag.Usage = usage
flag.Parse()
if *filter != "" {
rx, err := regexp.Compile(*filter)
if err != nil {
report(err)
os.Exit(exitCode)
}
filterRx = rx
}
for i := 0; i < flag.NArg(); i++ {
path := flag.Arg(i)
switch dir, err := os.Stat(path); {
case err != nil:
report(err)
case dir.IsDir():
filepath.Walk(path, visitFile)
default:
if err := processFile(path); err != nil {
report(err)
}
}
}
if fileCount > 0 {
if *list {
reportPositions()
}
reportCounts()
}
os.Exit(exitCode)
}
func processFile(filename string) error {
fileCount++
var perm os.FileMode = 0644
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
perm = fi.Mode().Perm()
src, err := ioutil.ReadAll(f)
if err != nil {
return err
}
file, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
if err != nil {
return err
}
modified := false
tryFile(file, &modified)
if !modified || !*rewrite {
return nil
}
var buf bytes.Buffer
if err := format.Node(&buf, fset, file); err != nil {
return err
}
res := buf.Bytes()
// make a temporary backup before overwriting original
bakname, err := backupFile(filename+".", src, perm)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, res, perm)
if err != nil {
os.Rename(bakname, filename)
return err
}
return os.Remove(bakname)
}
func visitFile(path string, f os.FileInfo, err error) error {
if err == nil && !excluded(path) && isGoFile(f) {
err = processFile(path)
}
// Don't complain if a file was deleted in the meantime (i.e.
// the directory changed concurrently while running tryhard).
if err != nil && !os.IsNotExist(err) {
report(err)
}
return nil
}
func excluded(path string) bool {
return filterRx != nil && filterRx.MatchString(path)
}
func isGoFile(f os.FileInfo) bool {
// ignore non-Go files
name := f.Name()
return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}
const chmodSupported = runtime.GOOS != "windows"
// backupFile writes data to a new file named filename<number> with permissions perm,
// with <number randomly chosen such that the file name is unique. backupFile returns
// the chosen file name.
func backupFile(filename string, data []byte, perm os.FileMode) (string, error) {
// create backup file
f, err := ioutil.TempFile(filepath.Dir(filename), filepath.Base(filename))
if err != nil {
return "", err
}
bakname := f.Name()
if chmodSupported {
err = f.Chmod(perm)
if err != nil {
f.Close()
os.Remove(bakname)
return bakname, err
}
}
// write data to backup file
_, err = f.Write(data)
if err1 := f.Close(); err == nil {
err = err1
}
return bakname, err
}