Skip to content

Commit

Permalink
Implement stricter golangci-lint configurations (#259)
Browse files Browse the repository at this point in the history
* Fix whitespace and false positivies on duplicates

* Fix no context on http request

* Ignore long lines on function definitions

* Add golangci yaml configuration file

* Resolve gocritic issues

* Constantify the instance response

* Ignore false positives on duplicates
  • Loading branch information
optik-aper committed Jul 24, 2023
1 parent a19273a commit d679d8f
Show file tree
Hide file tree
Showing 36 changed files with 309 additions and 292 deletions.
135 changes: 135 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
linters-settings:
dupl:
threshold: 100
funlen:
lines: -1 # the number of lines (code + empty lines) is not a right metric and leads to code without empty line or one-liner.
statements: 50
goconst:
min-len: 2
min-occurrences: 3
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- whyNoLint
gocyclo:
min-complexity: 15
goimports:
local-prefixes: github.com/golangci/golangci-lint
gomnd:
# don't include the "operation" and "assign"
checks:
- argument
- case
- condition
- return
ignored-numbers:
- '0'
- '1'
- '2'
- '3'
ignored-functions:
- strings.SplitN

govet:
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
lll:
line-length: 140
misspell:
locale: US
nolintlint:
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
revive:
rules:
- name: unexported-return
disabled: true

linters:
disable-all: true
enable:
- dogsled
- dupl
- errcheck
- exportloopref
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- noctx
- nolintlint
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace

# don't enable:
# - bodyclose
# - depguard
# - asciicheck
# - scopelint
# - gochecknoglobals
# - gocognit
# - godot
# - godox
# - goerr113
# - interfacer
# - maligned
# - nestif
# - prealloc
# - testpackage
# - wsl

issues:
# List of regexps of issue texts to exclude.
#
# But independently of this option we use default exclude patterns,
# it can be disabled by `exclude-use-default: false`.
# To list all excluded by default patterns execute `golangci-lint run --help`
#
# Default: https://golangci-lint.run/usage/false-positives/#default-exclusions
exclude:
- abcdef
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gocyclo
- errcheck
- dupl
- gosec
- lll

run:
timeout: 5m
3 changes: 1 addition & 2 deletions account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ func TestAccountServiceHandler_Get(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/account", func(w http.ResponseWriter, r *http.Request) {

response := `
{
{
"account" : {
"balance": -5519.11,
"pending_charges": 57.03,
Expand Down
2 changes: 1 addition & 1 deletion application.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type applicationBase struct {
}

// List retrieves a list of available applications that can be launched when creating a Vultr instance
func (a *ApplicationServiceHandler) List(ctx context.Context, options *ListOptions) ([]Application, *Meta, *http.Response, error) {
func (a *ApplicationServiceHandler) List(ctx context.Context, options *ListOptions) ([]Application, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/applications"

req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
Expand Down
1 change: 0 additions & 1 deletion application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func TestApplicationServiceHandler_List(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/applications", func(w http.ResponseWriter, r *http.Request) {

response := `
{
"applications": [
Expand Down
2 changes: 1 addition & 1 deletion backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (b *BackupServiceHandler) Get(ctx context.Context, backupID string) (*Backu
}

// List retrieves a list of all backups on the current account
func (b *BackupServiceHandler) List(ctx context.Context, options *ListOptions) ([]Backup, *Meta, *http.Response, error) {
func (b *BackupServiceHandler) List(ctx context.Context, options *ListOptions) ([]Backup, *Meta, *http.Response, error) { //nolint:dupl
uri := "/v2/backups"
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)

Expand Down
5 changes: 1 addition & 4 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func TestBackupServiceHandler_List(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/backups", func(w http.ResponseWriter, r *http.Request) {

response := `
{
"backups": [
Expand Down Expand Up @@ -71,7 +70,6 @@ func TestBackupServiceHandler_ListEmpty(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/backups", func(w http.ResponseWriter, r *http.Request) {

response := `
{
"backups": [],
Expand Down Expand Up @@ -117,7 +115,6 @@ func TestBackupServiceHandler_Get(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/backups/543d34149403a", func(w http.ResponseWriter, r *http.Request) {

response := `
{
"backup": {
Expand Down Expand Up @@ -156,7 +153,7 @@ func TestBackupServiceHandler_GetEmpty(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/backups/543d34149403a", func(w http.ResponseWriter, r *http.Request) {
response := `
response := `
{
"backup": {}
}
Expand Down
10 changes: 5 additions & 5 deletions bare_metal_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ type bareMetalBase struct {
BareMetal *BareMetalServer `json:"bare_metal"`
}

// BMBareMetalBase ...
// BMBareMetalBase represents the base struct for a Bare Metal server
type BMBareMetalBase struct {
BareMetalBandwidth map[string]BareMetalServerBandwidth `json:"bandwidth"`
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (b *BareMetalServerServiceHandler) Get(ctx context.Context, serverID string
}

// Update a Bare Metal server
func (b *BareMetalServerServiceHandler) Update(ctx context.Context, serverID string, bmReq *BareMetalUpdate) (*BareMetalServer, *http.Response, error) {
func (b *BareMetalServerServiceHandler) Update(ctx context.Context, serverID string, bmReq *BareMetalUpdate) (*BareMetalServer, *http.Response, error) { //nolint:lll
uri := fmt.Sprintf("%s/%s", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPatch, uri, bmReq)
if err != nil {
Expand Down Expand Up @@ -198,7 +198,7 @@ func (b *BareMetalServerServiceHandler) Delete(ctx context.Context, serverID str
}

// List all Bare Metal instances in your account.
func (b *BareMetalServerServiceHandler) List(ctx context.Context, options *ListOptions) ([]BareMetalServer, *Meta, *http.Response, error) {
func (b *BareMetalServerServiceHandler) List(ctx context.Context, options *ListOptions) ([]BareMetalServer, *Meta, *http.Response, error) { //nolint:dupl,lll
req, err := b.client.NewRequest(ctx, http.MethodGet, bmPath, nil)
if err != nil {
return nil, nil, nil, err
Expand Down Expand Up @@ -273,7 +273,7 @@ func (b *BareMetalServerServiceHandler) GetVNCUrl(ctx context.Context, serverID

// ListIPv4s information of a Bare Metal server.
// IP information is only available for Bare Metal servers in the "active" state.
func (b *BareMetalServerServiceHandler) ListIPv4s(ctx context.Context, serverID string, options *ListOptions) ([]IPv4, *Meta, *http.Response, error) {
func (b *BareMetalServerServiceHandler) ListIPv4s(ctx context.Context, serverID string, options *ListOptions) ([]IPv4, *Meta, *http.Response, error) { //nolint:lll,dupl
uri := fmt.Sprintf("%s/%s/ipv4", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand All @@ -299,7 +299,7 @@ func (b *BareMetalServerServiceHandler) ListIPv4s(ctx context.Context, serverID
// ListIPv6s information of a Bare Metal server.
// IP information is only available for Bare Metal servers in the "active" state.
// If the Bare Metal server does not have IPv6 enabled, then an empty array is returned.
func (b *BareMetalServerServiceHandler) ListIPv6s(ctx context.Context, serverID string, options *ListOptions) ([]IPv6, *Meta, *http.Response, error) {
func (b *BareMetalServerServiceHandler) ListIPv6s(ctx context.Context, serverID string, options *ListOptions) ([]IPv6, *Meta, *http.Response, error) { //nolint:lll,dupl
uri := fmt.Sprintf("%s/%s/ipv6", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion bare_metal_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,6 @@ func TestBareMetalServerServiceHandler_GetUpgrades(t *testing.T) {

if !reflect.DeepEqual(server, expected) {
t.Errorf("BareMetalServer.GetUpgrades returned %+v, expected %+v", server, expected)

}
}
func TestBareMetalServerServiceHandler_GetVNCUrl(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type invoiceItemsBase struct {
}

// ListHistory retrieves a list of all billing history on the current account
func (b *BillingServiceHandler) ListHistory(ctx context.Context, options *ListOptions) ([]History, *Meta, *http.Response, error) {
func (b *BillingServiceHandler) ListHistory(ctx context.Context, options *ListOptions) ([]History, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/billing/history"
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand All @@ -97,7 +97,7 @@ func (b *BillingServiceHandler) ListHistory(ctx context.Context, options *ListOp
}

// ListInvoices retrieves a list of all billing invoices on the current account
func (b *BillingServiceHandler) ListInvoices(ctx context.Context, options *ListOptions) ([]Invoice, *Meta, *http.Response, error) {
func (b *BillingServiceHandler) ListInvoices(ctx context.Context, options *ListOptions) ([]Invoice, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/billing/invoices"
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand Down Expand Up @@ -139,7 +139,7 @@ func (b *BillingServiceHandler) GetInvoice(ctx context.Context, invoiceID string
}

// ListInvoiceItems retrieves items in an invoice that matches the given invoiceID
func (b *BillingServiceHandler) ListInvoiceItems(ctx context.Context, invoiceID int, options *ListOptions) ([]InvoiceItem, *Meta, *http.Response, error) {
func (b *BillingServiceHandler) ListInvoiceItems(ctx context.Context, invoiceID int, options *ListOptions) ([]InvoiceItem, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := fmt.Sprintf("/v2/billing/invoices/%d/items", invoiceID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
Expand Down
6 changes: 0 additions & 6 deletions billing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func TestBillingServiceHandler_ListHistory(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/billing/history", func(w http.ResponseWriter, r *http.Request) {

response := `
{
"billing_history": [
Expand Down Expand Up @@ -73,7 +72,6 @@ func TestBillingServiceHandler_ListInvoices(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/billing/invoices", func(w http.ResponseWriter, r *http.Request) {

response := `
{
"billing_invoices": [
Expand Down Expand Up @@ -132,7 +130,6 @@ func TestBillingServiceHandler_ListHistoryEmpty(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/billing/history", func(w http.ResponseWriter, r *http.Request) {

response := `
{
"billing_history": [],
Expand Down Expand Up @@ -178,7 +175,6 @@ func TestBillingServiceHandler_ListInvoicesEmpty(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/billing/invoices", func(w http.ResponseWriter, r *http.Request) {

response := `
{
"billing_invoices": [],
Expand Down Expand Up @@ -224,7 +220,6 @@ func TestBillingServiceHandler_GetInvoice(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/billing/invoices/123456", func(w http.ResponseWriter, r *http.Request) {

response := `
{
"billing_invoice": {
Expand Down Expand Up @@ -263,7 +258,6 @@ func TestBillingServiceHandler_ListInvoiceItems(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/billing/invoices/123456/items", func(w http.ResponseWriter, r *http.Request) {

response := `
{
"invoice_items": [
Expand Down
2 changes: 1 addition & 1 deletion block_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (b *BlockStorageServiceHandler) Delete(ctx context.Context, blockID string)
}

// List returns a list of all block storage instances on your Vultr Account
func (b *BlockStorageServiceHandler) List(ctx context.Context, options *ListOptions) ([]BlockStorage, *Meta, *http.Response, error) {
func (b *BlockStorageServiceHandler) List(ctx context.Context, options *ListOptions) ([]BlockStorage, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/blocks"

req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
Expand Down
Loading

0 comments on commit d679d8f

Please sign in to comment.