Skip to content

Commit 93ae8a9

Browse files
committed
fix staticcheck issues
add staticcheck CI action add staticcheck in Makefile Signed-off-by: roywang <[email protected]>
1 parent af61a81 commit 93ae8a9

File tree

43 files changed

+136
-115
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+136
-115
lines changed

.github/workflows/go.yml

+20-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ jobs:
114114
- name: Run e2e tests
115115
run: make e2e-test
116116

117+
staticcheck:
118+
runs-on: ubuntu-20.04
119+
120+
steps:
121+
- name: Checkout
122+
uses: actions/checkout@v2
123+
with:
124+
submodules: true
125+
126+
- name: Cache Go Dependencies
127+
uses: actions/cache@v2
128+
with:
129+
path: .work/pkg
130+
key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }}
131+
restore-keys: ${{ runner.os }}-pkg-
132+
133+
- name: Static Check
134+
run: go run honnef.co/go/tools/cmd/staticcheck -- ./...
135+
117136
lint:
118137
runs-on: ubuntu-20.04
119138

@@ -164,4 +183,4 @@ jobs:
164183
run: go install golang.org/x/tools/cmd/goimports && make fmt
165184

166185
- name: Check Diff
167-
run: make check-diff
186+
run: make check-diff

Makefile

+19-3
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ endif
3737
all: build
3838

3939
# Run tests
40-
test: vet lint
40+
test: vet lint staticcheck
4141
go test -race -coverprofile=coverage.txt -covermode=atomic ./pkg/... ./cmd/...
4242
@$(OK) unit-tests pass
4343

4444
# Build manager binary
45-
build: fmt vet lint
45+
build: fmt vet lint staticcheck
4646
go run hack/chart/generate.go
4747
go build -o bin/vela -ldflags ${LDFLAGS} cmd/vela/main.go
4848
git checkout cmd/vela/fake/chart_source.go
@@ -102,10 +102,13 @@ fmt: goimports installcue
102102
vet:
103103
go vet ./...
104104

105+
staticcheck: staticchecktool
106+
$(STATICCHECK) ./...
107+
105108
lint: golangci
106109
$(GOLANGCILINT) run ./...
107110

108-
reviewable: manifests fmt vet lint
111+
reviewable: manifests fmt vet lint staticcheck
109112
go mod tidy
110113

111114
# Execute auto-gen code commands and ensure branch is clean.
@@ -206,6 +209,19 @@ else
206209
GOLANGCILINT=$(shell which golangci-lint)
207210
endif
208211

212+
.PHONY: staticchecktool
213+
staticchecktool:
214+
ifeq (, $(shell which staticcheck))
215+
@{ \
216+
set -e ;\
217+
echo 'installing honnef.co/go/tools/cmd/staticcheck ' ;\
218+
GO111MODULE=off go get honnef.co/go/tools/cmd/staticcheck ;\
219+
}
220+
STATICCHECK=$(GOBIN)/staticcheck
221+
else
222+
STATICCHECK=$(shell which staticcheck)
223+
endif
224+
209225
.PHONY: goimports
210226
goimports:
211227
ifeq (, $(shell which goimports))

apis/standard.oam.dev/v1alpha1/autoscaler_types.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package v1alpha1
1818

1919
import (
2020
"github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
21-
runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
2221
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2322
)
2423

@@ -91,7 +90,7 @@ type AutoscalerSpec struct {
9190
TargetWorkload TargetWorkload `json:"targetWorkload,omitempty"`
9291

9392
// WorkloadReference marks the owner of the workload
94-
WorkloadReference runtimev1alpha1.TypedReference `json:"workloadRef,omitempty"`
93+
WorkloadReference v1alpha1.TypedReference `json:"workloadRef,omitempty"`
9594
}
9695

9796
// TargetWorkload holds the a reference to the scale target Object
@@ -105,7 +104,7 @@ type TargetWorkload struct {
105104

106105
// AutoscalerStatus defines the observed state of Autoscaler
107106
type AutoscalerStatus struct {
108-
runtimev1alpha1.ConditionedStatus `json:",inline"`
107+
v1alpha1.ConditionedStatus `json:",inline"`
109108
}
110109

111110
// +kubebuilder:object:root=true

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -2135,8 +2135,6 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ
21352135
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
21362136
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
21372137
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 h1:DvY3Zkh7KabQE/kfzMvYvKirSiguP9Q/veMtkYyf0o8=
2138-
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 h1:DvY3Zkh7KabQE/kfzMvYvKirSiguP9Q/veMtkYyf0o8=
2139-
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
21402138
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
21412139
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
21422140
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=

pkg/appfile/addon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func generateSecretFromTerraformOutput(k8sClient client.Client, outputList []str
125125
for _, i := range outputList {
126126
line := strings.Split(i, "=")
127127
if len(line) != 2 {
128-
return fmt.Errorf("Terraform output isn't in the right format")
128+
return fmt.Errorf("terraform output isn't in the right format")
129129
}
130130
k := strings.TrimSpace(line[0])
131131
v := strings.TrimSpace(line[1])

pkg/appfile/addon_suit_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestAppFile(t *testing.T) {
4545
}
4646

4747
var _ = BeforeSuite(func(done Done) {
48-
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
48+
logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter)))
4949
ctx := context.Background()
5050
By("bootstrapping test environment")
5151
useExistCluster := false

pkg/appfile/addon_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ var _ = Describe("Test generateSecretFromTerraformOutput", func() {
4646
It("invalid output list", func() {
4747
outputList := []string{"name"}
4848
err := generateSecretFromTerraformOutput(k8sClient, outputList, name, addonNamespace)
49-
Expect(err).Should(Equal(fmt.Errorf("Terraform output isn't in the right format")))
49+
Expect(err).Should(Equal(fmt.Errorf("terraform output isn't in the right format")))
5050
})
5151
})

pkg/appfile/api/appfile_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/stretchr/testify/assert"
1010
v12 "k8s.io/api/core/v1"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1312
"k8s.io/apimachinery/pkg/runtime"
1413

1514
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
@@ -51,10 +50,10 @@ func TestBuildOAMApplication2(t *testing.T) {
5150
},
5251
},
5352
expectApp: &v1alpha2.Application{
54-
TypeMeta: v1.TypeMeta{
53+
TypeMeta: metav1.TypeMeta{
5554
Kind: "Application",
5655
APIVersion: "core.oam.dev/v1alpha2",
57-
}, ObjectMeta: v1.ObjectMeta{
56+
}, ObjectMeta: metav1.ObjectMeta{
5857
Name: "test",
5958
},
6059
Spec: v1alpha2.ApplicationSpec{
@@ -85,10 +84,10 @@ func TestBuildOAMApplication2(t *testing.T) {
8584
},
8685
},
8786
expectApp: &v1alpha2.Application{
88-
TypeMeta: v1.TypeMeta{
87+
TypeMeta: metav1.TypeMeta{
8988
Kind: "Application",
9089
APIVersion: "core.oam.dev/v1alpha2",
91-
}, ObjectMeta: v1.ObjectMeta{
90+
}, ObjectMeta: metav1.ObjectMeta{
9291
Name: "test",
9392
},
9493
Spec: v1alpha2.ApplicationSpec{

pkg/appfile/driver/local.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func getApplicationDir(envName string) (string, error) {
110110
appDir := filepath.Join(env.GetEnvDirByName(envName), "applications")
111111
_, err := system.CreateIfNotExist(appDir)
112112
if err != nil {
113-
err = fmt.Errorf("Getting application directory from env %s failed, error: %w ", envName, err)
113+
err = fmt.Errorf("getting application directory from env %s failed, error: %w ", envName, err)
114114
}
115115
return appDir, err
116116
}

pkg/appfile/driver/local_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func init() {
2424
tm, _ = template.Load()
2525
afile = api.NewAppFile()
2626
afile.Name = appName
27-
svcs := make(map[string]api.Service, 0)
27+
svcs := make(map[string]api.Service)
2828
svcs["wordpress"] = map[string]interface{}{
2929
"type": "webservice",
3030
"image": "wordpress:php7.4-apache",

pkg/appfile/parser_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/stretchr/testify/assert"
3131
corev1 "k8s.io/api/core/v1"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33-
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3433
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3534
"k8s.io/apimachinery/pkg/runtime"
3635
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -372,10 +371,10 @@ var _ = Describe("Test appFile parser", func() {
372371
ac, components, err := NewApplicationParser(k8sClient, nil).GenerateApplicationConfiguration(TestApp, "default")
373372
Expect(err).To(BeNil())
374373
expectAppConfig := &v1alpha2.ApplicationConfiguration{
375-
TypeMeta: v1.TypeMeta{
374+
TypeMeta: metav1.TypeMeta{
376375
Kind: "ApplicationConfiguration",
377376
APIVersion: "core.oam.dev/v1alpha2",
378-
}, ObjectMeta: v1.ObjectMeta{
377+
}, ObjectMeta: metav1.ObjectMeta{
379378
Name: "test",
380379
Namespace: "default",
381380
Labels: map[string]string{"application.oam.dev": "test"},
@@ -417,10 +416,10 @@ var _ = Describe("Test appFile parser", func() {
417416
Expect(ac).To(BeEquivalentTo(expectAppConfig))
418417

419418
expectComponent := &v1alpha2.Component{
420-
TypeMeta: v1.TypeMeta{
419+
TypeMeta: metav1.TypeMeta{
421420
Kind: "Component",
422421
APIVersion: "core.oam.dev/v1alpha2",
423-
}, ObjectMeta: v1.ObjectMeta{
422+
}, ObjectMeta: metav1.ObjectMeta{
424423
Name: "myweb",
425424
Namespace: "default",
426425
Labels: map[string]string{"application.oam.dev": "test"},

pkg/builtin/registry/registry.go

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func newCallCtx(io util.IOStreams, data map[string]interface{}) CallCtx {
8484
}
8585
}
8686

87+
// Run executes tasks
8788
// Deprecated: Run is deprecated, you should use DoTasks is builtin package, it will automatically register all internal functions
8889
func Run(spec map[string]interface{}, io util.IOStreams) (map[string]interface{}, error) {
8990
var (

pkg/builtin/registry/registry_runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Meta struct {
2020
Err error
2121
}
2222

23-
// LookupRunner fetch the value of context by filed
23+
// Lookup fetches the value of context by filed
2424
func (m *Meta) Lookup(field string) cue.Value {
2525
f := m.Obj.Lookup(field)
2626
if !f.Exists() {

pkg/commands/exec.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/oam-dev/kubevela/pkg/appfile"
1919
"github.com/oam-dev/kubevela/pkg/appfile/api"
2020
"github.com/oam-dev/kubevela/pkg/commands/util"
21-
velacmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
2221
"github.com/oam-dev/kubevela/pkg/oam"
2322
)
2423

@@ -48,7 +47,7 @@ type VelaExecOptions struct {
4847
}
4948

5049
// NewExecCommand creates `exec` command
51-
func NewExecCommand(c types.Args, ioStreams velacmdutil.IOStreams) *cobra.Command {
50+
func NewExecCommand(c types.Args, ioStreams util.IOStreams) *cobra.Command {
5251
o := &VelaExecOptions{
5352
kcExecOptions: &cmdexec.ExecOptions{
5453
StreamOptions: cmdexec.StreamOptions{

pkg/commands/logs.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ import (
1919
"github.com/oam-dev/kubevela/pkg/appfile"
2020
"github.com/oam-dev/kubevela/pkg/appfile/api"
2121
"github.com/oam-dev/kubevela/pkg/commands/util"
22-
cmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
2322
)
2423

2524
// NewLogsCommand creates `logs` command to tail logs of application
26-
func NewLogsCommand(c types.Args, ioStreams cmdutil.IOStreams) *cobra.Command {
25+
func NewLogsCommand(c types.Args, ioStreams util.IOStreams) *cobra.Command {
2726
largs := &Args{C: c}
2827
cmd := &cobra.Command{}
2928
cmd.Use = "logs"
@@ -73,7 +72,7 @@ type Args struct {
7372
}
7473

7574
// Run refer to the implementation at https://github.com/oam-dev/stern/blob/master/stern/main.go
76-
func (l *Args) Run(ctx context.Context, ioStreams cmdutil.IOStreams) error {
75+
func (l *Args) Run(ctx context.Context, ioStreams util.IOStreams) error {
7776

7877
clientSet, err := kubernetes.NewForConfig(l.C.Config)
7978
if err != nil {

pkg/commands/portforward.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ import (
2727
"github.com/oam-dev/kubevela/pkg/appfile"
2828
"github.com/oam-dev/kubevela/pkg/appfile/api"
2929
"github.com/oam-dev/kubevela/pkg/commands/util"
30-
velacmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
3130
"github.com/oam-dev/kubevela/pkg/oam"
3231
)
3332

3433
// VelaPortForwardOptions for vela port-forward
3534
type VelaPortForwardOptions struct {
3635
Cmd *cobra.Command
3736
Args []string
38-
ioStreams velacmdutil.IOStreams
37+
ioStreams util.IOStreams
3938

4039
context.Context
4140
VelaC types.Args
@@ -50,7 +49,7 @@ type VelaPortForwardOptions struct {
5049
}
5150

5251
// NewPortForwardCommand is vela port-forward command
53-
func NewPortForwardCommand(c types.Args, ioStreams velacmdutil.IOStreams) *cobra.Command {
52+
func NewPortForwardCommand(c types.Args, ioStreams util.IOStreams) *cobra.Command {
5453
o := &VelaPortForwardOptions{
5554
ioStreams: ioStreams,
5655
kcPortForwardOptions: &cmdpf.PortForwardOptions{
@@ -268,7 +267,7 @@ func splitPort(port string) (local, remote string) {
268267
}
269268

270269
type defaultPortForwarder struct {
271-
velacmdutil.IOStreams
270+
util.IOStreams
272271
}
273272

274273
func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts cmdpf.PortForwardOptions) error {

pkg/commands/up_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77

88
"github.com/spf13/cobra"
99
"github.com/stretchr/testify/assert"
10-
"k8s.io/client-go/kubernetes/scheme"
11-
"sigs.k8s.io/controller-runtime/pkg/client/fake"
1210

1311
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
1412
"github.com/oam-dev/kubevela/apis/types"
@@ -17,17 +15,15 @@ import (
1715
)
1816

1917
func TestUp(t *testing.T) {
20-
client := fake.NewFakeClientWithScheme(scheme.Scheme)
2118
ioStream := util.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
2219
env := types.EnvMeta{
2320
Name: "up",
2421
Namespace: "env-up",
2522
Issuer: "up",
2623
}
2724
o := serverlib.AppfileOptions{
28-
Kubecli: client,
29-
IO: ioStream,
30-
Env: &env,
25+
IO: ioStream,
26+
Env: &env,
3127
}
3228
app := &v1alpha2.Application{}
3329
app.Name = "app-up"

pkg/commands/util/init.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55

66
corev1 "k8s.io/api/core/v1"
7-
v1 "k8s.io/api/core/v1"
87
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
98
"k8s.io/apimachinery/pkg/api/errors"
109
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -27,7 +26,7 @@ func DoesNamespaceExist(c client.Client, namespace string) (bool, error) {
2726

2827
// NewNamespace create namespace
2928
func NewNamespace(c client.Client, namespace string) error {
30-
ns := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace,
29+
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace,
3130
// marking a special label for prometheus monitoring.
3231
Labels: OAMLabel}}
3332
err := c.Create(context.Background(), ns)

pkg/commands/util/init_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212
"k8s.io/apimachinery/pkg/runtime"
1313
"sigs.k8s.io/controller-runtime/pkg/client"
14+
15+
//lint:ignore SA1019 We will use pkg/envtest before upgrading controller-runtime to v1.0.0
1416
"sigs.k8s.io/controller-runtime/pkg/client/fake"
1517
)
1618

0 commit comments

Comments
 (0)