Skip to content

Commit

Permalink
Fixup golangci-lint issues
Browse files Browse the repository at this point in the history
Fix or ignore various golangci-lint issues.

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ committed Jul 16, 2024
1 parent c2f34f0 commit e4d01ea
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 68 deletions.
21 changes: 9 additions & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,46 @@ linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- errcheck
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- golint
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
- asciicheck
- gochecknoglobals
- gocognit
- godot
- godox
- maligned
- prealloc
# don't enable:
# - testpackage
# - wsl
# - lll
# - gomnd
# - nestif
# - goerr113
# - goerr113
#
linters-settings:
depguard:
rules:
main:
deny:
- pkg: "golang.org/x/exp/slices"
desc: "Use 'slices' instead."
4 changes: 2 additions & 2 deletions cmd/promql-langserver/promql-langserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"os"
"time"

kitlog "github.com/go-kit/kit/log"
kitlog "github.com/go-kit/log"
"github.com/prometheus-community/promql-langserver/config"
promClient "github.com/prometheus-community/promql-langserver/prometheus"
"github.com/prometheus/common/route"
Expand Down Expand Up @@ -70,7 +70,7 @@ func main() {
// register the different route
api.Register(router, "")
// start the http server
err = http.ListenAndServe(fmt.Sprint(":", conf.RESTAPIPort), router)
err = http.ListenAndServe(fmt.Sprint(":", conf.RESTAPIPort), router) //nolint: gosec
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ package config

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"strconv"
Expand All @@ -38,7 +37,7 @@ func ReadConfig(configFile string) (*Config, error) {
}

func readConfigFromYAML(configFile string) (*Config, error) {
b, err := ioutil.ReadFile(configFile)
b, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}
Expand All @@ -63,7 +62,7 @@ const (
TextFormat LogFormat = "text"
)

var mapLogFormat = map[LogFormat]bool{ // nolint: gochecknoglobals
var mapLogFormat = map[LogFormat]bool{ //nolint: gochecknoglobals
JSONFormat: true,
TextFormat: true,
}
Expand Down
2 changes: 0 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ func TestUnmarshalENV(t *testing.T) {
},
}
for _, testSuite := range testSuites {
// nolint
t.Run(testSuite.title, func(t *testing.T) {
os.Clearenv()
// nolint
for k, v := range testSuite.variables {
os.Setenv(k, v)
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/blang/semver v3.5.1+incompatible
github.com/go-kit/kit v0.12.0
github.com/go-kit/log v0.2.1
github.com/google/uuid v1.3.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/pkg/errors v0.9.1
Expand All @@ -23,7 +23,6 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/grafana/regexp v0.0.0-20220304095617-2e8d9baf4ac2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dennwc/varint v1.0.0 h1:kGNFFSSw8ToIy3obO/kKr8U9GZYUAxQEVuix4zfDWzE=
github.com/dennwc/varint v1.0.0/go.mod h1:hnItb35rvZvJrbTALZtY/iQfDs48JKRG1RPpgziApxA=
github.com/go-kit/kit v0.12.0 h1:e4o3o3IsBfAKQh5Qbbiqyfu97Ku7jrO/JbohvztANh4=
github.com/go-kit/kit v0.12.0/go.mod h1:lHd+EkCZPIwYItmGDDRdhinkzX2A1sj+M9biaEaizzs=
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
Expand Down
2 changes: 1 addition & 1 deletion langserver/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/prometheus-community/promql-langserver/internal/vendored/go-tools/lsp/protocol"
)

func TestCache(t *testing.T) { // nolint:funlen
func TestCache(t *testing.T) { //nolint:funlen
c := &DocumentCache{}

c.Init()
Expand Down
2 changes: 1 addition & 1 deletion langserver/cache/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (d *DocumentHandle) scanYamlTreeRec(node *yaml.Node, nodeEnd token.Pos, lin

// foundRelevantYamlPath is called for YAML AST Nodes that are suspected to contain a
// PromQL query.
func (d *DocumentHandle) foundRelevantYamlPath(node *yaml.Node, nodeEnd token.Pos, lineOffset int) error { // nolint: gocognit
func (d *DocumentHandle) foundRelevantYamlPath(node *yaml.Node, nodeEnd token.Pos, lineOffset int) error { //nolint: gocognit
if node.Kind != yaml.MappingNode {
return nil
}
Expand Down
24 changes: 12 additions & 12 deletions langserver/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *server) completeMetricName(ctx context.Context, completions *[]protocol
item := protocol.CompletionItem{
Label: match.Str,
SortText: fmt.Sprintf("__3__%09d", match.Score),
Kind: 12, //Value
Kind: 12, // Value
Documentation: allMetadata[match.Str][0].Help,
Detail: string(allMetadata[match.Str][0].Type),
TextEdit: &protocol.TextEdit{
Expand All @@ -135,8 +135,8 @@ func (s *server) completeMetricName(ctx context.Context, completions *[]protocol
item := protocol.CompletionItem{
Label: match.Str,
SortText: fmt.Sprintf("__2__%09d", match.Score),
Kind: 3, //Value
InsertTextFormat: 2, //Snippet
Kind: 3, // Value
InsertTextFormat: 2, // Snippet
TextEdit: &protocol.TextEdit{
Range: editRange,
NewText: match.Str,
Expand All @@ -162,8 +162,8 @@ func (s *server) completeFunctionName(completions *[]protocol.CompletionItem, lo
item := protocol.CompletionItem{
Label: match.Str,
SortText: fmt.Sprintf("__1__%09d", match.Score),
Kind: 3, //Function
InsertTextFormat: 2, //Snippet
Kind: 3, // Function
InsertTextFormat: 2, // Snippet
TextEdit: &protocol.TextEdit{
Range: editRange,
NewText: match.Str + "($1)",
Expand All @@ -184,8 +184,8 @@ func (s *server) completeFunctionName(completions *[]protocol.CompletionItem, lo
item := protocol.CompletionItem{
Label: match.Str,
SortText: fmt.Sprintf("__1__%09d", match.Score),
Kind: 3, //Function
InsertTextFormat: 2, //Snippet
Kind: 3, // Function
InsertTextFormat: 2, // Snippet
Detail: aggregators[match.Str],
TextEdit: &protocol.TextEdit{
Range: editRange,
Expand All @@ -198,7 +198,7 @@ func (s *server) completeFunctionName(completions *[]protocol.CompletionItem, lo
return nil
}

var aggregators = map[string]string{ // nolint:gochecknoglobals
var aggregators = map[string]string{ //nolint:gochecknoglobals
"sum": "calculate sum over dimensions",
"max": "select maximum over dimensions",
"min": "select minimum over dimensions",
Expand Down Expand Up @@ -313,7 +313,7 @@ func (s *server) completeLabel(ctx context.Context, completions *[]protocol.Comp
}
allNames, err := s.metadataService.LabelNames(ctx, metricName)
if err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(s.lifetime, &protocol.LogMessageParams{
Type: protocol.Error,
Message: errors.Wrapf(err, "could not get label data from prometheus").Error(),
Expand All @@ -340,7 +340,7 @@ OUTER:

item := protocol.CompletionItem{
Label: match.Str,
Kind: 12, //Value
Kind: 12, // Value
TextEdit: &protocol.TextEdit{
Range: editRange,
NewText: match.Str,
Expand All @@ -356,7 +356,7 @@ OUTER:
func (s *server) completeLabelValue(ctx context.Context, completions *[]protocol.CompletionItem, location *cache.Location, labelName string) error {
labelValues, err := s.metadataService.LabelValues(ctx, labelName)
if err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(s.lifetime, &protocol.LogMessageParams{
Type: protocol.Error,
Message: errors.Wrapf(err, "could not get label value data from Prometheus").Error(),
Expand Down Expand Up @@ -415,7 +415,7 @@ func (s *server) completeLabelValue(ctx context.Context, completions *[]protocol

item := protocol.CompletionItem{
Label: quoted,
Kind: 12, //Value
Kind: 12, // Value
TextEdit: &protocol.TextEdit{
Range: editRange,
NewText: quoted,
Expand Down
16 changes: 8 additions & 8 deletions langserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *server) DidChangeConfiguration(ctx context.Context, params *protocol.Di
if params == nil {
return nil
}
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(
s.lifetime,
&protocol.LogMessageParams{
Expand All @@ -50,19 +50,19 @@ func (s *server) DidChangeConfiguration(ctx context.Context, params *protocol.Di
config := localLSPConfiguration{}

// Go doesn't provide an easy way to cast nested maps into structs.
//As a workaround, the configuration is first converted back to the JSON that was provided by the client and then marshalled back into the expected structure of the settings.
//If you are reading this code and are aware of a better solution to do this, feel free to submit a PR.
// As a workaround, the configuration is first converted back to the JSON that was provided by the client and then marshalled back into the expected structure of the settings.
// If you are reading this code and are aware of a better solution to do this, feel free to submit a PR.
rawData, marshallError := json.Marshal(params.Settings)
if marshallError != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(ctx, &protocol.LogMessageParams{
Type: protocol.Error,
Message: "unable to serialize the configuration",
})
return nil
}
if err := json.Unmarshal(rawData, &config); err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(ctx, &protocol.LogMessageParams{
Type: protocol.Error,
Message: errors.Wrap(err, "unexpected configuration format").Error(),
Expand All @@ -71,15 +71,15 @@ func (s *server) DidChangeConfiguration(ctx context.Context, params *protocol.Di
}

if err := s.setURLFromChangeConfiguration(config); err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(ctx, &protocol.LogMessageParams{
Type: protocol.Info,
Message: err.Error(),
})
}

if err := s.setMetadataLookbackInterval(config); err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(ctx, &protocol.LogMessageParams{
Type: protocol.Info,
Message: err.Error(),
Expand All @@ -100,7 +100,7 @@ func (s *server) setURLFromChangeConfiguration(settings localLSPConfiguration) e

func (s *server) connectPrometheus(url string) error {
if err := s.metadataService.ChangeDataSource(url); err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.ShowMessage(s.lifetime, &protocol.ShowMessageParams{
Type: protocol.Error,
Message: fmt.Sprintf("Failed to connect to Prometheus at %s:\n\n%s ", url, err.Error()),
Expand Down
2 changes: 1 addition & 1 deletion langserver/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *server) Definition(ctx context.Context, params *protocol.DefinitionPara

defs := []protocol.Location{}

switch n := location.Node.(type) { // nolint: gocritic
switch n := location.Node.(type) {
case *promql.VectorSelector:
queries, err := location.Doc.GetQueries()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions langserver/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ func (s *server) GetDiagnostics(uri protocol.DocumentURI) (*protocol.PublishDiag
func (s *server) diagnostics(uri protocol.DocumentURI) {
reply, err := s.GetDiagnostics(uri)
if err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(s.lifetime, &protocol.LogMessageParams{
Type: protocol.Error,
Message: err.Error(),
})
}

if err = s.client.PublishDiagnostics(s.lifetime, reply); err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(s.lifetime, &protocol.LogMessageParams{
Type: protocol.Error,
Message: errors.Wrapf(err, "failed to publish diagnostics").Error(),
Expand All @@ -74,7 +74,7 @@ func (s *server) clearDiagnostics(ctx context.Context, uri protocol.DocumentURI,
}

if err := s.client.PublishDiagnostics(ctx, diagnostics); err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(s.lifetime, &protocol.LogMessageParams{
Type: protocol.Error,
Message: errors.Wrapf(err, "failed to publish diagnostics").Error(),
Expand Down
4 changes: 2 additions & 2 deletions langserver/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func (s *server) Initialized(ctx context.Context, _ *protocol.InitializedParams)

if len(s.prometheusURL) > 0 {
if err := s.connectPrometheus(s.prometheusURL); err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(ctx, &protocol.LogMessageParams{
Type: protocol.Info,
Message: err.Error(),
})
}
} else {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(ctx, &protocol.LogMessageParams{
Type: protocol.Info,
Message: "No Prometheus",
Expand Down
4 changes: 2 additions & 2 deletions langserver/headlessClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"context"
"fmt"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/promql-langserver/internal/vendored/go-tools/lsp/protocol"
)

Expand Down
6 changes: 3 additions & 3 deletions langserver/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/rakyll/statik/fs"

"github.com/prometheus-community/promql-langserver/langserver/cache"
// Do not remove! Side effects of init() needed
// Do not remove! Side effects of init() needed.
_ "github.com/prometheus-community/promql-langserver/langserver/documentation/functions_statik"
)

Expand Down Expand Up @@ -106,7 +106,7 @@ func (s *server) nodeToDocMarkdown(ctx context.Context, location *cache.Location
if metric != "" {
doc, err := s.getRecordingRuleDocs(location.Doc, metric)
if err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(s.lifetime, &protocol.LogMessageParams{
Type: protocol.Error,
Message: errors.Wrapf(err, "failed to get recording rule data").Error(),
Expand All @@ -116,7 +116,7 @@ func (s *server) nodeToDocMarkdown(ctx context.Context, location *cache.Location
if doc == "" {
doc, err = s.getMetricDocs(ctx, metric)
if err != nil {
// nolint: errcheck
//nolint: errcheck
s.client.LogMessage(s.lifetime, &protocol.LogMessageParams{
Type: protocol.Error,
Message: errors.Wrapf(err, "failed to get metric data").Error(),
Expand Down
Loading

0 comments on commit e4d01ea

Please sign in to comment.