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

Fix usage of the tool in "-mod=vendor" mode for projects with indirect dependencies #18

Merged
merged 3 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
vendor
.vscode
.idea
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ go:
- 1.9.x
- 1.10.x
- 1.11.x
- 1.12.x
- 1.13.x
- tip

matrix:
Expand All @@ -17,11 +19,11 @@ os:
- osx

install:
- go mod download || go get -u
- env GO111MODULE=on go mod download || go get -u

before_script:
- if [[ "$(go version)" =~ "go version go1.8" ]]; then echo Skipping golangci-lint; else make lint-prepare; fi
- if [[ "$TRAVIS_GO_VERSION" =~ ^1\.8 ]]; then echo 'Skipping golangci-lint install'; else make lint-prepare; fi

script:
- if [[ "$(go version)" =~ "go version go1.8" ]]; then echo Skipping golangci-lint; else make lint; fi
- make test
- if [[ "$TRAVIS_GO_VERSION" =~ ^1\.8 ]]; then echo 'Skipping golangci-lint run'; else env GO111MODULE=on make lint; fi
- env GO111MODULE=on make test
23 changes: 18 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@ PACKAGE := $(shell go list)
GOOS := $(shell go env GOOS)
GOARCH = $(shell go env GOARCH)
OBJ_DIR := $(GOPATH)/pkg/$(GOOS)_$(GOARCH)/$(PACKAGE)
GO_VERSION_MINOR := $(shell go version | sed -E 's/.*go([0-9]\.[0-9]+).*/\1/g')
TRAVIS_GO_VERSION ?= $(GO_VERSION_MINOR).x

GOLANGCI_LINT_1.9.x := v1.10.2
GOLANGCI_LINT_1.10.x := v1.15.0
GOLANGCI_LINT_1.11.x := v1.17.1
GOLANGCI_LINT_1.12.x := v1.19.1
GOLANGCI_LINT_1.13.x := v1.19.1
GOLANGCI_LINT_tip := v1.19.1
GOLANGCI_LINT := ${GOLANGCI_LINT_${TRAVIS_GO_VERSION}}

# Linter
.PHONY: lint-prepare
lint-prepare:
@echo "Installing golangci-lint"
@go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
@if [ $(GOLANGCI_LINT) == "" ]; then \
echo "Unknown Go version - using the latest linter"; \
GOLANGCI_LINT := v1.19.1; \
fi
@echo "Installing golangci-lint $(GOLANGCI_LINT)"
@[ -d $(GOPATH)/bin ] || mkdir -p $(GOPATH)/bin
@curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT)

.PHONY: lint
lint:
Expand Down Expand Up @@ -40,6 +55,4 @@ install:
uninstall:
@echo "Removing binaries and libraries"
@go clean -i ./...
@if [ -d $(OBJ_DIR) ]; then \
rm -rf $(OBJ_DIR); \
fi
@if [ -d $(OBJ_DIR) ]; then rm -rf $(OBJ_DIR); fi
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
[![Build Status](https://travis-ci.org/uudashr/gopkgs.svg?branch=master)](https://travis-ci.org/uudashr/gopkgs)
[![GoDoc](https://godoc.org/github.com/uudashr/gopkgs?status.svg)](https://godoc.org/github.com/uudashr/gopkgs)
[![Build Status](https://travis-ci.org/uudashr/gopkgs.svg?branch=master)](https://travis-ci.org/uudashr/gopkgs)[![GoDoc](https://godoc.org/github.com/uudashr/gopkgs?status.svg)](https://godoc.org/github.com/uudashr/gopkgs)

# gopkgs

Gopkgs is tools that provide list of available Go packages that can be imported.
`gopkgs` is a tool that provides list of available Go packages that can be imported.

This is an alternative to `go list all`, just faster.

## Installation

`$ go get -u github.com/uudashr/gopkgs/cmd/gopkgs`

or, using **Go 1.12+**:

`$ go get github.com/uudashr/gopkgs/cmd/gopkgs@latest`

## Usage
```

```plaintext
$ gopkgs -help
-format string
custom output format (default "{{.ImportPath}}")
Expand All @@ -34,8 +39,10 @@ Use -workDir={path} to speed up the package search. This will ignore any vendor
```

### Example

Get package name along with the import path.
```

```plaintext
$ gopkgs -format "{{.Name}};{{.ImportPath}}"
testing;github.com/mattes/migrate/source/testing
http;github.com/stretchr/testify/http
Expand All @@ -58,4 +65,4 @@ Use `-workDir={path}` flag, it will speed up the package search by ignoring the

## Related Project

This is based on https://github.com/haya14busa/gopkgs but taking slightly different path by simplifying its implementation.
This is based on <https://github.com/haya14busa/gopkgs> but takes slightly different path by simplifying its implementation.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/uudashr/gopkgs

go 1.12

require (
github.com/karrick/godirwalk v1.7.1
github.com/pkg/errors v0.8.0
github.com/karrick/godirwalk v1.12.0
github.com/pkg/errors v0.8.1
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/karrick/godirwalk v1.7.1 h1:r3iVkkQFDuU9wELXmnGx0oRcizvC744QszlA4OxqwFE=
github.com/karrick/godirwalk v1.7.1/go.mod h1:svlOlzFUryGta1ycTmmd6C6ytFMtCkPvQ/4fj4EXtU4=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/karrick/godirwalk v1.12.0 h1:nkS4xxsjiZMvVlazd0mFyiwD4BR9f3m6LXGhM2TUx3Y=
github.com/karrick/godirwalk v1.12.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
16 changes: 11 additions & 5 deletions gopkgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
pkgerrors "github.com/pkg/errors"
)

const (
mainPkg = "main"
testDataDir = "testdata"
nodeModulesDir = "node_modules"
)

// Pkg hold the information of the package.
type Pkg struct {
Dir string // directory containing package sources
Expand Down Expand Up @@ -126,7 +132,7 @@ func listFiles(srcDir, workDir string, noVendor bool) (<-chan goFile, <-chan err
// see: https://golang.org/cmd/go/#hdr-Description_of_package_lists

if de.IsDir() {
if name[0] == '.' || name[0] == '_' || name == "testdata" || name == "node_modules" {
if name[0] == '.' || name[0] == '_' || name == testDataDir || name == nodeModulesDir {
return filepath.SkipDir
}

Expand Down Expand Up @@ -205,7 +211,7 @@ func listModFiles(modDir string) (<-chan goFile, <-chan error) {
// see: https://golang.org/cmd/go/#hdr-Description_of_package_lists

if de.IsDir() {
if name[0] == '.' || name[0] == '_' || name == "testdata" || name == "node_modules" {
if name[0] == '.' || name[0] == '_' || name == testDataDir || name == nodeModulesDir {
return filepath.SkipDir
}

Expand Down Expand Up @@ -255,7 +261,7 @@ func collectPkgs(srcDir, workDir string, noVendor bool, out map[string]Pkg) erro
continue
}

if pkgName == "main" {
if pkgName == mainPkg {
// skip main package
continue
}
Expand Down Expand Up @@ -289,7 +295,7 @@ func collectModPkgs(m mod, out map[string]Pkg) error {
continue
}

if pkgName == "main" {
if pkgName == mainPkg {
// skip main package
continue
}
Expand Down Expand Up @@ -363,7 +369,7 @@ type mod struct {
}

func listMods(workDir string) ([]mod, error) {
cmdArgs := []string{"list", "-m", "-f={{.Path}};{{.Dir}}", "all"}
cmdArgs := []string{"list", "-m", `-mod=""`, "-f={{.Path}};{{.Dir}}", "all"}
cmd := exec.Command("go", cmdArgs...)
cmd.Dir = workDir
out, err := cmd.Output()
Expand Down