Skip to content

Commit

Permalink
Merge pull request #21 from nezorflame/module-v2
Browse files Browse the repository at this point in the history
Add new `v2` module and make `cmd/gopkgs` use it
  • Loading branch information
uudashr authored Oct 22, 2019
2 parents ad1ae73 + c2263ca commit 9ad6b82
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ 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_1.12.x := v1.21.0
GOLANGCI_LINT_1.13.x := v1.21.0
GOLANGCI_LINT_tip := v1.21.0
GOLANGCI_LINT := ${GOLANGCI_LINT_${TRAVIS_GO_VERSION}}

# Linter
.PHONY: lint-prepare
lint-prepare:
@if [ $(GOLANGCI_LINT) == "" ]; then \
echo "Unknown Go version - using the latest linter"; \
GOLANGCI_LINT := v1.19.1; \
GOLANGCI_LINT := $(GOLANGCI_LINT_tip); \
fi
@echo "Installing golangci-lint $(GOLANGCI_LINT)"
@[ -d $(GOPATH)/bin ] || mkdir -p $(GOPATH)/bin
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ or, using **Go 1.12+**:

## Usage

### Tool

```plaintext
$ gopkgs -help
Usage of gopkgs:
Expand All @@ -40,6 +42,14 @@ Use -format to custom the output using template syntax. The struct being passed
Use -workDir={path} to speed up the package search. This will ignore any vendor package outside the package root.
```

### Library

This project adheres to the Go modules [release strategy](https://github.com/golang/go/wiki/Modules#releasing-modules-v2-or-higher) by using the `Major subdirectory` approach.

Starting from version `v2.0.3`, you're able to use either `github.com/uudashr/gopkgs` or `github.com/uudashr/gopkgs/v2` versions independently.

The tool `cmd/gopkgs` uses `v2` package internally.

### Example

Get package name along with the import path.
Expand Down
10 changes: 10 additions & 0 deletions cmd/gopkgs/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/uudashr/gopkgs/cmd/gopkgs

go 1.13

require github.com/uudashr/gopkgs/v2 v2.0.3

replace (
github.com/uudashr/gopkgs => ../../
github.com/uudashr/gopkgs/v2 => ../../v2
)
4 changes: 4 additions & 0 deletions cmd/gopkgs/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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=
2 changes: 1 addition & 1 deletion cmd/gopkgs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"runtime/trace"
"text/tabwriter"

"github.com/uudashr/gopkgs"
gopkgs "github.com/uudashr/gopkgs/v2"
)

var usageInfo = `
Expand Down
3 changes: 0 additions & 3 deletions gopkgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,6 @@ func listMods(workDir string) ([]mod, error) {
for s.Scan() {
line := s.Text()
ls := strings.Split(line, ";")
if err != nil {
return nil, err
}
mods = append(mods, mod{path: ls[0], dir: ls[1]})
}
return mods, nil
Expand Down
7 changes: 5 additions & 2 deletions gopkgs_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package gopkgs_test

import "testing"
import "github.com/uudashr/gopkgs"
import (
"testing"

"github.com/uudashr/gopkgs"
)

func TestList(t *testing.T) {
if testing.Short() {
Expand Down
7 changes: 7 additions & 0 deletions v2/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/uudashr/gopkgs/v2

go 1.13

require github.com/uudashr/gopkgs v0.0.0

replace github.com/uudashr/gopkgs => ../
4 changes: 4 additions & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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=
25 changes: 25 additions & 0 deletions v2/gopkgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gopkgs

import (
v1 "github.com/uudashr/gopkgs"
)

// Pkg hold the information of the package.
type Pkg v1.Pkg

// Options for retrieve packages.
type Options v1.Options

// List packages on workDir.
func List(opts Options) (map[string]Pkg, error) {
result, err := v1.List(v1.Options(opts))
if err != nil {
return nil, err
}

pkgs := make(map[string]Pkg, len(result))
for key, pkg := range result {
pkgs[key] = Pkg(pkg)
}
return pkgs, nil
}
30 changes: 30 additions & 0 deletions v2/gopkgs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gopkgs_test

import (
"testing"

gopkgs "github.com/uudashr/gopkgs/v2"
)

func TestList(t *testing.T) {
if testing.Short() {
t.Skip("Skip non-short mode")
}

pkgs, err := gopkgs.List(gopkgs.Options{})
if err != nil {
t.Fatal("fail getting packages:", err)
}

if got := len(pkgs); got == 0 {
t.Error("got:", got, "want: greater than 0")
}
}

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

0 comments on commit 9ad6b82

Please sign in to comment.