Skip to content

Commit

Permalink
Add .golangci.yml and fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
xx4h committed Sep 30, 2024
1 parent da17acb commit 36a1643
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 83 deletions.
41 changes: 41 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
#########################
#########################
## Golang Linter rules ##
#########################
#########################

# configure golangci-lint
# see https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml
issues:
exclude-rules:
- path: _test\.go
linters:
- dupl
- gosec
- goconst
linters:
enable:
- gosec
- unconvert
- gocyclo
- goconst
- goimports
- gocritic
- govet
- revive
linters-settings:
errcheck:
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
check-blank: true
govet:
enable:
# report about shadowed variables
- shadowing
gocyclo:
# minimal code complexity to report, 30 by default
min-complexity: 15
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
11 changes: 5 additions & 6 deletions cmd/hctl/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"fmt"
"io"
"os"
"slices"
"strings"
Expand All @@ -27,7 +26,7 @@ import (
"github.com/xx4h/hctl/pkg"
)

func newCompletionCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
func newCompletionCmd() *cobra.Command {

cmd := &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Expand Down Expand Up @@ -117,7 +116,7 @@ func noMoreArgsComp() ([]string, cobra.ShellCompDirective) {
}

// support function for completion
func compListStates(toComplete string, ignoredStates []string, service string, state string, h *pkg.Hctl) ([]string, cobra.ShellCompDirective) {
func compListStates(_ string, ignoredStates []string, service string, state string, h *pkg.Hctl) ([]string, cobra.ShellCompDirective) {
states := h.GetStates()
services := h.GetServices()

Expand All @@ -127,15 +126,15 @@ func compListStates(toComplete string, ignoredStates []string, service string, s
var choices []string
for _, rel := range filteredStates {
if h.CompletionShortNamesEnabled() {
s := strings.Split(rel.EntityId, ".")
s := strings.Split(rel.EntityID, ".")
if slices.Contains(choices, s[1]) {
// if we've more than one, we add the second-n with long name
choices = append(choices, rel.EntityId)
choices = append(choices, rel.EntityID)
} else {
choices = append(choices, s[1])
}
} else {
choices = append(choices, rel.EntityId)
choices = append(choices, rel.EntityID)
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/hctl/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func filterStates(states []rest.HassState, ignoredStates []string) []rest.HassSt
for _, rel := range states {
found := false
for _, ignoredName := range ignoredStates {
if rel.EntityId == ignoredName {
if rel.EntityID == ignoredName {
found = true
break
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func filterCapable(states []rest.HassState, services []rest.HassService, service
}

for rel := range states {
s := strings.Split(states[rel].EntityId, ".")
s := strings.Split(states[rel].EntityID, ".")
stateDomain := s[0]
for svc := range capableServices {
if stateDomain == capableServices[svc].Domain {
Expand Down
5 changes: 2 additions & 3 deletions cmd/hctl/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package main

import (
"io"
"os"
"path"

Expand All @@ -27,12 +26,12 @@ import (
)

// initCmd represents the init command
func newInitCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
func newInitCmd(h *pkg.Hctl) *cobra.Command {

cmd := &cobra.Command{
Use: "init",
Short: "Initialize and config hctl",
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
// TODO: find better way to get this path (needs to work together with cmd/root.go)
userDir, err := os.UserHomeDir()
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions cmd/hctl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
package main

import (
"io"

"github.com/spf13/cobra"

"github.com/xx4h/hctl/pkg"
)

// listCmd represents the list command
func newListCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
func newListCmd(h *pkg.Hctl) *cobra.Command {

var domains []string
var services []string
Expand All @@ -33,7 +31,7 @@ func newListCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
Short: "List all existing entities or services",
Aliases: []string{"l"},
ValidArgs: []string{"entities", "services"},
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
if len(args) == 0 || args[0] == "entities" {
h.DumpStates(domains)
} else if args[0] == "services" {
Expand Down
4 changes: 2 additions & 2 deletions cmd/hctl/off.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
o "github.com/xx4h/hctl/pkg/output"
)

func newOffCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
func newOffCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {

cmd := &cobra.Command{
Use: "off",
Expand All @@ -36,7 +36,7 @@ func newOffCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
}
return compListStates(toComplete, args, "turn_off", "on", h)
},
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
c := h.GetRest()
obj, state, sub, err := c.TurnOff(args[0])
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions cmd/hctl/on.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
package main

import (
"io"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/xx4h/hctl/pkg"
o "github.com/xx4h/hctl/pkg/output"
)

func newOnCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
func newOnCmd(h *pkg.Hctl) *cobra.Command {

cmd := &cobra.Command{
Use: "on",
Expand All @@ -36,7 +34,7 @@ func newOnCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
}
return compListStates(toComplete, args, "turn_on", "off", h)
},
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
c := h.GetRest()
obj, state, sub, err := c.TurnOn(args[0])
if err != nil {
Expand Down
18 changes: 10 additions & 8 deletions cmd/hctl/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
)

// rootCmd represents the base command when called without any subcommands
func newRootCmd(h *pkg.Hctl, out io.Writer, args []string) *cobra.Command {
func newRootCmd(h *pkg.Hctl, out io.Writer, _ []string) *cobra.Command {
var logLevel string

banner, err := o.GetBanner()
Expand All @@ -47,7 +47,7 @@ func newRootCmd(h *pkg.Hctl, out io.Writer, args []string) *cobra.Command {
Use: appName,
Short: "A command line tool to control your home automation",
Long: fmt.Sprintf("%s\nHctl is a CLI tool to control your home automation", banner),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
lvl, err := zerolog.ParseLevel(logLevel)
if err != nil {
return err
Expand All @@ -58,12 +58,12 @@ func newRootCmd(h *pkg.Hctl, out io.Writer, args []string) *cobra.Command {
}

cmd.AddCommand(
newVersionCmd(h, out),
newInitCmd(h, out),
newListCmd(h, out),
newVersionCmd(out),
newInitCmd(h),
newListCmd(h),
newToggleCmd(h, out),
newCompletionCmd(h, out),
newOnCmd(h, out),
newCompletionCmd(),
newOnCmd(h),
newOffCmd(h, out),
)

Expand All @@ -80,5 +80,7 @@ func runCmd() {
log.Fatal().Msgf("Error: %v", err)
}
rootCmd = newRootCmd(h, os.Stdout, os.Args[1:])
_ = rootCmd.Execute()
if err := rootCmd.Execute(); err != nil {
log.Error().Msgf("Error: %v", err)
}
}
4 changes: 2 additions & 2 deletions cmd/hctl/toggle.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// toggleCmd represents the toggle command
func newToggleCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
func newToggleCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "toggle",
Short: "Toggle on/off a light or switch",
Expand All @@ -37,7 +37,7 @@ func newToggleCmd(h *pkg.Hctl, out io.Writer) *cobra.Command {
}
return compListStates(toComplete, args, "toggle", "", h)
},
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, args []string) {
c := h.GetRest()
obj, state, sub, err := c.Toggle(args[0])
if err != nil {
Expand Down
15 changes: 12 additions & 3 deletions cmd/hctl/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ import (
"fmt"
"io"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/xx4h/hctl/pkg"
o "github.com/xx4h/hctl/pkg/output"
)

// versionCmd represents the version command
func newVersionCmd(hctl *pkg.Hctl, out io.Writer) *cobra.Command {
func newVersionCmd(out io.Writer) *cobra.Command {
var short bool

cmd := &cobra.Command{
Use: "version",
Short: "Print version info",
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
printVersion(out, short)
},
}
Expand All @@ -41,6 +42,14 @@ func newVersionCmd(hctl *pkg.Hctl, out io.Writer) *cobra.Command {
}

func printVersion(out io.Writer, short bool) {
if !short {
banner, err := o.GetBanner()
if err != nil {
log.Error().Msgf("Could not get banner: %v", err)
}
fmt.Fprint(out, banner)

}
const format = "%-10s %s\n"
fmt.Fprintf(out, format, "Version:", version)
fmt.Fprintf(out, format, "Commit:", commit)
Expand Down
6 changes: 5 additions & 1 deletion cmd/hctl/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func Test_printVersion(t *testing.T) {
b := new(bytes.Buffer)
printVersion(b, tt.short)
o := b.String()
if ok, _ := regexp.MatchString(tt.rexOut, o); !ok {
ok, err := regexp.MatchString(tt.rexOut, o)
if err != nil {
t.Errorf("error: %v", err)
}
if !ok {
t.Errorf("got %q, want %q", o, tt.rexOut)
}
})
Expand Down
24 changes: 12 additions & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ import (
)

type Config struct {
Hub ConfigHub `yaml:"hub"`
Completion ConfigCompletion `yaml:"completion"`
Handling ConfigHandling `yaml:"handling"`
Logging ConfigLogging `yamn:"logging"`
Hub Hub `yaml:"hub"`
Completion Completion `yaml:"completion"`
Handling Handling `yaml:"handling"`
Logging Logging `yamn:"logging"`
Viper *viper.Viper
}

type ConfigHub struct {
type Hub struct {
Type string `yaml:"type"`
Url string `yaml:"url"`
URL string `yaml:"url"`
Token string `yaml:"token"`
}

type ConfigCompletion struct {
type Completion struct {
ShortNames bool `yaml:"shortNames"`
WhatIf bool `yaml:"whatif"`
}

type ConfigHandling struct {
type Handling struct {
Fuzz bool `yaml:"fuzz"`
}

type ConfigLogging struct {
type Logging struct {
LogLevel string `yaml:"log_level"`
}

Expand Down Expand Up @@ -79,9 +79,9 @@ func NewConfig() (*Config, error) {
return nil, err
}

log_level := v.GetString("log_level")
if log_level != "" {
lvl, err := zerolog.ParseLevel(log_level)
logLevel := v.GetString("log_level")
if logLevel != "" {
lvl, err := zerolog.ParseLevel(logLevel)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/hctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (

type Hctl struct {
cfg *config.Config
//out io.ReadWriteCloser
//log *zerolog.Logger
// out io.ReadWriteCloser
// log *zerolog.Logger
}

func NewHctl() (*Hctl, error) {
Expand All @@ -56,7 +56,7 @@ func (h *Hctl) CompletionShortNamesEnabled() bool {
}

func (h *Hctl) GetRest() *rest.Hass {
return rest.New(h.cfg.Hub.Url, h.cfg.Hub.Token, h.cfg.Handling.Fuzz)
return rest.New(h.cfg.Hub.URL, h.cfg.Hub.Token, h.cfg.Handling.Fuzz)
}

func (h *Hctl) GetServices() []rest.HassService {
Expand Down
Loading

0 comments on commit 36a1643

Please sign in to comment.