Skip to content

Commit

Permalink
Enables go-linter action for the repo
Browse files Browse the repository at this point in the history
This patch enables a new Github Action which does static code anaylsis
and a format check for each push and pull request. It also removes all
the existing linter errors in the codebase

Signed-off-by: Sagar Muchhal <[email protected]>
  • Loading branch information
srm09 committed Sep 17, 2020
1 parent e26c127 commit 5696adf
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .ci/prebuild/gofmt_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

func main() {
e := echo.New()
if !e.Empty(e.Run("gofmt -l .")) {
if !e.Empty(e.Run("gofmt -s -l .")) {
fmt.Println("Go code failed gofmt check:")
e.Runout("gofmt -d .")
e.Runout("gofmt -s -d .")
os.Exit(1)
}
}
13 changes: 11 additions & 2 deletions .github/workflows/compile-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ jobs:

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: test
run: |
sudo ufw allow 2200:2300/tcp
sudo ufw enable
sudo ufw status verbose
GO111MODULE=on go get sigs.k8s.io/[email protected]
GO111MODULE=on go test -timeout 600s -v -p 1 ./...
GO111MODULE=on go test -timeout 600s -v -p 1 ./...
- name: Run gofmt
run: GO111MODULE=on go run .ci/prebuild/gofmt_check.go

- name: Run linter
uses: golangci/golangci-lint-action@v2
with:
version: v1.29
only-new-issues: true
13 changes: 0 additions & 13 deletions k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,6 @@ func splitParamList(nses string) []string {
return strings.Split(nses, " ")
}

func strSliceContains(values []string, val string) bool {
if len(values) == 0 {
return false
}

for _, v := range values {
if strings.EqualFold(strings.TrimSpace(v), strings.TrimSpace(val)) {
return true
}
}
return false
}

func filterByNames(result SearchResult, names string) SearchResult {
if len(names) == 0 {
return result
Expand Down
9 changes: 2 additions & 7 deletions k8s/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ func _getPodContainers(podItem unstructured.Unstructured) ([]corev1.Container, e
return nil, fmt.Errorf("error converting container objects: %s", err)
}

for _, c := range pod.Spec.InitContainers {
containers = append(containers, c)
}

for _, c := range pod.Spec.Containers {
containers = append(containers, c)
}
containers = append(containers, pod.Spec.InitContainers...)
containers = append(containers, pod.Spec.Containers...)
containers = append(containers, _getPodEphemeralContainers(pod)...)
return containers, nil
}
Expand Down
2 changes: 1 addition & 1 deletion k8s/result_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (w *ResultWriter) GetResultDir() string {
}

func (w *ResultWriter) Write(searchResults []SearchResult) error {
if searchResults == nil || len(searchResults) == 0 {
if len(searchResults) == 0 {
return fmt.Errorf("cannot write empty (or nil) search result")
}

Expand Down
2 changes: 1 addition & 1 deletion k8s/search_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func contains(arr []string, item string) bool {
return false
}
for _, str := range arr {
if strings.ToLower(str) == strings.ToLower(item) {
if strings.EqualFold(str, item) {
return true
}
}
Expand Down
15 changes: 8 additions & 7 deletions k8s/search_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,26 @@ func convertToStruct(obj unstructured.Unstructured) starlark.Value {
return convertToStarlarkPrimitive(obj.Object)
}

// convertToStarlarkPrimitive returns a starlark value based on the Golang type passed
// as input to the function.
func convertToStarlarkPrimitive(input interface{}) starlark.Value {
var value starlark.Value
switch input.(type) {
switch val := input.(type) {
case string:
value = starlark.String(input.(string))
value = starlark.String(val)
case int, int32, int64:
value = starlark.MakeInt64(input.(int64))
value = starlark.MakeInt64(val.(int64))
case bool:
value = starlark.Bool(input.(bool))
value = starlark.Bool(val)
case []interface{}:
interfaceArr, _ := input.([]interface{})
var structs []starlark.Value
for _, i := range interfaceArr {
for _, i := range val {
structs = append(structs, convertToStarlarkPrimitive(i))
}
value = starlark.NewList(structs)
case map[string]interface{}:
dict := starlark.StringDict{}
for k, v := range input.(map[string]interface{}) {
for k, v := range val {
dict[k] = convertToStarlarkPrimitive(v)
}
value = starlarkstruct.FromStringDict(starlarkstruct.Default, dict)
Expand Down
3 changes: 1 addition & 2 deletions k8s/search_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package k8s

import (
"encoding/json"
"fmt"
"io/ioutil"

"go.starlark.net/starlark"
Expand Down Expand Up @@ -70,7 +69,7 @@ var _ = Describe("SearchResult", func() {
)

Context("For Namespaced", func() {
It(fmt.Sprintf("creates a dictionary with Namespaced value"), func() {
It("creates a dictionary with Namespaced value", func() {
dict := sr.ToStarlarkValue()
val, err := dict.Attr("Namespaced")
Expect(err).NotTo(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion ssh/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func CopyFrom(args SSHArgs, agent Agent, rootDir string, sourcePath string) erro
targetPath := filepath.Join(rootDir, sourcePath)
targetDir := filepath.Dir(targetPath)
pathDir, pathFile := filepath.Split(sourcePath)
if strings.Index(pathFile, "*") != -1 {
if strings.Contains(pathFile, "*") {
targetPath = filepath.Join(rootDir, pathDir)
targetDir = targetPath
}
Expand Down
2 changes: 1 addition & 1 deletion starlark/crashd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"go.starlark.net/starlarkstruct"
)

// addDefaultCrashdConf initalizes a Starlark Dict with default
// addDefaultCrashdConf initializes a Starlark Dict with default
// crashd_config configuration data
func addDefaultCrashdConf(thread *starlark.Thread) error {
args := []starlark.Tuple{
Expand Down
7 changes: 5 additions & 2 deletions starlark/govalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"reflect"

"github.com/pkg/errors"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
Expand All @@ -22,7 +23,7 @@ func NewGoValue(val interface{}) *GoValue {
return &GoValue{val: val}
}

// Value returns the orginal value as an interface{}
// Value returns the original value as an interface{}
func (v *GoValue) Value() interface{} {
return v.val
}
Expand Down Expand Up @@ -78,7 +79,9 @@ func (v *GoValue) ToDict() (*starlark.Dict, error) {
if err != nil {
return nil, fmt.Errorf("ToDict failed value conversion: %s", err)
}
dict.SetKey(key, val)
if err := dict.SetKey(key, val); err != nil {
return nil, errors.Wrapf(err, "failed to add key: %s", key)
}
}
default:
return nil, fmt.Errorf("ToDict does not support %T", v.val)
Expand Down
2 changes: 1 addition & 1 deletion starlark/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func resourcesFunc(thread *starlark.Thread, b *starlark.Builtin, args starlark.T
// info needed to execute commands.
func enum(provider *starlarkstruct.Struct) (*starlark.List, error) {
if provider == nil {
fmt.Errorf("missing provider")
return nil, fmt.Errorf("missing provider")
}

var resources []starlark.Value
Expand Down
2 changes: 1 addition & 1 deletion starlark/starlark_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func setupLocalDefaults(thread *starlark.Thread) error {

// newPredeclareds creates string dictionary containing the
// global built-ins values and functions available to the
// runing script.
// running script.
func newPredeclareds() starlark.StringDict {
return starlark.StringDict{
identifiers.os: setupOSStruct(),
Expand Down
1 change: 0 additions & 1 deletion testing/kindcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

var (
kindVersion = "v0.7.0"
clusterName = "test-crashd-cluster"
)

type KindCluster struct {
Expand Down
11 changes: 3 additions & 8 deletions testing/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package testing

import (
"errors"
"flag"
"fmt"
"math/rand"
Expand All @@ -16,13 +15,9 @@ import (
"github.com/sirupsen/logrus"
)

const charset = "abcdefghijklmnopqrstuvwxyz"

var (
InfraSetupWait = time.Second * 11
rnd = rand.New(rand.NewSource(time.Now().Unix()))
sshContainerName = "test-sshd"
sshPort = NextPortValue()
InfraSetupWait = time.Second * 11
rnd = rand.New(rand.NewSource(time.Now().Unix()))
)

type TestSupport struct {
Expand Down Expand Up @@ -240,7 +235,7 @@ func (t *TestSupport) TearDown() error {
}

if errs != nil {
return errors.New(fmt.Sprintf("%v", errs))
return fmt.Errorf("%v", errs)
}

return nil
Expand Down

0 comments on commit 5696adf

Please sign in to comment.