Add misspell, gocritic, revive

This commit is contained in:
eikendev
2023-04-01 16:40:57 +02:00
parent affe8a4b89
commit bea9b3cb1d
20 changed files with 37 additions and 17 deletions

View File

@@ -21,9 +21,12 @@ clean:
test: test:
stdout=$$(gofumpt -l . 2>&1); if [ "$$stdout" ]; then exit 1; fi stdout=$$(gofumpt -l . 2>&1); if [ "$$stdout" ]; then exit 1; fi
go vet ./... go vet ./...
misspell $(GO_FILES)
gocyclo -over 10 $(GO_FILES) gocyclo -over 10 $(GO_FILES)
staticcheck ./... staticcheck ./...
errcheck ./... errcheck ./...
gocritic check -disable='#experimental,#opinionated' -@ifElseChain.minThreshold 3 ./...
revive -set_exit_status ./...
go test -v -cover ./... go test -v -cover ./...
gosec -exclude-dir=tests ./... gosec -exclude-dir=tests ./...
govulncheck ./... govulncheck ./...
@@ -31,8 +34,11 @@ test:
.PHONY: setup .PHONY: setup
setup: setup:
go install github.com/client9/misspell/cmd/misspell@latest
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
go install github.com/go-critic/go-critic/cmd/gocritic@latest
go install github.com/kisielk/errcheck@latest go install github.com/kisielk/errcheck@latest
go install github.com/mgechev/revive@latest
go install github.com/securego/gosec/v2/cmd/gosec@latest go install github.com/securego/gosec/v2/cmd/gosec@latest
go install golang.org/x/vuln/cmd/govulncheck@latest go install golang.org/x/vuln/cmd/govulncheck@latest
go install honnef.co/go/tools/cmd/staticcheck@latest go install honnef.co/go/tools/cmd/staticcheck@latest

View File

@@ -1,3 +1,4 @@
// Package main provides the main function as a starting point of this tool.
package main package main
import ( import (

View File

@@ -1,7 +1,9 @@
// Package api provides low-level functionality to interact with the PushBits API.
package api package api
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@@ -54,7 +56,7 @@ func Request(base, endpoint, method, proxy, username, password string, hasBody b
if hasBody { if hasBody {
reqBody, err := json.Marshal(data) reqBody, err := json.Marshal(data)
if err != nil { if err != nil {
log.Fatal(err) return nil, err
} }
reqBodyReader = strings.NewReader(string(reqBody)) reqBodyReader = strings.NewReader(string(reqBody))
@@ -62,7 +64,7 @@ func Request(base, endpoint, method, proxy, username, password string, hasBody b
req, err := http.NewRequest(method, url.String(), reqBodyReader) req, err := http.NewRequest(method, url.String(), reqBodyReader)
if err != nil { if err != nil {
log.Fatal(err) return nil, err
} }
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
@@ -77,19 +79,19 @@ func Request(base, endpoint, method, proxy, username, password string, hasBody b
defer handling.Close(resp.Body) defer handling.Close(resp.Body)
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
log.Fatalf("Request failed with HTTP %s.", resp.Status) return nil, fmt.Errorf("request failed with HTTP %s", resp.Status)
} }
bodyText, err := io.ReadAll(resp.Body) bodyText, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatal(err) return nil, err
} }
var obj interface{} var obj interface{}
err = json.Unmarshal(bodyText, &obj) err = json.Unmarshal(bodyText, &obj)
if err != nil { if err != nil {
log.Fatal(err) return nil, err
} }
return obj, nil return obj, nil

View File

@@ -18,7 +18,7 @@ type createCommand struct {
StrictCompatibility bool `long:"compat" help:"Enforce strict compatibility with Gotify"` StrictCompatibility bool `long:"compat" help:"Enforce strict compatibility with Gotify"`
} }
func (c *createCommand) Run(s *options.Options) error { func (c *createCommand) Run(_ *options.Options) error {
password := ui.GetCurrentPassword(c.Username) password := ui.GetCurrentPassword(c.Username)
data := map[string]interface{}{ data := map[string]interface{}{

View File

@@ -19,7 +19,7 @@ type deleteCommand struct {
ID uint `arg:"" help:"The ID of the application"` ID uint `arg:"" help:"The ID of the application"`
} }
func (c *deleteCommand) Run(s *options.Options) error { func (c *deleteCommand) Run(_ *options.Options) error {
password := ui.GetCurrentPassword(c.Username) password := ui.GetCurrentPassword(c.Username)
populatedEndpoint := fmt.Sprintf(deleteEndpoint, c.ID) populatedEndpoint := fmt.Sprintf(deleteEndpoint, c.ID)

View File

@@ -16,7 +16,7 @@ type listCommand struct {
options.AuthOptions options.AuthOptions
} }
func (c *listCommand) Run(s *options.Options) error { func (c *listCommand) Run(_ *options.Options) error {
password := ui.GetCurrentPassword(c.Username) password := ui.GetCurrentPassword(c.Username)
resp, err := api.Get(c.URL, listEndpoint, c.Proxy, c.Username, password) resp, err := api.Get(c.URL, listEndpoint, c.Proxy, c.Username, password)

View File

@@ -1,3 +1,4 @@
// Package application provides commands related to managing applications.
package application package application
// Command contains all subcommands provided by this package. // Command contains all subcommands provided by this package.

View File

@@ -19,7 +19,7 @@ type showCommand struct {
ID uint `arg:"" help:"The ID of the application"` ID uint `arg:"" help:"The ID of the application"`
} }
func (c *showCommand) Run(s *options.Options) error { func (c *showCommand) Run(_ *options.Options) error {
password := ui.GetCurrentPassword(c.Username) password := ui.GetCurrentPassword(c.Username)
populatedEndpoint := fmt.Sprintf(showEndpoint, c.ID) populatedEndpoint := fmt.Sprintf(showEndpoint, c.ID)

View File

@@ -22,7 +22,7 @@ type updateCommand struct {
StrictCompatibility bool `long:"compat" help:"Enforce strict compatibility with Gotify"` StrictCompatibility bool `long:"compat" help:"Enforce strict compatibility with Gotify"`
} }
func (c *updateCommand) Run(s *options.Options) error { func (c *updateCommand) Run(_ *options.Options) error {
password := ui.GetCurrentPassword(c.Username) password := ui.GetCurrentPassword(c.Username)
if !c.RefreshToken && c.StrictCompatibility { if !c.RefreshToken && c.StrictCompatibility {

View File

@@ -1,3 +1,5 @@
// Package buildconfig cpntains variables that are set during compliation.
package buildconfig package buildconfig
// Version of the build.
var Version = "unknown" var Version = "unknown"

View File

@@ -1,3 +1,4 @@
// Package commands contains functions that are exposed as dedicated commands of the tool.
package commands package commands
import ( import (
@@ -7,9 +8,11 @@ import (
"github.com/pushbits/cli/internal/options" "github.com/pushbits/cli/internal/options"
) )
// VersionCommand represents the options specific to the version command.
type VersionCommand struct{} type VersionCommand struct{}
func (c *VersionCommand) Run(s *options.Options) error { // Run is the function for the version command.
func (*VersionCommand) Run(_ *options.Options) error {
fmt.Printf("pbcli %s\n", buildconfig.Version) fmt.Printf("pbcli %s\n", buildconfig.Version)
return nil return nil

View File

@@ -1,3 +1,4 @@
// Package handling provides convenience functions for cleaning up resources.
package handling package handling
import ( import (
@@ -6,6 +7,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// Close closes an io resource and prints a warning if that fails.
func Close(c io.Closer) { func Close(c io.Closer) {
if err := c.Close(); err != nil { if err := c.Close(); err != nil {
log.Warn(err) log.Warn(err)

View File

@@ -1,6 +1,7 @@
// Package options defines the global options of this tool.
package options package options
// Options represents the global options. // Options represents the global options of this tool.
type Options struct { type Options struct {
Verbose bool `short:"v" help:"Show debugging information"` Verbose bool `short:"v" help:"Show debugging information"`
} }

View File

@@ -1,3 +1,4 @@
// Package ui provides utilities to interact with the user over the command line.
package ui package ui
import ( import (

View File

@@ -18,7 +18,7 @@ type createCommand struct {
MatrixID string `arg:"" help:"The Matrix ID of the user"` MatrixID string `arg:"" help:"The Matrix ID of the user"`
} }
func (c *createCommand) Run(s *options.Options) error { func (c *createCommand) Run(_ *options.Options) error {
password := ui.GetCurrentPassword(c.Username) password := ui.GetCurrentPassword(c.Username)
newPassword := ui.GetNewPassword(c.Name) newPassword := ui.GetNewPassword(c.Name)

View File

@@ -19,7 +19,7 @@ type deleteCommand struct {
ID uint `arg:"" help:"The ID of the user"` ID uint `arg:"" help:"The ID of the user"`
} }
func (c *deleteCommand) Run(s *options.Options) error { func (c *deleteCommand) Run(_ *options.Options) error {
password := ui.GetCurrentPassword(c.Username) password := ui.GetCurrentPassword(c.Username)
populatedEndpoint := fmt.Sprintf(deleteEndpoint, c.ID) populatedEndpoint := fmt.Sprintf(deleteEndpoint, c.ID)

View File

@@ -16,7 +16,7 @@ type listCommand struct {
options.AuthOptions options.AuthOptions
} }
func (c *listCommand) Run(s *options.Options) error { func (c *listCommand) Run(_ *options.Options) error {
password := ui.GetCurrentPassword(c.Username) password := ui.GetCurrentPassword(c.Username)
resp, err := api.Get(c.URL, listEndpoint, c.Proxy, c.Username, password) resp, err := api.Get(c.URL, listEndpoint, c.Proxy, c.Username, password)

View File

@@ -1,3 +1,4 @@
// Package user provides commands related to managing users.
package user package user
// Command contains all subcommands provided by this package. // Command contains all subcommands provided by this package.

View File

@@ -19,7 +19,7 @@ type showCommand struct {
ID uint `arg:"" help:"The ID of the user"` ID uint `arg:"" help:"The ID of the user"`
} }
func (c *showCommand) Run(s *options.Options) error { func (c *showCommand) Run(_ *options.Options) error {
password := ui.GetCurrentPassword(c.Username) password := ui.GetCurrentPassword(c.Username)
populatedEndpoint := fmt.Sprintf(showEndpoint, c.ID) populatedEndpoint := fmt.Sprintf(showEndpoint, c.ID)

View File

@@ -21,7 +21,7 @@ type updateCommand struct {
NewMatrixID string `long:"new-matrixid" help:"The new Matrix ID of the user"` NewMatrixID string `long:"new-matrixid" help:"The new Matrix ID of the user"`
} }
func (c *updateCommand) Run(s *options.Options) error { func (c *updateCommand) Run(_ *options.Options) error {
password := ui.GetCurrentPassword(c.Username) password := ui.GetCurrentPassword(c.Username)
data := map[string]interface{}{} data := map[string]interface{}{}