Skip to content

Commit

Permalink
[lint] Add golangci-lint automation + fix existing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Jonak committed Nov 22, 2023
1 parent 3de1d83 commit 5ca458c
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 185 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/golang-linter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: golangci-lint
on:
push:
branches:
- main
pull_request:

permissions:
contents: read
pull-requests: read

jobs:
lint:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- uses: actions/setup-go@v4
with:
go-version: '1.21'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.55
only-new-issues: true
env:
CGO_ENABLED: 0
15 changes: 15 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
linters:
enable:
- stylecheck
- gocritic
# - dupl
- durationcheck
# - goconst
- gofmt
- goimports
# - misspell
# - nestif

run:
skip-dirs:
- share
7 changes: 0 additions & 7 deletions global/global.go

This file was deleted.

18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ import (
)

func main() {
benchId := flag.String("bench-id", "", "The id of set of scripts to be run for compliance check")
benchID := flag.String("bench-id", "", "The id of set of scripts to be run for compliance check")
flag.String("NODE_TYPE", "", "Kubernetes node role master/worker")
flag.Parse()
config, err := scanner.LoadConfig()
if err != nil {
return
}
if *benchId == "" {
log.Error("Bench Id is required. Exiting.")
if *benchID == "" {
log.Error("bench-id is required. Exiting.")
return
}
script, found := config[*benchId]
script, found := config[*benchID]
if !found {
log.Error("BenchId not found. Exiting. ")
log.Error("bench-id not found. Exiting. ")
return
}
b := scanner.Bench{
Script: script,
}
b := scanner.Bench{Script: script}

b.RunScripts(context.Background())
if _, err := b.RunScripts(context.Background()); err != nil {
log.Errorf("RunScripts: %v", err)
}
}
2 changes: 1 addition & 1 deletion scanner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var configFile = getDfInstallDir() + "/usr/local/bin/compliance_check/config.jso

func LoadConfig() (map[string]Script, error) {
configFile, err := os.Open(configFile)
defer configFile.Close()
defer func() { _ = configFile.Close() }()
if err != nil {
log.Error("error in reading config json file:" + err.Error())
return nil, err
Expand Down
47 changes: 5 additions & 42 deletions scanner/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"
"text/template"

log "github.com/sirupsen/logrus"
)

type Bench struct {
Script Script
daemonOpts []string
childCmd *exec.Cmd
Script Script
childCmd *exec.Cmd
}

type DockerReplaceOpts struct {
Replace_docker_daemon_opts string
Replace_container_list string
ReplaceDockerDaemonOpts string
ReplaceContainerList string
}

type benchItem struct {
Expand Down Expand Up @@ -65,7 +62,7 @@ func (b *Bench) RunScripts(ctx context.Context) ([]benchItem, error) {
}
return nil, err
}
// global.SYS.AddToolProcess(pgid, 1, "host-bench", destPath)

err = cmd.Wait()
out := outb.Bytes()

Expand All @@ -91,40 +88,6 @@ func (b *Bench) RunScripts(ctx context.Context) ([]benchItem, error) {
return nil, nil
}

// replace the docker daemon config line, so that can run the script without pid=host
func (b *Bench) replaceTemplateVars(srcPath, dstPath string, containers []string) error {
dat, err := ioutil.ReadFile(srcPath)
if err != nil {
return err
}
f, err := os.Create(dstPath)
if err != nil {
return err
}
defer f.Close()

//containers only apply to container.sh, no effect to host.sh, because no <<<Containers>>> in it
var containerLines string
if len(containers) > 0 {
containerLines = "containers=\"\n" + strings.Join(containers, "\n") + "\"\n"
} else {
containerLines = "containers=\"\"\n"
}
r := DockerReplaceOpts{
Replace_docker_daemon_opts: strings.Join(b.daemonOpts, " "),
Replace_container_list: containerLines,
}
t := template.New("bench")
t.Delims("<<<", ">>>")
t.Parse(string(dat))

if err = t.Execute(f, r); err != nil {
log.WithFields(log.Fields{"error": err}).Error("Executing template error")
return err
}
return nil
}

func (b *Bench) getBenchMsg(out []byte) []benchItem {
list := make([]benchItem, 0)
scanner := bufio.NewScanner(strings.NewReader(string(out)))
Expand Down
68 changes: 0 additions & 68 deletions scanner/result_parser.go

This file was deleted.

Loading

0 comments on commit 5ca458c

Please sign in to comment.