Skip to content

Commit

Permalink
adjust golangci-lint settings and fix lint issues
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Troshin <anton@diagrid.io>
  • Loading branch information
antontroshin committed Jan 8, 2025
1 parent 489f6d2 commit 4f03b8f
Show file tree
Hide file tree
Showing 34 changed files with 143 additions and 165 deletions.
40 changes: 19 additions & 21 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ run:
concurrency: 4

# timeout for analysis, e.g. 30s, 5m, default is 1m
deadline: 10m
timeout: 10m

# exit code when at least one issue was found, default is 1
issues-exit-code: 1
Expand All @@ -16,28 +16,22 @@ run:
#build-tags:
# - mytag

issues:
# which dirs to skip: they won't be analyzed;
# can use regexp here: generated.*, regexp is applied on full path;
# default value is empty list, but next dirs are always skipped independently
# from this option's value:
# third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs:
exclude-dirs:
- ^pkg.*client.*clientset.*versioned.*
- ^pkg.*client.*informers.*externalversions.*
- pkg.*mod.*k8s.io.*

# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
# no need to include all autogenerated files, we confidently recognize
# autogenerated files. If it's not please let us know.
skip-files: []
# - ".*\\.my\\.go$"
# - lib/bad.go

# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
format: tab
formats:
- format: tab

# print lines of code with issue, default is true
print-issued-lines: true
Expand Down Expand Up @@ -71,9 +65,6 @@ linters-settings:
statements: 40

govet:
# report about shadowed variables
check-shadowing: true

# settings per analyzer
settings:
printf: # analyzer name, run `go tool vet help` to see all analyzers
Expand All @@ -82,13 +73,18 @@ linters-settings:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
- github.com/dapr/cli/pkg/print.FailureStatusEvent
- github.com/dapr/cli/pkg/print.SuccessStatusEvent
- github.com/dapr/cli/pkg/print.WarningStatusEvent
- github.com/dapr/cli/pkg/print.InfoStatusEvent
- github.com/dapr/cli/pkg/print.StatusEvent
- github.com/dapr/cli/pkg/print.Spinner

# enable or disable analyzers by name
enable:
- atomicalign
enable-all: false
disable:
- shadow
enable-all: false
disable-all: false
revive:
# linting errors below this confidence will be ignored, default is 0.8
Expand All @@ -106,9 +102,6 @@ linters-settings:
gocognit:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 10
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
dupl:
# tokens count to trigger issue, 150 by default
threshold: 100
Expand Down Expand Up @@ -141,7 +134,7 @@ linters-settings:
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
# if it's called for subdir of a project it can't find funcs usages. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false
exported-fields-are-used: false
unparam:
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
Expand Down Expand Up @@ -216,6 +209,10 @@ linters-settings:
# Allow case blocks to end with a whitespace.
# Allow declarations (var) to be cuddled.
allow-cuddle-declarations: false
testifylint:
disable:
- require-error


linters:
fast: false
Expand Down Expand Up @@ -274,4 +271,5 @@ linters:
- exportloopref
- execinquery
- err113
- fatcontext
- fatcontext
- forbidigo
2 changes: 1 addition & 1 deletion cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ dapr dashboard -k -p 0
// url for dashboard after port forwarding.
webURL := fmt.Sprintf("http://%s", net.JoinHostPort(dashboardHost, fmt.Sprint(portForward.LocalPort))) //nolint: perfsprint

print.InfoStatusEvent(os.Stdout, fmt.Sprintf("Dapr dashboard found in namespace:\t%s", foundNamespace))
print.InfoStatusEvent(os.Stdout, "Dapr dashboard found in namespace:\t"+foundNamespace)
print.InfoStatusEvent(os.Stdout, fmt.Sprintf("Dapr dashboard available at:\t%s\n", webURL))

err = browser.OpenURL(webURL)
Expand Down
2 changes: 1 addition & 1 deletion cmd/mtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ dapr mtls export -o ./certs
}

dir, _ := filepath.Abs(exportPath)
print.SuccessStatusEvent(os.Stdout, fmt.Sprintf("Trust certs successfully exported to %s", dir))
print.SuccessStatusEvent(os.Stdout, "Trust certs successfully exported to "+dir)
},
PostRun: func(cmd *cobra.Command, args []string) {
kubernetes.CheckForCertExpiry()
Expand Down
6 changes: 3 additions & 3 deletions cmd/renew_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ dapr mtls renew-cert -k --valid-until <no of days> --restart
print.InfoStatusEvent(os.Stdout, "Using password file to generate root certificate")
err = kubernetes.RenewCertificate(kubernetes.RenewCertificateParams{
RootPrivateKeyFilePath: privateKey,
ValidUntil: time.Hour * time.Duration(validUntil*24),
ValidUntil: time.Hour * time.Duration(validUntil*24), //nolint:gosec
Timeout: timeout,
ImageVariant: imageVariant,
})
Expand All @@ -113,7 +113,7 @@ dapr mtls renew-cert -k --valid-until <no of days> --restart
} else {
print.InfoStatusEvent(os.Stdout, "generating fresh certificates")
err = kubernetes.RenewCertificate(kubernetes.RenewCertificateParams{
ValidUntil: time.Hour * time.Duration(validUntil*24),
ValidUntil: time.Hour * time.Duration(validUntil*24), //nolint:gosec
Timeout: timeout,
ImageVariant: imageVariant,
})
Expand All @@ -129,7 +129,7 @@ dapr mtls renew-cert -k --valid-until <no of days> --restart
logErrorAndExit(err)
}
print.SuccessStatusEvent(os.Stdout,
fmt.Sprintf("Certificate rotation is successful! Your new certicate is valid through %s", expiry.Format(time.RFC1123)))
"Certificate rotation is successful! Your new certicate is valid through "+expiry.Format(time.RFC1123))

if restartDaprServices {
restartControlPlaneService()
Expand Down
10 changes: 5 additions & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,14 @@ dapr run --run-file /path/to/directory -k

stdErrPipe, pipeErr := output.AppCMD.StderrPipe()
if pipeErr != nil {
print.FailureStatusEvent(os.Stderr, fmt.Sprintf("Error creating stderr for App: %s", err.Error()))
print.FailureStatusEvent(os.Stderr, "Error creating stderr for App: "+err.Error())
appRunning <- false
return
}

stdOutPipe, pipeErr := output.AppCMD.StdoutPipe()
if pipeErr != nil {
print.FailureStatusEvent(os.Stderr, fmt.Sprintf("Error creating stdout for App: %s", err.Error()))
print.FailureStatusEvent(os.Stderr, "Error creating stdout for App: "+err.Error())
appRunning <- false
return
}
Expand All @@ -335,13 +335,13 @@ dapr run --run-file /path/to/directory -k
outScanner := bufio.NewScanner(stdOutPipe)
go func() {
for errScanner.Scan() {
fmt.Println(print.Blue(fmt.Sprintf("== APP == %s", errScanner.Text())))
fmt.Println(print.Blue("== APP == " + errScanner.Text()))
}
}()

go func() {
for outScanner.Scan() {
fmt.Println(print.Blue(fmt.Sprintf("== APP == %s", outScanner.Text())))
fmt.Println(print.Blue("== APP == " + outScanner.Text()))
}
}()

Expand Down Expand Up @@ -395,7 +395,7 @@ dapr run --run-file /path/to/directory -k
}

appCommand := strings.Join(args, " ")
print.InfoStatusEvent(os.Stdout, fmt.Sprintf("Updating metadata for app command: %s", appCommand))
print.InfoStatusEvent(os.Stdout, "Updating metadata for app command: "+appCommand)
err = metadata.Put(output.DaprHTTPPort, "appCommand", appCommand, output.AppID, unixDomainSocket)
if err != nil {
print.WarningStatusEvent(os.Stdout, "Could not update sidecar metadata for appCommand: %s", err.Error())
Expand Down
3 changes: 1 addition & 2 deletions pkg/kubernetes/annotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ func TestAnnotate(t *testing.T) {
var out bytes.Buffer
in := []io.Reader{inputFile}
for i, annotation := range tt.annotations {
annotation := annotation
annotator := NewK8sAnnotator(K8sAnnotatorConfig{
TargetResource: &annotation.targetResource,
TargetNamespace: &annotation.targetNamespace,
Expand Down Expand Up @@ -334,7 +333,7 @@ func TestAnnotate(t *testing.T) {

for i := range expectedDocs {
if tt.printOutput {
t.Logf(outDocs[i])
t.Logf(outDocs[i]) //nolint:govet
}
assert.YAMLEq(t, expectedDocs[i], outDocs[i])
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubernetes/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package kubernetes

import (
"bytes"
"fmt"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestComponents(t *testing.T) {
err := writeComponents(&buff,
func() (*v1alpha1.ComponentList, error) {
if len(tc.errString) > 0 {
return nil, fmt.Errorf(tc.errString)
return nil, errors.New(tc.errString)
}

return &v1alpha1.ComponentList{Items: tc.k8sConfig}, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubernetes/configurations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package kubernetes

import (
"bytes"
"fmt"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestConfigurations(t *testing.T) {
err := writeConfigurations(&buff,
func() (*v1alpha1.ConfigurationList, error) {
if len(tc.errString) > 0 {
return nil, fmt.Errorf(tc.errString)
return nil, errors.New(tc.errString)
}

return &v1alpha1.ConfigurationList{Items: tc.k8sConfig}, nil
Expand Down
16 changes: 8 additions & 8 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ func daprChartValues(config InitConfiguration, version string) (map[string]inter
helmVals := []string{
fmt.Sprintf("global.ha.enabled=%t", config.EnableHA),
fmt.Sprintf("global.mtls.enabled=%t", config.EnableMTLS),
fmt.Sprintf("global.tag=%s", utils.GetVariantVersion(version, config.ImageVariant)),
"global.tag=" + utils.GetVariantVersion(version, config.ImageVariant),
}
if len(config.ImageRegistryURI) != 0 {
helmVals = append(helmVals, fmt.Sprintf("global.registry=%s", config.ImageRegistryURI))
helmVals = append(helmVals, "global.registry="+config.ImageRegistryURI)
}
helmVals = append(helmVals, config.Args...)

Expand All @@ -265,9 +265,9 @@ func daprChartValues(config InitConfiguration, version string) (map[string]inter
if err != nil {
return nil, err
}
helmVals = append(helmVals, fmt.Sprintf("dapr_sentry.tls.root.certPEM=%s", string(rootCertBytes)),
fmt.Sprintf("dapr_sentry.tls.issuer.certPEM=%s", string(issuerCertBytes)),
fmt.Sprintf("dapr_sentry.tls.issuer.keyPEM=%s", string(issuerKeyBytes)),
helmVals = append(helmVals, "dapr_sentry.tls.root.certPEM="+string(rootCertBytes),
"dapr_sentry.tls.issuer.certPEM="+string(issuerCertBytes),
"dapr_sentry.tls.issuer.keyPEM="+string(issuerKeyBytes),
)
}

Expand Down Expand Up @@ -301,7 +301,7 @@ func install(releaseName, releaseVersion, helmRepo string, config InitConfigurat
}

if releaseName == daprReleaseName {
err = applyCRDs(fmt.Sprintf("v%s", version))
err = applyCRDs("v" + version)
if err != nil {
return err
}
Expand All @@ -311,7 +311,7 @@ func install(releaseName, releaseVersion, helmRepo string, config InitConfigurat
installClient.ReleaseName = releaseName
installClient.Namespace = config.Namespace
installClient.Wait = config.Wait
installClient.Timeout = time.Duration(config.Timeout) * time.Second
installClient.Timeout = time.Duration(config.Timeout) * time.Second //nolint:gosec

values, err := daprChartValues(config, version)
if err != nil {
Expand Down Expand Up @@ -340,7 +340,7 @@ func installThirdParty(releaseName, chartName, releaseVersion, helmRepo string,
installClient.ReleaseName = releaseName
installClient.Namespace = thirdPartyDevNamespace
installClient.Wait = config.Wait
installClient.Timeout = time.Duration(config.Timeout) * time.Second
installClient.Timeout = time.Duration(config.Timeout) * time.Second //nolint:gosec

values := map[string]interface{}{}

Expand Down
8 changes: 4 additions & 4 deletions pkg/kubernetes/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func TestListPodsInterface(t *testing.T) {
output, err := ListPodsInterface(k8s, map[string]string{
"test": "test",
})
assert.Nil(t, err, "unexpected error")
assert.NoError(t, err, "unexpected error")
assert.NotNil(t, output, "Expected empty list")
assert.Equal(t, 0, len(output.Items), "Expected length 0")
assert.Empty(t, output.Items, "Expected length 0")
})
t.Run("one matching pod", func(t *testing.T) {
k8s := fake.NewSimpleClientset((&v1.Pod{
Expand All @@ -46,9 +46,9 @@ func TestListPodsInterface(t *testing.T) {
output, err := ListPodsInterface(k8s, map[string]string{
"test": "test",
})
assert.Nil(t, err, "unexpected error")
assert.NoError(t, err, "unexpected error")
assert.NotNil(t, output, "Expected non empty list")
assert.Equal(t, 1, len(output.Items), "Expected length 0")
assert.Len(t, output.Items, 1, "Expected length 0")
assert.Equal(t, "test", output.Items[0].Name, "expected name to match")
assert.Equal(t, "test", output.Items[0].Namespace, "expected namespace to match")
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubernetes/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
package kubernetes

import (
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -133,7 +134,7 @@ func (pf *PortForward) Init() error {
return fmt.Errorf("can not get the local and remote ports: %w", err)
}
if len(ports) == 0 {
return fmt.Errorf("can not get the local and remote ports: error getting ports length")
return errors.New("can not get the local and remote ports: error getting ports length")
}

pf.LocalPort = int(ports[0].Local)
Expand Down
10 changes: 5 additions & 5 deletions pkg/kubernetes/renew_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func renewCertificate(rootCert, issuerCert, issuerKey []byte, timeout uint, imag
// Reuse the existing helm configuration values i.e. tags, registry, etc.
upgradeClient.ReuseValues = true
upgradeClient.Wait = true
upgradeClient.Timeout = time.Duration(timeout) * time.Second
upgradeClient.Timeout = time.Duration(timeout) * time.Second //nolint:gosec
upgradeClient.Namespace = status[0].Namespace

// Override the helm configuration values with the new certificates.
Expand All @@ -146,12 +146,12 @@ func createHelmParamsForNewCertificates(ca, issuerCert, issuerKey string) (map[s
args := []string{}

if ca != "" && issuerCert != "" && issuerKey != "" {
args = append(args, fmt.Sprintf("dapr_sentry.tls.root.certPEM=%s", ca),
fmt.Sprintf("dapr_sentry.tls.issuer.certPEM=%s", issuerCert),
fmt.Sprintf("dapr_sentry.tls.issuer.keyPEM=%s", issuerKey),
args = append(args, "dapr_sentry.tls.root.certPEM="+ca,
"dapr_sentry.tls.issuer.certPEM="+issuerCert,
"dapr_sentry.tls.issuer.keyPEM="+issuerKey,
)
} else {
return nil, fmt.Errorf("parameters not found")
return nil, errors.New("parameters not found")
}

for _, v := range args {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubernetes/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func createDeploymentConfig(client versioned.Interface, app runfileconfig.App) d
if app.AppPort != 0 {
dep.Spec.Template.Spec.Containers[0].Ports = []corev1.ContainerPort{
{
ContainerPort: int32(app.AppPort),
ContainerPort: int32(app.AppPort), //nolint:gosec
},
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/kubernetes/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func NewStatusClient() (*StatusClient, error) {

// List status for Dapr resources.
func (s *StatusClient) Status() ([]StatusOutput, error) {
//nolint
client := s.client
if client == nil {
return nil, errors.New("kubernetes client not initialized")
Expand Down
Loading

0 comments on commit 4f03b8f

Please sign in to comment.