Skip to content

Commit

Permalink
add a lint config and fix up linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorVin committed Feb 20, 2024
1 parent 05ee182 commit f77fb90
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ bomservice
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# editor swap files
*.swp
*.swo

# Dependency directories (remove the comment below to include it)
# vendor/
124 changes: 124 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.55.2 # use the fixed version to not introduce new linters unexpectedly

linters-settings:
govet:
auto-fix: true
check-shadowing: true
settings:
printf:
funcs:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
revive:
min-confidence: 0
gocyclo:
min-complexity: 15
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
depguard:
list-type: blacklist
packages:
# logging is allowed only by logutils.Log, logrus
# is allowed to use only in logutils package
- github.com/sirupsen/logrus
misspell:
locale: US
auto-fix: true
lll:
line-length: 140
goimports:
local-prefixes: github.com/golangci/golangci-lint
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- wrapperFunc
gofumpt:
extra-rules: true
wsl:
auto-fix: true

linters:
enable:
- errcheck
- gosimple
- govet
- gofmt
- gocyclo
- ineffassign
- stylecheck
- misspell
- staticcheck
- unused
- prealloc
- typecheck
# additional linters
- bodyclose
- gocritic
- goerr113
- goimports
- revive
- misspell
- noctx
- stylecheck
- gosec
enable-all: false
disable-all: true

run:
# build-tags:
skip-dirs:
- internal/fixtures
skip-files:
- "(.*/)*.*_test.go"

issues:
exclude-rules:
- linters:
- gosec
text: "weak cryptographic primitive"

- linters:
- stylecheck
text: "ST1016"

- linters:
- gocritic
text: "whyNoLint"

- linters:
- goerr113
text: "do not define dynamic errors"
exclude:
# Default excludes from `golangci-lint run --help` with EXC0002 removed
# EXC0001 errcheck: Almost all programs ignore errors on these functions and in most cases it's ok
- Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
# EXC0002 golint: Annoying issue about not having a comment. The rare codebase has such comments
# - (comment on exported (method|function|type|const)|should have( a package)? comment|comment should be of the form)
# EXC0003 golint: False positive when tests are defined in package 'test'
- func name will be used as test\.Test.* by other packages, and that stutters; consider calling this
# EXC0004 govet: Common false positives
- (possible misuse of unsafe.Pointer|should have signature)
# EXC0005 staticcheck: Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore
- ineffective break statement. Did you mean to break out of the outer loop
# EXC0006 gosec: Too many false-positives on 'unsafe' usage
- Use of unsafe calls should be audited
# EXC0007 gosec: Too many false-positives for parametrized shell calls
- Subprocess launch(ed with variable|ing should be audited)
# EXC0008 gosec: Duplicated errcheck checks
- (G104|G307)
# EXC0009 gosec: Too many issues in popular repos
- (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)
# EXC0010 gosec: False positive is triggered by 'src, err := ioutil.ReadFile(filename)'
- Potential file inclusion via variable
exclude-use-default: false
2 changes: 0 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package cmd

import (
"log"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -38,7 +37,6 @@ var rootCmd = &cobra.Command{
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
os.Exit(1)
}
}

Expand Down
11 changes: 7 additions & 4 deletions internal/parse/parse_xlsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
aocFieldName string = "MAC-AOC-ADDRESS"
bmcFieldName string = "MAC-ADDRESS"
ipmiFieldName string = "NUM-DEFIPMI"
//nolint:gosec // it's not a credential!
ipwdFieldName string = "NUM-DEFPWD"
)

Expand All @@ -34,6 +35,8 @@ func newCategoryColNum() *categoryColNum {
}

// ParseXlsxFile is the helper function to parse xlsx to boms.
//
//nolint:gocyclo,revive // this is inherently cyclomatic and yes, the name stutters
func ParseXlsxFile(fileBytes []byte) ([]sservice.Bom, error) {
file, err := xlsx.OpenBinary(fileBytes)
if err != nil {
Expand Down Expand Up @@ -70,7 +73,7 @@ func ParseXlsxFile(fileBytes []byte) ([]sservice.Bom, error) {
cells := row.Cells
serialNum := cells[categoryCol.serialNumCol].Value

if len(serialNum) == 0 {
if serialNum == "" {
return nil, errors.New("empty serial number")
}

Expand All @@ -83,7 +86,7 @@ func ParseXlsxFile(fileBytes []byte) ([]sservice.Bom, error) {
switch cells[categoryCol.subItemCol].Value {
case aocFieldName:
aocMacAddress := cells[categoryCol.subSerialCol].Value
if len(aocMacAddress) == 0 {
if aocMacAddress == "" {
return nil, errors.New("empty aoc mac address")
}

Expand All @@ -94,7 +97,7 @@ func ParseXlsxFile(fileBytes []byte) ([]sservice.Bom, error) {
bom.AocMacAddress += aocMacAddress
case bmcFieldName:
bmcMacAddress := cells[categoryCol.subSerialCol].Value
if len(bmcMacAddress) == 0 {
if bmcMacAddress == "" {
return nil, errors.New("empty bmc mac address")
}

Expand All @@ -111,7 +114,7 @@ func ParseXlsxFile(fileBytes []byte) ([]sservice.Bom, error) {
}
}

var retBoms []sservice.Bom
retBoms := make([]sservice.Bom, 0, len(bomsMap))
for _, bom := range bomsMap {
retBoms = append(retBoms, *bom)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/v1/client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func (e RequestError) Error() string {
}

// ClientError is returned when invalid arguments are provided to the client
//
//nolint:revive // yeah I know
type ClientError struct {
Message string
}
24 changes: 11 additions & 13 deletions pkg/api/v1/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,17 @@ func (r *Routes) Routes(g *gin.RouterGroup) {
}

bomService := g.Group("/bomservice")
{
bomService.POST("/upload-xlsx-file",
r.composeAuthHandler(createScopes("upload-xlsx-file")),
wrapAPICall(r.billOfMaterialsBatchUpload))

bomService.GET("/aoc-mac-address/:aoc_mac_address",
r.composeAuthHandler(readScopes("aoc-mac-address")),
wrapAPICall(r.getBomInfoByAOCMacAddr))

bomService.GET("/bmc-mac-address/:bmc_mac_address",
r.composeAuthHandler(readScopes("bmc-mac-address")),
wrapAPICall(r.getBomInfoByBMCMacAddr))
}
bomService.POST("/upload-xlsx-file",
r.composeAuthHandler(createScopes("upload-xlsx-file")),
wrapAPICall(r.billOfMaterialsBatchUpload))

bomService.GET("/aoc-mac-address/:aoc_mac_address",
r.composeAuthHandler(readScopes("aoc-mac-address")),
wrapAPICall(r.getBomInfoByAOCMacAddr))

bomService.GET("/bmc-mac-address/:bmc_mac_address",
r.composeAuthHandler(readScopes("bmc-mac-address")),
wrapAPICall(r.getBomInfoByBMCMacAddr))
}

func createScopes(items ...string) []string {
Expand Down

0 comments on commit f77fb90

Please sign in to comment.