From fcdabbe419631390d80e3243d0e74ae544422a9f Mon Sep 17 00:00:00 2001 From: Bence Csati Date: Wed, 17 Jan 2024 12:27:01 +0100 Subject: [PATCH] feat(lint) utilize and improve golangci-lint, connect to ci --- .github/workflows/ci.yaml | 21 ++++++++ .gitignore | 4 +- golangci.yml => .golangci.yaml | 50 +++++++++---------- Makefile | 22 +++++++- commands/player/saved.go | 2 +- commands/search/search.go | 5 +- .../searchPrompt/createSelectionPrompt.go | 3 +- .../search/searchPrompt/searchQueryPrompt.go | 7 +-- .../searchPrompt/searchResultsAlbumsPrompt.go | 5 +- .../searchResultsArtistsPrompt.go | 5 +- .../searchResultsAudiobooksPrompt.go | 5 +- .../searchResultsEpisodesPrompt.go | 5 +- .../searchResultsPlaylistsPrompt.go | 3 +- .../searchPrompt/searchResultsPrompt.go | 5 +- .../searchPrompt/searchResultsShowsPrompt.go | 3 +- .../searchPrompt/searchResultsTracksPrompt.go | 5 +- config/writeSecretsToHome.go | 2 +- config/writeTokenToHome.go | 2 +- server/server.go | 2 +- 19 files changed, 102 insertions(+), 54 deletions(-) rename golangci.yml => .golangci.yaml (79%) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2191319..a2060ad 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -30,6 +30,27 @@ jobs: - name: Build run: go build -v ./... + lint: + name: Lint + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Setup Go cache + uses: actions/cache@v2 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ github.job }}-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ github.job }}-${{ runner.os }}-go- + + - name: Lint + run: make lint + artifacts: name: Artifacts uses: ./.github/workflows/artifacts.yaml diff --git a/.gitignore b/.gitignore index e8ec7d1..fdd8b8b 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,6 @@ Thumbs.db temp.txt -dist/ +/dist/ +/build/ +/bin/ \ No newline at end of file diff --git a/golangci.yml b/.golangci.yaml similarity index 79% rename from golangci.yml rename to .golangci.yaml index 685067a..78555d6 100644 --- a/golangci.yml +++ b/.golangci.yaml @@ -1,42 +1,34 @@ run: - deadline: 5m # Maximum duration for linting processes + timeout: 5m # Maximum duration for linting processes modules-download-mode: readonly # Prevents updating the go.mod file during linting for reproducibility linters: enable: - - lll # Ensures lines do not exceed a specified length - - errcheck # Checks for unchecked errors in the code - - gofmt # Formats Go code according to standard style guidelines - - govet # Analyzes code for suspicious constructs - - golint # Provides suggestions for Go coding style and documentation - - gocyclo # Measures cyclomatic complexity of functions - - ineffassign # Detects ineffectual assignments in Go code - - structcheck # Identifies unused struct fields - - varcheck # Detects unused global variables and constants - - deadcode # Finds unused code in the program - - unconvert # Identifies unnecessary type conversions + - bodyclose # Ensures HTTP response bodies are closed properly + - dogsled # Checks for excessive use of blank identifiers in assignments + - dupl # Reports duplicated code blocks + - gochecknoglobals # Checks for global variables + - gochecknoinits # Warns about the use of init functions - goconst # Finds repeated strings that could be replaced by constants + - gocritic # A comprehensive linter with a wide range of checks + - gocyclo # Measures cyclomatic complexity of functions + - gofmt # Formats Go code according to standard style guidelines - godot # Checks if comments end in a period - goimports # Formats import lines, adding missing ones and removing unreferenced ones + - gosec # Scans code for security vulnerabilities + - gomnd # Detects magic numbers and suggests naming them - gomodguard # Manages allowed and blocked Go module dependencies - - gosimple # Simplifies Go code by applying simplifications - - staticcheck # Performs a range of static analysis checks for cleaner, more reliable, and more efficient Go code - - dupl # Reports duplicated code blocks - - nakedret # Identifies naked returns in functions larger than a specified size + - govet # Analyzes code for suspicious constructs + - ineffassign # Detects ineffectual assignments in Go code + - lll # Ensures lines do not exceed a specified length - misspell # Checks for misspelled words in the code + - nakedret # Identifies naked returns in functions larger than a specified size + - revive # Performs a range of static analysis checks for cleaner, more reliable, and more efficient Go code - prealloc # Suggests preallocations in loops to improve performance - - gosec # Scans code for security vulnerabilities - - interfacer # Suggests narrower interface types - - unparam # Reports unused function parameters - - funlen # Warns about long functions - - wsl # Enforces consistent whitespace usage + - staticcheck # Performs a range of static analysis checks for cleaner, more reliable, and more efficient Go code - testpackage # Encourages writing tests in a separate _test package - - bodyclose # Ensures HTTP response bodies are closed properly - - gocritic # A comprehensive linter with a wide range of checks - - gochecknoglobals # Checks for global variables - - gochecknoinits # Warns about the use of init functions - - dogsled # Checks for excessive use of blank identifiers in assignments - - gomnd # Detects magic numbers and suggests naming them + - unconvert # Identifies unnecessary type conversions + - unparam # Reports unused function parameters linters-settings: errcheck: @@ -47,6 +39,10 @@ linters-settings: tab-width: 8 # Width of a tab character (for line length calculation) gocyclo: min-complexity: 15 # Minimum complexity threshold to trigger a warning or error + misspell: + locale: US + revive: + confidence: 0 # Exclude specific files or paths from linting by certain linters issues: diff --git a/Makefile b/Makefile index e61ebb7..473bfe0 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ export PATH := $(abspath bin/):${PATH} # Dependency versions +GOLANGCI_VERSION = 1.53.3 GORELEASER_VERSION = 1.18.2 ##@ General @@ -36,11 +37,28 @@ binary-snapshot: ## Build binary snapshot test: ## Run tests go test -race -v ./ +.PHONY: lint +lint: lint-go +lint: ## Run linters + +.PHONY: lint-go +lint-go: + golangci-lint run $(if ${CI},--out-format github-actions,) + +.PHONY: fmt +fmt: ## Format code + golangci-lint run --fix + ##@ Dependencies -deps: bin/goreleaser +deps: golangci-lint goreleaser deps: ## Install dependencies -bin/goreleaser: +golangci-lint: + @mkdir -p bin + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- v${GOLANGCI_VERSION} + +goreleaser: + @mkdir -p bin go install github.com/sigstore/cosign/v2/cmd/cosign@latest curl -sfL https://goreleaser.com/static/run | VERSION=v${GORELEASER_VERSION} bash -s -- --version \ No newline at end of file diff --git a/commands/player/saved.go b/commands/player/saved.go index 900dbe4..d50eea7 100644 --- a/commands/player/saved.go +++ b/commands/player/saved.go @@ -137,7 +137,7 @@ func SavedCommand(cfg *config.Config) *cobra.Command { token := server.ReadUserModifyTokenOrFetchFromServer(cfg) // instead of Calling Play function, we are adding song to the queue and using Next function // otherwise song playing further nexts is not possible, seems like an API limitation. - //Play(token, result.PlayUrl) + // Play(token, result.PlayUrl) AddToQueue(cfg, token, result.PlayUrl) Next(cfg, token, false) } diff --git a/commands/search/search.go b/commands/search/search.go index 8f7dc43..cd42fab 100644 --- a/commands/search/search.go +++ b/commands/search/search.go @@ -1,9 +1,10 @@ package search import ( + "net/url" + "github.com/envoy49/go-spotify-cli/commands/cmdTypes" "github.com/envoy49/go-spotify-cli/config" - "net/url" "github.com/envoy49/go-spotify-cli/commands" "github.com/envoy49/go-spotify-cli/commands/player" @@ -66,7 +67,7 @@ func search(cfg *config.Config, accessToken string, query *cmdTypes.SpotifySearc if len(result.PlayUrl) > 0 { // instead of Calling Play function, we are adding song to the queue and using Next function // otherwise song playing further nexts is not possible - //player.Play(accessToken, playUrl) + // player.Play(accessToken, playUrl) player.AddToQueue(cfg, accessToken, result.PlayUrl) player.Next(cfg, accessToken, false) } diff --git a/commands/search/searchPrompt/createSelectionPrompt.go b/commands/search/searchPrompt/createSelectionPrompt.go index 7e61247..9905e92 100644 --- a/commands/search/searchPrompt/createSelectionPrompt.go +++ b/commands/search/searchPrompt/createSelectionPrompt.go @@ -1,9 +1,10 @@ package searchPrompt import ( + "strings" + "github.com/envoy49/go-spotify-cli/commands/cmdTypes" "github.com/manifoldco/promptui" - "strings" ) func CreateSelectionPrompt(config *cmdTypes.SelectionPromptConfig) promptui.Select { diff --git a/commands/search/searchPrompt/searchQueryPrompt.go b/commands/search/searchPrompt/searchQueryPrompt.go index c64ae97..47031cd 100644 --- a/commands/search/searchPrompt/searchQueryPrompt.go +++ b/commands/search/searchPrompt/searchQueryPrompt.go @@ -2,14 +2,15 @@ package searchPrompt import ( "fmt" - "github.com/envoy49/go-spotify-cli/commands/cmdTypes" - "github.com/manifoldco/promptui" "log" "strconv" "strings" + + "github.com/envoy49/go-spotify-cli/commands/cmdTypes" + "github.com/manifoldco/promptui" ) -// var searchTypes = []string{"Track", "Artist", "Album", "Playlist", "Show", "Episode", "Audiobook"} +// var searchTypes = []string{"Track", "Artist", "Album", "Playlist", "Show", "Episode", "Audiobook"}. var searchTypes = []string{"Track", "Episode"} func SpotifySearchQueryPrompt() (error, *cmdTypes.SpotifySearchQuery) { diff --git a/commands/search/searchPrompt/searchResultsAlbumsPrompt.go b/commands/search/searchPrompt/searchResultsAlbumsPrompt.go index 70f46c4..69ed0a2 100644 --- a/commands/search/searchPrompt/searchResultsAlbumsPrompt.go +++ b/commands/search/searchPrompt/searchResultsAlbumsPrompt.go @@ -2,12 +2,13 @@ package searchPrompt import ( "fmt" + "strconv" + "strings" + "github.com/envoy49/go-spotify-cli/commands" "github.com/envoy49/go-spotify-cli/commands/cmdTypes" "github.com/manifoldco/promptui" "github.com/sirupsen/logrus" - "strconv" - "strings" ) func AlbumsResultsPrompt(albums *cmdTypes.Albums) string { diff --git a/commands/search/searchPrompt/searchResultsArtistsPrompt.go b/commands/search/searchPrompt/searchResultsArtistsPrompt.go index fd07453..e6a0cfd 100644 --- a/commands/search/searchPrompt/searchResultsArtistsPrompt.go +++ b/commands/search/searchPrompt/searchResultsArtistsPrompt.go @@ -2,12 +2,13 @@ package searchPrompt import ( "fmt" + "strconv" + "strings" + "github.com/envoy49/go-spotify-cli/commands" "github.com/envoy49/go-spotify-cli/commands/cmdTypes" "github.com/manifoldco/promptui" "github.com/sirupsen/logrus" - "strconv" - "strings" ) func ArtistsResultsPrompt(artists *cmdTypes.Artists) string { diff --git a/commands/search/searchPrompt/searchResultsAudiobooksPrompt.go b/commands/search/searchPrompt/searchResultsAudiobooksPrompt.go index 21fc8f6..2fa4718 100644 --- a/commands/search/searchPrompt/searchResultsAudiobooksPrompt.go +++ b/commands/search/searchPrompt/searchResultsAudiobooksPrompt.go @@ -2,12 +2,13 @@ package searchPrompt import ( "fmt" + "strconv" + "strings" + "github.com/envoy49/go-spotify-cli/commands" "github.com/envoy49/go-spotify-cli/commands/cmdTypes" "github.com/manifoldco/promptui" "github.com/sirupsen/logrus" - "strconv" - "strings" ) func AudiobooksResultsPrompt(audiobooks *cmdTypes.Audiobooks) string { diff --git a/commands/search/searchPrompt/searchResultsEpisodesPrompt.go b/commands/search/searchPrompt/searchResultsEpisodesPrompt.go index a5d3803..28a746d 100644 --- a/commands/search/searchPrompt/searchResultsEpisodesPrompt.go +++ b/commands/search/searchPrompt/searchResultsEpisodesPrompt.go @@ -2,12 +2,13 @@ package searchPrompt import ( "fmt" + "os" + "strconv" + "github.com/envoy49/go-spotify-cli/commands" "github.com/envoy49/go-spotify-cli/commands/cmdTypes" "github.com/sirupsen/logrus" "golang.org/x/term" - "os" - "strconv" ) func EpisodesResultsPrompt(episodes *cmdTypes.Episodes) *cmdTypes.SearchPromptResults { diff --git a/commands/search/searchPrompt/searchResultsPlaylistsPrompt.go b/commands/search/searchPrompt/searchResultsPlaylistsPrompt.go index 84ecdb1..59a40ab 100644 --- a/commands/search/searchPrompt/searchResultsPlaylistsPrompt.go +++ b/commands/search/searchPrompt/searchResultsPlaylistsPrompt.go @@ -2,11 +2,12 @@ package searchPrompt import ( "fmt" + "strings" + "github.com/envoy49/go-spotify-cli/commands" "github.com/envoy49/go-spotify-cli/commands/cmdTypes" "github.com/manifoldco/promptui" "github.com/sirupsen/logrus" - "strings" ) func PlaylistsResultsPrompt(playlists *cmdTypes.Playlists) string { diff --git a/commands/search/searchPrompt/searchResultsPrompt.go b/commands/search/searchPrompt/searchResultsPrompt.go index a37b37a..72b958f 100644 --- a/commands/search/searchPrompt/searchResultsPrompt.go +++ b/commands/search/searchPrompt/searchResultsPrompt.go @@ -2,8 +2,9 @@ package searchPrompt import ( "encoding/json" - "github.com/envoy49/go-spotify-cli/commands/cmdTypes" "log" + + "github.com/envoy49/go-spotify-cli/commands/cmdTypes" ) func SpotifySearchResultsPrompt(body []byte) *cmdTypes.SearchPromptResults { @@ -18,7 +19,7 @@ func SpotifySearchResultsPrompt(body []byte) *cmdTypes.SearchPromptResults { return TracksResultsPrompt(response.Tracks) case response.Episodes != nil: return EpisodesResultsPrompt(response.Episodes) - //case response.Albums != nil: + // case response.Albums != nil: // return AlbumsResultsPrompt(response.Albums) //case response.Artists != nil: // return ArtistsResultsPrompt(response.Artists) diff --git a/commands/search/searchPrompt/searchResultsShowsPrompt.go b/commands/search/searchPrompt/searchResultsShowsPrompt.go index af3dc4d..e714156 100644 --- a/commands/search/searchPrompt/searchResultsShowsPrompt.go +++ b/commands/search/searchPrompt/searchResultsShowsPrompt.go @@ -2,11 +2,12 @@ package searchPrompt import ( "fmt" + "strings" + "github.com/envoy49/go-spotify-cli/commands" "github.com/envoy49/go-spotify-cli/commands/cmdTypes" "github.com/manifoldco/promptui" "github.com/sirupsen/logrus" - "strings" ) func ShowsResultsPrompt(shows *cmdTypes.Shows) string { diff --git a/commands/search/searchPrompt/searchResultsTracksPrompt.go b/commands/search/searchPrompt/searchResultsTracksPrompt.go index 123b079..17afe42 100644 --- a/commands/search/searchPrompt/searchResultsTracksPrompt.go +++ b/commands/search/searchPrompt/searchResultsTracksPrompt.go @@ -2,12 +2,13 @@ package searchPrompt import ( "fmt" + "os" + "strconv" + "github.com/envoy49/go-spotify-cli/commands" "github.com/envoy49/go-spotify-cli/commands/cmdTypes" "github.com/sirupsen/logrus" "golang.org/x/term" - "os" - "strconv" ) func TracksResultsPrompt(tracks *cmdTypes.Tracks) *cmdTypes.SearchPromptResults { diff --git a/config/writeSecretsToHome.go b/config/writeSecretsToHome.go index 15055dc..591ebba 100644 --- a/config/writeSecretsToHome.go +++ b/config/writeSecretsToHome.go @@ -41,7 +41,7 @@ func WriteSecretsToHomeDirectory(cfg *Config) (*Config, error) { // pass this a // Marshal the configData into YAML format data, err := yaml.Marshal(configData) if err != nil { - logrus.WithError(err).Error("Error marshalling data to YAML") + logrus.WithError(err).Error("Error marshaling data to YAML") return nil, err } diff --git a/config/writeTokenToHome.go b/config/writeTokenToHome.go index 99e8ff2..3fbaab9 100644 --- a/config/writeTokenToHome.go +++ b/config/writeTokenToHome.go @@ -101,7 +101,7 @@ func WriteTokenToHomeDirectory(configData *CombinedTokenStructure, initiateChann // Marshal the updated data data, err := yaml.Marshal(currentData) if err != nil { - logrus.WithError(err).Error("Error marshalling data to YAML") + logrus.WithError(err).Error("Error marshaling data to YAML") return } diff --git a/server/server.go b/server/server.go index e79c3b8..4a5e9ff 100644 --- a/server/server.go +++ b/server/server.go @@ -29,7 +29,7 @@ func Server(ctx context.Context, cfg *config.Config) { } }() - // Listen for the context being cancelled + // Listen for the context being canceled <-ctx.Done() // Create a deadline to wait for