Skip to content

Commit

Permalink
Add golangci-lint (#59)
Browse files Browse the repository at this point in the history
* add golangci-lint

* fix messed up go mod

* add readme chunk

* go 1.18

* add go workspace to make vscode happy

* vendorSha256 changed again apparently

* dont try to install golangci lint

* go work sync

* fix vendorSha256 again for some reason

* fix go 1.18 linter warnings
  • Loading branch information
devonboyer authored Jun 2, 2022
1 parent 27b129a commit 5508573
Show file tree
Hide file tree
Showing 27 changed files with 1,790 additions and 71 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: golangci-lint

on:
push:
workflow_call:

jobs:
golangci:
runs-on: ubuntu-20.04
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.18
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.46.2
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ tmp/*
.envrc.local

.vscode/*
!.vscode/settings.example.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix
45 changes: 45 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
linters:
# Disable all linters.
# Default: false
disable-all: true
# Enable specific linter
# https://golangci-lint.run/usage/linters/#enabled-by-default-linters
enable:
- depguard
- dogsled
- errcheck
- exportloopref
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- goprintffuncname
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- staticcheck
- typecheck
- unconvert
- unused
- varcheck
- whitespace

# linters disabled because of go1.18 (see https://github.com/golangci/golangci-lint/issues/2649):
# - bodyclose
# - noctx
# - structcheck

# don't enable:
# - wsl
# - gochecknoinits
# - gomnd

# Options for analysis running.
run:
skip-files:
- "internal/pb/*.pb.go$"
- internal/pb/object.go
6 changes: 6 additions & 0 deletions .vscode/settings.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"go.lintTool":"golangci-lint",
"go.lintFlags": [
"--fast"
]
}
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ INTERNAL_GO_FILES := $(shell find internal/ -type f -name '*.go')
MIGRATE_DIR := ./migrations
SERVICE := $(PROJECT).server

TOOLS_DIR := tools
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
GOLANGCI_LINT := $(TOOLS_BIN_DIR)/golangci-lint

.PHONY: install migrate migrate-create build release test
.PHONY: reset-db setup-local server server-profile client-update client-get client-rebuild client-pack health
.PHONY: k8s-clear k8s-build k8s-deploy k8s-client-update k8s-client-get k8s-client-rebuild k8s-client-pack k8s-health
.PHONY: upload-container-image

$(GOLANGCI_LINT): $(TOOLS_DIR)/go.mod
cd $(TOOLS_DIR) && go build -o bin/golangci-lint github.com/golangci/golangci-lint/cmd/golangci-lint

install:
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
Expand Down Expand Up @@ -215,3 +222,6 @@ load-test-get:

load-test-update:
$(call load-test,Update,update_increment.json,10000,1)

lint: $(GOLANGCI_LINT)
$(GOLANGCI_LINT) run ./... --fast
23 changes: 23 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ You will also require `npm`.
$ make install
```

### Setup VSCode (Optional)

We recommend using VSCode for development, and there's an example settings file at `.vscode/settings.example.json` to get started with:

```
cp .vscode/settings.example.json .vscode/settings.json
```

#### Install `golangci-lint`

```
brew tap golangci/tap
brew install golangci/tap/golangci-lint
```

### Build

This will build the server and client executables along with the `*.proto` files.
Expand All @@ -39,6 +54,14 @@ This will build the server and client executables along with the `*.proto` files
$ make build
```

### Lint

This will lint the whole project and report any problems:

```bash
$ make lint
```

## API Testing

Ensure there is a Postgres database named `dl_tests`. These tests will write to a real database instance
Expand Down
16 changes: 8 additions & 8 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func parseNewArgs(args []string) (*sharedArgs, *newArgs, error) {
template := set.Int64("template", -1, "Template ID")
patterns := set.String("patterns", "", "Comma separated pack patterns")

set.Parse(args)
_ = set.Parse(args)

if *id == -1 {
return nil, nil, errors.New("required arg: -id")
Expand Down Expand Up @@ -108,7 +108,7 @@ func parseGetArgs(args []string) (*sharedArgs, *getArgs, error) {
to := set.Int64("to", -1, "To version ID (optional)")
prefix := set.String("prefix", "", "Search prefix")

set.Parse(args)
_ = set.Parse(args)

if *project == -1 {
return nil, nil, errors.New("required arg: -project")
Expand Down Expand Up @@ -158,7 +158,7 @@ func parseRebuildArgs(args []string) (*sharedArgs, *rebuildArgs, error) {
prefix := set.String("prefix", "", "Search prefix")
dir := set.String("dir", "", "Output directory")

set.Parse(args)
_ = set.Parse(args)

if *project == -1 {
return nil, nil, errors.New("required arg: -project")
Expand Down Expand Up @@ -213,7 +213,7 @@ func parseUpdateArgs(args []string) (*sharedArgs, *updateArgs, error) {
project := set.Int64("project", -1, "Project ID (required)")
dir := set.String("dir", "", "Directory containing updated files")

set.Parse(args)
_ = set.Parse(args)

if *project == -1 {
return nil, nil, errors.New("required arg: -project")
Expand Down Expand Up @@ -250,7 +250,7 @@ func parseInspectArgs(args []string) (*sharedArgs, *inspectArgs, error) {
shared := parseSharedArgs(set)
project := set.Int64("project", -1, "Project ID (required)")

set.Parse(args)
_ = set.Parse(args)

if *project == -1 {
return nil, nil, errors.New("required arg: -project")
Expand Down Expand Up @@ -284,7 +284,7 @@ func parseSnapshotArgs(args []string) (*sharedArgs, *snapshotArgs, error) {

shared := parseSharedArgs(set)

set.Parse(args)
_ = set.Parse(args)

return shared, &snapshotArgs{}, nil
}
Expand All @@ -310,7 +310,7 @@ func parseResetArgs(args []string) (*sharedArgs, *resetArgs, error) {
shared := parseSharedArgs(set)
state := set.String("state", "", "State string from a snapshot command")

set.Parse(args)
_ = set.Parse(args)

return shared, &resetArgs{
state: *state,
Expand Down Expand Up @@ -400,7 +400,7 @@ func main() {
os.Exit(exitCode)
}()

defer logger.Sync()
defer logger.Sync() // nolint:errcheck

ctx, cancel := context.WithTimeout(context.Background(), 200*time.Second)
defer cancel()
Expand Down
8 changes: 4 additions & 4 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ func main() {
if err != nil {
stdlog.Fatal(err.Error())
}
defer logger.Sync()
defer logger.Sync() //nolint:errcheck

if args.prof != "" {
file, err := os.Create(args.prof)
if err != nil {
logger.Fatal(ctx, "open pprof file", zap.String("file", args.prof), zap.Error(err))
}
pprof.StartCPUProfile(file)
_ = pprof.StartCPUProfile(file)
defer pprof.StopCPUProfile()
}

Expand Down Expand Up @@ -166,7 +166,7 @@ func main() {
}
s.RegisterFs(ctx, fs)

osSignals := make(chan os.Signal)
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
go func() {
<-osSignals
Expand All @@ -178,7 +178,7 @@ func main() {
if args.prof != "" {
pprof.StopCPUProfile()
}
logger.Sync()
_ = logger.Sync()
os.Exit(0)
}()

Expand Down
7 changes: 5 additions & 2 deletions cmd/webui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
if err != nil {
stdlog.Fatal(err.Error())
}
defer logger.Sync()
defer logger.Sync() //nolint:errcheck

args := parseArgs()

Expand Down Expand Up @@ -80,5 +80,8 @@ func main() {
}

logger.Info(ctx, "start webui", zap.Int("port", args.port), zap.String("assets", args.assetsDir))
http.ListenAndServe(":"+strconv.Itoa(args.port), handler)
err = http.ListenAndServe(":"+strconv.Itoa(args.port), handler)
if err != nil {
logger.Fatal(ctx, "starting server", zap.Error(err))
}
}
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildGoModule rec {
version = "0.1.0";
src = ./.;
proxyVendor = true; # Fixes: cannot query module due to -mod=vendor running make install
vendorSha256 = "sha256-DHLAYj87G+hvadejergnvE3altTAdqrZEO5D5zVke1U=";
vendorSha256 = "sha256-cefXFcWh3J9uUjqfzdPjeagOyQe8MupGbKCfsP90+H4=";

outputs = [ "out" "client" "server" "webui" "assets" "migrations" ];

Expand Down
33 changes: 18 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.6.3
go.opentelemetry.io/otel/sdk v1.6.3
go.opentelemetry.io/otel/trace v1.6.3
go.uber.org/zap v1.16.0
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
go.uber.org/zap v1.17.0
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/grpc v1.45.0
google.golang.org/protobuf v1.28.0
)

require (
cloud.google.com/go v0.65.0 // indirect
cloud.google.com/go/compute v1.5.0 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/aead/chacha20poly1305 v0.0.0-20170617001512-233f39982aeb // indirect
github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 // indirect
Expand All @@ -44,20 +44,23 @@ require (
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/puddle v1.1.3 // indirect
github.com/klauspost/cpuid/v2 v2.0.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.4 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.6.3 // indirect
go.opentelemetry.io/proto/otlp v0.15.0 // indirect
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/multierr v1.5.0 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/tools v0.1.5 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit 5508573

Please sign in to comment.