summaryrefslogtreecommitdiff
path: root/src/cmd/gofix/main.go
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2012-01-30 15:38:19 +0100
committerOndřej Surý <ondrej@sury.org>2012-01-30 15:38:19 +0100
commit4cecda6c347bd6902b960c6a35a967add7070b0d (patch)
treea462e224ff41ec9f3eb1a0b6e815806f9e8804ad /src/cmd/gofix/main.go
parent6c7ca6e4d4e26e4c8cbe0d183966011b3b088a0a (diff)
downloadgolang-4cecda6c347bd6902b960c6a35a967add7070b0d.tar.gz
Imported Upstream version 2012.01.27upstream-weekly/2012.01.27
Diffstat (limited to 'src/cmd/gofix/main.go')
-rw-r--r--src/cmd/gofix/main.go75
1 files changed, 51 insertions, 24 deletions
diff --git a/src/cmd/gofix/main.go b/src/cmd/gofix/main.go
index e0709fc8b..ca7e1a0f3 100644
--- a/src/cmd/gofix/main.go
+++ b/src/cmd/gofix/main.go
@@ -6,15 +6,16 @@ package main
import (
"bytes"
- "exec"
"flag"
"fmt"
+ "go/ast"
"go/parser"
"go/printer"
"go/scanner"
"go/token"
"io/ioutil"
"os"
+ "os/exec"
"path/filepath"
"sort"
"strings"
@@ -28,14 +29,21 @@ var (
var allowedRewrites = flag.String("r", "",
"restrict the rewrites to this comma-separated list")
-var allowed map[string]bool
+var forceRewrites = flag.String("force", "",
+ "force these fixes to run even if the code looks updated")
+
+var allowed, force map[string]bool
var doDiff = flag.Bool("diff", false, "display diffs instead of rewriting files")
+// enable for debugging gofix failures
+const debug = false // display incorrectly reformatted source and exit
+
func usage() {
- fmt.Fprintf(os.Stderr, "usage: gofix [-diff] [-r fixname,...] [path ...]\n")
+ fmt.Fprintf(os.Stderr, "usage: gofix [-diff] [-r fixname,...] [-force fixname,...] [path ...]\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nAvailable rewrites are:\n")
+ sort.Sort(byName(fixes))
for _, f := range fixes {
fmt.Fprintf(os.Stderr, "\n%s\n", f.name)
desc := strings.TrimSpace(f.desc)
@@ -46,11 +54,11 @@ func usage() {
}
func main() {
- sort.Sort(fixes)
-
flag.Usage = usage
flag.Parse()
+ sort.Sort(byDate(fixes))
+
if *allowedRewrites != "" {
allowed = make(map[string]bool)
for _, f := range strings.Split(*allowedRewrites, ",") {
@@ -58,6 +66,13 @@ func main() {
}
}
+ if *forceRewrites != "" {
+ force = make(map[string]bool)
+ for _, f := range strings.Split(*forceRewrites, ",") {
+ force[f] = true
+ }
+ }
+
if flag.NArg() == 0 {
if err := processFile("standard input", true); err != nil {
report(err)
@@ -70,12 +85,12 @@ func main() {
switch dir, err := os.Stat(path); {
case err != nil:
report(err)
- case dir.IsRegular():
+ case dir.IsDir():
+ walkDir(path)
+ default:
if err := processFile(path, false); err != nil {
report(err)
}
- case dir.IsDirectory():
- walkDir(path)
}
}
@@ -93,11 +108,21 @@ var printConfig = &printer.Config{
tabWidth,
}
-func processFile(filename string, useStdin bool) os.Error {
+func gofmtFile(f *ast.File) ([]byte, error) {
+ var buf bytes.Buffer
+
+ ast.SortImports(fset, f)
+ err := printConfig.Fprint(&buf, fset, f)
+ if err != nil {
+ return nil, err
+ }
+ return buf.Bytes(), nil
+}
+
+func processFile(filename string, useStdin bool) error {
var f *os.File
- var err os.Error
+ var err error
var fixlog bytes.Buffer
- var buf bytes.Buffer
if useStdin {
f = os.Stdin
@@ -133,14 +158,17 @@ func processFile(filename string, useStdin bool) os.Error {
// AST changed.
// Print and parse, to update any missing scoping
// or position information for subsequent fixers.
- buf.Reset()
- _, err = printConfig.Fprint(&buf, fset, newFile)
+ newSrc, err := gofmtFile(newFile)
if err != nil {
return err
}
- newSrc := buf.Bytes()
newFile, err = parser.ParseFile(fset, filename, newSrc, parserMode)
if err != nil {
+ if debug {
+ fmt.Printf("%s", newSrc)
+ report(err)
+ os.Exit(exitCode)
+ }
return err
}
}
@@ -156,12 +184,10 @@ func processFile(filename string, useStdin bool) os.Error {
// output of the printer run on a standard AST generated by the parser,
// but the source we generated inside the loop above is the
// output of the printer run on a mangled AST generated by a fixer.
- buf.Reset()
- _, err = printConfig.Fprint(&buf, fset, newFile)
+ newSrc, err := gofmtFile(newFile)
if err != nil {
return err
}
- newSrc := buf.Bytes()
if *doDiff {
data, err := diff(src, newSrc)
@@ -185,14 +211,14 @@ var gofmtBuf bytes.Buffer
func gofmt(n interface{}) string {
gofmtBuf.Reset()
- _, err := printConfig.Fprint(&gofmtBuf, fset, n)
+ err := printConfig.Fprint(&gofmtBuf, fset, n)
if err != nil {
- return "<" + err.String() + ">"
+ return "<" + err.Error() + ">"
}
return gofmtBuf.String()
}
-func report(err os.Error) {
+func report(err error) {
scanner.PrintError(os.Stderr, err)
exitCode = 2
}
@@ -201,7 +227,7 @@ func walkDir(path string) {
filepath.Walk(path, visitFile)
}
-func visitFile(path string, f *os.FileInfo, err os.Error) os.Error {
+func visitFile(path string, f os.FileInfo, err error) error {
if err == nil && isGoFile(f) {
err = processFile(path, false)
}
@@ -211,12 +237,13 @@ func visitFile(path string, f *os.FileInfo, err os.Error) os.Error {
return nil
}
-func isGoFile(f *os.FileInfo) bool {
+func isGoFile(f os.FileInfo) bool {
// ignore non-Go files
- return f.IsRegular() && !strings.HasPrefix(f.Name, ".") && strings.HasSuffix(f.Name, ".go")
+ name := f.Name()
+ return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}
-func diff(b1, b2 []byte) (data []byte, err os.Error) {
+func diff(b1, b2 []byte) (data []byte, err error) {
f1, err := ioutil.TempFile("", "gofix")
if err != nil {
return nil, err