Skip to content

Commit

Permalink
support gomod
Browse files Browse the repository at this point in the history
  • Loading branch information
3Xpl0it3r committed Sep 30, 2022
1 parent 29d3292 commit 647a43f
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/
gopkgs
vendor/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/gopkgs.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions cmd/gopkgs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import (
"bufio"
"flag"
"fmt"
"github.com/3Xpl0it3r/gopkgs"
"os"
"sort"
"strings"
"text/template"

"github.com/haya14busa/gopkgs"
)

var (
fullpath = flag.Bool("fullpath", false, `output absolute file path to package directory. ("/usr/lib/go/src/net/http")`)
short = flag.Bool("short", true, `output vendorless import path ("net/http", "foo/bar/vendor/a/b")`)
f = flag.String("f", "", "alternate format for the output using the syntax of template package. e.g. {{.Name}};{{ImportPathShort}}")
includeName = flag.Bool("include-name", false, "fill Pkg.Name which can be used with -f flag")
noVendor = flag.Bool("no-vendor", false, "exclude vendor dependencies except under workDir ")
)

var usageInfo = `
Expand Down Expand Up @@ -122,11 +122,9 @@ func main() {

w := bufio.NewWriter(os.Stdout)
defer w.Flush()

opt := gopkgs.DefaultOption()
opt.IncludeName = *includeName
pkgs := gopkgs.Packages(opt)

if *fullpath {
sort.Sort(ByFullPath(pkgs))
// Fullpaths is already unique
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/3Xpl0it3r/gopkgs

go 1.18

require golang.org/x/tools v0.1.12 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
2 changes: 1 addition & 1 deletion gopkgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"strings"

"github.com/haya14busa/gopkgs/x/tools/imports"
"github.com/3Xpl0it3r/gopkgs/x/tools/imports"
)

// Pkg represents exported go packages.
Expand Down
4 changes: 3 additions & 1 deletion gopkgs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gopkgs

import "testing"
import (
"testing"
)

func TestPackages(t *testing.T) {
pkgs := Packages(&Option{IncludeName: false})
Expand Down
1 change: 1 addition & 0 deletions x/tools/imports/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func GoPath() map[string]*Pkg {
populateIgnoreOnce.Do(populateIgnore)
scanGoRootOnce.Do(scanGoRoot) // async
scanGoPathOnce.Do(scanGoPath)
scanGoModPathOnce.Do(scanGoMod)
<-scanGoRootDone
dirScanMu.Lock()
defer dirScanMu.Unlock()
Expand Down
12 changes: 12 additions & 0 deletions x/tools/imports/fastwalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package imports

import (
"errors"
"go/build"
"os"
"path/filepath"
"runtime"
"strings"
)

// traverseLink is a sentinel error for fastWalk, similar to filepath.SkipDir.
Expand Down Expand Up @@ -168,5 +170,15 @@ func (w *walker) walk(root string, runUserCallback bool) error {
}
}

if strings.Contains(root, build.Default.GOROOT+"/src/internal") {
return nil
}
if strings.Contains(root, build.Default.GOROOT+"/src/runtime") {
return nil
}
if strings.Contains(root, build.Default.GOROOT+"/src/cmd") {
return nil
}

return readDir(root, w.onDirEnt)
}
63 changes: 59 additions & 4 deletions x/tools/imports/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ var (
// scanGoPathOnce guards calling scanGoPath (for $GOPATH)
scanGoPathOnce sync.Once

scanGoModPathOnce sync.Once

// populateIgnoreOnce guards calling populateIgnore
populateIgnoreOnce sync.Once
ignoredDirs []os.FileInfo
Expand Down Expand Up @@ -490,14 +492,28 @@ var scanGoRootDone = make(chan struct{}) // closed when scanGoRoot is done

func scanGoRoot() {
go func() {
scanGoDirs(true)
scanGoDirs(true, nil)
close(scanGoRootDone)
}()
}

func scanGoPath() { scanGoDirs(false) }
func scanGoPath() { scanGoDirs(false, nil) }

func scanGoMod() {
curDir, err := os.Getwd()
if err != nil {
return
}
if rootDir, ok := findGoModPath(curDir); ok {
scanGoDirs(false, []string{rootDir})
}
}

func scanGoDirs(goRoot bool) {
func scanGoWorkspace() {

}

func scanGoDirs(goRoot bool, extPath []string) {
if Debug {
which := "$GOROOT"
if !goRoot {
Expand All @@ -512,8 +528,15 @@ func scanGoDirs(goRoot bool) {
}
dirScanMu.Unlock()

for _, srcDir := range build.Default.SrcDirs() {
var srcDirs []string
if len(extPath) == 0 || extPath == nil {
srcDirs = build.Default.SrcDirs()
} else {
srcDirs = extPath
}
for _, srcDir := range srcDirs {
isGoroot := srcDir == filepath.Join(build.Default.GOROOT, "src")

if isGoroot != goRoot {
continue
}
Expand Down Expand Up @@ -976,3 +999,35 @@ func fileInDir(file, dir string) bool {
// Check for boundary: either nothing (file == dir), or a slash.
return len(rest) == 0 || rest[0] == '/' || rest[0] == '\\'
}

func findGoModPath(path string) (string, bool) {
if strings.Compare(path, "/") == 0 {
return "", false
}
rd, err := ioutil.ReadDir(path)
if err != nil {
return "", false
}
for _, fi := range rd {
if strings.Compare(fi.Name(), "go.mod") == 0 {
return path, true
}
if strings.Compare(fi.Name(), ".git") == 0 {
return path, true
}
}
return findGoModPath(getParentDirectory(path))
}

func substr(s string, pos, length int) string {
runes := []rune(s)
l := pos + length
if l > len(runes) {
l = len(runes)
}
return string(runes[pos:l])
}

func getParentDirectory(dirctory string) string {
return substr(dirctory, 0, strings.LastIndex(dirctory, "/"))
}

0 comments on commit 647a43f

Please sign in to comment.