Skip to content

Commit c4e3226

Browse files
hieblmistarius
authored andcommitted
tools: adopt llformat formatting targets
1 parent ea03440 commit c4e3226

5 files changed

Lines changed: 98 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ _testmain.go
2626

2727
/loop-debug
2828
/loopd-debug
29+
/tools/llformat
2930

3031
output*.log
3132

Makefile

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ GOTEST := GO111MODULE=on go test -v
77

88

99
GOIMPORTS_PKG := github.com/rinchsan/gosimports/cmd/gosimports
10+
LLFORMAT_PKG := github.com/bhandras/llformat/cmd/llformat
1011

1112
GO_BIN := ${GOPATH}/bin
1213
GOIMPORTS_BIN := $(GO_BIN)/gosimports
14+
LLFORMAT_BIN := $(CURDIR)/$(TOOLS_DIR)/llformat
1315

1416
GOBUILD := CGO_ENABLED=0 GO111MODULE=on go build -v
1517
GOINSTALL := CGO_ENABLED=0 GO111MODULE=on go install -v
@@ -35,6 +37,7 @@ UNIT := $(GOLIST) | $(XARGS) env $(GOTEST) $(TEST_FLAGS)
3537
ifneq ($(workers),)
3638
LINT_WORKERS = --concurrency=$(workers)
3739
endif
40+
FMT_BASE := $(if $(base),$(base),origin/master)
3841

3942
DOCKER_TOOLS = docker run \
4043
--rm \
@@ -64,6 +67,11 @@ $(GOIMPORTS_BIN):
6467
@$(call print, "Installing goimports.")
6568
cd $(TOOLS_DIR); go install -trimpath $(GOIMPORTS_PKG)
6669

70+
$(LLFORMAT_BIN): $(TOOLS_DIR)/go.mod $(TOOLS_DIR)/go.sum
71+
@$(call print, "Installing llformat.")
72+
cd $(TOOLS_DIR); GOBIN="$(CURDIR)/$(TOOLS_DIR)" \
73+
go install -trimpath $(LLFORMAT_PKG)
74+
6775

6876
# ============
6977
# INSTALLATION
@@ -132,11 +140,15 @@ unit-postgres-race:
132140
# UTILITIES
133141
# =========
134142

135-
fmt: $(GOIMPORTS_BIN)
136-
@$(call print, "Fixing imports.")
137-
gosimports -w $(GOFILES_NOVENDOR)
138-
@$(call print, "Formatting source.")
139-
gofmt -l -w -s $(GOFILES_NOVENDOR)
143+
fmt: $(LLFORMAT_BIN)
144+
@$(call print, "Formatting all handwritten Go source.")
145+
@./scripts/llformat-files.sh all | \
146+
xargs -0 -n 1 $(LLFORMAT_BIN) -w
147+
148+
fmt-changed: $(LLFORMAT_BIN)
149+
@$(call print, "Formatting Go source changes against $(FMT_BASE).")
150+
@./scripts/llformat-files.sh changed "$(FMT_BASE)" | \
151+
xargs -0 -n 1 $(LLFORMAT_BIN) -w
140152

141153
lint: docker-tools
142154
@$(call print, "Linting source.")

scripts/llformat-files.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
usage() {
6+
echo "usage: $0 all|changed [base]" >&2
7+
}
8+
9+
is_format_file() {
10+
local file="$1"
11+
12+
[[ -f "$file" ]] || return 1
13+
[[ "$file" == *.go ]] || return 1
14+
15+
case "$file" in
16+
*.pb.go | *.pb.gw.go | *.pb.json.go | *.pb.validate.go | \
17+
*.connect.go | *.gen.go | *_gen.go | *_generated.go | \
18+
*.sql.go | db.go | */db.go | models.go | */models.go | \
19+
querier.go | */querier.go | .git/* | */.git/* | \
20+
vendor/* | */vendor/* | third_party/* | */third_party/* | \
21+
testdata/* | */testdata/*)
22+
return 1
23+
;;
24+
esac
25+
26+
return 0
27+
}
28+
29+
print_file() {
30+
local file="$1"
31+
32+
if is_format_file "$file"; then
33+
printf '%s\0' "$file"
34+
fi
35+
}
36+
37+
list_all() {
38+
local file
39+
40+
while IFS= read -r -d '' file; do
41+
print_file "${file#./}"
42+
done < <(find . -type f -name '*.go' -print0)
43+
}
44+
45+
list_changed() {
46+
local base="$1"
47+
local file
48+
49+
{
50+
git diff --name-only --diff-filter=ACMR "$base"...HEAD
51+
git diff --name-only --diff-filter=ACMR
52+
git diff --cached --name-only --diff-filter=ACMR
53+
git ls-files --others --exclude-standard
54+
} | sort -u | while IFS= read -r file; do
55+
print_file "$file"
56+
done
57+
}
58+
59+
mode="${1:-}"
60+
61+
case "$mode" in
62+
all)
63+
list_all
64+
;;
65+
changed)
66+
list_changed "${2:-origin/master}"
67+
;;
68+
*)
69+
usage
70+
exit 2
71+
;;
72+
esac

tools/go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/lightninglabs/loop/tools
33
go 1.26
44

55
require (
6+
github.com/bhandras/llformat v0.1.1-beta // indirect
67
// Once golangci-lint v2.4.1 update it here.
78
github.com/golangci/golangci-lint/v2 v2.10.1
89
github.com/rinchsan/gosimports v0.3.8
@@ -222,3 +223,8 @@ require (
222223
go.augendre.info/fatcontext v0.9.0 // indirect
223224
go.yaml.in/yaml/v3 v3.0.4 // indirect
224225
)
226+
227+
tool (
228+
github.com/bhandras/llformat/cmd/llformat
229+
github.com/golangci/golangci-lint/v2/cmd/golangci-lint
230+
)

tools/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
102102
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
103103
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
104104
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
105+
github.com/bhandras/llformat v0.1.1-beta h1:eXOyGfkvISWM7d5RUcwhvXPidEsDwLzOB1LN+d5FE9Q=
106+
github.com/bhandras/llformat v0.1.1-beta/go.mod h1:fudtFasc8GnV1JFw+tB9tb7W0wj7IRUvUdGkOaw+q0s=
105107
github.com/bkielbasa/cyclop v1.2.3 h1:faIVMIGDIANuGPWH031CZJTi2ymOQBULs9H21HSMa5w=
106108
github.com/bkielbasa/cyclop v1.2.3/go.mod h1:kHTwA9Q0uZqOADdupvcFJQtp/ksSnytRMe8ztxG8Fuo=
107109
github.com/blizzy78/varnamelen v0.8.0 h1:oqSblyuQvFsW1hbBHh1zfwrKe3kcSj0rnXkKzsQ089M=

0 commit comments

Comments
 (0)