2013-12-17 19:21:03 -07:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/scanner"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/imports"
|
2013-12-17 19:21:03 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// main operation modes
|
|
|
|
list = flag.Bool("l", false, "list files whose formatting differs from goimport's")
|
|
|
|
write = flag.Bool("w", false, "write result to (source) file instead of stdout")
|
|
|
|
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
|
2015-12-10 23:32:07 -07:00
|
|
|
srcdir = flag.String("srcdir", "", "choose imports as if source code is from `dir`")
|
2013-12-17 19:21:03 -07:00
|
|
|
|
2014-02-12 09:35:02 -07:00
|
|
|
options = &imports.Options{
|
|
|
|
TabWidth: 8,
|
|
|
|
TabIndent: true,
|
|
|
|
Comments: true,
|
2014-04-28 15:15:33 -06:00
|
|
|
Fragment: true,
|
2014-02-12 09:35:02 -07:00
|
|
|
}
|
2013-12-17 19:21:03 -07:00
|
|
|
exitCode = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&options.AllErrors, "e", false, "report all errors (not just the first 10 on different lines)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func report(err error) {
|
|
|
|
scanner.PrintError(os.Stderr, err)
|
|
|
|
exitCode = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
func usage() {
|
|
|
|
fmt.Fprintf(os.Stderr, "usage: goimports [flags] [path ...]\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isGoFile(f os.FileInfo) bool {
|
|
|
|
// ignore non-Go files
|
|
|
|
name := f.Name()
|
|
|
|
return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
|
|
|
|
}
|
|
|
|
|
|
|
|
func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error {
|
|
|
|
opt := options
|
|
|
|
if stdin {
|
|
|
|
nopt := *options
|
|
|
|
nopt.Fragment = true
|
|
|
|
opt = &nopt
|
|
|
|
}
|
|
|
|
|
|
|
|
if in == nil {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
in = f
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := ioutil.ReadAll(in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:32:07 -07:00
|
|
|
target := filename
|
|
|
|
if *srcdir != "" {
|
|
|
|
// Pretend that file is from *srcdir in order to decide
|
|
|
|
// visible imports correctly.
|
|
|
|
target = filepath.Join(*srcdir, filepath.Base(filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := imports.Process(target, src, opt)
|
2013-12-17 19:21:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(src, res) {
|
|
|
|
// formatting has changed
|
|
|
|
if *list {
|
|
|
|
fmt.Fprintln(out, filename)
|
|
|
|
}
|
|
|
|
if *write {
|
|
|
|
err = ioutil.WriteFile(filename, res, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *doDiff {
|
|
|
|
data, err := diff(src, res)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("computing diff: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("diff %s gofmt/%s\n", filename, filename)
|
|
|
|
out.Write(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !*list && !*write && !*doDiff {
|
|
|
|
_, err = out.Write(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func visitFile(path string, f os.FileInfo, err error) error {
|
|
|
|
if err == nil && isGoFile(f) {
|
|
|
|
err = processFile(path, nil, os.Stdout, false)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
report(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func walkDir(path string) {
|
|
|
|
filepath.Walk(path, visitFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
|
|
|
// call gofmtMain in a separate function
|
|
|
|
// so that it can use defer and have them
|
|
|
|
// run before the exit.
|
|
|
|
gofmtMain()
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:09:40 -06:00
|
|
|
// parseFlags parses command line flags and returns the paths to process.
|
|
|
|
// It's a var so that custom implementations can replace it in other files.
|
|
|
|
var parseFlags = func() []string {
|
|
|
|
flag.Parse()
|
|
|
|
return flag.Args()
|
|
|
|
}
|
|
|
|
|
2013-12-17 19:21:03 -07:00
|
|
|
func gofmtMain() {
|
|
|
|
flag.Usage = usage
|
2015-03-27 12:09:40 -06:00
|
|
|
paths := parseFlags()
|
2013-12-17 19:21:03 -07:00
|
|
|
|
|
|
|
if options.TabWidth < 0 {
|
|
|
|
fmt.Fprintf(os.Stderr, "negative tabwidth %d\n", options.TabWidth)
|
|
|
|
exitCode = 2
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:09:40 -06:00
|
|
|
if len(paths) == 0 {
|
2013-12-17 19:21:03 -07:00
|
|
|
if err := processFile("<standard input>", os.Stdin, os.Stdout, true); err != nil {
|
|
|
|
report(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:09:40 -06:00
|
|
|
for _, path := range paths {
|
2013-12-17 19:21:03 -07:00
|
|
|
switch dir, err := os.Stat(path); {
|
|
|
|
case err != nil:
|
|
|
|
report(err)
|
|
|
|
case dir.IsDir():
|
|
|
|
walkDir(path)
|
|
|
|
default:
|
|
|
|
if err := processFile(path, nil, os.Stdout, false); err != nil {
|
|
|
|
report(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func diff(b1, b2 []byte) (data []byte, err error) {
|
|
|
|
f1, err := ioutil.TempFile("", "gofmt")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(f1.Name())
|
|
|
|
defer f1.Close()
|
|
|
|
|
|
|
|
f2, err := ioutil.TempFile("", "gofmt")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(f2.Name())
|
|
|
|
defer f2.Close()
|
|
|
|
|
|
|
|
f1.Write(b1)
|
|
|
|
f2.Write(b2)
|
|
|
|
|
|
|
|
data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
|
|
|
|
if len(data) > 0 {
|
|
|
|
// diff exits with a non-zero status when the files don't match.
|
|
|
|
// Ignore that failure as long as we get output.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|