Skip to content

Commit

Permalink
improvement(processor): improve detection if package is in a module
Browse files Browse the repository at this point in the history
Improve code layout for reading and add stricter detection if a package is in a module by checking each path part.

Issue: GH-30

Signed-off-by: Ryan Currah <ryan@currah.ca>
  • Loading branch information
ryancurrah committed Jan 23, 2023
1 parent 349d1ba commit 5295705
Show file tree
Hide file tree
Showing 28 changed files with 838 additions and 609 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: 1.17

- uses: actions/checkout@v3
go-version: stable

- name: Build
run: make build
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: 1.17

- uses: actions/checkout@v3
go-version: stable

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ linters:
enable:
- asciicheck
- bodyclose
- deadcode
- dogsled
- dupl
- durationcheck
Expand Down Expand Up @@ -100,7 +99,6 @@ linters:
- rowserrcheck
- sqlclosecheck
- staticcheck
- structcheck
- stylecheck
- testpackage
- thelper
Expand All @@ -109,6 +107,5 @@ linters:
- unconvert
- unparam
- unused
- varcheck
- whitespace
- wsl
13 changes: 6 additions & 7 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ builds:
env:
- CGO_ENABLED=0
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
- name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
checksum:
name_template: 'checksums.txt'
dockers:
Expand All @@ -21,7 +21,6 @@ dockers:
dockerfile: Dockerfile.goreleaser
build_flag_templates:
- "--pull"
- "--build-arg=gomodguard_VERSION={{.Version}}"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.name={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
Expand Down
8 changes: 2 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
ARG GO_VERSION=1.14.2
ARG ALPINE_VERSION=3.11
ARG gomodguard_VERSION=

# ---- Build container
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS builder
FROM golang:alpine AS builder
WORKDIR /gomodguard
COPY . .
RUN apk add --no-cache git
RUN go build -o gomodguard cmd/gomodguard/main.go

# ---- App container
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION}
FROM golang:alpine
WORKDIR /
RUN apk --no-cache add ca-certificates
COPY --from=builder gomodguard/gomodguard /
Expand Down
6 changes: 1 addition & 5 deletions Dockerfile.goreleaser
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
ARG GO_VERSION=1.14.2
ARG ALPINE_VERSION=3.11
ARG gomodguard_VERSION=

# ---- App container
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION}
FROM golang:alpine
WORKDIR /
RUN apk --no-cache add ca-certificates
COPY gomodguard /gomodguard
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ cover:
dockerrun: dockerbuild
docker run -v "${current_dir}/.gomodguard.yaml:/.gomodguard.yaml" ryancurrah/gomodguard:latest

.PHONY: snapshot
snapshot:
goreleaser --rm-dist --snapshot

.PHONY: release
release:
goreleaser --rm-dist
Expand All @@ -39,4 +43,4 @@ install-tools-mac:

.PHONY: install-go-tools
install-go-tools:
go get github.com/t-yuki/gocover-cobertura
go install -v github.com/t-yuki/gocover-cobertura
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Resulting checkstyle file
## Install

```
go get -u github.com/ryancurrah/gomodguard/cmd/gomodguard
go install github.com/ryancurrah/gomodguard/cmd/gomodguard
```

## Develop
Expand Down
2 changes: 1 addition & 1 deletion _example/.gomodguard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ blocked:
modules: # List of blocked modules
- github.com/uudashr/go-module: # Blocked module
recommendations: # Recommended modules that should be used instead (Optional)
- golang.org/x/mod
- golang.org/x/mod
reason: "`mod` is the official go.mod parser library." # Reason why the recommended module should be used (Optional)
- github.com/gofrs/uuid:
recommendations:
Expand Down
6 changes: 3 additions & 3 deletions _example/blocked_example.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package gomodguard

import (
"io/ioutil"
"os"

"github.com/gofrs/uuid"
"github.com/mitchellh/go-homedir"
"github.com/ryancurrah/gomodguard"
module "github.com/uudashr/go-module"
)

func aBlockedImport() { // nolint: deadcode,unused
b, err := ioutil.ReadFile("go.mod")
func aBlockedImport() { //nolint: deadcode,unused
b, err := os.ReadFile("go.mod")
if err != nil {
panic(err)
}
Expand Down
39 changes: 39 additions & 0 deletions allowed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gomodguard

import "strings"

// Allowed is a list of modules and module
// domains that are allowed to be used.
type Allowed struct {
Modules []string `yaml:"modules"`
Domains []string `yaml:"domains"`
}

// IsAllowedModule returns true if the given module
// name is in the allowed modules list.
func (a *Allowed) IsAllowedModule(moduleName string) bool {
allowedModules := a.Modules

for i := range allowedModules {
if strings.TrimSpace(moduleName) == strings.TrimSpace(allowedModules[i]) {
return true
}
}

return false
}

// IsAllowedModuleDomain returns true if the given modules domain is
// in the allowed module domains list.
func (a *Allowed) IsAllowedModuleDomain(moduleName string) bool {
allowedDomains := a.Domains

for i := range allowedDomains {
if strings.HasPrefix(strings.TrimSpace(strings.ToLower(moduleName)),
strings.TrimSpace(strings.ToLower(allowedDomains[i]))) {
return true
}
}

return false
}
70 changes: 70 additions & 0 deletions allowed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package gomodguard_test

import (
"reflect"
"testing"

"github.com/ryancurrah/gomodguard"
)

func TestAllowedIsAllowedModule(t *testing.T) {
var tests = []struct {
testName string
allowedModules gomodguard.Allowed
lintedModuleName string
wantIsAllowedModule bool
}{
{
"module is allowed",
gomodguard.Allowed{Modules: []string{"github.com/someallowed/module"}},
"github.com/someallowed/module",
true,
},
{
"module not allowed",
gomodguard.Allowed{},
"github.com/someblocked/module",
false,
},
}

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
isAllowedModule := tt.allowedModules.IsAllowedModule(tt.lintedModuleName)
if !reflect.DeepEqual(isAllowedModule, tt.wantIsAllowedModule) {
t.Errorf("got '%v' want '%v'", isAllowedModule, tt.wantIsAllowedModule)
}
})
}
}

func TestAllowedIsAllowedModuleDomain(t *testing.T) {
var tests = []struct {
testName string
allowedModules gomodguard.Allowed
lintedModuleName string
wantIsAllowedModuleDomain bool
}{
{
"module is allowed",
gomodguard.Allowed{Domains: []string{"github.com"}},
"github.com/someallowed/module",
true,
},
{
"module not allowed",
gomodguard.Allowed{},
"github.com/someblocked/module",
false,
},
}

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
isAllowedModuleDomain := tt.allowedModules.IsAllowedModuleDomain(tt.lintedModuleName)
if !reflect.DeepEqual(isAllowedModuleDomain, tt.wantIsAllowedModuleDomain) {
t.Errorf("got '%v' want '%v'", isAllowedModuleDomain, tt.wantIsAllowedModuleDomain)
}
})
}
}
Loading

0 comments on commit 5295705

Please sign in to comment.