Skip to content

Commit f81a051

Browse files
committed
fix staticcheck linter
Signed-off-by: Tim Ramlot <[email protected]>
1 parent 4e2402c commit f81a051

File tree

7 files changed

+30
-33
lines changed

7 files changed

+30
-33
lines changed

.golangci.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ linters:
1717
- nilerr
1818
- noctx
1919
- predeclared
20-
- staticcheck
2120
- unconvert
2221
- unparam
2322
- usestdlibvars

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func setFlagsFromEnv(prefix string, fs *pflag.FlagSet) {
6767
}
6868
// remove trailing _ to reduce common errors with the prefix, i.e. people setting it to MY_PROG_
6969
cleanPrefix := strings.TrimSuffix(prefix, "_")
70-
name := fmt.Sprintf("%s_%s", cleanPrefix, strings.Replace(strings.ToUpper(f.Name), "-", "_", -1))
70+
name := fmt.Sprintf("%s_%s", cleanPrefix, strings.ReplaceAll(strings.ToUpper(f.Name), "-", "_"))
7171
if e, ok := os.LookupEnv(name); ok {
7272
_ = f.Value.Set(e)
7373
}

pkg/agent/config.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func InitAgentCmdFlags(c *cobra.Command, cfg *AgentCmdFlags) {
247247
"private-key-path",
248248
"",
249249
"",
250-
fmt.Sprintf("To be used in conjunction with --client-id. The path to the private key file for the service account."),
250+
"To be used in conjunction with --client-id. The path to the private key file for the service account.",
251251
)
252252
c.PersistentFlags().BoolVarP(
253253
&cfg.OneShot,
@@ -531,8 +531,8 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
531531
// Validation of `venafi-cloud.upload_path`.
532532
{
533533
var uploadPath string
534-
switch {
535-
case res.TLSPKMode == VenafiCloudKeypair:
534+
switch res.TLSPKMode { // nolint:exhaustive
535+
case VenafiCloudKeypair:
536536
if cfg.VenafiCloud == nil || cfg.VenafiCloud.UploadPath == "" {
537537
errs = multierror.Append(errs, fmt.Errorf("the venafi-cloud.upload_path field is required when using the %s mode", res.TLSPKMode))
538538
break // Skip to the end of the switch statement.
@@ -544,7 +544,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
544544
}
545545

546546
uploadPath = cfg.VenafiCloud.UploadPath
547-
case res.TLSPKMode == VenafiCloudVenafiConnection:
547+
case VenafiCloudVenafiConnection:
548548
// The venafi-cloud.upload_path was initially meant to let users
549549
// configure HTTP proxies, but it has never been used since HTTP
550550
// proxies don't rewrite paths. Thus, we've disabled the ability to
@@ -577,18 +577,18 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
577577
if res.TLSPKMode != Off {
578578
var clusterID string
579579
var organizationID string // Only used by the old jetstack-secure mode.
580-
switch {
581-
case res.TLSPKMode == VenafiCloudKeypair:
580+
switch res.TLSPKMode { // nolint:exhaustive
581+
case VenafiCloudKeypair:
582582
if cfg.ClusterID == "" {
583583
errs = multierror.Append(errs, fmt.Errorf("cluster_id is required in %s mode", res.TLSPKMode))
584584
}
585585
clusterID = cfg.ClusterID
586-
case res.TLSPKMode == VenafiCloudVenafiConnection:
586+
case VenafiCloudVenafiConnection:
587587
if cfg.ClusterID == "" {
588588
errs = multierror.Append(errs, fmt.Errorf("cluster_id is required in %s mode", res.TLSPKMode))
589589
}
590590
clusterID = cfg.ClusterID
591-
case res.TLSPKMode == JetstackSecureOAuth || res.TLSPKMode == JetstackSecureAPIToken:
591+
case JetstackSecureOAuth, JetstackSecureAPIToken:
592592
if cfg.OrganizationID == "" {
593593
errs = multierror.Append(errs, fmt.Errorf("organization_id is required"))
594594
}
@@ -637,7 +637,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
637637
}
638638

639639
// Validation of --install-namespace.
640-
var installNS string = flags.InstallNS
640+
installNS := flags.InstallNS
641641
if flags.InstallNS == "" {
642642
var err error
643643
installNS, err = getInClusterNamespace()
@@ -650,7 +650,7 @@ func ValidateAndCombineConfig(log logr.Logger, cfg Config, flags AgentCmdFlags)
650650
// Validation of --venafi-connection and --venafi-connection-namespace.
651651
if res.TLSPKMode == VenafiCloudVenafiConnection {
652652
res.VenConnName = flags.VenConnName
653-
var venConnNS string = flags.VenConnNS
653+
venConnNS := flags.VenConnNS
654654
if flags.VenConnNS == "" {
655655
venConnNS = installNS
656656
}
@@ -714,8 +714,8 @@ func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClie
714714

715715
var preflightClient client.Client
716716
metadata := &api.AgentMetadata{Version: version.PreflightVersion, ClusterID: cfg.ClusterID}
717-
switch {
718-
case cfg.TLSPKMode == JetstackSecureOAuth:
717+
switch cfg.TLSPKMode {
718+
case JetstackSecureOAuth:
719719
// Note that there are no command line flags to configure the
720720
// JetstackSecureOAuth mode.
721721
credsBytes, err := readCredentialsFile(flagCredentialsPath)
@@ -734,7 +734,7 @@ func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClie
734734
if err != nil {
735735
errs = multierror.Append(errs, err)
736736
}
737-
case cfg.TLSPKMode == VenafiCloudKeypair:
737+
case VenafiCloudKeypair:
738738
var creds client.Credentials
739739

740740
if flagClientID != "" && flagCredentialsPath != "" {
@@ -777,7 +777,7 @@ func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClie
777777
if err != nil {
778778
errs = multierror.Append(errs, err)
779779
}
780-
case cfg.TLSPKMode == VenafiCloudVenafiConnection:
780+
case VenafiCloudVenafiConnection:
781781
var restCfg *rest.Config
782782
restCfg, err := kubeconfig.LoadRESTConfig("")
783783
if err != nil {
@@ -789,13 +789,13 @@ func validateCredsAndCreateClient(log logr.Logger, flagCredentialsPath, flagClie
789789
if err != nil {
790790
errs = multierror.Append(errs, err)
791791
}
792-
case cfg.TLSPKMode == JetstackSecureAPIToken:
792+
case JetstackSecureAPIToken:
793793
var err error
794794
preflightClient, err = client.NewAPITokenClient(metadata, flagAPIToken, cfg.Server)
795795
if err != nil {
796796
errs = multierror.Append(errs, err)
797797
}
798-
case cfg.TLSPKMode == Off:
798+
case Off:
799799
// No client needed in this mode.
800800
default:
801801
panic(fmt.Errorf("programmer mistake: auth mode not implemented: %s", cfg.TLSPKMode))

pkg/datagatherer/k8s/dynamic.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,27 @@ func (c *ConfigDynamic) UnmarshalYAML(unmarshal func(interface{}) error) error {
7676

7777
// validate validates the configuration.
7878
func (c *ConfigDynamic) validate() error {
79-
var errors []string
79+
var errs []string
8080
if len(c.ExcludeNamespaces) > 0 && len(c.IncludeNamespaces) > 0 {
81-
errors = append(errors, "cannot set excluded and included namespaces")
81+
errs = append(errs, "cannot set excluded and included namespaces")
8282
}
8383

8484
if c.GroupVersionResource.Resource == "" {
85-
errors = append(errors, "invalid configuration: GroupVersionResource.Resource cannot be empty")
85+
errs = append(errs, "invalid configuration: GroupVersionResource.Resource cannot be empty")
8686
}
8787

8888
for i, selectorString := range c.FieldSelectors {
8989
if selectorString == "" {
90-
errors = append(errors, fmt.Sprintf("invalid field selector %d: must not be empty", i))
90+
errs = append(errs, fmt.Sprintf("invalid field selector %d: must not be empty", i))
9191
}
9292
_, err := fields.ParseSelector(selectorString)
9393
if err != nil {
94-
errors = append(errors, fmt.Sprintf("invalid field selector %d: %s", i, err))
94+
errs = append(errs, fmt.Sprintf("invalid field selector %d: %s", i, err))
9595
}
9696
}
9797

98-
if len(errors) > 0 {
99-
return fmt.Errorf(strings.Join(errors, ", "))
98+
if len(errs) > 0 {
99+
return errors.New(strings.Join(errs, ", "))
100100
}
101101

102102
return nil

pkg/datagatherer/k8s/dynamic_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"k8s.io/client-go/dynamic/dynamicinformer"
2424
"k8s.io/client-go/dynamic/fake"
2525
"k8s.io/client-go/informers"
26-
"k8s.io/client-go/kubernetes"
2726
fakeclientset "k8s.io/client-go/kubernetes/fake"
2827
k8scache "k8s.io/client-go/tools/cache"
2928

@@ -968,8 +967,7 @@ func TestDynamicGathererNativeResources_Fetch(t *testing.T) {
968967
var wg sync.WaitGroup
969968
ctx := context.Background()
970969

971-
var clientset kubernetes.Interface
972-
clientset = fakeclientset.NewSimpleClientset(tc.addObjects...)
970+
clientset := fakeclientset.NewSimpleClientset(tc.addObjects...)
973971

974972
// init the datagatherer's informer with the client
975973
dg, err := tc.config.newDataGathererWithClient(ctx, nil, clientset)

pkg/internal/cyberark/identity/mock.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (mis *mockIdentityServer) handleStartAuthentication(w http.ResponseWriter,
115115

116116
if err := checkRequestHeaders(r); err != nil {
117117
w.WriteHeader(http.StatusForbidden)
118-
_, _ = w.Write([]byte(fmt.Sprintf(`{"message":"issues with headers sent to mock server: %s"}`, err.Error())))
118+
fmt.Fprintf(w, `{"message":"issues with headers sent to mock server: %s"}`, err.Error())
119119
return
120120
}
121121

@@ -126,7 +126,7 @@ func (mis *mockIdentityServer) handleStartAuthentication(w http.ResponseWriter,
126126

127127
if err := decoder.Decode(&reqBody); err != nil {
128128
w.WriteHeader(http.StatusInternalServerError)
129-
_, _ = w.Write([]byte(fmt.Sprintf(`{"message":"failed to unmarshal request body: %s"}`, err)))
129+
fmt.Fprintf(w, `{"message":"failed to unmarshal request body: %s"}`, err)
130130
return
131131
}
132132

@@ -172,7 +172,7 @@ func (mis *mockIdentityServer) handleAdvanceAuthentication(w http.ResponseWriter
172172

173173
if err := checkRequestHeaders(r); err != nil {
174174
w.WriteHeader(http.StatusForbidden)
175-
_, _ = w.Write([]byte(fmt.Sprintf(`{"message":"issues with headers sent to mock server: %s"}`, err.Error())))
175+
fmt.Fprintf(w, `{"message":"issues with headers sent to mock server: %s"}`, err.Error())
176176
}
177177

178178
decoder := json.NewDecoder(r.Body)
@@ -182,7 +182,7 @@ func (mis *mockIdentityServer) handleAdvanceAuthentication(w http.ResponseWriter
182182

183183
if err := decoder.Decode(&advanceBody); err != nil {
184184
w.WriteHeader(http.StatusInternalServerError)
185-
_, _ = w.Write([]byte(fmt.Sprintf(`{"message":"failed to unmarshal request body: %s"}`, err)))
185+
fmt.Fprintf(w, `{"message":"failed to unmarshal request body: %s"}`, err)
186186
return
187187
}
188188

pkg/logs/logs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ level=INFO msg="this is a happy log that should show as INFO" source=agent
410410

411411
for _, line := range strings.Split(given, "\n") {
412412
// Simulate the current agent's logs.
413-
logger.Printf(line)
413+
logger.Printf(line) // nolint:staticcheck
414414
}
415415

416416
assert.Equal(t, expect, gotBuf.String())

0 commit comments

Comments
 (0)