diff --git a/Makefile b/Makefile index 784f928..53f306d 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,12 @@ clean: test: stdout=$$(gofumpt -l . 2>&1); if [ "$$stdout" ]; then exit 1; fi go vet ./... + misspell $(GO_FILES) gocyclo -over 10 $(GO_FILES) staticcheck ./... errcheck ./... + gocritic check -disable='#experimental,#opinionated' -@ifElseChain.minThreshold 3 ./... + revive -set_exit_status ./... go test -v -cover ./... gosec -exclude-dir=tests ./... govulncheck ./... @@ -31,8 +34,11 @@ test: .PHONY: setup setup: + go install github.com/client9/misspell/cmd/misspell@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/mgechev/revive@latest go install github.com/securego/gosec/v2/cmd/gosec@latest go install golang.org/x/vuln/cmd/govulncheck@latest go install honnef.co/go/tools/cmd/staticcheck@latest diff --git a/cmd/pbcli/main.go b/cmd/pbcli/main.go index a6cf6e2..3e92ffc 100644 --- a/cmd/pbcli/main.go +++ b/cmd/pbcli/main.go @@ -1,3 +1,4 @@ +// Package main provides the main function as a starting point of this tool. package main import ( diff --git a/internal/api/mod.go b/internal/api/mod.go index b088cab..5216172 100644 --- a/internal/api/mod.go +++ b/internal/api/mod.go @@ -1,7 +1,9 @@ +// Package api provides low-level functionality to interact with the PushBits API. package api import ( "encoding/json" + "fmt" "io" "net/http" "net/url" @@ -54,7 +56,7 @@ func Request(base, endpoint, method, proxy, username, password string, hasBody b if hasBody { reqBody, err := json.Marshal(data) if err != nil { - log.Fatal(err) + return nil, err } 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) if err != nil { - log.Fatal(err) + return nil, err } 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) 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) if err != nil { - log.Fatal(err) + return nil, err } var obj interface{} err = json.Unmarshal(bodyText, &obj) if err != nil { - log.Fatal(err) + return nil, err } return obj, nil diff --git a/internal/application/create.go b/internal/application/create.go index 4a3011d..3b0908f 100644 --- a/internal/application/create.go +++ b/internal/application/create.go @@ -18,7 +18,7 @@ type createCommand struct { 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) data := map[string]interface{}{ diff --git a/internal/application/delete.go b/internal/application/delete.go index 23a996f..61768cb 100644 --- a/internal/application/delete.go +++ b/internal/application/delete.go @@ -19,7 +19,7 @@ type deleteCommand struct { 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) populatedEndpoint := fmt.Sprintf(deleteEndpoint, c.ID) diff --git a/internal/application/list.go b/internal/application/list.go index 44a0ea4..e1fa5d1 100644 --- a/internal/application/list.go +++ b/internal/application/list.go @@ -16,7 +16,7 @@ type listCommand struct { options.AuthOptions } -func (c *listCommand) Run(s *options.Options) error { +func (c *listCommand) Run(_ *options.Options) error { password := ui.GetCurrentPassword(c.Username) resp, err := api.Get(c.URL, listEndpoint, c.Proxy, c.Username, password) diff --git a/internal/application/mod.go b/internal/application/mod.go index 25dd525..7b24223 100644 --- a/internal/application/mod.go +++ b/internal/application/mod.go @@ -1,3 +1,4 @@ +// Package application provides commands related to managing applications. package application // Command contains all subcommands provided by this package. diff --git a/internal/application/show.go b/internal/application/show.go index 9461596..ecf6f01 100644 --- a/internal/application/show.go +++ b/internal/application/show.go @@ -19,7 +19,7 @@ type showCommand struct { 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) populatedEndpoint := fmt.Sprintf(showEndpoint, c.ID) diff --git a/internal/application/update.go b/internal/application/update.go index 95232bf..49b0608 100644 --- a/internal/application/update.go +++ b/internal/application/update.go @@ -22,7 +22,7 @@ type updateCommand struct { 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) if !c.RefreshToken && c.StrictCompatibility { diff --git a/internal/buildconfig/buildconfig.go b/internal/buildconfig/buildconfig.go index fa15b93..151ca17 100644 --- a/internal/buildconfig/buildconfig.go +++ b/internal/buildconfig/buildconfig.go @@ -1,3 +1,5 @@ +// Package buildconfig cpntains variables that are set during compliation. package buildconfig +// Version of the build. var Version = "unknown" diff --git a/internal/commands/version.go b/internal/commands/version.go index db1bb3e..2dedee9 100644 --- a/internal/commands/version.go +++ b/internal/commands/version.go @@ -1,3 +1,4 @@ +// Package commands contains functions that are exposed as dedicated commands of the tool. package commands import ( @@ -7,9 +8,11 @@ import ( "github.com/pushbits/cli/internal/options" ) +// VersionCommand represents the options specific to the version command. 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) return nil diff --git a/internal/handling/handling.go b/internal/handling/handling.go index ff7962f..c24bb65 100644 --- a/internal/handling/handling.go +++ b/internal/handling/handling.go @@ -1,3 +1,4 @@ +// Package handling provides convenience functions for cleaning up resources. package handling import ( @@ -6,6 +7,7 @@ import ( log "github.com/sirupsen/logrus" ) +// Close closes an io resource and prints a warning if that fails. func Close(c io.Closer) { if err := c.Close(); err != nil { log.Warn(err) diff --git a/internal/options/mod.go b/internal/options/mod.go index 11c98d1..0b5fdbf 100644 --- a/internal/options/mod.go +++ b/internal/options/mod.go @@ -1,6 +1,7 @@ +// Package options defines the global options of this tool. package options -// Options represents the global options. +// Options represents the global options of this tool. type Options struct { Verbose bool `short:"v" help:"Show debugging information"` } diff --git a/internal/ui/mod.go b/internal/ui/mod.go index 25726c7..585719b 100644 --- a/internal/ui/mod.go +++ b/internal/ui/mod.go @@ -1,3 +1,4 @@ +// Package ui provides utilities to interact with the user over the command line. package ui import ( diff --git a/internal/user/create.go b/internal/user/create.go index 7a7af08..31cc3c4 100644 --- a/internal/user/create.go +++ b/internal/user/create.go @@ -18,7 +18,7 @@ type createCommand struct { 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) newPassword := ui.GetNewPassword(c.Name) diff --git a/internal/user/delete.go b/internal/user/delete.go index 9c37909..62bac34 100644 --- a/internal/user/delete.go +++ b/internal/user/delete.go @@ -19,7 +19,7 @@ type deleteCommand struct { 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) populatedEndpoint := fmt.Sprintf(deleteEndpoint, c.ID) diff --git a/internal/user/list.go b/internal/user/list.go index ad8bdc0..8adb91d 100644 --- a/internal/user/list.go +++ b/internal/user/list.go @@ -16,7 +16,7 @@ type listCommand struct { options.AuthOptions } -func (c *listCommand) Run(s *options.Options) error { +func (c *listCommand) Run(_ *options.Options) error { password := ui.GetCurrentPassword(c.Username) resp, err := api.Get(c.URL, listEndpoint, c.Proxy, c.Username, password) diff --git a/internal/user/mod.go b/internal/user/mod.go index 7b04a50..40148d1 100644 --- a/internal/user/mod.go +++ b/internal/user/mod.go @@ -1,3 +1,4 @@ +// Package user provides commands related to managing users. package user // Command contains all subcommands provided by this package. diff --git a/internal/user/show.go b/internal/user/show.go index d60fefa..e745ac2 100644 --- a/internal/user/show.go +++ b/internal/user/show.go @@ -19,7 +19,7 @@ type showCommand struct { 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) populatedEndpoint := fmt.Sprintf(showEndpoint, c.ID) diff --git a/internal/user/update.go b/internal/user/update.go index a31a668..6107455 100644 --- a/internal/user/update.go +++ b/internal/user/update.go @@ -21,7 +21,7 @@ type updateCommand struct { 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) data := map[string]interface{}{}