Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable more linters #234

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@ linters:

disable:
# TODO: remove the line and fix the issues
- depguard
- err113
- forbidigo
- mnd
- inamedparam
- ireturn
- musttag
- nestif
- paralleltest
- predeclared
- revive
- wrapcheck
- wsl

- cyclop # some functions need refactoring, I'll deal with that later
- nlreturn # keeps the code concise
- depguard # I'm not that pedantic
- exhaustruct # it's ok not to specify all the fields in a struct definition
- godox # I like leaving TODOs in the code
- varnamelen # short variable names are okay
- nlreturn # keeps the code concise
- predeclared # need to rename the print action
- testpackage # keep the tests close to the code
- exhaustruct # it's ok not to specify all the fields in a struct definition
- varnamelen # short variable names are okay

# deprecated
- exportloopref
Expand All @@ -43,11 +39,10 @@ issues:
# using globals is recommended with spf13/cobra
- gochecknoglobals

- path: internal/version
linters:
- gochecknoglobals

linters-settings:
nolintlint:
require-explanation: true

gci:
sections:
- standard
Expand Down
2 changes: 1 addition & 1 deletion internal/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ func Map(client *gh.Client) ActionsMap {
}

type Runner interface {
Run(*notifications.Notification, []string, io.Writer) error
Run(n *notifications.Notification, params []string, out io.Writer) error
}
1 change: 1 addition & 0 deletions internal/actions/print/print.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
Package print implements an [actions.Runner] that prints a notification.
*/
//nolint:forbidigo // This action is correctly named.
package print
nobe4 marked this conversation as resolved.
Show resolved Hide resolved

import (
Expand Down
1 change: 1 addition & 0 deletions internal/actions/read/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (a *Runner) Run(n *notifications.Notification, _ []string, w io.Writer) err
if err != nil && err.Error() != "unexpected end of JSON input" {
return err
}

defer r.Body.Close()

n.Unread = false
Expand Down
1 change: 1 addition & 0 deletions internal/actions/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (*Runner) Run(n *notifications.Notification, tags []string, w io.Writer) er
n.Meta.Tags = append(n.Meta.Tags, tagsToAdd...)

newTags := []string{}

for _, tag := range n.Meta.Tags {
if !slices.Contains(tagsToRemove, tag) {
newTags = append(newTags, tag)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

type Requestor interface {
Request(string, string, io.Reader) (*http.Response, error)
Request(method string, url string, body io.Reader) (*http.Response, error)
}
1 change: 1 addition & 0 deletions internal/api/mock/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func LoadCallsFromFile(path string) ([]Call, error) {
}

calls := make([]Call, len(rawCalls))

for i, rawCall := range rawCalls {
body, err := json.Marshal(rawCall.Response.Body)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/api/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (m *Mock) call(verb, endpoint string) (Call, error) {
}

m.index++

slog.Debug("mock call", "verb", verb, "endpoint", endpoint, "call", call)

return call, nil
Expand Down
19 changes: 14 additions & 5 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
"log/slog"
"os"
"path/filepath"
"syscall"
"time"
)

type RefreshReadWriter interface {
Read(any) error
Write(any) error
Refresh(time.Time)
Read(d any) error
Write(d any) error
Refresh(t time.Time)
RefreshedAt() time.Time
}

Comment on lines +21 to 26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a linter suggested you to add variable name here? If so, which one?

I'm curious about adding it to my own settings

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will take a look

Expand Down Expand Up @@ -47,6 +48,7 @@ func (c *FileCache) Read(out any) error {
slog.Debug("cache doesn't exist", "path", c.path)
return nil
}

return err
}

Expand Down Expand Up @@ -93,9 +95,16 @@ func (c *FileCache) Write(in any) error {
return err
}

if err := os.MkdirAll(filepath.Dir(c.path), 0o755); err != nil {
if err := os.MkdirAll(
filepath.Dir(c.path),
syscall.S_IRUSR|syscall.S_IWUSR|syscall.S_IXUSR,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What linter suggested you to use this?

I have never met this constants

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was mnd, for the "magic number" 755 and 6xx in other places.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a new rule for this

sashamelentyev/usestdlibvars#101

Thank you for letting me know it

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing, this will be very useful!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if you saw it, but my linter suggestion went in a dead end

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I noticed. I might switch to x/sys, but as long as no linter complains about it I'll leave it 🤣

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I add myself a depguard rules about using syscall package to enforce using golang.orgg/x/sys

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you do, feel free to ping me, I'll make the same change :)

Copy link
Contributor

@ccoVeille ccoVeille Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

@nobe4 nobe4 Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi, I decided to move back to using numbers with an updated mnd rule: #240

); err != nil {
return err
}

return os.WriteFile(c.path, marshaled, 0o600)
return os.WriteFile(
c.path,
marshaled,
syscall.S_IRUSR|syscall.S_IWUSR,
)
}
4 changes: 4 additions & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func runConfig(_ *cobra.Command, _ []string) error {
if err != nil {
return err
}

//nolint:forbidigo // This is an expected print statement.
fmt.Printf("Config sourced from: %s\n\n%s\n", config.Path, marshaled)
nobe4 marked this conversation as resolved.
Show resolved Hide resolved

return nil
Expand All @@ -66,6 +68,8 @@ func initConfig() error {
slog.Error("Failed to save initial config", "err", err)
return err
}

//nolint:forbidigo // This is an expected print statement.
fmt.Printf("Initial config saved to %s\n", initialPath)

return nil
Expand Down
9 changes: 8 additions & 1 deletion internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Execute() error {
return rootCmd.Execute()
}

//nolint:lll
//nolint:lll // Having the whole flag definition on a single line is OK.
func init() {
rootCmd.Root().CompletionOptions.DisableDefaultCmd = true

Expand All @@ -79,6 +79,7 @@ func setupGlobals(_ *cobra.Command, _ []string) error {
logger.Init(verbosityFlag)

var err error

config, err = configPkg.New(configPathFlag)
if err != nil {
slog.Error("Failed to load the config", "path", configPathFlag, "err", err)
Expand Down Expand Up @@ -188,7 +189,10 @@ func display(notifications notifications.Notifications) error {
}

func displayTable(n notifications.Notifications) {
nobe4 marked this conversation as resolved.
Show resolved Hide resolved
//nolint:forbidigo // This is an expected print statement.
fmt.Println(n)

//nolint:forbidigo // This is an expected print statement.
fmt.Printf("Found %d notifications %s\n",
len(n),
text.RelativeTimeAgo(
Expand All @@ -204,13 +208,15 @@ func displayJSON(notifications notifications.Notifications) error {
return err
}

//nolint:forbidigo // This is an expected print statement.
fmt.Printf("%s\n", marshaled)

return nil
}

func displayTags(n notifications.Notifications) error {
for tag, count := range n.TagsMap() {
//nolint:forbidigo // This is an expected print statement.
fmt.Printf("%s: %d\n", tag, count)
}

Expand All @@ -223,6 +229,7 @@ func displayRepl(n notifications.Notifications) error {
slog.Error("Failed to create an API REST client", "err", err)
return err
}

manager.SetCaller(caller)

// Launching bubbletea will occupy STDOUT and STDERR, so we need to redirect
Expand Down
8 changes: 7 additions & 1 deletion internal/cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ See synchronization logic at https://pkg.go.dev/github.com/nobe4/gh-not/internal
}
)

//nolint:lll
//nolint:lll // Having the whole flag definition on a single line is OK.
func init() {
rootCmd.AddCommand(syncCmd)

Expand All @@ -53,6 +53,7 @@ func init() {

func runSync(_ *cobra.Command, _ []string) error {
var caller api.Requestor

var err error

if notificationDumpPath != "" {
Expand All @@ -64,6 +65,7 @@ func runSync(_ *cobra.Command, _ []string) error {
return err
}
}

manager.SetCaller(caller)

manager.ForceStrategy = forceStrategy
Expand All @@ -73,25 +75,29 @@ func runSync(_ *cobra.Command, _ []string) error {
slog.Error("Failed to load the notifications", "err", err)
return err
}

loadedNotifications := len(manager.Notifications)

if err := manager.Refresh(); err != nil {
slog.Error("Failed to refresh the notifications", "err", err)
return err
}

refreshedNotifications := len(manager.Notifications)

if err := manager.Apply(); err != nil {
slog.Error("Failed to applying rules", "err", err)
return err
}

visibleNotifications := len(manager.Notifications.Visible())

if err := manager.Save(); err != nil {
slog.Error("Failed to save the notifications", "err", err)
return err
}

//nolint:forbidigo // This is an expected print statement.
fmt.Printf("Loaded %d, refreshed %d, visible %d at %s\n",
loadedNotifications,
refreshedNotifications,
Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Data struct {

// Endpoint is the configuration for the GitHub API endpoint.
//
//nolint:lll
//nolint:lll // Links can be long.
type Endpoint struct {
// Pull all notifications from the endpoint.
// By default, only the unread notifications are fetched.
Expand Down Expand Up @@ -77,6 +77,7 @@ type View struct {

func Default(path string) (*viper.Viper, string) {
slog.Debug("loading default configuration")

if path == "" {
path = filepath.Join(ConfigDir(), "config.yaml")
slog.Debug("path is empty, setting default path", "default path", path)
Expand Down Expand Up @@ -110,6 +111,7 @@ func New(path string) (*Config, error) {
return nil, err
}
}

c.Path = c.viper.ConfigFileUsed()

if err := c.viper.Unmarshal(&c.Data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

import "path"

//nolint:gochecknoglobals
//nolint:gochecknoglobals,mnd // This is used as a default in a couple of places.
var Defaults = map[string]any{
"cache.ttl_in_hours": 1,
"cache.path": path.Join(StateDir(), "cache.json"),
Expand Down
3 changes: 2 additions & 1 deletion internal/config/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type (

type KeyBinding []string

//nolint:gochecknoglobals
//nolint:gochecknoglobals // This replacement list is used a lot.
var unicodeReplacement = []string{
"up", "↑",
"down", "↓",
Expand All @@ -28,6 +28,7 @@ var unicodeReplacement = []string{

func (k Keymap) Binding(mode, action string) key.Binding {
keys := k[mode][action]

return key.NewBinding(
key.WithKeys(keys...),
key.WithHelp(keys.Help(), action),
Expand Down
2 changes: 2 additions & 0 deletions internal/config/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func ConfigDir() string {
d, _ := os.UserHomeDir()
path = filepath.Join(d, ".config", "gh-not")
}

return path
}

Expand All @@ -42,5 +43,6 @@ func StateDir() string {
c, _ := os.UserHomeDir()
path = filepath.Join(c, ".local", "state", "gh-not")
}

return path
}
8 changes: 8 additions & 0 deletions internal/gh/enrichments.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (c *Client) Enrich(n *notifications.Notification) error {
if err != nil {
return err
}

n.Author = threadExtra.User
n.Subject.State = threadExtra.State
n.Subject.HTMLURL = threadExtra.HTMLURL
Expand All @@ -38,6 +39,7 @@ func (c *Client) Enrich(n *notifications.Notification) error {
if err != nil {
return err
}

n.LatestCommentor = LastCommentor

return nil
Expand All @@ -49,10 +51,12 @@ func (c *Client) getThreadExtra(n *notifications.Notification) (ThreadExtra, err
}

slog.Debug("getting the thread extra", "id", n.ID, "url", n.Subject.URL)

resp, err := c.API.Request(http.MethodGet, n.Subject.URL, nil)
if err != nil {
return ThreadExtra{}, err
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
Expand All @@ -61,6 +65,7 @@ func (c *Client) getThreadExtra(n *notifications.Notification) (ThreadExtra, err
}

extra := ThreadExtra{}

err = json.Unmarshal(body, &extra)
if err != nil {
return ThreadExtra{}, err
Expand All @@ -75,10 +80,12 @@ func (c *Client) getLastCommentor(n *notifications.Notification) (notifications.
}

slog.Debug("getting the last commentor", "id", n.ID, "url", n.Subject.LatestCommentURL)

resp, err := c.API.Request(http.MethodGet, n.Subject.LatestCommentURL, nil)
if err != nil {
return notifications.User{}, err
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
Expand All @@ -89,6 +96,7 @@ func (c *Client) getLastCommentor(n *notifications.Notification) (notifications.
comment := struct {
User notifications.User `json:"user"`
}{}

err = json.Unmarshal(body, &comment)
if err != nil {
return notifications.User{}, err
Expand Down
Loading
Loading