Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.

Commit 4c838e4

Browse files
Merge pull request #15 from NETWAYS/chore/update-deps
Update Dependencies and move to slog
2 parents 8eedda2 + fcf811c commit 4c838e4

File tree

18 files changed

+163
-123
lines changed

18 files changed

+163
-123
lines changed

.github/dependabot.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ updates:
66
interval: weekly
77
time: '10:00'
88
open-pull-requests-limit: 10
9+
10+
- package-ecosystem: "github-actions"
11+
directory: "/"
12+
schedule:
13+
interval: monthly

.github/workflows/go.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ jobs:
1717
fetch-depth: 0
1818

1919
- name: Set up Go
20-
uses: actions/setup-go@v3
20+
uses: actions/setup-go@v4
2121
with:
22-
go-version: 1.18
22+
go-version: 1.21
2323

2424
- name: Test
2525
run: go test -v ./...
@@ -29,7 +29,7 @@ jobs:
2929

3030
- name: Run goreleaser in release mode
3131
if: success() && startsWith(github.ref, 'refs/tags/v')
32-
uses: goreleaser/goreleaser-action@v3
32+
uses: goreleaser/goreleaser-action@v4
3333
with:
3434
version: latest
3535
args: release --rm-dist

.github/workflows/golangci-lint.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Set up Go
14-
uses: actions/setup-go@v3
14+
uses: actions/setup-go@v4
1515
with:
16-
go-version: 1.18
16+
go-version: 1.21
1717

1818
- uses: actions/checkout@v3
19-
20-
- name: golangci-lint
21-
uses: golangci/golangci-lint-action@v3
22-
with:
23-
version: v1.51

.golangci.yml

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
run:
22
timeout: 5m
3-
skip-files:
4-
- '(.+)_test\.go'
5-
3+
tests: false
64
linters:
7-
disable-all: false
8-
disable:
9-
- musttag
10-
presets:
11-
- bugs
12-
- unused
13-
fast: false
5+
enable-all: true
6+
disable:
7+
- cyclop
8+
- depguard
9+
- exhaustivestruct
10+
- exhaustruct
11+
- forbidigo
12+
- forcetypeassert
13+
- gci
14+
- gochecknoglobals
15+
- gochecknoinits
16+
- godox
17+
- goerr113
18+
- gofumpt
19+
- gomnd
20+
- ifshort
21+
- lll
22+
- musttag
23+
- nakedret
24+
- nlreturn
25+
- nolintlint
26+
- nonamedreturns
27+
- tagliatelle
28+
- varnamelen
29+
- wrapcheck
30+
linters-settings:
31+
estif:
32+
min-complexity: 4
33+
maligned:
34+
suggest-new: true
35+
funlen:
36+
lines: 80

api.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"log/slog"
1011
"net/http"
1112
"net/url"
1213
"time"
13-
14-
log "github.com/sirupsen/logrus"
1514
)
1615

1716
type RestAPI struct {
1817
URL string
1918
Client *http.Client
19+
Logger *slog.Logger
2020
}
2121

2222
func (a RestAPI) ExecuteCheck(command string, arguments map[string]interface{}, timeout uint32) (*APICheckResult, error) { //nolint:lll
@@ -31,14 +31,11 @@ func (a RestAPI) ExecuteCheck(command string, arguments map[string]interface{},
3131
defer cancel()
3232

3333
// Build request
34-
requestUrl := a.URL + "/v1/checker?command=" + url.QueryEscape(command)
34+
requestURL := a.URL + "/v1/checker?command=" + url.QueryEscape(command)
3535

36-
log.WithFields(log.Fields{
37-
"body": string(body),
38-
"url": requestUrl,
39-
}).Debug("sending request")
36+
a.Logger.Debug("sending request", "body", string(body), "url", requestURL)
4037

41-
req, err := http.NewRequestWithContext(ctx, "POST", requestUrl, bytes.NewReader(body))
38+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewReader(body))
4239
if err != nil {
4340
return nil, fmt.Errorf("could not build request: %w", err)
4441
}
@@ -53,6 +50,7 @@ func (a RestAPI) ExecuteCheck(command string, arguments map[string]interface{},
5350
if errors.Is(err, context.DeadlineExceeded) {
5451
return nil, fmt.Errorf("timeout during HTTP request: %w", err)
5552
}
53+
5654
return nil, fmt.Errorf("executing API request failed: %w", err)
5755
}
5856

@@ -64,9 +62,9 @@ func (a RestAPI) ExecuteCheck(command string, arguments map[string]interface{},
6462
return nil, fmt.Errorf("could not read result: %w", err)
6563
}
6664

67-
log.WithField("body", string(resultBody)).Debug("received response")
65+
a.Logger.Debug("received response", "body", string(resultBody))
6866

69-
if resp.StatusCode != 200 {
67+
if resp.StatusCode != http.StatusOK {
7068
return nil, fmt.Errorf("API request not successful code=%d: %s", resp.StatusCode, string(resultBody))
7169
}
7270

api_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"log/slog"
45
"net/http"
56
"net/http/httptest"
7+
"os"
68
"strings"
79
"testing"
810
"time"
@@ -23,7 +25,7 @@ func TestApiCmd(t *testing.T) {
2325
w.WriteHeader(http.StatusOK)
2426
w.Write([]byte(`{"Invoke-IcingaCheckFoo": {"exitcode": 0, "checkresult": "[OK] \"foo\"", "perfdata": ["'foo'=1.00%;;;0;100 "]}}`))
2527
})),
26-
api: RestAPI{},
28+
api: RestAPI{Logger: slog.New(slog.NewTextHandler(os.Stdout, nil))},
2729
expected: APICheckResult{
2830
ExitCode: 0,
2931
CheckResult: "[OK] \"foo\"",
@@ -58,7 +60,13 @@ func TestApiCmd(t *testing.T) {
5860
}
5961

6062
func TestApiTimeout(t *testing.T) {
61-
api := RestAPI{}
63+
opts := &slog.HandlerOptions{
64+
Level: slog.LevelDebug,
65+
}
66+
67+
logger := slog.New(slog.NewTextHandler(os.Stdout, opts))
68+
69+
api := RestAPI{Logger: logger}
6270

6371
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6472
// Wait for the context timeout to kick in

api_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (p *APIPerfdataList) UnmarshalJSON(data []byte) error {
4141
var value []string
4242

4343
// catch empty object and return empty []string
44-
if bytes.Compare(data, []byte("{}")) == 0 {
44+
if bytes.Equal(data, []byte("{}")) {
4545
value = []string{}
4646
} else {
4747
err := json.Unmarshal(data, &value)

api_types_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package main
22

33
import (
44
"encoding/json"
5-
"github.com/stretchr/testify/assert"
65
"testing"
6+
7+
"github.com/stretchr/testify/assert"
78
)
89

910
func TestAPIPerfdataList_UnmarshalJSON(t *testing.T) {

config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ const (
1616
)
1717

1818
type Config struct {
19-
Timeout uint32
2019
API string
2120
Command string
22-
Arguments map[string]interface{}
2321
CertName string
2422
CAFile string
23+
Arguments map[string]interface{}
24+
Timeout uint32
2525
Insecure bool
2626
Debug bool
2727
PrintVersion bool
2828
}
2929

3030
var (
31-
// ErrVersionRequested returned when --version flag is set
31+
// ErrVersionRequested returned when --version flag is set.
3232
ErrVersionRequested = errors.New("version was requested by a flag")
3333

3434
// ErrNoCommand is returned when no PowerShell command could be parsed from flags.

config_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package main
22

33
import (
4-
flag "github.com/spf13/pflag"
5-
"github.com/stretchr/testify/assert"
64
"os"
75
"testing"
6+
7+
flag "github.com/spf13/pflag"
8+
"github.com/stretchr/testify/assert"
89
)
910

1011
func quietTest() func() {

0 commit comments

Comments
 (0)