Skip to content

Commit

Permalink
chore: Enable more golangci-lint rules (#23)
Browse files Browse the repository at this point in the history
* refactor: import-shadowing: The name 'config' shadows an import name

* refactor: if-return: redundant if ...; err != nil check

* chore: Enable more rules in .golangci.yml
  • Loading branch information
sonjek authored Apr 6, 2024
1 parent 3281e51 commit 641cac4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 35 deletions.
32 changes: 21 additions & 11 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
run:
concurrency: 4
deadline: 5m
timeout: 5m

output:
sort-results: true

linters:
# https://golangci-lint.run/usage/linters/#enabled-by-default
enable:
- asciicheck
- depguard
Expand All @@ -26,6 +19,8 @@ linters:
- unconvert

linters-settings:
gofmt:
simplify: true
depguard:
rules:
main:
Expand All @@ -36,11 +31,26 @@ linters-settings:
desc: "Use 'errors' or 'fmt' instead of github.com/pkg/errors"
- pkg: "golang.org/x/exp/slices"
desc: "Use 'slices' instead."

revive:
# https://github.com/mgechev/revive/blob/master/defaults.toml
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
enable-all-rules: true
ignore-generated-header: true
severity: warning
rules:
# A lot of false positives: incorrectly identifies channel draining as "empty code block".
# See https://github.com/mgechev/revive/issues/386
- name: empty-block
- name: line-length-limit
severity: error
arguments: [120]
- name: add-constant
disabled: true
- name: cognitive-complexity
disabled: true
- name: cyclomatic
disabled: true
- name: unused-receiver
disabled: true
- name: unhandled-error
arguments:
- "fmt.Println"
- "fmt.Printf"
6 changes: 3 additions & 3 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

func main() {
config := config.NewConfig()
mc := mouse.NewController(config)
trayIcon := tray.NewTray(mc, config)
conf := config.NewConfig()
mc := mouse.NewController(conf)
trayIcon := tray.NewTray(mc, conf)
trayIcon.Run()
}
10 changes: 5 additions & 5 deletions internal/mouse/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import (
)

type Controller struct {
config *config.Config
conf *config.Config
LastX, LastY int
}

func NewController(config *config.Config) *Controller {
func NewController(conf *config.Config) *Controller {
return &Controller{
config: config,
conf: conf,
}
}

func (c *Controller) MoveMouse() {
for c.config.Enabled {
for c.conf.Enabled {
// Sleep before the check
c.sleep()

// Check if the current time is within working hours.
// If not, there is no reason to move the cursor.
isWorkingHours := utils.IsInWorkingHours(c.config.WorkingHoursInterval)
isWorkingHours := utils.IsInWorkingHours(c.conf.WorkingHoursInterval)
if !isWorkingHours {
continue
}
Expand Down
24 changes: 12 additions & 12 deletions internal/tray/tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func loadIcon() ([]byte, error) {

type Tray struct {
mouseController *mouse.Controller
config *config.Config
conf *config.Config
workingHoursMenuItems map[string]*systray.MenuItem
}

func NewTray(mouseController *mouse.Controller, config *config.Config) *Tray {
func NewTray(mouseController *mouse.Controller, conf *config.Config) *Tray {
return &Tray{
mouseController: mouseController,
config: config,
conf: conf,
workingHoursMenuItems: make(map[string]*systray.MenuItem),
}
}
Expand Down Expand Up @@ -66,42 +66,42 @@ func (t *Tray) onReady() {
mQuit := systray.AddMenuItem("Quit", "Quit the application")

// Hide the enable option since it's already enabled by default
if t.config.Enabled {
if t.conf.Enabled {
mEnable.Hide()
} else {
mDisable.Hide()
}

// Add interval selection submenu items
for _, hours := range t.config.WorkingHours {
for _, hours := range t.conf.WorkingHours {
t.addWorkingHoursItems(mWorkingHours, hours)
}

// Set a marker for the default working hours interval
t.workingHoursMenuItems[t.config.WorkingHoursInterval].Check()
t.workingHoursMenuItems[t.conf.WorkingHoursInterval].Check()

workingHoursIntervalClicks := t.createWorkingHoursIntervalClicksChannel()

go func() {
for {
select {
case <-mEnable.ClickedCh:
t.config.Enabled = true
t.conf.Enabled = true
mEnable.Hide()
mDisable.Show()
mWorkingHours.Enable()
go t.mouseController.MoveMouse()
case <-mDisable.ClickedCh:
t.config.Enabled = false
t.conf.Enabled = false
mDisable.Hide()
mEnable.Show()
mWorkingHours.Disable()
case workingHoursInterval := <-workingHoursIntervalClicks:
// When an hours interval item is clicked, update the workingHoursInterval interval and checkmarks
t.config.SetWorkingHoursInterval(workingHoursInterval)
t.updateNightModeIntervalChecks(t.config.WorkingHoursInterval)
t.conf.SetWorkingHoursInterval(workingHoursInterval)
t.updateNightModeIntervalChecks(t.conf.WorkingHoursInterval)
case <-mAbout.ClickedCh:
if err := utils.OpenWebPage(t.config.GitRepo); err != nil {
if err := utils.OpenWebPage(t.conf.GitRepo); err != nil {
panic(err)
}
case <-mQuit.ClickedCh:
Expand All @@ -112,7 +112,7 @@ func (t *Tray) onReady() {
}()

// Start moving the mouse in a circle immediately if enabled
if t.config.Enabled {
if t.conf.Enabled {
go t.mouseController.MoveMouse()
}
}
Expand Down
5 changes: 1 addition & 4 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ func OpenWebPage(url string) error {
return errors.New("unsupported platform")
}

if err := cmd.Start(); err != nil {
return err
}
return nil
return cmd.Start()
}

func IsInWorkingHours(timeWindow string) bool {
Expand Down

0 comments on commit 641cac4

Please sign in to comment.