Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync srcdir flag from goimports #42

Merged
merged 1 commit into from
Dec 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion goreturns.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
list = flag.Bool("l", false, "list files whose formatting differs from goreturns'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")
srcdir = flag.String("srcdir", "", "choose imports as if source code is from `dir`. When operating on a single file, dir may instead be the complete file name.")

goimports = flag.Bool("i", true, "run goimports on the file prior to processing")

Expand Down Expand Up @@ -88,9 +89,33 @@ func processFile(pkgDir, filename string, in io.Reader, out io.Writer, stdin boo

var res = src // This holds the result of processing so far.

target := filename
if *srcdir != "" {
// Determine whether the provided -srcdirc is a directory or file
// and then use it to override the target.
//
// See https://github.com/dominikh/go-mode.el/issues/146
f, err := os.Open(*srcdir)
if err != nil {
return err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return err
}
if isGoFile(stat) {
target = *srcdir
} else {
// Pretend that file is from *srcdir in order to decide
// visible imports correctly.
target = filepath.Join(*srcdir, filepath.Base(filename))
}
}

if *goimports {
var err error
res, err = imports.Process(filename, res, &imports.Options{
res, err = imports.Process(target, res, &imports.Options{
Fragment: opt.Fragment,
AllErrors: opt.AllErrors,
Comments: true,
Expand Down