From 4bb725db9e51d196b88f2333acbfd0917ecd9c1d Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Sat, 16 May 2020 11:59:24 -0700 Subject: [PATCH] golangci-lint configuration file golangci-lint can run all the currently enabled linters, and as far as I can tell, does it in under 5 seconds as opposed to over 180 seconds (compare `time make cilint` and `time make lint`). Some of the linters that are listed in the "enabled" section but commented out looked like a good idea to me, and fairly low hanging fruit to fix, but they are not passing at the moment. All the linters covered in the current Makefile are run. TODO: - replace lint target in Makefile with golangci-lint - remove .github/workflow/errcheck.yml --- .github/workflows/golangci-lint.yml | 20 +++++++++++++++++++ .golangci.yml | 15 ++++++++++++++ Makefile | 3 +++ app/focus_tracker.go | 2 +- app/wtf_app.go | 1 + cfg/config_files.go | 2 +- modules/cds/queue/display.go | 7 ++++--- modules/cds/status/display.go | 13 ++++++------ .../cryptolive/price/widget.go | 1 - modules/digitalclock/clocks.go | 8 ++++---- modules/digitalclock/display.go | 2 +- modules/gcal/display.go | 7 ++++--- modules/gitlabtodo/widget.go | 2 +- modules/hibp/widget.go | 2 +- modules/jenkins/widget.go | 4 ++-- modules/kubernetes/widget.go | 7 ++++--- modules/pocket/client.go | 4 ++-- modules/resourceusage/widget.go | 2 +- modules/transmission/display.go | 7 ++++--- modules/travisci/widget.go | 4 ++-- modules/zendesk/tickets.go | 1 - utils/utils_test.go | 4 ++-- view/base.go | 7 ++++--- 23 files changed, 84 insertions(+), 41 deletions(-) create mode 100644 .github/workflows/golangci-lint.yml create mode 100644 .golangci.yml diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 00000000..2154755f --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,20 @@ +name: golangci-lint +on: + push: + tags: + - v* + branches: + - master + pull_request: +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v0.2.0 + with: + # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. + version: v1.27 + args: ./... diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..fe25baee --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,15 @@ +linters: + enable: + - vet + - errcheck + - staticcheck +# - dupl +# - funlen +# - goconst +# - gocritic + - gofmt +# - golint +# - misspell +# - stylecheck + - unconvert +# - whitespace diff --git a/Makefile b/Makefile index e4d813b0..b5335541 100644 --- a/Makefile +++ b/Makefile @@ -86,6 +86,9 @@ install: @echo "${APP} installed into ${INSTALLPATH}" ## lint: runs a number of code quality checks against the source code +cilint: + golangci-lint run + lint: @echo "\033[35mhttps://github.com/kisielk/errcheck\033[0m" errcheck ./app diff --git a/app/focus_tracker.go b/app/focus_tracker.go index a66f72bd..c647fc3d 100644 --- a/app/focus_tracker.go +++ b/app/focus_tracker.go @@ -200,7 +200,7 @@ func (tracker *FocusTracker) focusables() []wtf.Wtfable { } // Sort for deterministic ordering - sort.SliceStable(focusable[:], func(i, j int) bool { + sort.SliceStable(focusable, func(i, j int) bool { iTop := focusable[i].CommonSettings().Top jTop := focusable[j].CommonSettings().Top diff --git a/app/wtf_app.go b/app/wtf_app.go index 98637bb8..5bb40a74 100644 --- a/app/wtf_app.go +++ b/app/wtf_app.go @@ -109,6 +109,7 @@ func (wtfApp *WtfApp) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { switch string(event.Rune()) { case "/": return nil + default: } } diff --git a/cfg/config_files.go b/cfg/config_files.go index 077d1458..184af654 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -78,7 +78,7 @@ func WtfConfigDir() (string, error) { if configDir == "" { configDir = WtfConfigDirV2 } else { - configDir = configDir + "/wtf/" + configDir += "/wtf/" } configDir, err := expandHomeDir(configDir) if err != nil { diff --git a/modules/cds/queue/display.go b/modules/cds/queue/display.go index 5e27a996..9275c9f0 100644 --- a/modules/cds/queue/display.go +++ b/modules/cds/queue/display.go @@ -73,11 +73,12 @@ func (widget *Widget) generateQueueJobLine(id int64, parameters []sdk.Parameter, row[2] = pad(run, 7) row[3] = pad(prj+"/"+workflow+"/"+node, 40) - if status == sdk.StatusBuilding { + switch { + case status == sdk.StatusBuilding: row[1] = pad(fmt.Sprintf(" %s.%s ", executedJob.WorkerName, executedJob.WorkerID), 27) - } else if bookedBy.ID != 0 { + case bookedBy.ID != 0: row[1] = pad(fmt.Sprintf(" %s.%d ", bookedBy.Name, bookedBy.ID), 27) - } else { + default: row[1] = pad("", 27) } diff --git a/modules/cds/status/display.go b/modules/cds/status/display.go index 8e01707a..be75fa9c 100644 --- a/modules/cds/status/display.go +++ b/modules/cds/status/display.go @@ -43,17 +43,18 @@ func (widget *Widget) displayStatus() string { ) for _, line := range status.Lines { - if line.Status == sdk.MonitoringStatusWarn && strings.Contains(line.Component, "Global") { + switch { + case line.Status == sdk.MonitoringStatusWarn && strings.Contains(line.Component, "Global"): globalWarn = append(globalWarn, line.String()) - } else if line.Status != sdk.MonitoringStatusOK && strings.Contains(line.Component, "Global") { + case line.Status != sdk.MonitoringStatusOK && strings.Contains(line.Component, "Global"): globalRed = append(globalRed, line.String()) - } else if strings.Contains(line.Component, "Global") { + case strings.Contains(line.Component, "Global"): global = append(global, line.String()) - } else if line.Status == sdk.MonitoringStatusWarn { + case line.Status == sdk.MonitoringStatusWarn: warn = append(warn, line.String()) - } else if line.Status == sdk.MonitoringStatusOK { + case line.Status == sdk.MonitoringStatusOK: ok = append(ok, line.String()) - } else { + default: red = append(red, line.String()) } } diff --git a/modules/cryptoexchanges/cryptolive/price/widget.go b/modules/cryptoexchanges/cryptolive/price/widget.go index d0381fce..229d55ef 100644 --- a/modules/cryptoexchanges/cryptolive/price/widget.go +++ b/modules/cryptoexchanges/cryptolive/price/widget.go @@ -129,7 +129,6 @@ func (widget *Widget) updateCurrencies() { setPrices(&jsonResponse, fromCurrency) } - } func makeRequest(currency *fromCurrency) *http.Request { diff --git a/modules/digitalclock/clocks.go b/modules/digitalclock/clocks.go index e22ff3b2..4ef2dcef 100644 --- a/modules/digitalclock/clocks.go +++ b/modules/digitalclock/clocks.go @@ -13,7 +13,7 @@ const AM = "A" const PM = "P" const minRowsForBorder = 3 -// Converts integer to string along with makes sure the lenght of string is > 2 +// Converts integer to string along with makes sure the length of string is > 2 func intStrConv(val int) string { valStr := strconv.Itoa(val) @@ -39,9 +39,9 @@ func getHourMinute(hourFormat string) string { } - strMintues := intStrConv(time.Now().Minute()) - strMintues = strMintues + AMPM - return strHours + getColon() + strMintues + strMinutes := intStrConv(time.Now().Minute()) + strMinutes += AMPM + return strHours + getColon() + strMinutes } // Returns the : with blinking based on the seconds diff --git a/modules/digitalclock/display.go b/modules/digitalclock/display.go index bcdf63b7..9320563e 100644 --- a/modules/digitalclock/display.go +++ b/modules/digitalclock/display.go @@ -3,7 +3,7 @@ package digitalclock import "strings" func mergeLines(outString []string) string { - return strings.Join(outString[:], "\n") + return strings.Join(outString, "\n") } func renderWidget(widgetSettings Settings) string { diff --git a/modules/gcal/display.go b/modules/gcal/display.go index 5dad4d23..ba0b8b15 100644 --- a/modules/gcal/display.go +++ b/modules/gcal/display.go @@ -147,11 +147,12 @@ func (widget *Widget) timeUntil(calEvent *CalEvent) string { untilStr := "" color := "[lightblue]" - if days > 0 { + switch { + case days > 0: untilStr = fmt.Sprintf("%dd", days) - } else if hours > 0 { + case hours > 0: untilStr = fmt.Sprintf("%dh", hours) - } else { + default: untilStr = fmt.Sprintf("%dm", mins) if mins < 30 { color = "[red]" diff --git a/modules/gitlabtodo/widget.go b/modules/gitlabtodo/widget.go index 54102c98..e4ef1990 100644 --- a/modules/gitlabtodo/widget.go +++ b/modules/gitlabtodo/widget.go @@ -90,7 +90,7 @@ func (widget *Widget) getTodos(apiKey string) ([]*gitlab.Todo, error) { func (widget *Widget) trimTodoBody(body string) string { r := []rune(body) - // Cut at first occurance of a newline + // Cut at first occurence of a newline for i, a := range r { if a == '\n' { return string(r[:i]) diff --git a/modules/hibp/widget.go b/modules/hibp/widget.go index f55c11ca..91cb4e60 100644 --- a/modules/hibp/widget.go +++ b/modules/hibp/widget.go @@ -68,7 +68,7 @@ func (widget *Widget) content() (string, string, bool) { return title, widget.err.Error(), true } - title = title + widget.sinceDateForTitle() + title += widget.sinceDateForTitle() str := "" for _, status := range widget.statuses { diff --git a/modules/jenkins/widget.go b/modules/jenkins/widget.go index 8a6d0cd7..6f466a33 100644 --- a/modules/jenkins/widget.go +++ b/modules/jenkins/widget.go @@ -82,7 +82,7 @@ func (widget *Widget) content() (string, string, bool) { row := fmt.Sprintf( `[%s] [%s]%-6s[white]`, widget.RowColor(idx), - widget.jobColor(&job), + widget.jobColor(job), jobName, ) @@ -92,7 +92,7 @@ func (widget *Widget) content() (string, string, bool) { return title, str, false } -func (widget *Widget) jobColor(job *Job) string { +func (widget *Widget) jobColor(job Job) string { switch job.Color { case "blue": // Override color if successBallColor boolean param provided in config diff --git a/modules/kubernetes/widget.go b/modules/kubernetes/widget.go index 7aaf2cac..0d40d8d1 100644 --- a/modules/kubernetes/widget.go +++ b/modules/kubernetes/widget.go @@ -198,11 +198,12 @@ func (client *clientInstance) getNodes() ([]string, error) { var nodeStatus string for _, condition := range node.Status.Conditions { if condition.Reason == "KubeletReady" { - if condition.Status == "True" { + switch { + case condition.Status == "True": nodeStatus = "Ready" - } else if condition.Reason == "False" { + case condition.Reason == "False": nodeStatus = "NotReady" - } else { + default: nodeStatus = "Unknown" } } diff --git a/modules/pocket/client.go b/modules/pocket/client.go index 81268598..4b73b9a1 100644 --- a/modules/pocket/client.go +++ b/modules/pocket/client.go @@ -178,7 +178,7 @@ func (client *Client) GetAccessToken(requestToken string) (accessToken string, e } -/*LinkState represents links states to be retrived +/*LinkState represents link states to be retrieved According to the api https://getpocket.com/developer/docs/v3/retrieve there are 3 states: 1-archive @@ -197,7 +197,7 @@ const ( Unread LinkState = "unread" ) -// GetLinks retrive links of a given states https://getpocket.com/developer/docs/v3/retrieve +// GetLinks retrieve links of a given states https://getpocket.com/developer/docs/v3/retrieve func (client *Client) GetLinks(state LinkState) (response ItemLists, err error) { url := fmt.Sprintf("%s/get?sort=newest&state=%s&consumer_key=%s&access_token=%s", client.baseURL, state, client.consumerKey, *client.accessToken) req := request{ diff --git a/modules/resourceusage/widget.go b/modules/resourceusage/widget.go index 5fdbef28..637dfaf9 100644 --- a/modules/resourceusage/widget.go +++ b/modules/resourceusage/widget.go @@ -121,7 +121,7 @@ func MakeGraph(widget *Widget) { } } - widget.BarGraph.BuildBars(stats[:]) + widget.BarGraph.BuildBars(stats) } diff --git a/modules/transmission/display.go b/modules/transmission/display.go index b9bcf762..84dac301 100644 --- a/modules/transmission/display.go +++ b/modules/transmission/display.go @@ -57,11 +57,12 @@ func (widget *Widget) torrentPercentDone(torrent *transmissionrpc.Torrent) strin pctDone := *torrent.PercentDone str := fmt.Sprintf("%3d%%↓", int(pctDone*100)) - if pctDone == 0.0 { + switch pctDone { + case 0.0: str = "[gray::b]" + str - } else if pctDone == 1.0 { + case 1.0: str = "[green::b]" + str - } else { + default: str = "[lightblue::b]" + str } diff --git a/modules/travisci/widget.go b/modules/travisci/widget.go index 493e4d4e..51f47b3b 100644 --- a/modules/travisci/widget.go +++ b/modules/travisci/widget.go @@ -77,7 +77,7 @@ func (widget *Widget) content() (string, string, bool) { row := fmt.Sprintf( rowFormat, widget.RowColor(idx), - buildColor(&build), + buildColor(build), build.Repository.Name, build.Number, build.Branch.Name, @@ -92,7 +92,7 @@ func (widget *Widget) content() (string, string, bool) { return title, str, false } -func buildColor(build *Build) string { +func buildColor(build Build) string { switch build.State { case "broken": return "red" diff --git a/modules/zendesk/tickets.go b/modules/zendesk/tickets.go index 00720981..2b742dd5 100644 --- a/modules/zendesk/tickets.go +++ b/modules/zendesk/tickets.go @@ -67,7 +67,6 @@ func (widget *Widget) listTickets(pag ...string) (*TicketArray, error) { } return TicketStruct, err - } func (widget *Widget) newTickets() (*TicketArray, error) { diff --git a/utils/utils_test.go b/utils/utils_test.go index cf486f08..1d005ffa 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -70,7 +70,7 @@ func Test_ExecuteCommand(t *testing.T) { } func Test_FindMatch(t *testing.T) { - expected := [][]string([][]string{[]string{"SSID: 7E5B5C", "7E5B5C"}}) + expected := [][]string{{"SSID: 7E5B5C", "7E5B5C"}} result := FindMatch(`s*SSID: (.+)s*`, "SSID: 7E5B5C") assert.Equal(t, expected, result) @@ -115,7 +115,7 @@ func Test_ReadFileBytes(t *testing.T) { expected []byte }{ { - name: "with non-existant file", + name: "with non-existent file", file: "/tmp/junk-daa6bf613f4c.md", expected: []byte{}, }, diff --git a/view/base.go b/view/base.go index 0196e08f..e2fe5952 100644 --- a/view/base.go +++ b/view/base.go @@ -65,11 +65,12 @@ func (base *Base) ConfigText() string { } func (base *Base) ContextualTitle(defaultStr string) string { - if defaultStr == "" && base.FocusChar() == "" { + switch { + case defaultStr == "" && base.FocusChar() == "": return "" - } else if defaultStr != "" && base.FocusChar() == "" { + case defaultStr != "" && base.FocusChar() == "": return fmt.Sprintf(" %s ", defaultStr) - } else if defaultStr == "" && base.FocusChar() != "" { + case defaultStr == "" && base.FocusChar() != "": return fmt.Sprintf(" [darkgray::u]%s[::-][white] ", base.FocusChar()) }