From 4ed3197985153aa5fb22c19cf06a98feac134b34 Mon Sep 17 00:00:00 2001 From: sputn1ck Date: Fri, 20 May 2022 09:00:26 +0200 Subject: [PATCH] make+tools: tidy makefile, add gosimports --- Makefile | 76 ++++++++++++++++++++++++++++++++------------------ tools/go.mod | 5 +++- tools/go.sum | 2 ++ tools/tools.go | 1 + 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 2eaa520..a1270b3 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,12 @@ TOOLS_DIR := tools GOTEST := GO111MODULE=on go test -v + +GOIMPORTS_PKG := github.com/rinchsan/gosimports/cmd/gosimports + GO_BIN := ${GOPATH}/bin +GOIMPORTS_BIN := $(GO_BIN)/gosimports + GOBUILD := GO111MODULE=on go build -v GOINSTALL := GO111MODULE=on go install -v GOMOD := GO111MODULE=on go mod @@ -14,7 +19,7 @@ COMMIT := $(shell git describe --abbrev=40 --dirty) LDFLAGS := -ldflags "-X $(PKG).Commit=$(COMMIT)" DEV_TAGS = dev -GOFILES_NOVENDOR = $(shell find . -type f -name '*.go' -not -path "./vendor/*") +GOFILES_NOVENDOR = $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -name "*pb.go" -not -name "*pb.gw.go" -not -name "*.pb.json.go") GOLIST := go list $(PKG)/... | grep -v '/vendor/' XARGS := xargs -L 1 @@ -37,34 +42,14 @@ define print echo $(GREEN)$1$(NC) endef -$(LINT_BIN): - @$(call print, "Fetching linter") - $(DEPGET) $(LINT_PKG)@$(LINT_COMMIT) - -unit: - @$(call print, "Running unit tests.") - $(UNIT) - -fmt: - @$(call print, "Formatting source.") - gofmt -l -w -s $(GOFILES_NOVENDOR) - -lint: docker-tools - @$(call print, "Linting source.") - $(DOCKER_TOOLS) golangci-lint run -v $(LINT_WORKERS) - -docker-tools: - @$(call print, "Building tools docker image.") - docker build -q -t loop-tools $(TOOLS_DIR) +# ============ +# DEPENDENCIES +# ============ -mod-tidy: - @$(call print, "Tidying modules.") - $(GOMOD) tidy +$(GOIMPORTS_BIN): + @$(call print, "Installing goimports.") + cd $(TOOLS_DIR); go install -trimpath $(GOIMPORTS_PKG) -mod-check: - @$(call print, "Checking modules.") - $(GOMOD) tidy - if test -n "$$(git status | grep -e "go.mod\|go.sum")"; then echo "Running go mod tidy changes go.mod/go.sum"; git status; git diff; exit 1; fi # ============ # INSTALLATION @@ -101,3 +86,40 @@ clean: @$(call print, "Cleaning up.") rm -f ./loop-debug ./loopd-debug rm -rf ./vendor + +# ======= +# TESTING +# ======= + +unit: + @$(call print, "Running unit tests.") + $(UNIT) + +# ========= +# UTILITIES +# ========= + +fmt: $(GOIMPORTS_BIN) + @$(call print, "Fixing imports.") + gosimports -w $(GOFILES_NOVENDOR) + @$(call print, "Formatting source.") + gofmt -l -w -s $(GOFILES_NOVENDOR) + +lint: docker-tools + @$(call print, "Linting source.") + $(DOCKER_TOOLS) golangci-lint run -v $(LINT_WORKERS) + +docker-tools: + @$(call print, "Building tools docker image.") + docker build -q -t loop-tools $(TOOLS_DIR) + +mod-tidy: + @$(call print, "Tidying modules.") + $(GOMOD) tidy + +mod-check: + @$(call print, "Checking modules.") + $(GOMOD) tidy + if test -n "$$(git status | grep -e "go.mod\|go.sum")"; then echo "Running go mod tidy changes go.mod/go.sum"; git status; git diff; exit 1; fi + + diff --git a/tools/go.mod b/tools/go.mod index 5c06ec5..8c2c4c1 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -2,4 +2,7 @@ module github.com/lightninglabs/loop/tools go 1.16 -require github.com/golangci/golangci-lint v1.46.2 // indirect +require ( + github.com/golangci/golangci-lint v1.46.2 // indirect + github.com/rinchsan/gosimports v0.1.5 // indirect +) diff --git a/tools/go.sum b/tools/go.sum index 64db646..4113952 100644 --- a/tools/go.sum +++ b/tools/go.sum @@ -696,6 +696,8 @@ github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567/go.mod h1:DWNGW8 github.com/remyoudompheng/go-dbus v0.0.0-20121104212943-b7232d34b1d5/go.mod h1:+u151txRmLpwxBmpYn9z3d1sdJdjRPQpsXuYeY9jNls= github.com/remyoudompheng/go-liblzma v0.0.0-20190506200333-81bf2d431b96/go.mod h1:90HvCY7+oHHUKkbeMCiHt1WuFR2/hPJ9QrljDG+v6ls= github.com/remyoudompheng/go-misc v0.0.0-20190427085024-2d6ac652a50e/go.mod h1:80FQABjoFzZ2M5uEa6FUaJYEmqU2UOKojlFVak1UAwI= +github.com/rinchsan/gosimports v0.1.5 h1:Z/l9lS79z0xgKC6fLJYmDdY44D0LFwo3MzaMtWvMKpY= +github.com/rinchsan/gosimports v0.1.5/go.mod h1:102/jU2cwf9fpa/YM9D9o4gSen2Vg8Jl80Sxctgd9N0= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= diff --git a/tools/tools.go b/tools/tools.go index 3e1ae43..4df1bcd 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -5,4 +5,5 @@ package loop import ( _ "github.com/golangci/golangci-lint/cmd/golangci-lint" + _ "github.com/rinchsan/gosimports/cmd/gosimports" )