Skip to content

Commit 1780f00

Browse files
authored
Merge pull request #70 from ziggie1984/update-go-and-add-linter
add basic CI to the repo
2 parents 49d94f0 + 4d8205c commit 1780f00

File tree

10 files changed

+1773
-12
lines changed

10 files changed

+1773
-12
lines changed

.github/workflows/main.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
lint:
11+
name: Lint
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Set up Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version: '1.24.6'
24+
25+
- name: Check formatting
26+
run: make fmt-check
27+
28+
- name: Run linter
29+
run: make lint
30+
31+
build:
32+
name: Build
33+
runs-on: ubuntu-latest
34+
35+
steps:
36+
- name: Checkout code
37+
uses: actions/checkout@v4
38+
39+
- name: Set up Go
40+
uses: actions/setup-go@v5
41+
with:
42+
go-version: '1.24.6'
43+
44+
- name: Build
45+
run: make build
46+
47+
- name: Build sphinx-cli
48+
run: make sphinx-cli
49+
50+
unit-tests:
51+
name: Unit Tests
52+
runs-on: ubuntu-latest
53+
strategy:
54+
matrix:
55+
unit_type:
56+
- unit
57+
- unit-cover
58+
59+
steps:
60+
- name: Checkout code
61+
uses: actions/checkout@v4
62+
63+
- name: Set up Go
64+
uses: actions/setup-go@v5
65+
with:
66+
go-version: '1.24.6'
67+
68+
- name: Run unit tests
69+
run: make ${{ matrix.unit_type }}
70+
71+
- name: Send coverage
72+
uses: coverallsapp/github-action@v2
73+
if: matrix.unit_type == 'unit-cover'
74+
continue-on-error: true
75+
with:
76+
github-token: ${{ secrets.GITHUB_TOKEN }}
77+
file: coverage.txt
78+
format: golang

.golangci.yml

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
version: "2"
2+
run:
3+
# If you change this please run `make lint` to see where else it needs to be
4+
# updated as well.
5+
go: "1.24.6"
6+
7+
# timeout for analysis
8+
timeout: 10m
9+
10+
linters:
11+
default: all
12+
disable:
13+
# Global variables are used in many places throughout the code base.
14+
- gochecknoglobals
15+
16+
# We want to allow short variable names.
17+
- varnamelen
18+
19+
# We want to allow TODOs.
20+
- godox
21+
22+
# Init functions are used by loggers throughout the codebase.
23+
- gochecknoinits
24+
25+
# Allow using default empty values.
26+
- exhaustruct
27+
28+
# Allow tests to be put in the same package.
29+
- testpackage
30+
31+
# Disable tagalign.
32+
- tagalign
33+
34+
# TODO(yy): create a list of allowed packages to import before enabling
35+
# this linter.
36+
- depguard
37+
38+
# Deprecated - this is replaced by wsl_v5.
39+
- wsl
40+
41+
# All available settings of specific linters.
42+
settings:
43+
nlreturn:
44+
# Size of the block (including return statement that is still "OK")
45+
# so no return split required.
46+
block-size: 3
47+
48+
funlen:
49+
# Checks the number of lines in a function.
50+
# If lower than 0, disable the check.
51+
lines: 100
52+
# Checks the number of statements in a function.
53+
statements: 50
54+
55+
wsl_v5:
56+
# We adopt a more relaxed cuddling rule by enabling
57+
# `allow-whole-block`. This allows a variable declaration to be
58+
# "cuddled" with a following block if the variable is used anywhere
59+
# within that block, not just as the first statement.
60+
allow-whole-block: true
61+
allow-first-in-block: false
62+
63+
# Disable the leading-whitespace check to resolve a conflict with the
64+
# whitespace linter, which requires a blank line after a function
65+
# signature. This is the standard Go style.
66+
disable:
67+
- leading-whitespace
68+
69+
lll:
70+
# Max line length, lines longer will be reported.
71+
# '\t' is counted as 1 character by default, and can be changed with the
72+
# tab-width option.
73+
# Default: 120.
74+
line-length: 80
75+
# Tab width in spaces.
76+
# Default: 1
77+
tab-width: 8
78+
79+
whitespace:
80+
multi-func: true
81+
multi-if: true
82+
83+
# Defines a set of rules to ignore issues.
84+
# It does not skip the analysis, and so does not ignore "typecheck" errors.
85+
exclusions:
86+
# Mode of the generated files analysis.
87+
#
88+
# - `strict`: sources are excluded by strictly following the Go generated file convention.
89+
# Source files that have lines matching only the following regular expression will be excluded: `^// Code generated .* DO NOT EDIT\.$`
90+
# This line must appear before the first non-comment, non-blank text in the file.
91+
# https://go.dev/s/generatedcode
92+
# - `lax`: sources are excluded if they contain lines like `autogenerated file`, `code generated`, `do not edit`, etc.
93+
# - `disable`: disable the generated files exclusion.
94+
#
95+
# Default: strict
96+
generated: lax
97+
98+
# Which file paths to exclude: they will be analyzed, but issues from them won't be reported.
99+
# "/" will be replaced by the current OS file path separator to properly work on Windows.
100+
# Default: []
101+
paths:
102+
- rpc/legacyrpc/
103+
- wallet/deprecated.go
104+
105+
rules:
106+
# Exclude gosec from running for tests so that tests with weak randomness
107+
# (math/rand) will pass the linter.
108+
- path: _test\.go
109+
linters:
110+
- gosec
111+
- funlen
112+
- revive
113+
# Allow duplications in tests so it's easier to follow a single unit
114+
# test.
115+
- dupl
116+
# Allow returning unwrapped errors in tests.
117+
- wrapcheck
118+
119+
- path: mock*
120+
linters:
121+
- revive
122+
# forcetypeassert is skipped for the mock because the test would fail if
123+
# the returned value doesn't match the type, so there's no need to check
124+
# the convert.
125+
- forcetypeassert
126+
127+
# Allow fmt.Printf() in commands.
128+
- path: cmd/commands/*
129+
linters:
130+
- forbidigo
131+
132+
# Allow fmt.Printf() in config parsing.
133+
- path: config\.go
134+
linters:
135+
- forbidigo
136+
137+
issues:
138+
# Show only new issues created after git revision `REV`.
139+
# Default: ""
140+
new-from-rev: 49d94f09c3382dcc9e5c0f61089109d54213b7c5
141+
# Maximum issues count per one linter.
142+
# Set to 0 to disable.
143+
# Default: 50
144+
max-issues-per-linter: 0
145+
# Maximum count of issues with the same text.
146+
# Set to 0 to disable.
147+
# Default: 3
148+
max-same-issues: 0
149+
# Make issues output unique by line.
150+
# Default: true
151+
uniq-by-line: false

Makefile

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
PKG := github.com/lightningnetwork/lightning-onion
2+
TOOLS_DIR := tools
3+
4+
GOBUILD := GO111MODULE=on go build -v
5+
GOINSTALL := GO111MODULE=on go install -v
6+
7+
GOFILES = $(shell find . -type f -name '*.go')
8+
9+
RM := rm -f
10+
CP := cp
11+
MAKE := make
12+
XARGS := xargs -L 1
13+
14+
# GO_VERSION is the Go version used for the release build, docker files, and
15+
# GitHub Actions. This is the reference version for the project. All other Go
16+
# versions are checked against this version.
17+
GO_VERSION = 1.24.6
18+
19+
# Linting uses a lot of memory, so keep it under control by limiting the number
20+
# of workers if requested.
21+
ifneq ($(workers),)
22+
LINT_WORKERS = --concurrency=$(workers)
23+
endif
24+
25+
DOCKER_TOOLS = docker run \
26+
--rm \
27+
-v $(shell bash -c "mkdir -p /tmp/go-build-cache; echo /tmp/go-build-cache"):/root/.cache/go-build \
28+
-v $$(pwd):/build lightning-onion-tools
29+
30+
GREEN := "\\033[0;32m"
31+
NC := "\\033[0m"
32+
define print
33+
echo $(GREEN)$1$(NC)
34+
endef
35+
36+
#? default: Run `make build`
37+
default: build
38+
39+
#? all: Run `make build` and `make check`
40+
all: build check
41+
42+
# ============
43+
# INSTALLATION
44+
# ============
45+
46+
#? build: Compile and build lightning-onion
47+
build:
48+
@$(call print, "Compiling lightning-onion.")
49+
$(GOBUILD) $(PKG)/...
50+
51+
#? sphinx-cli: Build the sphinx-cli binary
52+
sphinx-cli:
53+
@$(call print, "Building sphinx-cli.")
54+
$(GOBUILD) -o sphinx-cli ./cmd/main.go
55+
56+
# =======
57+
# TESTING
58+
# =======
59+
60+
#? check: Run `make unit`
61+
check: unit
62+
63+
#? unit: Run unit tests
64+
unit:
65+
@$(call print, "Running unit tests.")
66+
go test -v ./...
67+
68+
#? unit-cover: Run unit coverage tests
69+
unit-cover:
70+
@$(call print, "Running unit coverage tests.")
71+
go test -coverprofile=coverage.txt -covermode=atomic ./...
72+
73+
#? unit-race: Run unit race tests
74+
unit-race:
75+
@$(call print, "Running unit race tests.")
76+
env CGO_ENABLED=1 GORACE="history_size=7 halt_on_errors=1" go test -race ./...
77+
78+
# =========
79+
# UTILITIES
80+
# =========
81+
82+
#? fmt: Fix imports and format source code
83+
fmt: docker-tools
84+
@$(call print, "Fixing imports.")
85+
$(DOCKER_TOOLS) gosimports -w $(GOFILES)
86+
@$(call print, "Formatting source.")
87+
$(DOCKER_TOOLS) gofmt -l -w -s $(GOFILES)
88+
89+
#? check-go-version-yaml: Verify that the Go version is correct in all YAML files
90+
check-go-version-yaml:
91+
@$(call print, "Checking for target Go version (v$(GO_VERSION)) in YAML files (*.yaml, *.yml)")
92+
./scripts/check-go-version-yaml.sh $(GO_VERSION)
93+
94+
#? check-go-version-dockerfile: Verify that the Go version is correct in all Dockerfile files
95+
check-go-version-dockerfile:
96+
@$(call print, "Checking for target Go version (v$(GO_VERSION)) in Dockerfile files (*Dockerfile)")
97+
./scripts/check-go-version-dockerfile.sh $(GO_VERSION)
98+
99+
#? check-go-version: Verify that the Go version is correct in all project files
100+
check-go-version: check-go-version-dockerfile check-go-version-yaml
101+
102+
#? fmt-check: Make sure source code is formatted and imports are correct
103+
fmt-check: fmt
104+
@$(call print, "Checking fmt results.")
105+
if test -n "$$(git status --porcelain)"; then echo "code not formatted correctly, please run `make fmt` again!"; git status; git diff; exit 1; fi
106+
107+
#? lint-config-check: Verify golangci-lint configuration
108+
lint-config-check: docker-tools
109+
@$(call print, "Verifying golangci-lint configuration.")
110+
$(DOCKER_TOOLS) golangci-lint config verify -v
111+
112+
#? lint: Lint source and check errors
113+
lint: check-go-version lint-config-check
114+
@$(call print, "Linting source.")
115+
$(DOCKER_TOOLS) golangci-lint run -v $(LINT_WORKERS)
116+
117+
#? docker-tools: Build tools docker image
118+
docker-tools:
119+
@$(call print, "Building tools docker image.")
120+
docker build -q -t lightning-onion-tools -f $(TOOLS_DIR)/Dockerfile .
121+
122+
#? clean: Clean source
123+
clean:
124+
@$(call print, "Cleaning source.$(NC)")
125+
$(RM) coverage.txt
126+
127+
#? tidy-module: Run 'go mod tidy' for all modules
128+
tidy-module:
129+
@$(call print, "Running 'go mod tidy' for main module")
130+
go mod tidy
131+
@$(call print, "Running 'go mod tidy' for tools module")
132+
cd $(TOOLS_DIR) && go mod tidy
133+
134+
#? tidy-module-check: Run 'go mod tidy' for all modules and check results
135+
tidy-module-check: tidy-module
136+
if test -n "$$(git status --porcelain)"; then echo "modules not updated, please run `make tidy-module` again!"; git status; exit 1; fi
137+
138+
.PHONY: all \
139+
default \
140+
build \
141+
sphinx-cli \
142+
check \
143+
unit \
144+
unit-cover \
145+
unit-race \
146+
fmt \
147+
fmt-check \
148+
tidy-module \
149+
tidy-module-check \
150+
lint \
151+
lint-config-check \
152+
docker-tools \
153+
clean
154+
155+
#? help: Get more info on make commands
156+
help: Makefile
157+
@echo " Choose a command run in lightning-onion:"
158+
@sed -n 's/^#?//p' $< | column -t -s ':' | sort | sed -e 's/^/ /'
159+
160+
.PHONY: help

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ require (
2121
gopkg.in/yaml.v3 v3.0.1 // indirect
2222
)
2323

24-
go 1.21.4
24+
go 1.24.6

0 commit comments

Comments
 (0)