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

Add a -d flag that allows parsing a directory instead of an import path #9

Merged
merged 1 commit into from
Mar 6, 2013
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
40 changes: 31 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,30 @@ func main() {
ignorePkg.Set("fmt")
flag.Var(ignorePkg, "ignorepkg", "comma-separated list of package paths to ignore")
flag.Parse()

pkgName := flag.Arg(0)
if pkgName == "" {
flag.Usage()
Fatal("you must specify a package")
}

files, err := getFiles(pkgName)
if err != nil {
Fatal("could not import %s: %s", pkgName, err)
var err1, err2 error

// First try to treat pkgName as import path...
pkg, err1 := importPathToPkg(pkgName)
if err1 != nil {
// ... then attempt as file path
pkg, err2 = directoryToPkg(pkgName)
}

if err2 != nil {
// Print both errors so the user can see in what ways the
// package lookup failed.
Fatal("could not import %s: %s\n%s", pkgName, err1, err2)
}

files := getFiles(pkg)

if err := checkFiles(files, ignore.re, ignorePkg.items); err != nil {
if err == ErrCheckErrors {
os.Exit(1)
Expand All @@ -114,20 +127,29 @@ func main() {
}
}

// getFiles returns all the Go files found at a package path
func getFiles(path string) ([]string, error) {
// directoryToPkg returns a Package found in a directory
func directoryToPkg(directory string) (*build.Package, error) {
ctx := build.Default
ctx.CgoEnabled = false
pkg, err := ctx.ImportDir(directory, 0)
return pkg, err
}

// importPathToPkg returns a Package found at a package path
func importPathToPkg(path string) (*build.Package, error) {
ctx := build.Default
ctx.CgoEnabled = false
pkg, err := ctx.Import(path, ".", 0)
if err != nil {
return nil, err
}
return pkg, err
}

// getFiles returns all the Go files found in a package
func getFiles(pkg *build.Package) []string {
files := make([]string, len(pkg.GoFiles))
for i, fileName := range pkg.GoFiles {
files[i] = filepath.Join(pkg.Dir, fileName)
}
return files, nil
return files
}

type file struct {
Expand Down