LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "flag" | 9 "flag" |
10 "fmt" | 10 "fmt" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 var printConfig = &printer.Config{ | 103 var printConfig = &printer.Config{ |
104 printerMode, | 104 printerMode, |
105 tabWidth, | 105 tabWidth, |
106 } | 106 } |
107 | 107 |
108 func gofmtFile(f *ast.File) ([]byte, error) { | 108 func gofmtFile(f *ast.File) ([]byte, error) { |
109 var buf bytes.Buffer | 109 var buf bytes.Buffer |
110 | 110 |
111 ast.SortImports(fset, f) | 111 ast.SortImports(fset, f) |
112 » _, err := printConfig.Fprint(&buf, fset, f) | 112 » err := printConfig.Fprint(&buf, fset, f) |
113 if err != nil { | 113 if err != nil { |
114 return nil, err | 114 return nil, err |
115 } | 115 } |
116 return buf.Bytes(), nil | 116 return buf.Bytes(), nil |
117 } | 117 } |
118 | 118 |
119 func processFile(filename string, useStdin bool) error { | 119 func processFile(filename string, useStdin bool) error { |
120 var f *os.File | 120 var f *os.File |
121 var err error | 121 var err error |
122 var fixlog bytes.Buffer | 122 var fixlog bytes.Buffer |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 return nil | 196 return nil |
197 } | 197 } |
198 | 198 |
199 return ioutil.WriteFile(f.Name(), newSrc, 0) | 199 return ioutil.WriteFile(f.Name(), newSrc, 0) |
200 } | 200 } |
201 | 201 |
202 var gofmtBuf bytes.Buffer | 202 var gofmtBuf bytes.Buffer |
203 | 203 |
204 func gofmt(n interface{}) string { | 204 func gofmt(n interface{}) string { |
205 gofmtBuf.Reset() | 205 gofmtBuf.Reset() |
206 » _, err := printConfig.Fprint(&gofmtBuf, fset, n) | 206 » err := printConfig.Fprint(&gofmtBuf, fset, n) |
207 if err != nil { | 207 if err != nil { |
208 return "<" + err.Error() + ">" | 208 return "<" + err.Error() + ">" |
209 } | 209 } |
210 return gofmtBuf.String() | 210 return gofmtBuf.String() |
211 } | 211 } |
212 | 212 |
213 func report(err error) { | 213 func report(err error) { |
214 scanner.PrintError(os.Stderr, err) | 214 scanner.PrintError(os.Stderr, err) |
215 exitCode = 2 | 215 exitCode = 2 |
216 } | 216 } |
217 | 217 |
218 func walkDir(path string) { | 218 func walkDir(path string) { |
219 filepath.Walk(path, visitFile) | 219 filepath.Walk(path, visitFile) |
220 } | 220 } |
221 | 221 |
222 func visitFile(path string, f os.FileInfo, err error) error { | 222 func visitFile(path string, f os.FileInfo, err error) error { |
223 if err == nil && isGoFile(f) { | 223 if err == nil && isGoFile(f) { |
224 err = processFile(path, false) | 224 err = processFile(path, false) |
225 } | 225 } |
226 if err != nil { | 226 if err != nil { |
227 report(err) | 227 report(err) |
228 } | 228 } |
229 return nil | 229 return nil |
230 } | 230 } |
231 | 231 |
232 func isGoFile(f os.FileInfo) bool { | 232 func isGoFile(f os.FileInfo) bool { |
233 // ignore non-Go files | 233 // ignore non-Go files |
234 name := f.Name() | 234 name := f.Name() |
235 » mode := f.Mode() | 235 » return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(
name, ".go") |
236 » return !mode.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuff
ix(name, ".go") | |
237 } | 236 } |
238 | 237 |
239 func diff(b1, b2 []byte) (data []byte, err error) { | 238 func diff(b1, b2 []byte) (data []byte, err error) { |
240 f1, err := ioutil.TempFile("", "gofix") | 239 f1, err := ioutil.TempFile("", "gofix") |
241 if err != nil { | 240 if err != nil { |
242 return nil, err | 241 return nil, err |
243 } | 242 } |
244 defer os.Remove(f1.Name()) | 243 defer os.Remove(f1.Name()) |
245 defer f1.Close() | 244 defer f1.Close() |
246 | 245 |
247 f2, err := ioutil.TempFile("", "gofix") | 246 f2, err := ioutil.TempFile("", "gofix") |
248 if err != nil { | 247 if err != nil { |
249 return nil, err | 248 return nil, err |
250 } | 249 } |
251 defer os.Remove(f2.Name()) | 250 defer os.Remove(f2.Name()) |
252 defer f2.Close() | 251 defer f2.Close() |
253 | 252 |
254 f1.Write(b1) | 253 f1.Write(b1) |
255 f2.Write(b2) | 254 f2.Write(b2) |
256 | 255 |
257 data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOut
put() | 256 data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOut
put() |
258 if len(data) > 0 { | 257 if len(data) > 0 { |
259 // diff exits with a non-zero status when the files don't match. | 258 // diff exits with a non-zero status when the files don't match. |
260 // Ignore that failure as long as we get output. | 259 // Ignore that failure as long as we get output. |
261 err = nil | 260 err = nil |
262 } | 261 } |
263 return | 262 return |
264 } | 263 } |
LEFT | RIGHT |