Skip to content

Commit

Permalink
chore: enable yet more linters (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobe4 authored Jan 8, 2025
1 parent 6693bd8 commit 97bf1da
Show file tree
Hide file tree
Showing 23 changed files with 177 additions and 117 deletions.
43 changes: 38 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ linters:
enable-all: true

disable:
# TODO: remove the line and fix the issues
- err113
- ireturn
- revive

- cyclop # some functions need refactoring, I'll deal with that later
- depguard # I'm not that pedantic
- exhaustruct # it's ok not to specify all the fields in a struct definition
Expand All @@ -27,6 +22,7 @@ issues:
linters:
- bodyclose
- funlen
- revive

- path: internal/cmd
linters:
Expand All @@ -35,6 +31,11 @@ issues:
# using globals is recommended with spf13/cobra
- gochecknoglobals

- path: internal/repl
linters:
# returning interface is the way to work with charmbracelet/bubbletea
- ireturn

linters-settings:
nolintlint:
require-explanation: true
Expand All @@ -48,10 +49,42 @@ linters-settings:
revive:
enable-all-rules: true
rules:
- name: add-constant
disabled: true

- name: context-as-argument
arguments:
- allowTypesBefore: "*testing.T"

- name: comment-spacings
arguments:
- "nolint"

- name: cognitive-complexity
disabled: true # TODO: re enable to simplify functions

- name: cyclomatic
disabled: true # TODO: re enable to simplify functions

- name: max-public-structs
disabled: true # I might enable that later


- name: import-alias-naming
arguments:
- "^[a-z][A-Za-z0-9]{0,}$"

- name: line-length-limit
disabled: true # using lll

- name: unhandled-error
arguments:
- "fmt.Printf"
- "fmt.Println"
- "fmt.Fprint"
- "fmt.Fprintf"
- "fmt.Fprintln"

tagliatelle:
case:
rules:
Expand Down
4 changes: 2 additions & 2 deletions internal/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"github.com/nobe4/gh-not/internal/notifications"
)

type ActionsMap map[string]Runner
type Map map[string]Runner

func Map(client *gh.Client) ActionsMap {
func GetMap(client *gh.Client) Map {
return map[string]Runner{
"pass": &pass.Runner{},
"debug": &debug.Runner{},
Expand Down
4 changes: 3 additions & 1 deletion internal/actions/assign/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ type Body struct {
Assignees []string `json:"assignees"`
}

var errNoAssignees = errors.New("no assignees provided")

func (a *Runner) Run(n *notifications.Notification, assignees []string, w io.Writer) error {
slog.Debug("assigning notification", "notification", n, "assignees", assignees)

if len(assignees) == 0 {
return errors.New("no assignees provided")
return errNoAssignees
}

url, ok := issueURL(n.Subject.URL)
Expand Down
9 changes: 5 additions & 4 deletions internal/actions/assign/assign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/nobe4/gh-not/internal/notifications"
)

var errExpected = errors.New("expected error")

func TestRun(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -60,22 +62,21 @@ func TestRun(t *testing.T) {
api := &mock.Mock{}
client := gh.Client{API: api}
runner := Runner{Client: &client}
expectedError := errors.New("expected error")

api.Calls = append(api.Calls, mock.Call{
Verb: "POST",
URL: "https://api.github.com/repos/owner/repo/issues/123/assignees",
Data: `{"assignees":["user"]}`,
Error: expectedError,
Error: errExpected,
})
n := &notifications.Notification{
Subject: notifications.Subject{
URL: "https://api.github.com/repos/owner/repo/pulls/123",
},
}

if err := runner.Run(n, []string{"user"}, w); !errors.Is(err, expectedError) {
t.Fatalf("expected %#v but got %#v", expectedError, err)
if err := runner.Run(n, []string{"user"}, w); !errors.Is(err, errExpected) {
t.Fatalf("expected %#v but got %#v", errExpected, err)
}

if err := api.Done(); err != nil {
Expand Down
15 changes: 10 additions & 5 deletions internal/actions/open/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@ import (
"github.com/nobe4/gh-not/internal/notifications"
)

var errNoURL = errors.New("no URL to open")

type Runner struct {
Client *gh.Client
}

func (a *Runner) Run(n *notifications.Notification, _ []string, w io.Writer) error {
func (*Runner) Run(n *notifications.Notification, _ []string, w io.Writer) error {
slog.Debug("open notification in browser", "notification", n)

browser := browser.New("", w, w)
b := browser.New("", w, w)

if n.Subject.HTMLURL == "" {
return errors.New("no URL to open")
return errNoURL
}

err := browser.Browse(n.Subject.HTMLURL)
fmt.Fprint(w, colors.Blue("OPEN ")+n.Subject.URL)

return fmt.Errorf("failed to open browser: %w", err)
if err := b.Browse(n.Subject.HTMLURL); err != nil {
return fmt.Errorf("failed to open browser: %w", err)
}

return nil
}
3 changes: 2 additions & 1 deletion internal/api/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ func New(path string) *API {
return &API{path: path}
}

func (a *API) Request(verb string, url string, _ io.Reader) (*http.Response, error) {
func (a *API) Request(verb string, _ string, _ io.Reader) (*http.Response, error) {
if verb == "GET" {
return a.readFile()
}

//nolint:err113 // this is a TODO
return nil, errors.New("TODO")
}

Expand Down
4 changes: 1 addition & 3 deletions internal/api/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"fmt"

gh "github.com/cli/go-gh/v2/pkg/api"

"github.com/nobe4/gh-not/internal/api"
)

func New() (api.Requestor, error) {
func New() (*gh.RESTClient, error) {
client, err := gh.DefaultRESTClient()
if err != nil {
return nil, fmt.Errorf("failed to create client: %w", err)
Expand Down
10 changes: 5 additions & 5 deletions internal/api/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@ type Mock struct {
index int
}

type MockError struct {
type Error struct {
verb string
endpoint string
message string
}

func (e *MockError) Error() string {
func (e *Error) Error() string {
return fmt.Sprintf("mock error for call [%s %s]: %s", e.verb, e.endpoint, e.message)
}

func (m *Mock) Done() error {
if m.index < len(m.Calls) {
return &MockError{"", "", fmt.Sprintf("%d calls remaining", len(m.Calls)-m.index)}
return &Error{"", "", fmt.Sprintf("%d calls remaining", len(m.Calls)-m.index)}
}

return nil
}

func (m *Mock) call(verb, endpoint string) (Call, error) {
if m.index >= len(m.Calls) {
return Call{}, &MockError{verb, endpoint, "unexpected call: no more calls"}
return Call{}, &Error{verb, endpoint, "unexpected call: no more calls"}
}

call := m.Calls[m.index]
if (call.Verb != "" && call.Verb != verb) || (call.URL != "" && call.URL != endpoint) {
return Call{}, &MockError{
return Call{}, &Error{
verb,
endpoint,
fmt.Sprintf("unexpected call: mismatch, expected [%s %s]", call.Verb, call.URL),
Expand Down
22 changes: 11 additions & 11 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ type RefreshReadWriter interface {
RefreshedAt() time.Time
}

type FileCache struct {
type File struct {
path string
wrap *CacheWrap
wrap *Wrap
}

type CacheWrap struct {
type Wrap struct {
Data any `json:"data"`
RefreshedAt time.Time `json:"refreshed_at"`
}

func NewFileCache(path string) *FileCache {
return &FileCache{
func NewFileCache(path string) *File {
return &File{
path: path,
wrap: &CacheWrap{RefreshedAt: time.Unix(0, 0)},
wrap: &Wrap{RefreshedAt: time.Unix(0, 0)},
}
}

func (c *FileCache) Read(out any) error {
func (c *File) Read(out any) error {
slog.Debug("Reading cache", "path", c.path)

content, err := os.ReadFile(c.path)
Expand Down Expand Up @@ -70,15 +70,15 @@ func (c *FileCache) Read(out any) error {
return fmt.Errorf("failed to unmarshal cache: %w", err)
}

func (c *FileCache) Refresh(t time.Time) {
func (c *File) Refresh(t time.Time) {
c.wrap.RefreshedAt = t
}

func (c *FileCache) RefreshedAt() time.Time {
func (c *File) RefreshedAt() time.Time {
return c.wrap.RefreshedAt
}

func (c *FileCache) deprecatedRead(content []byte) error {
func (c *File) deprecatedRead(content []byte) error {
slog.Warn("Cache is in an format deprecated in v0.5.0. Attempting to read from the old format.")

if err := json.Unmarshal(content, c.wrap.Data); err != nil {
Expand All @@ -90,7 +90,7 @@ func (c *FileCache) deprecatedRead(content []byte) error {
return nil
}

func (c *FileCache) Write(in any) error {
func (c *File) Write(in any) error {
c.wrap.Data = in

marshaled, err := json.Marshal(c.wrap)
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
Short: "Print the config to stdout",
RunE: runConfig,
}
errNoEditor = errors.New("EDITOR environment variable not set")
)

func init() {
Expand Down Expand Up @@ -78,7 +79,7 @@ func editConfig() error {

editor := os.Getenv("EDITOR")
if editor == "" {
return errors.New("EDITOR environment variable not set")
return errNoEditor
}

cmd := exec.Command(editor, config.Path)
Expand Down
Loading

0 comments on commit 97bf1da

Please sign in to comment.