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

integration: add static code analysis tool; #377

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 27 additions & 10 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

jobs:
terraform-fmt:
name: Terraform fmt
name: terraform fmt
runs-on: ubuntu-20.04
steps:
- name: Check out code into the Go module directory
Expand All @@ -20,7 +20,7 @@ jobs:
run: terraform fmt -check -recursive .

go-fmt:
name: Go fmt
name: go fmt
runs-on: ubuntu-20.04
steps:
- name: Check out code into the Go module directory
Expand All @@ -33,11 +33,11 @@ jobs:
run: test -z $(gofmt -l .)

build:
name: Go build
name: go build
runs-on: ubuntu-20.04
steps:
- name: Set up Go 1.15
uses: actions/setup-go@v1
uses: actions/setup-go@v2
with:
go-version: 1.15

Expand All @@ -51,11 +51,11 @@ jobs:
run: go build -v ./...

go-vet:
name: Go vet
name: static code analysis (go vet)
runs-on: ubuntu-20.04
steps:
- name: Set up Go 1.15
uses: actions/setup-go@v1
uses: actions/setup-go@v2
with:
go-version: 1.15

Expand All @@ -68,12 +68,29 @@ jobs:
- name: Static code analysis
run: go vet ./...

golangci-lint:
name: static code analysis (golangci-lint)
runs-on: ubuntu-20.04
steps:
- name: Set up Go 1.15
uses: actions/setup-go@v2
with:
go-version: 1.15

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Static code analysis (golangci-lint)
uses: golangci/golangci-lint-action@v2
with:
version: v1.29
args: --timeout 5m --print-issued-lines --print-linter-name --out-format=colored-line-number --disable structcheck
go-test:
name: Go test
name: go test
runs-on: ubuntu-20.04
steps:
- name: Set up Go 1.15
uses: actions/setup-go@v1
uses: actions/setup-go@v2
with:
go-version: 1.15

Expand All @@ -87,11 +104,11 @@ jobs:
run: go test ./...

go-test-integration:
name: Go test integration
name: go test (integration)
runs-on: ubuntu-20.04
steps:
- name: Set up Go 1.15
uses: actions/setup-go@v1
uses: actions/setup-go@v2
with:
go-version: 1.15

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ require (
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
gopkg.in/yaml.v2 v2.2.7 // indirect
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2
honnef.co/go/tools v0.0.1-2020.1.6 // indirect
)

go 1.15
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0/go.mod h1:/LWChgwKmv
github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM=
github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/gopher-lua v0.0.0-20190206043414-8bfc7677f583 h1:SZPG5w7Qxq7bMcMVl6e3Ht2X7f+AAGQdzjkbyOnNNZ8=
github.com/yuin/gopher-lua v0.0.0-20190206043414-8bfc7677f583/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
Expand All @@ -508,6 +509,7 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
Expand All @@ -519,6 +521,7 @@ golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvx
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -538,6 +541,7 @@ golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -598,6 +602,8 @@ golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgw
golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200410194907-79a7a3126eef/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -656,3 +662,4 @@ honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20180920025451-e3ad64cb4ed3/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.6/go.mod h1:pyyisuGw24ruLjrr1ddx39WE0y9OooInRzEYLhQB2YY=
7 changes: 6 additions & 1 deletion pkg/apiserver/auth/config_google.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package auth

import (
"context"
"fmt"
"github.com/applike/gosoline/pkg/cfg"
"github.com/applike/gosoline/pkg/mon"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"google.golang.org/api/oauth2/v2"
"google.golang.org/api/option"
"net/http"
"regexp"
"sync"
Expand Down Expand Up @@ -56,8 +58,11 @@ func NewConfigGoogleHandler(config cfg.Config, logger mon.Logger) gin.HandlerFun
}

func NewConfigGoogleAuthenticator(config cfg.Config, logger mon.Logger) Authenticator {
oauth2Service, err := oauth2.New(http.DefaultClient)
// it will never be used, because we specify an http client here already
ctx := context.Background()
clientOption := option.WithHTTPClient(http.DefaultClient)

oauth2Service, err := oauth2.NewService(ctx, clientOption)
if err != nil {
logger.Panic(err, "failed creating google oauth2 client")
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/apiserver/crud/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ func AddDeleteHandler(logger mon.Logger, d *apiserver.Definitions, version int,
}

func AddListHandler(logger mon.Logger, d *apiserver.Definitions, version int, basePath string, handler ListHandler) {
path, _ := getHandlerPaths(version, basePath)

plural := inflection.Plural(basePath)
path = fmt.Sprintf("/v%d/%s", version, plural)
path := fmt.Sprintf("/v%d/%s", version, plural)
d.POST(path, NewListHandler(logger, handler))
}

Expand Down
1 change: 0 additions & 1 deletion pkg/apiserver/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func (d *Definition) getAbsolutePath() string {
groupPath := d.group.getAbsolutePath()

absolutePath := fmt.Sprintf("%s/%s", groupPath, d.relativePath)
absolutePath = fmt.Sprintf("%s/%s", groupPath, d.relativePath)
absolutePath = strings.TrimRight(absolutePath, "/")

return removeDuplicates(absolutePath)
Expand Down
7 changes: 3 additions & 4 deletions pkg/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ type ApiServer struct {
kernel.EssentialModule
kernel.ServiceStage

logger mon.Logger
server *http.Server
listener net.Listener
defineRouter Definer
logger mon.Logger
server *http.Server
listener net.Listener
}

func New(definer Definer) kernel.ModuleFactory {
Expand Down
12 changes: 4 additions & 8 deletions pkg/blob/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestStreamBytes(t *testing.T) {

reader := stream.AsReader()

buf := make([]byte, 1, 1)
buf := make([]byte, 1)
n, err := reader.Read(buf)
assert.NoError(t, err)
assert.Equal(t, 1, n)
Expand Down Expand Up @@ -103,9 +103,7 @@ func TestStreamReaderWithSeek(t *testing.T) {

assert.GreaterOrEqual(t, len(buffer), len(data))

for i, b := range data {
buffer[i] = b
}
copy(buffer, data)

reader.On("Read", mock.Anything).Return(0, io.EOF).Once()
}).Return(len(data), nil).Once()
Expand All @@ -124,7 +122,7 @@ func TestStreamReaderWithoutSeek(t *testing.T) {
stream := blob.StreamReader(reader)
streamReader := stream.AsReader()

buffer := make([]byte, 1, 1)
buffer := make([]byte, 1)

reader.On("Read", buffer).Return(1, nil)
n, err := streamReader.Read(buffer)
Expand All @@ -150,9 +148,7 @@ func TestStreamReaderWithoutSeek(t *testing.T) {

assert.GreaterOrEqual(t, len(buffer), len(data))

for i, b := range data {
buffer[i] = b
}
copy(buffer, data)

reader.On("Read", mock.Anything).Return(0, io.EOF).Once()
}).Return(len(data), nil).Once()
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/ccache.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *Cache) Expire(key string) bool {
return false
}

remaining := item.Expires().Sub(time.Now())
remaining := time.Until(item.Expires())
item.Extend(-1 * remaining)

return true
Expand Down
2 changes: 1 addition & 1 deletion pkg/cast/string.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cast

func ToSlicePtrString(s []string) []*string {
out := make([]*string, len(s), len(s))
out := make([]*string, len(s))

for i, v := range s {
v := v
Expand Down
4 changes: 2 additions & 2 deletions pkg/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type config struct {
envKeyReplacer *strings.Replacer
}

var templateRegex = regexp.MustCompile("{([\\w.\\-]+)}")
var templateRegex = regexp.MustCompile(`{([\w.\-]+)}`)

func New() GosoConf {
return NewWithInterfaces(os.LookupEnv)
Expand Down Expand Up @@ -532,7 +532,7 @@ func (c *config) resolveEnvKey(prefix string, key string) string {
key = strings.Join([]string{prefix, key}, ".")
}

rp := regexp.MustCompile("\\[(\\d)\\]")
rp := regexp.MustCompile(`\[(\d)\]`)
matches := rp.FindAllStringSubmatch(key, -1)

for _, m := range matches {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cfg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (s *ConfigTestSuite) TestConfig_UnmarshalKey_Map() {
"KEY_KEY3_FOO": "env",
})

cm := make(map[string]configMap, 0)
cm := map[string]configMap{}
s.config.UnmarshalKey("key", &cm)

s.Len(cm, 3)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cfg/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *OptionsTestSuite) TestWithConfigSetting() {

actual = s.config.Get("a")
expectedMsi = expected.Msi()
s.Equal(expected.Msi(), actual)
s.Equal(expectedMsi, actual)
}

func TestOptionsTestSuite(t *testing.T) {
Expand Down
10 changes: 2 additions & 8 deletions pkg/cloud/aws/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ func IsAwsError(err error, awsCode string) bool {
}

func IsAwsErrorCodeRequestCanceled(err error) bool {
if IsAwsError(err, request.CanceledErrorCode) {
return true
}

return false
return IsAwsError(err, request.CanceledErrorCode)
}

func CheckConnectionError(_ interface{}, err error) exec.ErrorType {
Expand All @@ -77,9 +73,7 @@ func CheckConnectionError(_ interface{}, err error) exec.ErrorType {
func IsConnectionError(err error) bool {
var awsErr awserr.Error

if errors.As(err, &awsErr) {
err = awsErr.OrigErr()
}
errors.As(err, &awsErr)

return exec.IsConnectionError(awsErr)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/conc/ddb_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newDdbLock(manager *ddbLockProvider, ctx context.Context, resource string,

func (l *ddbLock) Renew(ctx context.Context, lockTime time.Duration) error {
if l == nil {
return NotOwnedError
return ErrNotOwned
}

err := l.manager.renew(ctx, lockTime, l.resource, l.token)
Expand All @@ -44,7 +44,7 @@ func (l *ddbLock) Renew(ctx context.Context, lockTime time.Duration) error {

func (l *ddbLock) Release() error {
if l == nil {
return NotOwnedError
return ErrNotOwned
}

// stop the debug thread if needed
Expand Down
6 changes: 3 additions & 3 deletions pkg/conc/ddb_lock_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (m *ddbLockProvider) Acquire(ctx context.Context, resource string) (Distrib
}

if result.ConditionalCheckFailed {
return OwnedLockError
return ErrOwnedLock
}

m.logger.WithContext(ctx).WithFields(mon.Fields{
Expand Down Expand Up @@ -143,7 +143,7 @@ func (m *ddbLockProvider) renew(ctx context.Context, lockTime time.Duration, res
}

if result.ConditionalCheckFailed {
return backoff.Permanent(NotOwnedError)
return backoff.Permanent(ErrNotOwned)
}

m.logger.WithContext(ctx).WithFields(mon.Fields{
Expand All @@ -170,7 +170,7 @@ func (m *ddbLockProvider) release(ctx context.Context, resource string, token st
}

if result.ConditionalCheckFailed {
return NotOwnedError
return ErrNotOwned
}

m.logger.WithContext(ctx).WithFields(mon.Fields{
Expand Down