Skip to content

Commit 434a90d

Browse files
authored
ci: integrate golangci-lint with new workflow (#41)
* ci: integrate golangci-lint with new workflow - Renamed .github/workflows/go.yml to go-standards.yml - Integrated golangci-lint into the Go Standards workflow - Updated golangci-lint configuration to optimize for CI workflows - Added comments and detailed explanations to the workflow and configuration files - Utilized latest GitHub actions versions, including actions/upload-artifact@v4 This commit enhances code quality checks by incorporating comprehensive linting and ensuring compatibility with the latest GitHub actions. * fix: resolve linting errors in Clipper CLI tool and tests - Fixed unchecked errors in the Clipper CLI tool. - Corrected variable names to follow naming conventions. These changes ensure the code meets the quality standards set by GolangCI and improve code readability and maintainability. * fix: resolve golangci-lint errors in Clipper CLI tool and tests - Fixed issues reported by golangci-lint in the Clipper CLI tool. - Updated test files to address linting errors. - Enhanced code quality and ensured compliance with linting rules. This commit improves code quality by adhering to golangci-lint standards, enhancing maintainability and readability. * fix!: Removed depguard linter from golangci-lint configuration. * refact(ci): update Golangci output formats - Adjusted golangci-lint action configuration to generate checkstyle-formatted report. - Updated artifact upload step to reflect new report file format. * chore(gitignore): exclude *-report.yml files * ci: change the go setup version to stable - To ensure we always run on the latest stable version of Go we will use the stable option as review by @ccoVeile * ci(tests): improve CI integration with unit tests
1 parent 532c75d commit 434a90d

File tree

9 files changed

+393
-431
lines changed

9 files changed

+393
-431
lines changed

.github/workflows/go-standards.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# This workflow will build, test, and lint a Golang project.
2+
# For more information see:
3+
# - https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
4+
# - https://golangci-lint.run/usage/install/#github-action
5+
6+
name: Go Standards (Build, Test & Lint)
7+
8+
on:
9+
push:
10+
branches: ["main", "ci", "test"]
11+
pull_request:
12+
branches: ["main", "ci", "test"]
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
build-test-lint:
19+
# This job runs on the latest Ubuntu environment
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
# Step 1: Check out the repository
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
26+
27+
# Step 2: Set up Go environment
28+
- name: Set up Go
29+
uses: actions/setup-go@v5
30+
with:
31+
go-version: stable
32+
33+
# Step 3: Build the project
34+
- name: Build the project
35+
run: go build -v ./...
36+
37+
# Step 4: Test the project
38+
- name: Run tests
39+
run: go test -v ./...
40+
41+
# Step 5: Run golangci-lint
42+
- name: Run golangci-lint
43+
uses: golangci/golangci-lint-action@v6
44+
with:
45+
version: v1.59.1
46+
# Specify the output format for the linting report
47+
args: --out-format=checkstyle:golangci-lint-report.yml,github-actions
48+
49+
# Step 6: Upload golangci-lint report as an artifact
50+
- name: Upload golangci-lint report
51+
uses: actions/upload-artifact@v4
52+
with:
53+
name: golangci-lint-report
54+
path: golangci-lint-report.yml

.github/workflows/go.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ go.work
2222

2323
# Binary files
2424
bin/
25+
26+
# Report files
27+
*-report.yml

.golangci.yaml

Lines changed: 78 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,82 @@
1-
---
2-
# golangci-lint configuration file made by @ccoVeille
3-
# Source: https://github.com/ccoVeille/golangci-lint-config-examples/tree/main/03-safe
4-
# License: MIT
5-
#
6-
linters:
7-
# some linters are enabled by default
8-
# https://golangci-lint.run/usage/linters/
9-
#
10-
# enable some extra linters
11-
enable:
12-
# Errcheck is a program for checking for unchecked errors in Go code.
13-
- errcheck
14-
15-
# Linter for Go source code that specializes in simplifying code.
16-
- gosimple
17-
18-
# Vet examines Go source code and reports suspicious constructs.
19-
- govet
20-
21-
# Detects when assignments to existing variables are not used.
22-
- ineffassign
23-
24-
# It's a set of rules from staticcheck. See https://staticcheck.io/
25-
- staticcheck
26-
27-
# Fast, configurable, extensible, flexible, and beautiful linter for Go.
28-
# Drop-in replacement of golint.
29-
- revive
30-
31-
# check imports order and makes it always deterministic.
32-
- gci
33-
34-
# make sure to use t.Helper() when needed
35-
- thelper
36-
37-
# mirror suggests rewrites to avoid unnecessary []byte/string conversion
38-
- mirror
39-
40-
# detect the possibility to use variables/constants from the Go standard library.
41-
- usestdlibvars
42-
43-
# Finds commonly misspelled English words.
44-
- misspell
45-
46-
# Checks for duplicate words in the source code.
47-
- dupword
48-
49-
linters-settings:
50-
gci: # define the section orders for imports
51-
sections:
52-
# Standard section: captures all standard packages.
53-
- standard
54-
# Default section: catchall that is not standard or custom
55-
- default
56-
# linters that related to local tool, so they should be separated
57-
- localmodule
58-
59-
revive:
60-
rules:
61-
# these are the default revive rules
62-
# you can remove the whole "rules" node if you want
63-
# BUT
64-
# ! /!\ they all need to be present when you want to add more rules than the default ones
65-
# otherwise, you won't have the default rules, but only the ones you define in the "rules" node
66-
67-
# Blank import should be only in a main or test package, or have a comment justifying it.
68-
- name: blank-imports
69-
70-
# context.Context() should be the first parameter of a function when provided as argument.
71-
- name: context-as-argument
72-
73-
# Basic types should not be used as a key in `context.WithValue`
74-
- name: context-keys-type
75-
76-
# Importing with `.` makes the programs much harder to understand
77-
- name: dot-imports
1+
# This is a golangci-lint config file for Clipper CI workflows
2+
# For more details on configuring golangci-lint, visit: https://golangci-lint.run/usage/configuration/
3+
# Special thanks to @ccoVeille for the original configuration
784

79-
# Empty blocks make code less readable and could be a symptom of a bug or unfinished refactoring.
80-
- name: empty-block
5+
run:
6+
# Timeout for golangci-lint execution
7+
timeout: 5m
818

82-
# for better readability, variables of type `error` must be named with the prefix `err`.
83-
- name: error-naming
9+
# Additional parameters to ensure all files are checked and caching is used
10+
modules-download-mode: readonly
11+
allow-parallel-runners: true
8412

85-
# for better readability, the errors should be last in the list of returned values by a function.
86-
- name: error-return
87-
88-
# for better readability, error messages should not be capitalized or end with punctuation or a newline.
89-
- name: error-strings
90-
91-
# report when replacing `errors.New(fmt.Sprintf())` with `fmt.Errorf()` is possible
92-
- name: errorf
93-
94-
# incrementing an integer variable by 1 is recommended to be done using the `++` operator
95-
- name: increment-decrement
96-
97-
# highlights redundant else-blocks that can be eliminated from the code
98-
- name: indent-error-flow
99-
100-
# This rule suggests a shorter way of writing ranges that do not use the second value.
101-
- name: range
102-
103-
# receiver names in a method should reflect the struct name (p for Person, for example)
104-
- name: receiver-naming
105-
106-
# redefining built in names (true, false, append, make) can lead to bugs very difficult to detect.
107-
- name: redefines-builtin-id
108-
109-
# redundant else-blocks that can be eliminated from the code.
110-
- name: superfluous-else
111-
112-
# prevent confusing name for variables when using `time` package
113-
- name: time-naming
114-
115-
# warns when an exported function or method returns a value of an un-exported type.
116-
- name: unexported-return
117-
118-
# spots and proposes to remove unreachable code. also helps to spot errors
119-
- name: unreachable-code
120-
121-
# Functions or methods with unused parameters can be a symptom of an unfinished refactoring or a bug.
122-
- name: unused-parameter
123-
124-
# report when a variable declaration can be simplified
125-
- name: var-declaration
126-
127-
# warns when initialism, variable or package naming conventions are not followed.
128-
- name: var-naming
129-
130-
dupword:
131-
# Keywords used to ignore detection.
132-
# Default: []
133-
ignore:
134-
# - "blah" # this will accept "blah blah …" as a valid duplicate word
135-
136-
misspell:
137-
# Correct spellings using locale preferences for US or UK.
138-
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
139-
# Default ("") is to use a neutral variety of English.
140-
locale: US
141-
142-
# List of words to ignore
143-
# among the one defined in https://github.com/golangci/misspell/blob/master/words.go
144-
ignore-words:
145-
# - valor
146-
# - and
13+
linters:
14+
# Disable all linters initially
15+
disable-all: true
14716

148-
# Extra word corrections.
149-
extra-words:
150-
# - typo: "whattever"
151-
# correction: "whatever"
17+
# Enable specific linters with comments explaining each one
18+
enable:
19+
- errcheck # Checks for unchecked errors in Go code
20+
- thelper # Detects Go test helpers without t.Helper() call
21+
- unused # Checks for unused constants, variables, functions, and types
22+
- unparam # Reports unused function parameters
23+
- unconvert # Removes unnecessary type conversions
24+
- whitespace # Detects leading and trailing whitespace
25+
- ineffassign # Detects when assignments to existing variables are not used
26+
- asasalint # Checks for passing []any as any in variadic func(...any)
27+
- wastedassign # Finds wasted assignment statements
28+
- misspell # Finds commonly misspelled English words in comments
29+
- funlen # Detects long functions
30+
- bodyclose # Checks whether HTTP response body is closed successfully
31+
- reassign # Checks that package variables are not reassigned
32+
- tagliatelle # Checks struct tags
33+
- decorder # Checks declaration order and count of types, constants, variables, and functions
34+
- revive # Fast, configurable, extensible linter for Go. Drop-in replacement for golint
35+
- dupword # Checks for duplicate words in the source code
36+
- cyclop # Checks function and package cyclomatic complexity
37+
- gocognit # Computes and checks the cognitive complexity of functions
38+
- goconst # Finds repeated strings that could be replaced by a constant
39+
- gocritic # Provides diagnostics that check for bugs, performance, and style issues
40+
- godot # Checks if comments end in a period
41+
- godox # Detects FIXME, TODO, and other comment keywords
42+
- errname # Checks that sentinel errors are prefixed with `Err` and error types are suffixed with `Error`
43+
- varnamelen # Checks that the length of a variable's name matches its scope
44+
- testpackage # Ensures use of a separate _test package
45+
- gochecknoinits # Checks that no init functions are present in Go code
46+
47+
issues:
48+
# Use default exclusions
49+
exclude-use-default: true
50+
51+
# Set limits for reported issues
52+
max-issues-per-linter: 0
53+
max-same-issues: 0
54+
55+
severity:
56+
# Severity levels for issues
57+
error: true
58+
warning: true
59+
info: true
60+
ignore: false
61+
62+
# Performance optimization settings
63+
performance:
64+
golangci-lint:
65+
concurrency: 4
66+
max-same-issues: 3
67+
max-issues-per-linter: 10
68+
69+
# Caching settings to improve performance
70+
caches:
71+
enable: true
72+
cache-dir: /tmp/golangci-lint-cache
73+
74+
# CI-specific settings
75+
ci:
76+
# Enable fail-fast to stop on the first failure
77+
fail-fast: true
78+
79+
# Run golangci-lint as a pre-commit hook
80+
pre-commit:
81+
run: true
82+
mode: diff

cli/options/options.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ import (
1010
type Config struct {
1111
Text string
1212
FilePaths []string
13-
Html bool
13+
HTML bool
1414
Markdown bool
1515
MimeType bool
1616
LineNumbers bool
1717
ShouldFormat bool
1818
ShowVersion bool
1919
}
2020

21-
// Package-level variables for version information (set at build time or default)
21+
// Package-level variables for version information (set at build time or default).
2222
var (
23-
Version = "dev" // Default for development builds
24-
BuildMetadata = "git/source" // Default for development builds
23+
Version = "dev" // Default for development builds.
24+
BuildMetadata = "git/source" // Default for development builds.
2525
)
2626

27-
// GetVersion formats the version string for display
27+
// GetVersion formats the version string for display.
2828
func GetVersion() string {
2929
versionStr := strings.TrimSpace(Version)
3030

@@ -60,7 +60,7 @@ func ParseFlags() *Config {
6060
return &Config{
6161
Text: *text,
6262
FilePaths: flag.Args(),
63-
Html: *htmlWrap,
63+
HTML: *htmlWrap,
6464
Markdown: *markdownWrap,
6565
MimeType: *mimeType,
6666
LineNumbers: *lineNumbers,

0 commit comments

Comments
 (0)