if err != nil {
return fmt.Errorf("computing diff: %s", err)
}
- fmt.Printf("diff -u %s %s\n", filepath.ToSlash(filename+".orig"), filepath.ToSlash(filename))
+ fmt.Fprintf(out, "diff -u %s %s\n", filepath.ToSlash(filename+".orig"), filepath.ToSlash(filename))
out.Write(data)
}
}
}
func visitFile(path string, f fs.DirEntry, err error) error {
- if err == nil && isGoFile(f) {
- err = processFile(path, nil, os.Stdout, false)
+ if err != nil || !isGoFile(f) {
+ return err
}
- // Don't complain if a file was deleted in the meantime (i.e.
- // the directory changed concurrently while running gofmt).
- if err != nil && !os.IsNotExist(err) {
+ if err := processFile(path, nil, os.Stdout, false); err != nil {
report(err)
}
return nil
}
-func walkDir(path string) {
- filepath.WalkDir(path, visitFile)
-}
-
func main() {
// call gofmtMain in a separate function
// so that it can use defer and have them
initParserMode()
initRewrite()
- if flag.NArg() == 0 {
+ args := flag.Args()
+ if len(args) == 0 {
if *write {
fmt.Fprintln(os.Stderr, "error: cannot use -w with standard input")
exitCode = 2
return
}
- for i := 0; i < flag.NArg(); i++ {
- path := flag.Arg(i)
- switch dir, err := os.Stat(path); {
- case err != nil:
+ for _, arg := range args {
+ if err := filepath.WalkDir(arg, visitFile); err != nil {
report(err)
- case dir.IsDir():
- walkDir(path)
- default:
- if err := processFile(path, nil, os.Stdout, false); err != nil {
- report(err)
- }
}
}
}