Skip to content

Commit

Permalink
lint and check, cleanup idiomatic go issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jac18281828 committed Sep 13, 2024
1 parent a4017b5 commit 879c9e2
Show file tree
Hide file tree
Showing 33 changed files with 386 additions and 151 deletions.
33 changes: 33 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile
{
"name": "Existing Dockerfile",
"build": {
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerfile": "../Dockerfile"
},
"customizations": {
"vscode": {
"extensions": [
"golang.go"
]
}
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Uncomment the next line to run commands after the container is created.
// "postCreateCommand": "cat /etc/os-release",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "devcontainer"
}
11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.idea
*.sw*
*.terraform
*.terraform*
/bin
/scripts/runLocal
sidecar.db*
*.db*
/sqlite
/sqlite*
go-sidecar
20 changes: 20 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for more information:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
# https://containers.dev/guide/dependabot

version: 2
updates:
- package-ecosystem: "devcontainers"
directory: "/"
schedule:
interval: weekly
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: weekly
- package-ecosystem: "go"
directory: "/"
schedule:
interval: weekly
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
name: build-container

on: push

jobs:
test:
runs-on: ubuntu-24.04
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ sidecar.db*
*.db*
/sqlite
/sqlite*
go-sidecar
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters:
enable:
- govet
- staticcheck
- unused
- errcheck
106 changes: 106 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "install",
"type": "shell",
"command": "make deps/go",
"options": {
"cwd": "${workspaceFolder}",
},
"group": {
"kind": "build"
}
},
{
"label": "fmt",
"type": "shell",
"command": "gofmt -l .",
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn": "install",
"group": {
"kind": "build"
}
},
{
"label": "vet",
"type": "shell",
"command": "go vet ./...",
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn": "fmtcheck",
"group": {
"kind": "build"
}
},
{
"label": "lint",
"type": "shell",
"command": "golangci-lint run -c .golangci.yml",
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn": "vet",
"group": {
"kind": "build"
}
},
{
"label": "build",
"type": "shell",
"command": "go build ./...",
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn": "lint",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "clean",
"type": "shell",
"command": "go clean ./...",
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn": "build",
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "test",
"type": "shell",
"command": "go test ./...",
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn": "lint",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "staticcheck",
"type": "shell",
"command": "staticcheck ./...",
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn": "test",
"group": {
"kind": "test",
"isDefault": false
}
},
]
}
34 changes: 22 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
FROM golang:1.22-bullseye as build
FROM golang:1.22-bullseye AS build

RUN apt-get update
RUN apt-get install -y make postgresql-client

RUN mkdir /build
RUN useradd --create-home -s /bin/bash gobuild
RUN usermod -a -G sudo gobuild
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

COPY . /build

WORKDIR /build
ARG PROJECT=go-sidecar
RUN mkdir -p /workspaces/${PROJECT}
WORKDIR /workspaces/${PROJECT}
COPY --chown=gobuild:gobuild . .

# system and linux dependencies
RUN make deps-linux
RUN chown -R gobuild:gobuild /go

# local dependencies
ENV USER=gobuild
ENV GOBIN=/go/bin
ENV PATH=$PATH:${GOBIN}
USER gobuild

RUN make build
RUN git config --global --add safe.directory /workspaces/${PROJECT}

# FROM golang:1.22-bullseye as run
#
# RUN apt-get update
# RUN apt-get install -y vim postgresql-client
#
# COPY --from=build /build/bin/cmd /bin
RUN make yamlfmt
RUN make fmtcheck
RUN make vet
RUN make lint
RUN make test
41 changes: 38 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

.PHONY: deps proto

deps/dev:
go install github.com/golangci/golangci-lint/cmd/[email protected]
go install honnef.co/go/tools/cmd/staticcheck@latest
go install github.com/google/yamlfmt/cmd/yamlfmt@latest

deps/go:
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
Expand All @@ -16,7 +21,7 @@ deps/go:
google.golang.org/grpc/cmd/protoc-gen-go-grpc
go mod tidy

deps-linux: deps/go
deps-linux: deps/go deps/dev
BIN="/usr/local/bin" VERSION="1.32.2" && \
curl -sSL "https://github.com/bufbuild/buf/releases/download/v${VERSION}/buf-$(uname -s)-$(uname -m)" -o "${BIN}/buf" && \
chmod +x "${BIN}/buf"
Expand All @@ -35,7 +40,7 @@ clean:

.PHONY: build/cmd/sidecar
build/cmd/sidecar:
go build -o bin/cmd/sidecar cmd/sidecar/main.go
go build -ldflags="-s -w" -o bin/cmd/sidecar cmd/sidecar/main.go

.PHONY: build
build: build/cmd/sidecar
Expand All @@ -46,9 +51,39 @@ docker-buildx-self:
docker-buildx:
docker-buildx build --platform linux/amd64 --push -t 767397703211.dkr.ecr.us-east-1.amazonaws.com/go-sidecar:$(shell date +%s) -t 767397703211.dkr.ecr.us-east-1.amazonaws.com/go-sidecar:latest .

.PHONY: yamlfmt
yamlfmt:
yamlfmt -lint .github/workflows/*.yml .github/*.yml

.PHONY: fmt
fmt:
gofmt -l .


.PHONY: fmtcheck
fmtcheck:
@unformatted_files=$$(gofmt -l .); \
if [ -n "$$unformatted_files" ]; then \
echo "The following files are not properly formatted:"; \
echo "$$unformatted_files"; \
echo "Please run 'gofmt -w .' to format them."; \
exit 1; \
fi
.PHONY: vet
vet:
go vet ./...

.PHONY: lint
lint:
golangci-lint run

.PHONY: test
test:
TESTING=true go test -v -p 1 -parallel 1 ./...

.PHONY: staticcheck
staticcheck:
staticcheck ./...

.PHONY: ci-test
ci-test: test
ci-test: test lint vet fmtcheck yamlfmt
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/ethereum/go-ethereum v1.14.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0
github.com/holiman/uint256 v1.3.1
github.com/mattn/go-sqlite3 v1.14.23
github.com/rs/cors v1.7.0
github.com/stretchr/testify v1.9.0
github.com/wealdtech/go-merkletree/v2 v2.6.0
Expand Down Expand Up @@ -40,13 +40,13 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/iden3/go-iden3-crypto v0.0.16 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.23 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APP
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
Expand Down
Loading

0 comments on commit 879c9e2

Please sign in to comment.