Skip to content

Commit 184e4b0

Browse files
authored
Upgrade to Go 1.21 and make general improvements (#52)
1 parent 3a5107a commit 184e4b0

File tree

10 files changed

+74
-67
lines changed

10 files changed

+74
-67
lines changed

.github/workflows/code-quality.yml renamed to .github/workflows/code-checks.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Code Quality
1+
name: Code Checks
22

33
on:
44
pull_request:
@@ -14,9 +14,22 @@ jobs:
1414
- name: Set up Go environment
1515
uses: actions/setup-go@v4
1616
with:
17-
go-version: 1.19
17+
go-version: 1.21
1818
- name: Run linters
1919
uses: golangci/golangci-lint-action@v3
2020
with:
2121
args: "--timeout=3m --verbose"
2222
version: v1.55.2
23+
unit-test:
24+
name: Unit Test
25+
runs-on: ubuntu-latest
26+
steps:
27+
- name: Check out repository
28+
uses: actions/checkout@v3
29+
- name: Set up Go environment
30+
uses: actions/setup-go@v4
31+
with:
32+
go-version: 1.21
33+
- name: Run unit tests
34+
run: |
35+
make test

.github/workflows/testing.yml

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

.golangci.yml

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,42 @@ run:
44
linters:
55
enable-all: true
66
disable:
7-
- deadcode # deprecated
8-
- exhaustive # not practical
9-
- exhaustivestruct # deprecated
10-
- exhaustruct # not practical
11-
- golint # deprecated
12-
- ifshort # deprecated
13-
- interfacer # deprecated
14-
- ireturn # good principle but occasional exceptions exist
15-
- maligned # deprecated
16-
- nolintlint # (*) disabled until 'leading space' clash with gofmt/gofumpt formatting is resolved
17-
- nonamedreturns # named returns are required to handle errors from deferred functions
18-
- nosnakecase # deprecated
19-
- scopelint # deprecated
20-
- structcheck # deprecated
21-
- varcheck # deprecated
22-
- varnamelen # disallows short variable names even if they are appropriate
23-
- wsl # too aggressive
24-
- depguard
7+
- cyclop # better judged on a case-by-case basis
8+
- depguard # we do not restrict package imports
9+
- exhaustruct # mandatory initialization of all struct fields is impractical
10+
- funlen # better judged on a case-by-case basis
11+
- gochecknoglobals # better judged on a case-by-case basis
12+
- gocognit # better judged on a case-by-case basis
13+
- goconst # better judged on a case-by-case basis
14+
- gomnd # better judged on a case-by-case basis
15+
- nestif # better judged on a case-by-case basis
16+
- nonamedreturns # named returns are required to handle errors from deferred functions
17+
- varnamelen # better judged on a case-by-case basis
18+
- wsl # too aggressive
19+
20+
# linters disabled because they are deprecated:
21+
- deadcode
22+
- exhaustivestruct
23+
- golint
24+
- ifshort
25+
- interfacer
26+
- maligned
27+
- nosnakecase
28+
- scopelint
29+
- structcheck
30+
- varcheck
2531

2632
linters-settings:
27-
funlen:
28-
lines: -1
2933
lll:
3034
tab-width: 4
35+
tagliatelle:
36+
case:
37+
rules:
38+
json: snake
3139

3240
issues:
3341
exclude-rules:
34-
- path: ^.*main\.go$
42+
- path: cmd/
3543
linters:
3644
- cyclop
3745
- forbidigo

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ TEST_SERVICE=test-generated-service
44

55
all: lint test build test-generated-service
66

7-
# install development tools
7+
# install the necessary development tools
88
PHONY+= install-tools
99
install-tools:
10-
@go install github.com/githubnemo/CompileDaemon@latest
10+
@go install github.com/4meepo/tagalign/cmd/tagalign@latest
1111
@go install github.com/daixiang0/gci@latest
12+
@go install github.com/golangci/golangci-lint/cmd/[email protected]
13+
@go install github.com/ypresto/CompileDaemon@bump-sys-fork # using fork until bug resolved: https://github.com/githubnemo/CompileDaemon/pull/76
1214
@go install golang.org/x/tools/cmd/goimports@latest
13-
@go install mvdan.cc/gofumpt@v0.3.0 # v0.3.1 fails to install on Go 1.16
15+
@go install mvdan.cc/gofumpt@latest
1416

1517
# run formatting utilities
1618
PHONY+= format
1719
format:
1820
@goimports -l ./
1921
@gci write ./
2022
@gofumpt -l -w ./
23+
@tagalign -fix -sort ./...
2124

2225
# run linters
2326
PHONY+= lint

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/puppetlabs/go-libs
22

3-
go 1.19
3+
go 1.21
44

55
require (
66
github.com/cnjack/throttle v0.0.0-20160727064406-525175b56e18

internal/log/logger.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const DefaultLevel = logrus.DebugLevel
1212

1313
// init runs when this library is first loaded.
1414
// This function sets the defaults that we want to use.
15-
// nolint: gochecknoinits // pragmatic usage of init function, not doing much harm here
15+
//
16+
//nolint:gochecknoinits
1617
func init() {
1718
logrus.SetFormatter(&logrus.TextFormatter{
1819
ForceColors: true,

pkg/concurrency/dispatcher.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ var errTimedOutWaitingToSubmitTask = errors.New("timed out waiting to submit tas
4040
// NewDispatcher create a new Dispatcher. The ID is used to identify the dispatcher in log messages.
4141
// workers is the number of go routines that this dispatcher will create to process work.
4242
// queueSize is the size of the channel used to store tasks.
43-
func NewDispatcher(id string, workers, queueSize int) Dispatcher {
43+
//
44+
//nolint:ireturn,nolintlint // the nolintlint linter generates a false positive here
45+
func NewDispatcher(id string, workers int, queueSize int) Dispatcher {
4446
return &dispatcher{
4547
ID: id,
4648
workers: workers,

pkg/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ var (
2222
errViperConfigNonPointerArgument = errors.New("reading Viper config requires a pointer argument")
2323
)
2424

25-
// nolint:cyclop // naturally complex, we may be able to simplify this function
2625
func setUpViperConfig(cfg interface{}, v *viper.Viper) error {
2726
// Get the type from the pointer or the struct itself - N.B. It will be a struct when called recursively.
2827
var t reflect.Type
2928
cfgType := reflect.TypeOf(cfg).Kind()
29+
30+
//nolint:exhaustive
3031
switch cfgType {
3132
case reflect.Ptr:
3233
t = reflect.TypeOf(cfg).Elem()

pkg/config/config_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ type StructMissingDefaultTags struct {
3838

3939
// StructEmptyDefaultTag holds one field with no default tag
4040
type StructEmptyDefaultTag struct {
41-
TestVal string `env:"TEST_VAL" default:""`
41+
TestVal string `default:"" env:"TEST_VAL"`
4242
}
4343

4444
// StructAllTagged holds multiple fields with tags.
4545
type StructAllTagged struct {
46-
TestVal string `env:"TEST_VAL" default:"xyz"`
47-
TestVal2 string `env:"TEST_VAL2" default:"abc"`
48-
TestInt int `env:"TEST_INT" default:"2"`
46+
TestVal string `default:"xyz" env:"TEST_VAL"`
47+
TestVal2 string `default:"abc" env:"TEST_VAL2"`
48+
TestInt int `default:"2" env:"TEST_INT"`
4949
}
5050

5151
// InnerStructError is the nested struct
@@ -55,41 +55,41 @@ type InnerStructError struct {
5555

5656
// OuterStructInnerError is the nested struct
5757
type OuterStructInnerError struct {
58-
TestNestOuter string `env:"TEST_NEST_OUTER" default:"outer"`
58+
TestNestOuter string `default:"outer" env:"TEST_NEST_OUTER"`
5959
InnerStructError
6060
}
6161

6262
// InnerStruct is the nested struct
6363
type InnerStruct struct {
64-
TestNestInner string `env:"TEST_NEST_INNER" default:"inner"`
64+
TestNestInner string `default:"inner" env:"TEST_NEST_INNER"`
6565
InnerMostStruct
6666
}
6767

6868
// InnerMostStruct is the nested struct
6969
type InnerMostStruct struct {
70-
TestNestInnerMost string `env:"TEST_NEST_INNER_MOST" default:"innermost"`
70+
TestNestInnerMost string `default:"innermost" env:"TEST_NEST_INNER_MOST"`
7171
}
7272

7373
// OuterStruct is the struct containing the nested struct
7474
type OuterStruct struct {
75-
TestNestOuter string `env:"TEST_NEST_OUTER" default:"outer"`
75+
TestNestOuter string `default:"outer" env:"TEST_NEST_OUTER"`
7676
InnerStruct
7777
}
7878

7979
// Database contains the postgres config.
8080
type Database struct {
81-
DBName string `env:"UT_DB_NAME" default:"nottest"`
82-
Host string `env:"UT_DB_HOST" default:"xyz"`
83-
Password string `env:"UT_DB_PASSWORD" default:"12345"`
84-
Port uint `env:"UT_DB_PORT" default:"5"`
85-
SSLMode string `env:"UT_DB_SSL_MODE" default:"disabled"`
86-
User string `env:"UT_DB_USER" default:"abc"`
81+
DBName string `default:"nottest" env:"UT_DB_NAME"`
82+
Host string `default:"xyz" env:"UT_DB_HOST"`
83+
Password string `default:"12345" env:"UT_DB_PASSWORD"`
84+
Port uint `default:"5" env:"UT_DB_PORT"`
85+
SSLMode string `default:"disabled" env:"UT_DB_SSL_MODE"`
86+
User string `default:"abc" env:"UT_DB_USER"`
8787
}
8888

8989
// WebServer contains webserver configuration.
9090
type WebServer struct {
91-
APIReadTimeout time.Duration `env:"UT_WS_API_READ_TIMEOUT" default:"60s"`
92-
APIWriteTimeout time.Duration `env:"UT_WS_API_WRITE_TIMEOUT" default:"60s"`
91+
APIReadTimeout time.Duration `default:"60s" env:"UT_WS_API_READ_TIMEOUT"`
92+
APIWriteTimeout time.Duration `default:"60s" env:"UT_WS_API_WRITE_TIMEOUT"`
9393
}
9494

9595
// AppConfig The configuration.

pkg/service/service.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ var (
9393
errRecoveredFromPanic = errors.New("recovered from panic")
9494
)
9595

96-
// nolint: gochecknoglobals // pragmatic use of global variable, not doing much harm here
9796
var routerMap = make(map[string]*gin.RouterGroup)
9897

9998
// Defining the readiness handler for potential use by k8s.

0 commit comments

Comments
 (0)