diff options
author | Daniel Martí <mvdan@mvdan.cc> | 2021-01-15 23:10:48 +0000 |
---|---|---|
committer | Daniel Martí <mvdan@mvdan.cc> | 2021-04-28 17:39:34 +0000 |
commit | a54762586fd7b91cb1a8b8bb404db87e6b976d59 (patch) | |
tree | 96a9642e4597da5224628c3a305b70a760d63f48 /src/cmd/gofmt | |
parent | 22a56b629d86508a36d1dd8b90124ccd9dc90e06 (diff) | |
download | go-a54762586fd7b91cb1a8b8bb404db87e6b976d59.tar.gz go-a54762586fd7b91cb1a8b8bb404db87e6b976d59.zip |
cmd/gofmt: simplify arg handling
First, we can use flag.Args instead of flag.NArg and flag.Arg.
Second, just call filepath.WalkDir directly on each argument. We don't
need to check if each argument is a directory or not, since the function
will still work on regular files as expected.
To continue giving an error in the "gofmt does-not-exist.go" case, we
now need to return and handle errors from filepath.WalkDir, too.
Arguably, that should have always been the case.
While at it, I noticed that the printinf of the "diff" command did not
obey the "out" parameter. Fix that.
Finally, remove the code to ignore IsNotExist errors. It was added in CL
19301, though it didn't include tests and its reasoning is dubious.
Using gofmt on a directory treewhile another program is concurrently
editing or removing files is inherently racy. Hiding errors can hide
valid problems from the user, and such racy usages aren't supported.
Change-Id: I2e74cc04c53eeefb25231d804752b53562b97371
Reviewed-on: https://go-review.googlesource.com/c/go/+/284138
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Trust: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/cmd/gofmt')
-rw-r--r-- | src/cmd/gofmt/gofmt.go | 29 |
1 files changed, 8 insertions, 21 deletions
diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go index 2793c2c2a4..fef86c941f 100644 --- a/src/cmd/gofmt/gofmt.go +++ b/src/cmd/gofmt/gofmt.go @@ -151,7 +151,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error 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) } } @@ -164,21 +164,15 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error } 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 @@ -206,7 +200,8 @@ func gofmtMain() { 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 @@ -218,17 +213,9 @@ func gofmtMain() { 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) - } } } } |