Skip to content

Commit

Permalink
Merge pull request #127 from stmcginnis/golangci-lint
Browse files Browse the repository at this point in the history
Add golangci-lint linting
  • Loading branch information
stmcginnis authored Mar 20, 2021
2 parents 261f348 + fa052b8 commit f9a1a28
Show file tree
Hide file tree
Showing 78 changed files with 485 additions and 345 deletions.
19 changes: 13 additions & 6 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- main
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:

Expand All @@ -30,12 +31,18 @@ jobs:
- name: Run build
run: go build .

# Vet and lint
- name: Run vet & lint
run: |
go vet .
golint .
# Run unit tests
- name: Run tests
run: make test

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.38.0
args: --timeout=5m -v
118 changes: 118 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
linters-settings:
dupl:
threshold: 100
funlen:
lines: 100
statements: 50
goheader:
values:
const:
- LICENSE: BSD-3-Clause
template: |-
SPDX-License-Identifier: {{ LICENSE }}
goconst:
min-len: 2
min-occurrences: 5
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- deprecatedComment
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- whyNoLint
- wrapperFunc
gocyclo:
min-complexity: 25
goimports:
local-prefixes: github.com/stmcginnis
golint:
min-confidence: 0
gomnd:
settings:
mnd:
checks: argument,case,condition,return
govet:
check-shadowing: true
misspell:
locale: US
nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped

linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- funlen
- goconst
- gocritic
- gocyclo
- goheader
- goimports
- golint
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- noctx
- nolintlint
- rowserrcheck
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace

# don't enable:
# - asciicheck
# - exhaustive
# - gochecknoinits
# - gochecknoglobals
# - gocognit
# - godot
# - godox
# - goerr113
# - interfacer
# - lll
# - nestif
# - prealloc
# - testpackage
# - revive
# - scopelint
# - wsl

issues:
exclude:
- 'declaration of "(err|ctx)" shadows declaration at'
- 'G402: TLS InsecureSkipVerify may be true'
- '`(F|f)ibre` is a misspelling'
- '`(C|c)ancell.*` is a misspelling'
run:
skip-dirs:
- examples

# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.38.0 # use the fixed version to not introduce new linters unexpectedly
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

PKGS := $(shell go list ./... | grep -v example | grep -v tools)

all: build test
all: lint build test

test:
go test -v $(PKGS)

build:
go build

lint:
golangci-lint run -v

clean:
go clean
65 changes: 30 additions & 35 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type ClientConfig struct {
}

// setupClientWithConfig setups the client using the client config
func setupClientWithConfig(ctx context.Context, config ClientConfig) (c *APIClient, err error) {
func setupClientWithConfig(ctx context.Context, config *ClientConfig) (c *APIClient, err error) {
if !strings.HasPrefix(config.Endpoint, "http") {
return c, fmt.Errorf("endpoint must starts with http or https")
}
Expand Down Expand Up @@ -154,46 +154,43 @@ func setupClientWithEndpoint(ctx context.Context, endpoint string) (c *APIClient
}

// setupClientAuth setups the authentication in the client using the client config
func (c *APIClient) setupClientAuth(config ClientConfig) error {
func (c *APIClient) setupClientAuth(config *ClientConfig) error {
if config.Session != nil {
c.auth = &redfish.AuthToken{
Session: config.Session.ID,
Token: config.Session.Token,
}
} else {
if config.Username != "" {
var auth *redfish.AuthToken
if config.BasicAuth {
auth = &redfish.AuthToken{
Username: config.Username,
Password: config.Password,
BasicAuth: true,
}
} else {
var err error
auth, err = c.Service.CreateSession(config.Username, config.Password)
if err != nil {
return err
}
} else if config.Username != "" {
var auth *redfish.AuthToken
if config.BasicAuth {
auth = &redfish.AuthToken{
Username: config.Username,
Password: config.Password,
BasicAuth: true,
}
} else {
var err error
auth, err = c.Service.CreateSession(config.Username, config.Password)
if err != nil {
return err
}

c.auth = auth
}

c.auth = auth
}

return nil
}

// Connect creates a new client connection to a Redfish service.
func Connect(config ClientConfig) (c *APIClient, err error) {

client, err := setupClientWithConfig(context.Background(), config)
func Connect(config ClientConfig) (c *APIClient, err error) { // nolint:gocritic
client, err := setupClientWithConfig(context.Background(), &config)
if err != nil {
return c, err
}

// Authenticate with the service
err = client.setupClientAuth(config)
err = client.setupClientAuth(&config)
if err != nil {
return c, err
}
Expand All @@ -202,14 +199,14 @@ func Connect(config ClientConfig) (c *APIClient, err error) {
}

// ConnectContext is the same as Connect, but sets the ctx.
func ConnectContext(ctx context.Context, config ClientConfig) (c *APIClient, err error) {
client, err := setupClientWithConfig(ctx, config)
func ConnectContext(ctx context.Context, config ClientConfig) (c *APIClient, err error) { // nolint:gocritic
client, err := setupClientWithConfig(ctx, &config)
if err != nil {
return c, err
}

// Authenticate with the service
err = client.setupClientAuth(config)
err = client.setupClientAuth(&config)
if err != nil {
return c, err
}
Expand Down Expand Up @@ -243,7 +240,7 @@ func (c *APIClient) CloneWithSession() (*APIClient, error) {
return nil, fmt.Errorf("client already has a session")
}

newClient := APIClient(*c)
newClient := *c
newClient.HTTPClient = c.HTTPClient
service, err := ServiceRoot(&newClient)
if err != nil {
Expand Down Expand Up @@ -317,7 +314,7 @@ func (c *APIClient) Delete(url string) (*http.Response, error) {
}

// runRequest performs JSON REST calls
func (c *APIClient) runRequest(method string, url string, payload interface{}) (*http.Response, error) {
func (c *APIClient) runRequest(method, url string, payload interface{}) (*http.Response, error) {
if url == "" {
return nil, fmt.Errorf("unable to execute request, no target provided")
}
Expand All @@ -335,7 +332,7 @@ func (c *APIClient) runRequest(method string, url string, payload interface{}) (
}

// runRequestWithMultipartPayload performs REST calls with a multipart payload
func (c *APIClient) runRequestWithMultipartPayload(method string, url string, payload map[string]io.Reader) (*http.Response, error) {
func (c *APIClient) runRequestWithMultipartPayload(method, url string, payload map[string]io.Reader) (*http.Response, error) {
if url == "" {
return nil, fmt.Errorf("unable to execute request, no target provided")
}
Expand Down Expand Up @@ -366,7 +363,7 @@ func (c *APIClient) runRequestWithMultipartPayload(method string, url string, pa
}

// runRawRequest actually performs the REST calls.
func (c *APIClient) runRawRequest(method string, url string, payloadBuffer io.ReadSeeker, contentType string) (*http.Response, error) {
func (c *APIClient) runRawRequest(method, url string, payloadBuffer io.ReadSeeker, contentType string) (*http.Response, error) {
if url == "" {
return nil, common.ConstructError(0, []byte("unable to execute request, no target provided"))
}
Expand All @@ -391,11 +388,9 @@ func (c *APIClient) runRawRequest(method string, url string, payloadBuffer io.Re
if c.auth.Token != "" {
req.Header.Set("X-Auth-Token", c.auth.Token)
req.Header.Set("Cookie", fmt.Sprintf("sessionKey=%s", c.auth.Token))
} else {
if c.auth.BasicAuth == true && c.auth.Username != "" && c.auth.Password != "" {
encodedAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v:%v", c.auth.Username, c.auth.Password)))
req.Header.Set("Authorization", fmt.Sprintf("Basic %v", encodedAuth))
}
} else if c.auth.BasicAuth && c.auth.Username != "" && c.auth.Password != "" {
encodedAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v:%v", c.auth.Username, c.auth.Password)))
req.Header.Set("Authorization", fmt.Sprintf("Basic %v", encodedAuth))
}
}
req.Close = true
Expand Down
Loading

0 comments on commit f9a1a28

Please sign in to comment.