Skip to content

Commit

Permalink
refactor: enable & enforce linter rules (#840)
Browse files Browse the repository at this point in the history
See #836
  • Loading branch information
phm07 authored Aug 22, 2024
1 parent f7c9ac6 commit 15b26d6
Show file tree
Hide file tree
Showing 200 changed files with 480 additions and 529 deletions.
16 changes: 8 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ linters:
enable:
- bodyclose
# - dupl
# - errcheck
- errcheck
- exhaustive
# - gocritic
- gocritic
# - golint
- gci
- gomodguard
- gosec
# - gosimple
- gosimple
- govet
# - ineffassign
# - misspell
# - revive
- ineffassign
- misspell
- revive
- rowserrcheck
# - scopelint
# - staticcheck
- typecheck
# - unparam
- unparam
- unused
# - whitespace
- whitespace

issues:
include:
Expand Down
1 change: 0 additions & 1 deletion cmd/hcloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func init() {
}

func main() {

cfg := config.New()
if err := cfg.Read(nil); err != nil {
log.Fatalf("unable to read config file \"%s\": %s\n", cfg.Path(), err)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewRootCommand(s state.State) *cobra.Command {

cmd.PersistentFlags().AddFlagSet(s.Config().FlagSet())

cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
cmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
var err error
out := os.Stdout
quiet, err := config.OptionQuiet.Get(s.Config())
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/all/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var allCmds = []base.ListCmd{
}

var ListCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
BaseCobraCommand: func(hcapi2.Client) *cobra.Command {

var resources []string
for _, cmd := range allCmds {
Expand All @@ -66,7 +66,7 @@ Listed resources are:

return cmd
},
Run: func(s state.State, cmd *cobra.Command, args []string) error {
Run: func(s state.State, cmd *cobra.Command, _ []string) error {

paid, _ := cmd.Flags().GetBool("paid")
labelSelector, _ := cmd.Flags().GetString("selector")
Expand Down Expand Up @@ -154,9 +154,8 @@ Listed resources are:
}
if outOpts.IsSet("json") {
return util.DescribeJSON(schema)
} else {
return util.DescribeYAML(schema)
}
return util.DescribeYAML(schema)
}

for i, lc := range cmds {
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/all/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

//go:embed testdata/all_list_response.json
var allListResponseJson string
var allListResponseJSON string

func TestListAll(t *testing.T) {
fx := testutil.NewFixture(t)
Expand Down Expand Up @@ -288,5 +288,5 @@ func TestListAllPaidJSON(t *testing.T) {

assert.NoError(t, err)
assert.Empty(t, errOut)
assert.JSONEq(t, allListResponseJson, jsonOut)
assert.JSONEq(t, allListResponseJSON, jsonOut)
}
3 changes: 1 addition & 2 deletions internal/cmd/base/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ func (cc *CreateCmd) CobraCommand(s state.State) *cobra.Command {
if isSchema {
if outputFlags.IsSet("json") {
return util.DescribeJSON(schema)
} else {
return util.DescribeYAML(schema)
}
return util.DescribeYAML(schema)
} else if cc.PrintResource != nil && resource != nil {
cc.PrintResource(s, cmd, resource)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/base/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ type fakeResource struct {
}

var fakeCreateCmd = &base.CreateCmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
BaseCobraCommand: func(hcapi2.Client) *cobra.Command {
return &cobra.Command{
Use: "create",
}
},
Run: func(s state.State, cmd *cobra.Command, strings []string) (any, any, error) {
Run: func(_ state.State, cmd *cobra.Command, _ []string) (any, any, error) {
cmd.Println("Creating fake resource")

resource := &fakeResource{
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/base/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (dc *DeleteCmd) Run(s state.State, cmd *cobra.Command, args []string) error

if len(actions) > 0 {
// TODO: We do not check if an action fails for a specific resource
if err := s.WaitForActions(cmd, s, actions...); err != nil {
if err := s.WaitForActions(s, cmd, actions...); err != nil {
errs = append(errs, err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/base/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ var mu = sync.Mutex{}
var fakeDeleteCmd = &base.DeleteCmd{
ResourceNameSingular: "Fake resource",
ResourceNamePlural: "Fake resources",
Delete: func(s state.State, cmd *cobra.Command, resource interface{}) (*hcloud.Action, error) {
Delete: func(_ state.State, cmd *cobra.Command, _ interface{}) (*hcloud.Action, error) {
defer mu.Unlock()
cmd.Println("Deleting fake resource")
return nil, nil
},

Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
Fetch: func(_ state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
mu.Lock()
cmd.Println("Fetching fake resource")

Expand All @@ -42,7 +42,7 @@ var fakeDeleteCmd = &base.DeleteCmd{
return resource, nil, nil
},

NameSuggestions: func(client hcapi2.Client) func() []string {
NameSuggestions: func(hcapi2.Client) func() []string {
return nil
},
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/base/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var fakeDescribeCmd = &base.DescribeCmd{
ResourceNameSingular: "Fake resource",

Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) {
Fetch: func(_ state.State, cmd *cobra.Command, _ string) (interface{}, interface{}, error) {
cmd.Println("Fetching fake resource")

resource := &fakeResource{
Expand All @@ -26,14 +26,14 @@ var fakeDescribeCmd = &base.DescribeCmd{
return resource, util.Wrap("resource", resource), nil
},

PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error {
PrintText: func(_ state.State, cmd *cobra.Command, resource interface{}) error {
rsc := resource.(*fakeResource)
cmd.Printf("ID: %d\n", rsc.ID)
cmd.Printf("Name: %s\n", rsc.Name)
return nil
},

NameSuggestions: func(client hcapi2.Client) func() []string {
NameSuggestions: func(hcapi2.Client) func() []string {
return nil
},
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/base/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (lc *ListCmd) CobraCommand(s state.State) *cobra.Command {
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: s.EnsureToken,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
return lc.Run(s, cmd)
},
}
Expand Down Expand Up @@ -89,9 +89,8 @@ func (lc *ListCmd) Run(s state.State, cmd *cobra.Command) error {
schema := lc.Schema(resources)
if outOpts.IsSet("json") {
return util.DescribeJSON(schema)
} else {
return util.DescribeYAML(schema)
}
return util.DescribeYAML(schema)
}

cols := lc.DefaultColumns
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/base/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var fakeListCmd = &base.ListCmd{
return i
},

OutputTable: func(client hcapi2.Client) *output.Table {
OutputTable: func(hcapi2.Client) *output.Table {
return output.NewTable().
AddAllowedFields(hcloud.Firewall{}).
AddFieldFn("id", func(obj interface{}) string {
Expand All @@ -40,7 +40,7 @@ var fakeListCmd = &base.ListCmd{

DefaultColumns: []string{"id", "name"},

Fetch: func(s state.State, set *pflag.FlagSet, opts hcloud.ListOpts, sort []string) ([]interface{}, error) {
Fetch: func(_ state.State, _ *pflag.FlagSet, _ hcloud.ListOpts, sort []string) ([]interface{}, error) {
resources := []*fakeResource{
{
ID: 456,
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/base/set_rdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (rc *SetRdnsCmd) CobraCommand(s state.State) *cobra.Command {

// Run executes a setRDNS command.
func (rc *SetRdnsCmd) Run(s state.State, cmd *cobra.Command, args []string) error {

var hostnamePtr *string
if reset, _ := cmd.Flags().GetBool("reset"); reset {
hostnamePtr = nil
Expand Down Expand Up @@ -80,7 +79,7 @@ func (rc *SetRdnsCmd) Run(s state.State, cmd *cobra.Command, args []string) erro
return err
}

if err := s.WaitForActions(cmd, s, action); err != nil {
if err := s.WaitForActions(s, cmd, action); err != nil {
return err
}

Expand Down
1 change: 0 additions & 1 deletion internal/cmd/base/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (uc *UpdateCmd) CobraCommand(s state.State) *cobra.Command {

// Run executes a update command.
func (uc *UpdateCmd) Run(s state.State, cmd *cobra.Command, args []string) error {

idOrName := args[0]
resource, _, err := uc.Fetch(s, cmd, idOrName)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/cmd/certificate/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (
)

var CreateCmd = base.CreateCmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
BaseCobraCommand: func(hcapi2.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "create [options] --name <name> (--type managed --domain <domain> | --type uploaded --cert-file <file> --key-file <file>)",
Short: "Create or upload a Certificate",
}

cmd.Flags().String("name", "", "Certificate name (required)")
cmd.MarkFlagRequired("name")
_ = cmd.MarkFlagRequired("name")

cmd.Flags().StringP("type", "t", string(hcloud.CertificateTypeUploaded),
fmt.Sprintf("Type of certificate to create. Valid choices: %v, %v",
hcloud.CertificateTypeUploaded, hcloud.CertificateTypeManaged))
cmd.RegisterFlagCompletionFunc(
_ = cmd.RegisterFlagCompletionFunc(
"type",
cmpl.SuggestCandidates(string(hcloud.CertificateTypeUploaded), string(hcloud.CertificateTypeManaged)),
)
Expand All @@ -39,7 +39,7 @@ var CreateCmd = base.CreateCmd{

return cmd
},
Run: func(s state.State, cmd *cobra.Command, strings []string) (any, any, error) {
Run: func(s state.State, cmd *cobra.Command, _ []string) (any, any, error) {
certType, err := cmd.Flags().GetString("type")
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -129,7 +129,7 @@ func createManaged(s state.State, cmd *cobra.Command) (*hcloud.Certificate, erro
if err != nil {
return nil, err
}
if err := s.WaitForActions(cmd, s, res.Action); err != nil {
if err := s.WaitForActions(s, cmd, res.Action); err != nil {
return nil, err
}
defer cmd.Printf("Certificate %d created\n", res.Certificate.ID)
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/certificate/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
)

//go:embed testdata/managed_create_response.json
var managedCreateResponseJson string
var managedCreateResponseJSON string

//go:embed testdata/uploaded_create_response.json
var uploadedCreateResponseJson string
var uploadedCreateResponseJSON string

func TestCreateManaged(t *testing.T) {
fx := testutil.NewFixture(t)
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestCreateManagedJSON(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expOut, out)

assert.JSONEq(t, managedCreateResponseJson, jsonOut)
assert.JSONEq(t, managedCreateResponseJSON, jsonOut)
}

func TestCreateUploaded(t *testing.T) {
Expand Down Expand Up @@ -194,5 +194,5 @@ func TestCreateUploadedJSON(t *testing.T) {

assert.NoError(t, err)
assert.Equal(t, expOut, out)
assert.JSONEq(t, uploadedCreateResponseJson, jsonOut)
assert.JSONEq(t, uploadedCreateResponseJSON, jsonOut)
}
4 changes: 2 additions & 2 deletions internal/cmd/certificate/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ var DeleteCmd = base.DeleteCmd{
ResourceNamePlural: "certificates",
ShortDescription: "Delete a certificate",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names },
Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
Fetch: func(s state.State, _ *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
return s.Client().Certificate().Get(s, idOrName)
},
Delete: func(s state.State, cmd *cobra.Command, resource interface{}) (*hcloud.Action, error) {
Delete: func(s state.State, _ *cobra.Command, resource interface{}) (*hcloud.Action, error) {
certificate := resource.(*hcloud.Certificate)
_, err := s.Client().Certificate().Delete(s, certificate)
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/certificate/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var DescribeCmd = base.DescribeCmd{
JSONKeyGetByID: "certificate",
JSONKeyGetByName: "certificates",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.Certificate().Names },
Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) {
Fetch: func(s state.State, _ *cobra.Command, idOrName string) (interface{}, interface{}, error) {
cert, _, err := s.Client().Certificate().Get(s, idOrName)
if err != nil {
return nil, nil, err
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/certificate/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ var UpdateCmd = base.UpdateCmd{
ResourceNameSingular: "certificate",
ShortDescription: "Update a certificate",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names },
Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
Fetch: func(s state.State, _ *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
return s.Client().Certificate().Get(s, idOrName)
},
DefineFlags: func(cmd *cobra.Command) {
cmd.Flags().String("name", "", "Certificate Name")
},
Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error {
Update: func(s state.State, _ *cobra.Command, resource interface{}, flags map[string]pflag.Value) error {
certificate := resource.(*hcloud.Certificate)
updOpts := hcloud.CertificateUpdateOpts{
Name: flags["name"].String(),
Expand Down
2 changes: 0 additions & 2 deletions internal/cmd/cmpl/suggestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func SuggestCandidatesCtx(

return sel, cobra.ShellCompDirectiveDefault
}

}

// SuggestNothing returns a function that provides no suggestions.
Expand Down Expand Up @@ -98,7 +97,6 @@ func SuggestArgs(
// file completion.
func NoFileCompletion(f func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective)) func(
*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {

return func(command *cobra.Command, i []string, s string) ([]string, cobra.ShellCompDirective) {
candidates, _ := f(command, i, s)
return candidates, cobra.ShellCompDirectiveNoFileComp
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/config/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func runAdd(s state.State, cmd *cobra.Command, args []string) error {
switch opt.T().(type) {
case []string:
before := util.AnyToStringSlice(val)
newVal := append(before, values...)
newVal = util.RemoveDuplicates(newVal)
newVal := util.RemoveDuplicates(append(before, values...))
val = newVal
added = util.ToAnySlice(util.SliceDiff[[]string](newVal, before))
default:
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/config/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ func runList(s state.State, cmd *cobra.Command, _ []string) error {
schema := util.Wrap("options", options)
if outOpts.IsSet("json") {
return util.DescribeJSON(schema)
} else {
return util.DescribeYAML(schema)
}
return util.DescribeYAML(schema)
}

cols := outputColumns
Expand Down
Loading

0 comments on commit 15b26d6

Please sign in to comment.