Skip to content

Commit 92d78a2

Browse files
Fix linter golint (#2240)
* feat(lint): add golint check and fix issues
1 parent 31846a5 commit 92d78a2

File tree

25 files changed

+60
-46
lines changed

25 files changed

+60
-46
lines changed

cmd/operator-sdk/add/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var (
3636
skipGeneration bool
3737
)
3838

39-
func newAddApiCmd() *cobra.Command {
39+
func newAddAPICmd() *cobra.Command {
4040
apiCmd := &cobra.Command{
4141
Use: "api",
4242
Short: "Adds a new api definition under pkg/apis",

cmd/operator-sdk/add/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func NewCmd() *cobra.Command {
2525
Long: "",
2626
}
2727

28-
cmd.AddCommand(newAddApiCmd())
28+
cmd.AddCommand(newAddAPICmd())
2929
cmd.AddCommand(newAddControllerCmd())
3030
cmd.AddCommand(newAddCRDCmd())
3131
return cmd

cmd/operator-sdk/new/cmd.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ import (
3535
"sigs.k8s.io/controller-runtime/pkg/client/config"
3636
)
3737

38-
func NewCmd() *cobra.Command {
38+
func NewCmd() *cobra.Command { //nolint:golint
39+
/*
40+
The nolint here is used to hide the warning
41+
"func name will be used as new.NewCmd by other packages,
42+
and that stutters; consider calling this Cmd"
43+
which is a false positive.
44+
*/
3945
newCmd := &cobra.Command{
4046
Use: "new <project-name>",
4147
Short: "Creates a new operator application",

cmd/operator-sdk/scorecard/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewCmd() *cobra.Command {
3232
Short: "Run scorecard tests",
3333
Long: `Runs blackbox scorecard tests on an operator
3434
`,
35-
RunE: scorecard.ScorecardTests,
35+
RunE: scorecard.Tests,
3636
}
3737

3838
scorecardCmd.Flags().String(scorecard.ConfigOpt, "", fmt.Sprintf("config file (default is '<project_dir>/%s'; the config file's extension and format can be .yaml, .json, or .toml)", scorecard.DefaultConfigFile))

hack/tests/check-lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ source ./hack/common.sh
66
DEV_LINTERS=(
77
##todo(camilamacedo86): The following checks requires fixes in the code.
88
##todo(camilamacedo86): they should be enabled and added in the CI
9-
"--enable=golint"
109
"--enable=gocyclo"
1110
"--enable=lll"
1211
"--enable=gosec" # NOT add this one to CI since was defined that it should be optional for now at least.
@@ -45,4 +44,5 @@ golangci-lint run --disable-all \
4544
--enable=errcheck \
4645
--enable=dupl \
4746
--enable=unparam \
47+
--enable=golint \
4848
${LINTERS[@]}

internal/flags/watch/flags.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ import (
2323

2424
// WatchFlags provides flag for configuration of a controller's reconcile period and for a
2525
// watches.yaml file, which is used to configure dynamic operators (e.g. Ansible and Helm).
26-
type WatchFlags struct {
26+
type WatchFlags struct { //nolint:golint
27+
/*
28+
The nolint is regards to: type name will be used as watch.WatchFlags by other packages, and that stutters; consider calling this Flags (golint)
29+
todo(camilamacedo86): Note that we decided to not introduce breakchanges to add the linters and it should be done after.
30+
From @joelanford: Even though watch.WatchFlags is an internal type, it is embedded in exported types, which means that changing it to watch.Flags is a breaking change.
31+
*/
32+
2733
ReconcilePeriod time.Duration
2834
WatchesFile string
2935
}

internal/scaffold/add_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestAddController(t *testing.T) {
24-
r, err := NewResource(appApiVersion, appKind)
24+
r, err := NewResource(appAPIVersion, appKind)
2525
if err != nil {
2626
t.Fatal(err)
2727
}

internal/scaffold/addtoscheme_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestAddToScheme(t *testing.T) {
24-
r, err := NewResource(appApiVersion, appKind)
24+
r, err := NewResource(appAPIVersion, appKind)
2525
if err != nil {
2626
t.Fatal(err)
2727
}

internal/scaffold/ansible/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ const (
2727
MoleculeTestLocalDir = MoleculeDir + filePathSep + "test-local"
2828
)
2929

30-
// Arrays can't be constants but this should be a constant
30+
// AnsibleDelims is a slice of two strings representing the left and right delimiters for ansible templates. Arrays can't be constants but this should be a constant.
3131
var AnsibleDelims = [2]string{"[[", "]]"}

internal/scaffold/controller_kind_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestControllerKind(t *testing.T) {
24-
r, err := NewResource(appApiVersion, appKind)
24+
r, err := NewResource(appAPIVersion, appKind)
2525
if err != nil {
2626
t.Fatal(err)
2727
}

internal/scaffold/cr_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestCR(t *testing.T) {
24-
r, err := NewResource(appApiVersion, appKind)
24+
r, err := NewResource(appAPIVersion, appKind)
2525
if err != nil {
2626
t.Fatal(err)
2727
}
@@ -38,7 +38,7 @@ func TestCR(t *testing.T) {
3838
}
3939

4040
func TestCRCustomSpec(t *testing.T) {
41-
r, err := NewResource(appApiVersion, appKind)
41+
r, err := NewResource(appAPIVersion, appKind)
4242
if err != nil {
4343
t.Fatal(err)
4444
}

internal/scaffold/crd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestCRDNonGoProjectDefault(t *testing.T) {
127127
s, buf := setupScaffoldAndWriter()
128128
s.Fs = afero.NewMemMapFs()
129129

130-
r, err := NewResource(appApiVersion, appKind)
130+
r, err := NewResource(appAPIVersion, appKind)
131131
if err != nil {
132132
t.Fatal(err)
133133
}
@@ -174,7 +174,7 @@ func TestCRDNonGoProjectCustom(t *testing.T) {
174174
s, buf := setupScaffoldAndWriter()
175175
s.Fs = afero.NewMemMapFs()
176176

177-
r, err := NewResource(appApiVersion, appKind)
177+
r, err := NewResource(appAPIVersion, appKind)
178178
if err != nil {
179179
t.Fatal(err)
180180
}

internal/scaffold/doc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestDoc(t *testing.T) {
24-
r, err := NewResource(appApiVersion, appKind)
24+
r, err := NewResource(appAPIVersion, appKind)
2525
if err != nil {
2626
t.Fatal(err)
2727
}

internal/scaffold/helm/chart_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestCreateChart(t *testing.T) {
4242
latestVersion = "1.2.3"
4343
previousVersion = "1.2.0"
4444
nonExistentVersion = "0.0.1"
45-
customApiVersion = "example.com/v1"
45+
customAPIVersion = "example.com/v1"
4646
customKind = "MyApp"
4747
customExpectName = "myapp"
4848
expectDerivedKind = "TestChart"
@@ -111,9 +111,9 @@ func TestCreateChart(t *testing.T) {
111111
},
112112
{
113113
name: "from scaffold with apiVersion and kind",
114-
apiVersion: customApiVersion,
114+
apiVersion: customAPIVersion,
115115
kind: customKind,
116-
expectResource: mustNewResource(t, customApiVersion, customKind),
116+
expectResource: mustNewResource(t, customAPIVersion, customKind),
117117
expectChartName: customExpectName,
118118
expectChartVersion: "0.1.0",
119119
},
@@ -148,8 +148,8 @@ func TestCreateChart(t *testing.T) {
148148
{
149149
name: "from repo and name implicit latest with apiVersion",
150150
helmChart: latest.repoAndName,
151-
apiVersion: customApiVersion,
152-
expectResource: mustNewResource(t, customApiVersion, expectDerivedKind),
151+
apiVersion: customAPIVersion,
152+
expectResource: mustNewResource(t, customAPIVersion, expectDerivedKind),
153153
expectChartName: chartName,
154154
expectChartVersion: latestVersion,
155155
},
@@ -164,9 +164,9 @@ func TestCreateChart(t *testing.T) {
164164
{
165165
name: "from repo and name implicit latest with apiVersion and kind",
166166
helmChart: latest.repoAndName,
167-
apiVersion: customApiVersion,
167+
apiVersion: customAPIVersion,
168168
kind: customKind,
169-
expectResource: mustNewResource(t, customApiVersion, customKind),
169+
expectResource: mustNewResource(t, customAPIVersion, customKind),
170170
expectChartName: chartName,
171171
expectChartVersion: latestVersion,
172172
},
@@ -301,10 +301,9 @@ func runTestCase(t *testing.T, testDir string, tc createChartTestCase) {
301301
if tc.expectErr {
302302
assert.Error(t, err)
303303
return
304-
} else {
305-
assert.NoError(t, err)
306304
}
307305

306+
assert.NoError(t, err)
308307
assert.Equal(t, tc.expectResource, resource)
309308
assert.Equal(t, tc.expectChartName, chart.GetMetadata().GetName())
310309
assert.Equal(t, tc.expectChartVersion, chart.GetMetadata().GetVersion())

internal/scaffold/olm-catalog/csv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,13 @@ func (s *CSV) updateCSVVersions(csv *olmapiv1alpha1.ClusterServiceVersion) error
326326

327327
// updateCSVFromManifestFiles gathers relevant data from generated and
328328
// user-defined manifests and updates csv.
329-
func (g *CSV) updateCSVFromManifests(cfg *CSVConfig, csv *olmapiv1alpha1.ClusterServiceVersion) (err error) {
329+
func (s *CSV) updateCSVFromManifests(cfg *CSVConfig, csv *olmapiv1alpha1.ClusterServiceVersion) (err error) {
330330
paths := append(cfg.CRDCRPaths, cfg.OperatorPath)
331331
paths = append(paths, cfg.RolePaths...)
332332
manifestGVKMap := map[schema.GroupVersionKind][][]byte{}
333333
crGVKSet := map[schema.GroupVersionKind]struct{}{}
334334
for _, path := range paths {
335-
info, err := g.getFS().Stat(path)
335+
info, err := s.getFS().Stat(path)
336336
if err != nil {
337337
return err
338338
}

internal/scaffold/register_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestRegister(t *testing.T) {
24-
r, err := NewResource(appApiVersion, appKind)
24+
r, err := NewResource(appAPIVersion, appKind)
2525
if err != nil {
2626
t.Fatal(err)
2727
}

internal/scaffold/test_setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const (
3030
// test constants describing an app operator project
3131
appProjectName = "app-operator"
3232
appRepo = "github.com/example-inc/" + appProjectName
33-
appApiVersion = "app.example.com/v1alpha1"
33+
appAPIVersion = "app.example.com/v1alpha1"
3434
appKind = "AppService"
3535
)
3636

internal/scaffold/types_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestTypes(t *testing.T) {
24-
r, err := NewResource(appApiVersion, appKind)
24+
r, err := NewResource(appAPIVersion, appKind)
2525
if err != nil {
2626
t.Fatal(err)
2727
}

internal/scorecard/scorecard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func getPlugins(version string, selector labels.Selector) ([]Plugin, error) {
105105
return plugins, nil
106106
}
107107

108-
func ScorecardTests(cmd *cobra.Command, args []string) error {
108+
func Tests(cmd *cobra.Command, args []string) error {
109109
if err := initConfig(); err != nil {
110110
return err
111111
}

pkg/ansible/paramconv/paramconv.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ func ToSnake(s string) string {
8383
}
8484
bits := []string{}
8585
n := ""
86-
real_i := -1
86+
iReal := -1
8787

8888
for i, v := range s {
89-
real_i += 1
89+
iReal++
9090
// treat acronyms as words, eg for JSONData -> JSON is a whole word
9191
nextCaseIsChanged := false
9292
if i+1 < len(s) {
@@ -96,22 +96,22 @@ func ToSnake(s string) string {
9696
}
9797
}
9898

99-
if real_i > 0 && n[len(n)-1] != '_' && nextCaseIsChanged {
99+
if iReal > 0 && n[len(n)-1] != '_' && nextCaseIsChanged {
100100
// add underscore if next letter case type is changed
101101
if v >= 'A' && v <= 'Z' {
102102
bits = append(bits, strings.ToLower(n))
103103
n = string(v)
104-
real_i = 0
104+
iReal = 0
105105
} else if v >= 'a' && v <= 'z' {
106106
bits = append(bits, strings.ToLower(n+string(v)))
107107
n = ""
108-
real_i = -1
108+
iReal = -1
109109
}
110110
} else if v == ' ' || v == '_' || v == '-' {
111111
// replace spaces/underscores with delimiters
112112
bits = append(bits, strings.ToLower(n))
113113
n = ""
114-
real_i = -1
114+
iReal = -1
115115
} else {
116116
n = n + string(v)
117117
}

pkg/ansible/runner/internal/inputdir/inputdir.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ func (i *InputDir) Write() error {
135135
// ANSIBLE_INVENTORY takes precedence over our generated hosts file
136136
// so if the envvar is set we don't bother making it, we just copy
137137
// the inventory into our runner directory
138-
ansible_inventory := os.Getenv("ANSIBLE_INVENTORY")
139-
if ansible_inventory == "" {
138+
ansibleInventory := os.Getenv("ANSIBLE_INVENTORY")
139+
if ansibleInventory == "" {
140140
// If ansible-runner is running in a python virtual environment, propagate
141141
// that to ansible.
142142
venv := os.Getenv("VIRTUAL_ENV")
@@ -151,18 +151,18 @@ func (i *InputDir) Write() error {
151151
return err
152152
}
153153
} else {
154-
fi, err := os.Stat(ansible_inventory)
154+
fi, err := os.Stat(ansibleInventory)
155155
if err != nil {
156156
return err
157157
}
158158
switch mode := fi.Mode(); {
159159
case mode.IsDir():
160-
err = i.copyInventory(ansible_inventory, filepath.Join(i.Path, "inventory"))
160+
err = i.copyInventory(ansibleInventory, filepath.Join(i.Path, "inventory"))
161161
if err != nil {
162162
return err
163163
}
164164
case mode.IsRegular():
165-
err = i.copyInventory(ansible_inventory, filepath.Join(i.Path, "inventory/hosts"))
165+
err = i.copyInventory(ansibleInventory, filepath.Join(i.Path, "inventory/hosts"))
166166
if err != nil {
167167
return err
168168
}

pkg/apis/scorecard/formatting.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
var _ ScorecardFormatter = &scapiv1alpha1.ScorecardOutput{}
2323
var _ ScorecardFormatter = &scapiv1alpha2.ScorecardOutput{}
2424

25-
type ScorecardFormatter interface {
25+
type ScorecardFormatter interface { //nolint:golint
26+
// todo(camilamacedo86): The no lint here is for pkg/apis/scorecard/formatting.go:25:6: type name will be used as scorecard.ScorecardFormatter by other packages, and that stutters; consider calling this Formatter (golint)
27+
// However, was decided to not move forward with it now in order to not introduce breakchanges with the task to add the linter. We should to do it after.
2628
MarshalText() (string, error)
2729
}

pkg/k8sutil/k8sutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func GetGVKsFromAddToScheme(addToSchemeFunc func(*runtime.Scheme) error) ([]sche
149149
}
150150
schemeAllKnownTypes := s.AllKnownTypes()
151151
ownGVKs := []schema.GroupVersionKind{}
152-
for gvk, _ := range schemeAllKnownTypes {
152+
for gvk := range schemeAllKnownTypes {
153153
if !isKubeMetaKind(gvk.Kind) {
154154
ownGVKs = append(ownGVKs, gvk)
155155
}

pkg/leader/leader.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ func Become(ctx context.Context, lockName string) error {
8181
log.Info("Found existing lock with my name. I was likely restarted.")
8282
log.Info("Continuing as the leader.")
8383
return nil
84-
} else {
85-
log.Info("Found existing lock", "LockOwner", existingOwner.Name)
8684
}
85+
log.Info("Found existing lock", "LockOwner", existingOwner.Name)
8786
}
8887
case apierrors.IsNotFound(err):
8988
log.Info("No pre-existing lock was found.")

pkg/test/context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525
"k8s.io/client-go/restmapper"
2626
)
2727

28-
type TestCtx struct {
28+
type TestCtx struct { //nolint:golint
29+
// todo(camilamacedo86): The no lint here is for type name will be used as test.TestCtx by other packages, and that stutters; consider calling this Ctx (golint)
30+
// However, was decided to not move forward with it now in order to not introduce breakchanges with the task to add the linter. We should to do it after.
2931
id string
3032
cleanupFns []cleanupFn
3133
namespace string

0 commit comments

Comments
 (0)