Skip to content

Commit

Permalink
Add no-vendor options
Browse files Browse the repository at this point in the history
  • Loading branch information
uudashr committed May 5, 2018
1 parent 4b1b4f5 commit b77c6c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
12 changes: 8 additions & 4 deletions cmd/gopkgs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func init() {

func main() {
var (
flagFormat = flag.String("format", "{{.ImportPath}}", "custom output format")
flagWorkDir = flag.String("workDir", "", "importable packages only for workDir")
flagHelp = flag.Bool("help", false, "show this message")
flagFormat = flag.String("format", "{{.ImportPath}}", "custom output format")
flagWorkDir = flag.String("workDir", "", "importable packages only for workDir")
flagNoVendor = flag.Bool("no-vendor", false, "exclude vendor dependencies except under workDir (if specified)")
flagHelp = flag.Bool("help", false, "show this message")
)

flag.Parse()
Expand All @@ -53,7 +54,10 @@ func main() {
os.Exit(1)
}

pkgs, err := gopkgs.Packages(*flagWorkDir)
pkgs, err := gopkgs.Packages(gopkgs.Options{
WorkDir: *flagWorkDir,
NoVendor: *flagNoVendor,
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
30 changes: 24 additions & 6 deletions gopkgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ type Pkg struct {
Name string // package name
}

// Options for retrieve packages.
type Options struct {
WorkDir string // Will return importable package under WorkDir. Any vendor dependencies outside the WorkDir will be ignored.
NoVendor bool // Will not retrieve vendor dependencies, except inside WorkDir (if specified)
}

// Packages available to import.
//
// workDir availibility will return importable packages only based
// on workDir (vendor packages outside the workDir will be ignored).
func Packages(workDir string) (map[string]*Pkg, error) {
func Packages(opts Options) (map[string]*Pkg, error) {
fset := token.NewFileSet()

var pkgsMu sync.Mutex
pkgs := make(map[string]*Pkg)

workDir := opts.WorkDir
if workDir != "" && !filepath.IsAbs(workDir) {
wd, err := filepath.Abs(workDir)
if err != nil {
Expand Down Expand Up @@ -57,8 +61,22 @@ func Packages(workDir string) (map[string]*Pkg, error) {
return walk.SkipDir
}

if name == "vendor" && workDir != "" && workDir != pathDir {
return walk.SkipDir
// if name == "vendor" && workDir != "" && workDir != pathDir {
// return walk.SkipDir
// }

if name == "vendor" {
if workDir != "" {
if workDir != pathDir {
return walk.SkipDir
}

return nil
}

if opts.NoVendor {
return walk.SkipDir
}
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions gopkgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func TestPackages(t *testing.T) {
t.Skip("Skip non-short mode")
}

pkgs, err := gopkgs.Packages("")
pkgs, err := gopkgs.Packages(gopkgs.Options{})
if err != nil {
t.Fatal("fail getting packages:", err)
}
Expand All @@ -20,7 +20,7 @@ func TestPackages(t *testing.T) {

func BenchmarkPackages(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := gopkgs.Packages(""); err != nil {
if _, err := gopkgs.Packages(gopkgs.Options{}); err != nil {
b.Fatal("err:", err)
}
}
Expand Down

0 comments on commit b77c6c9

Please sign in to comment.