Skip to content

Commit 1013ae9

Browse files
authored
Enable more golangci-lint (#346)
Cleanup the code with additional linters and formatters. * Fixup Copyright lines. * Apply linter/formatter fixes. Signed-off-by: SuperQ <[email protected]>
1 parent a7e9fca commit 1013ae9

File tree

78 files changed

+377
-350
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+377
-350
lines changed

.golangci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ version: "2"
22
linters:
33
enable:
44
- misspell
5+
- modernize
6+
- revive
57
- sloglint
68
exclusions:
79
generated: lax
@@ -15,5 +17,20 @@ linters:
1517
- errcheck
1618
path: _test.go
1719
formatters:
20+
enable:
21+
- gci
22+
- gofumpt
23+
- goimports
1824
exclusions:
1925
generated: lax
26+
settings:
27+
gci:
28+
sections:
29+
- standard
30+
- default
31+
- prefix(github.com/prometheus-community/fortigate_exporter)
32+
gofumpt:
33+
extra-rules: true
34+
goimports:
35+
local-prefixes:
36+
- github.com/prometheus-community/fortigate_exporter

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 The Prometheus Authors
1+
# Copyright The Prometheus Authors
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at

fortigate_exporter.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2025 The Prometheus Authors
1+
// Copyright The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -37,13 +37,13 @@ import (
3737
"runtime/debug"
3838
"strings"
3939

40-
"github.com/prometheus-community/fortigate_exporter/pkg/probe"
41-
42-
"github.com/prometheus-community/fortigate_exporter/internal/config"
43-
fortiHTTP "github.com/prometheus-community/fortigate_exporter/pkg/http"
4440
"github.com/prometheus/client_golang/prometheus"
4541
"github.com/prometheus/client_golang/prometheus/promauto"
4642
"github.com/prometheus/client_golang/prometheus/promhttp"
43+
44+
"github.com/prometheus-community/fortigate_exporter/internal/config"
45+
fortiHTTP "github.com/prometheus-community/fortigate_exporter/pkg/http"
46+
"github.com/prometheus-community/fortigate_exporter/pkg/probe"
4747
)
4848

4949
var (
@@ -105,7 +105,7 @@ func main() {
105105
}
106106

107107
http.Handle("/metrics", promhttp.Handler())
108-
http.HandleFunc("/probe", probe.ProbeHandler)
108+
http.HandleFunc("/probe", probe.Handler)
109109
go func() {
110110
if err := http.ListenAndServe(savedConfig.Listen, nil); err != nil {
111111
log.Fatalf("Unable to serve: %v", err)

internal/config/main.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2025 The Prometheus Authors
1+
// Copyright The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -28,7 +28,7 @@ type FortiExporterParameter struct {
2828
ScrapeTimeout *int
2929
TLSTimeout *int
3030
TLSInsecure *bool
31-
TlsExtraCAs *string
31+
TLSExtraCAs *string
3232
MaxBGPPaths *int
3333
MaxVPNUsers *int
3434
}
@@ -39,16 +39,18 @@ type FortiExporterConfig struct {
3939
ScrapeTimeout int
4040
TLSTimeout int
4141
TLSInsecure bool
42-
TlsExtraCAs []LocalCert
42+
TLSExtraCAs []LocalCert
4343
MaxBGPPaths int
4444
MaxVPNUsers int
4545
}
4646

4747
type AuthKeys map[Target]TargetAuth
4848

49-
type Target string
50-
type Token string
51-
type ProbeList []string
49+
type (
50+
Target string
51+
Token string
52+
ProbeList []string
53+
)
5254

5355
type Probes struct {
5456
Include ProbeList
@@ -72,7 +74,7 @@ var (
7274
ScrapeTimeout: flag.Int("scrape-timeout", 30, "max seconds to allow a scrape to take"),
7375
TLSTimeout: flag.Int("https-timeout", 10, "TLS Handshake timeout in seconds"),
7476
TLSInsecure: flag.Bool("insecure", false, "Allow insecure certificates"),
75-
TlsExtraCAs: flag.String("extra-ca-certs", "", "comma-separated files containing extra PEMs to trust for TLS connections in addition to the system trust store"),
77+
TLSExtraCAs: flag.String("extra-ca-certs", "", "comma-separated files containing extra PEMs to trust for TLS connections in addition to the system trust store"),
7678
MaxBGPPaths: flag.Int("max-bgp-paths", 10000, "How many BGP Paths to receive when counting routes, needs to be greater than or equal to the number of routes or metrics will not be generated"),
7779
MaxVPNUsers: flag.Int("max-vpn-users", 0, "How many VPN Users to receive when counting users, needs to be greater than or equal the number of users or metrics will not be generated (0 eq. none by default)"),
7880
}
@@ -93,6 +95,7 @@ func MustReInit() {
9395
log.Fatalf("config.ReInit failed: %+v", err)
9496
}
9597
}
98+
9699
func ReInit() error {
97100
flag.Parse()
98101

@@ -120,7 +123,7 @@ func ReInit() error {
120123
log.Printf("Loaded %d API keys", len(savedConfig.AuthKeys))
121124

122125
// parse ExtraCAs
123-
for _, eca := range strings.Split(*parameter.TlsExtraCAs, ",") {
126+
for eca := range strings.SplitSeq(*parameter.TLSExtraCAs, ",") {
124127
if eca == "" {
125128
continue
126129
}
@@ -135,7 +138,7 @@ func ReInit() error {
135138
Path: eca,
136139
Content: certs,
137140
}
138-
savedConfig.TlsExtraCAs = append(savedConfig.TlsExtraCAs, certObject)
141+
savedConfig.TLSExtraCAs = append(savedConfig.TLSExtraCAs, certObject)
139142
}
140143

141144
return nil

internal/utils/files/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2025 The Prometheus Authors
1+
// Copyright The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at

internal/version/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2025 The Prometheus Authors
1+
// Copyright The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at

internal/version/main_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2025 The Prometheus Authors
1+
// Copyright The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -36,24 +36,24 @@ import (
3636

3737
func TestVersionParseOK(t *testing.T) {
3838
for _, tv := range []struct {
39-
v string
40-
maj int
41-
min int
42-
ok bool
39+
v string
40+
major int
41+
minor int
42+
ok bool
4343
}{
44-
{v: "v6.4.4", maj: 6, min: 4, ok: true},
44+
{v: "v6.4.4", major: 6, minor: 4, ok: true},
4545
{v: "1.0.0", ok: false},
4646
} {
4747
t.Run(tv.v, func(t *testing.T) {
48-
maj, min, ok := ParseVersion(tv.v)
48+
major, minor, ok := ParseVersion(tv.v)
4949
if !tv.ok {
5050
if ok {
5151
t.Errorf("Expected %q to fail to parse, succeeded", tv.v)
5252
}
5353
return
5454
}
55-
if maj != tv.maj || min != tv.min {
56-
t.Errorf("Expected %q to be (%d, %d), was (%d, %d)", tv.v, tv.maj, tv.min, maj, min)
55+
if major != tv.major || minor != tv.minor {
56+
t.Errorf("Expected %q to be (%d, %d), was (%d, %d)", tv.v, tv.major, tv.minor, major, minor)
5757
}
5858
})
5959
}

pkg/http/forti_token_client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2025 The Prometheus Authors
1+
// Copyright The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -41,13 +41,13 @@ import (
4141
"github.com/prometheus-community/fortigate_exporter/internal/config"
4242
)
4343

44-
type HTTPClient interface {
44+
type Client interface {
4545
Do(req *http.Request) (*http.Response, error)
4646
}
4747

4848
type fortiTokenClient struct {
4949
tgt url.URL
50-
hc HTTPClient
50+
hc Client
5151
ctx context.Context
5252
tok config.Token
5353
}
@@ -61,7 +61,7 @@ func (c *fortiTokenClient) newGetRequest(url string) (*http.Request, error) {
6161
return r, nil
6262
}
6363

64-
func (c *fortiTokenClient) Get(path string, query string, obj interface{}) error {
64+
func (c *fortiTokenClient) Get(path, query string, obj any) error {
6565
u := c.tgt
6666
u.Path = path
6767
u.RawQuery = query
@@ -91,6 +91,6 @@ func (c *fortiTokenClient) String() string {
9191
return c.tgt.String()
9292
}
9393

94-
func newFortiTokenClient(ctx context.Context, tgt url.URL, hc HTTPClient, token config.Token) (*fortiTokenClient, error) {
94+
func newFortiTokenClient(ctx context.Context, tgt url.URL, hc Client, token config.Token) (*fortiTokenClient, error) {
9595
return &fortiTokenClient{tgt, hc, ctx, token}, nil
9696
}

pkg/http/forti_token_client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2025 The Prometheus Authors
1+
// Copyright The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -40,12 +40,12 @@ import (
4040
"testing"
4141
)
4242

43-
type fakeHTTPClient struct {
43+
type fakeClient struct {
4444
status int
4545
body string
4646
}
4747

48-
func (c *fakeHTTPClient) Do(r *http.Request) (*http.Response, error) {
48+
func (c *fakeClient) Do(_ *http.Request) (*http.Response, error) {
4949
return &http.Response{
5050
Body: io.NopCloser(strings.NewReader(c.body)),
5151
StatusCode: c.status,
@@ -56,7 +56,7 @@ func newClient(sc int, b string) (*fortiTokenClient, error) {
5656
return newFortiTokenClient(
5757
context.Background(),
5858
url.URL{Scheme: "https", Host: "localhost"},
59-
&fakeHTTPClient{sc, b},
59+
&fakeClient{sc, b},
6060
"TEST-TOKEN",
6161
)
6262
}

pkg/http/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2025 The Prometheus Authors
1+
// Copyright The Prometheus Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -27,11 +27,10 @@ import (
2727
)
2828

2929
type FortiHTTP interface {
30-
Get(path string, query string, obj interface{}) error
30+
Get(path, query string, obj any) error
3131
}
3232

3333
func NewFortiClient(ctx context.Context, tgt url.URL, hc *http.Client, aConfig config.FortiExporterConfig) (FortiHTTP, error) {
34-
3534
auth, ok := aConfig.AuthKeys[config.Target(tgt.String())]
3635
if !ok {
3736
return nil, fmt.Errorf("no API authentication registered for %q", tgt.String())
@@ -56,8 +55,7 @@ func Configure(config config.FortiExporterConfig) error {
5655
log.Fatalf("Unable to fetch system CA store: %v", err)
5756
return err
5857
}
59-
for _, cert := range config.TlsExtraCAs {
60-
58+
for _, cert := range config.TLSExtraCAs {
6159
if ok := roots.AppendCertsFromPEM(cert.Content); !ok {
6260
return fmt.Errorf("failed to append certs from PEM %q, unknown error", cert.Path)
6361
}

0 commit comments

Comments
 (0)