Skip to content

Commit

Permalink
feat(ci): add a bunch more linters
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Sep 22, 2023
1 parent 33f1f2e commit e3dd812
Show file tree
Hide file tree
Showing 37 changed files with 233 additions and 139 deletions.
44 changes: 44 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,72 @@ run:
linters:
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- decorder
- dogsled
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exportloopref
- forbidigo
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- gofumpt
- goheader
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- importas
- ineffassign
- loggercheck
- makezero
- mirror
- misspell
- musttag
- nakedret
- nestif
- nilerr
- nosprintfhostport
- paralleltest
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- stylecheck
- tenv
- testableexamples
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- wastedassign
- whitespace
- zerologlint

issues:
exclude-rules:
Expand Down
10 changes: 5 additions & 5 deletions cmd/gonic/gonic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Package main is the gonic server entrypoint
//
//nolint:lll,gocyclo
//nolint:lll,gocyclo,forbidigo
package main

import (
Expand Down Expand Up @@ -370,8 +368,10 @@ func main() {

const pathAliasSep = "->"

type pathAliases []pathAlias
type pathAlias struct{ alias, path string }
type (
pathAliases []pathAlias
pathAlias struct{ alias, path string }
)

func (pa pathAliases) String() string {
var strs []string
Expand Down
2 changes: 2 additions & 0 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func randKey() string {
}

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

key := SettingKey(randKey())
value := "howdy"

Expand Down
4 changes: 1 addition & 3 deletions db/model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Package db provides database helpers and models
//
//nolint:lll // struct tags get very long and can't be split
package db

Expand Down Expand Up @@ -238,7 +236,7 @@ func (a *Album) GenreStrings() []string {
}

func (a *Album) ArtistsStrings() []string {
var artists = append([]*Artist(nil), a.Artists...)
artists := append([]*Artist(nil), a.Artists...)
sort.Slice(artists, func(i, j int) bool {
return artists[i].ID < artists[j].ID
})
Expand Down
5 changes: 4 additions & 1 deletion fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
)

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

unq := func(base, filename string, count uint) string {
r, err := unique(base, filename, count)
require.NoError(t, err)
Expand Down Expand Up @@ -40,6 +42,8 @@ func TestUniquePath(t *testing.T) {
}

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

base := t.TempDir()
name := filepath.Join(base, "test")
_, err := os.Create(name)
Expand All @@ -52,5 +56,4 @@ func TestFirst(t *testing.T) {
r, err := First(p("one"), p("two"), p("test"), p("four"))
require.NoError(t, err)
require.Equal(t, p("test"), r)

}
16 changes: 9 additions & 7 deletions jukebox/jukebox.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,15 @@ func (j *Jukebox) getDecode(dest any, property string) error {
return nil
}

type mpvPlaylist []mpvPlaylistItem
type mpvPlaylistItem struct {
ID int
Filename string
Current bool
Playing bool
}
type (
mpvPlaylist []mpvPlaylistItem
mpvPlaylistItem struct {
ID int
Filename string
Current bool
Playing bool
}
)

func waitUntil(timeout time.Duration, f func() bool) bool {
quit := time.NewTicker(timeout)
Expand Down
12 changes: 7 additions & 5 deletions jukebox/jukebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ import (
"go.senan.xyz/gonic/jukebox"
)

func newJukebox(t *testing.T) *jukebox.Jukebox {
sockPath := filepath.Join(t.TempDir(), "mpv.sock")
func newJukebox(tb testing.TB) *jukebox.Jukebox {
tb.Helper()

sockPath := filepath.Join(tb.TempDir(), "mpv.sock")

j := jukebox.New()
err := j.Start(
sockPath,
[]string{jukebox.MPVArg("--ao", "null")},
)
if errors.Is(err, jukebox.ErrMPVTooOld) {
t.Skip("old mpv found, skipping")
tb.Skip("old mpv found, skipping")
}
if err != nil {
t.Fatalf("start jukebox: %v", err)
tb.Fatalf("start jukebox: %v", err)
}
t.Cleanup(func() {
tb.Cleanup(func() {
j.Quit()
})
return j
Expand Down
8 changes: 5 additions & 3 deletions mime/mime.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ func init() {
}
}

var TypeByExtension = stdmime.TypeByExtension
var ParseMediaType = stdmime.ParseMediaType
var FormatMediaType = stdmime.FormatMediaType
var (
TypeByExtension = stdmime.TypeByExtension
ParseMediaType = stdmime.ParseMediaType
FormatMediaType = stdmime.FormatMediaType
)

func TypeByAudioExtension(ext string) string {
if _, ok := supportedAudioTypes[strings.ToLower(ext)]; !ok {
Expand Down
36 changes: 15 additions & 21 deletions mockfs/mockfs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:thelper
package mockfs

import (
Expand Down Expand Up @@ -27,37 +28,39 @@ type MockFS struct {
db *db.DB
}

func New(t testing.TB) *MockFS { return newMockFS(t, []string{""}, "") }
func NewWithDirs(t testing.TB, dirs []string) *MockFS { return newMockFS(t, dirs, "") }
func NewWithExcludePattern(t testing.TB, excludePattern string) *MockFS {
return newMockFS(t, []string{""}, excludePattern)
func New(tb testing.TB) *MockFS { return newMockFS(tb, []string{""}, "") }
func NewWithDirs(tb testing.TB, dirs []string) *MockFS { return newMockFS(tb, dirs, "") }
func NewWithExcludePattern(tb testing.TB, excludePattern string) *MockFS {
return newMockFS(tb, []string{""}, excludePattern)
}

func newMockFS(t testing.TB, dirs []string, excludePattern string) *MockFS {
func newMockFS(tb testing.TB, dirs []string, excludePattern string) *MockFS {
tb.Helper()

dbc, err := db.NewMock()
if err != nil {
t.Fatalf("create db: %v", err)
tb.Fatalf("create db: %v", err)
}
t.Cleanup(func() {
tb.Cleanup(func() {
if err := dbc.Close(); err != nil {
t.Fatalf("close db: %v", err)
tb.Fatalf("close db: %v", err)
}
})

if err := dbc.Migrate(db.MigrationContext{}); err != nil {
t.Fatalf("migrate db db: %v", err)
tb.Fatalf("migrate db db: %v", err)
}
dbc.LogMode(false)

tmpDir := t.TempDir()
tmpDir := tb.TempDir()

var absDirs []string
for _, dir := range dirs {
absDirs = append(absDirs, filepath.Join(tmpDir, dir))
}
for _, absDir := range absDirs {
if err := os.MkdirAll(absDir, os.ModePerm); err != nil {
t.Fatalf("mk abs dir: %v", err)
tb.Fatalf("mk abs dir: %v", err)
}
}

Expand All @@ -70,7 +73,7 @@ func newMockFS(t testing.TB, dirs []string, excludePattern string) *MockFS {
scanner := scanner.New(absDirs, dbc, multiValueSettings, tagReader, excludePattern)

return &MockFS{
t: t,
t: tb,
scanner: scanner,
dir: tmpDir,
tagReader: tagReader,
Expand Down Expand Up @@ -399,15 +402,6 @@ func (m *Tags) Bitrate() int { return firstInt(100, m.RawBitrate) }

var _ tags.Parser = (*Tags)(nil)

func first(or string, strs ...string) string {
for _, str := range strs {
if str != "" {
return str
}
}
return or
}

func firstInt(or int, ints ...int) int {
for _, int := range ints {
if int > 0 {
Expand Down
14 changes: 8 additions & 6 deletions playlist/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"time"
)

var ErrInvalidPathFormat = errors.New("invalid path format")
var ErrInvalidBasePath = errors.New("invalid base path")
var ErrNoUserPrefix = errors.New("no user prefix")
var (
ErrInvalidPathFormat = errors.New("invalid path format")
ErrInvalidBasePath = errors.New("invalid base path")
ErrNoUserPrefix = errors.New("no user prefix")
)

const (
extM3U = ".m3u"
Expand All @@ -31,7 +33,6 @@ type Store struct {
func NewStore(basePath string) (*Store, error) {
if basePath == "" {
return nil, ErrInvalidBasePath

}

// sanity check layout, just in case someone tries to use an existing folder
Expand Down Expand Up @@ -108,6 +109,7 @@ const (
func encodeAttr(name, value string) string {
return fmt.Sprintf("%s%s:%s", attrPrefix, name, strconv.Quote(value))
}

func decodeAttr(line string) (name, value string) {
if !strings.HasPrefix(line, attrPrefix) {
return "", ""
Expand Down Expand Up @@ -169,10 +171,10 @@ func (s *Store) Write(relPath string, playlist *Playlist) error {
defer lock(&s.mu)()

absPath := filepath.Join(s.basePath, relPath)
if err := os.MkdirAll(filepath.Dir(absPath), 0777); err != nil {
if err := os.MkdirAll(filepath.Dir(absPath), 0o777); err != nil {
return fmt.Errorf("make m3u base dir: %w", err)
}
file, err := os.OpenFile(absPath, os.O_RDWR|os.O_CREATE, 0666)
file, err := os.OpenFile(absPath, os.O_RDWR|os.O_CREATE, 0o666)
if err != nil {
return fmt.Errorf("create m3u: %w", err)
}
Expand Down
2 changes: 2 additions & 0 deletions playlist/playlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

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

require := require.New(t)

tmp := t.TempDir()
Expand Down
12 changes: 7 additions & 5 deletions podcasts/podcasts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (

var ErrNoAudioInFeedItem = errors.New("no audio in feed item")

const downloadAllWaitInterval = 3 * time.Second
const fetchUserAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11`
const (
downloadAllWaitInterval = 3 * time.Second
fetchUserAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11`
)

type Podcasts struct {
db *db.DB
Expand Down Expand Up @@ -96,7 +98,6 @@ func (p *Podcasts) AddNewPodcast(rssURL string, feed *gofeed.Feed) (*db.Podcast,
rootDir, err := fileutil.Unique(filepath.Join(p.baseDir, fileutil.Safe(feed.Title)), "")
if err != nil {
return nil, fmt.Errorf("find unique podcast dir: %w", err)

}
podcast := db.Podcast{
Description: feed.Description,
Expand All @@ -105,7 +106,7 @@ func (p *Podcasts) AddNewPodcast(rssURL string, feed *gofeed.Feed) (*db.Podcast,
URL: rssURL,
RootDir: rootDir,
}
if err := os.Mkdir(podcast.RootDir, 0755); err != nil && !os.IsExist(err) {
if err := os.Mkdir(podcast.RootDir, 0o755); err != nil && !os.IsExist(err) {
return nil, err
}
if err := p.db.Save(&podcast).Error; err != nil {
Expand Down Expand Up @@ -248,7 +249,8 @@ func isAudio(rawItemURL string) (bool, error) {
}

func itemToEpisode(podcastID, size, duration int, audio string,
item *gofeed.Item) *db.PodcastEpisode {
item *gofeed.Item,
) *db.PodcastEpisode {
return &db.PodcastEpisode{
PodcastID: podcastID,
Description: item.Description,
Expand Down
Loading

0 comments on commit e3dd812

Please sign in to comment.