Skip to content

Commit

Permalink
[chore] Add golangci-lint (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 committed Feb 11, 2024
1 parent 6241394 commit 8c8d671
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 73 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/golangci-lint.yml
@@ -0,0 +1,28 @@
name: golangci-lint
on:
push:
branches:
- main
- next
pull_request:

permissions:
contents: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.21'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
# Require: The version of golangci-lint to use.
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
version: v1.54
11 changes: 11 additions & 0 deletions .golangci.yaml
@@ -0,0 +1,11 @@
linters:
# Enable specific linter
# https://golangci-lint.run/usage/linters/#enabled-by-default
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
- whitespace
16 changes: 8 additions & 8 deletions cmd/root.go
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/robinovitch61/wander/internal/dev"
Expand Down Expand Up @@ -208,7 +209,7 @@ func init() {
rootCmd.PersistentFlags().BoolP(cliLong, rootNameToArg[cliLong].cliShort, rootNameToArg[cliLong].defaultIfBool, rootNameToArg[cliLong].description)

// colors, config or env var only
viper.BindPFlag("", rootCmd.PersistentFlags().Lookup(rootNameToArg["logo-color"].cfgFileEnvVar))
_ = viper.BindPFlag("", rootCmd.PersistentFlags().Lookup(rootNameToArg["logo-color"].cfgFileEnvVar))

for _, cliLong = range []string{
"addr",
Expand Down Expand Up @@ -247,7 +248,7 @@ func init() {
} else {
rootCmd.PersistentFlags().StringP(cliLong, c.cliShort, c.defaultString, c.description)
}
viper.BindPFlag(cliLong, rootCmd.PersistentFlags().Lookup(c.cfgFileEnvVar))
_ = viper.BindPFlag(cliLong, rootCmd.PersistentFlags().Lookup(c.cfgFileEnvVar))
}

// serve
Expand All @@ -265,7 +266,7 @@ func init() {
} else {
serveCmd.PersistentFlags().StringP(cliLong, c.cliShort, c.defaultString, c.description)
}
viper.BindPFlag(cliLong, serveCmd.PersistentFlags().Lookup(c.cfgFileEnvVar))
_ = viper.BindPFlag(cliLong, serveCmd.PersistentFlags().Lookup(c.cfgFileEnvVar))
}

// exec
Expand Down Expand Up @@ -305,9 +306,8 @@ func initConfig(cmd *cobra.Command, nameToArg map[string]arg) error {
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
} else {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// no config file found, that's ok
} else {
var configFileNotFoundError viper.ConfigFileNotFoundError
if !errors.As(err, &configFileNotFoundError) {
fmt.Println(err)
os.Exit(1)
}
Expand All @@ -333,14 +333,14 @@ func bindFlags(cmd *cobra.Command, nameToArg map[string]arg) {
val := v.Get(viperName)
err := cmd.Flags().Set(cliLong, fmt.Sprintf("%v", val))
if err != nil {
fmt.Println(fmt.Sprintf("error setting flag %s: %v", cliLong, err))
fmt.Printf("error setting flag %s: %v\n", cliLong, err)
os.Exit(1)
}
}
})
}

func mainEntrypoint(cmd *cobra.Command, args []string) {
func mainEntrypoint(cmd *cobra.Command, _ []string) {
dev.Debug("~STARTING UP~")
rootOpts := getRootOpts(cmd)
initialModel, options := setup(cmd, rootOpts, "")
Expand Down
5 changes: 1 addition & 4 deletions cmd/util.go
Expand Up @@ -44,10 +44,7 @@ func validateToken(token string) error {
}

func trueIfTrue(v string) bool {
if strings.ToLower(strings.TrimSpace(v)) == "true" {
return true
}
return false
return strings.ToLower(strings.TrimSpace(v)) == "true"
}

func retrieveLogoColor() string {
Expand Down
20 changes: 0 additions & 20 deletions internal/tui/components/app/app.go
Expand Up @@ -86,7 +86,6 @@ type Model struct {

eventsStream nomad.EventsStream
event string
meta map[string]string

logsStream nomad.LogsStream
lastLogFinished bool
Expand Down Expand Up @@ -434,7 +433,6 @@ func (m *Model) handleKeyMsg(msg tea.KeyMsg) tea.Cmd {

if !m.currentPageFilterFocused() && !m.currentPageViewportSaving() {
switch {

case key.Matches(msg, keymap.KeyMap.Compact):
m.toggleCompact()
return nil
Expand Down Expand Up @@ -516,9 +514,7 @@ func (m *Model) handleKeyMsg(msg tea.KeyMsg) tea.Cmd {
m.getCurrentPageModel().SetLoading(true)
return m.getCurrentPageCmd()
}

}

if key.Matches(msg, keymap.KeyMap.Exec) {
if selectedPageRow, err := m.getCurrentPageModel().GetSelectedPageRow(); err == nil {
if m.currentPage.ShowsTasks() {
Expand Down Expand Up @@ -636,7 +632,6 @@ func (m *Model) handleKeyMsg(msg tea.KeyMsg) tea.Cmd {
}

if m.currentPage == nomad.JobTasksPage {

taskInfo, err := nomad.TaskInfoFromKey(selectedPageRow.Key)
if err != nil {
m.err = err
Expand Down Expand Up @@ -691,21 +686,6 @@ func (m *Model) getCurrentPageModel() *page.Model {
return m.pageModels[m.currentPage]
}

func (m *Model) appendToViewport(content string, startOnNewLine bool) {
stringRows := strings.Split(content, "\n")
var pageRows []page.Row
for _, row := range stringRows {
stripOS := formatter.StripOSCommandSequences(row)
stripped := formatter.StripANSI(stripOS)
// bell seems to mess with parent terminal
if stripped != "\a" {
pageRows = append(pageRows, page.Row{Row: stripped})
}
}
m.getCurrentPageModel().AppendToViewport(pageRows, startOnNewLine)
m.getCurrentPageModel().ScrollViewportToBottom()
}

func (m *Model) updateKeyHelp() {
newKeyHelp := nomad.GetPageKeyHelp(m.currentPage, m.currentPageFilterFocused(), m.currentPageFilterApplied(), m.currentPageViewportSaving(), m.logType, m.compact, m.inJobsMode)
m.header.SetKeyHelp(newKeyHelp)
Expand Down
5 changes: 0 additions & 5 deletions internal/tui/components/toast/toast.go
Expand Up @@ -20,7 +20,6 @@ type Model struct {
ID int
message string
Timeout time.Duration
initialized bool
Visible bool
MessageStyle lipgloss.Style
}
Expand Down Expand Up @@ -63,10 +62,6 @@ type TimeoutMsg struct {
ID int
}

func (m Model) timeoutAfterDuration() tea.Cmd {
return tea.Tick(m.Timeout, func(t time.Time) tea.Msg { return TimeoutMsg{m.ID} })
}

func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
Expand Down
4 changes: 1 addition & 3 deletions internal/tui/components/viewport/viewport.go
Expand Up @@ -415,9 +415,7 @@ func (m *Model) updateWrappedHeader() {
var allWrappedHeader []string
for _, line := range m.header {
wrappedLinesForLine := m.getWrappedLines(line)
for _, wrappedLine := range wrappedLinesForLine {
allWrappedHeader = append(allWrappedHeader, wrappedLine)
}
allWrappedHeader = append(allWrappedHeader, wrappedLinesForLine...)
}
m.wrappedHeader = allWrappedHeader
}
Expand Down
6 changes: 1 addition & 5 deletions internal/tui/formatter/formatter.go
Expand Up @@ -46,10 +46,6 @@ type Table struct {
HeaderRows, ContentRows []string
}

func (t *Table) isEmpty() bool {
return len(t.HeaderRows) == 0 && len(t.ContentRows) == 0
}

type tableConfig struct {
writer *tablewriter.Table
string *strings.Builder
Expand Down Expand Up @@ -120,7 +116,7 @@ func pluralize(s string, q float64) string {

func FormatTimeNsSinceNow(t int64) string {
tm := time.Unix(0, t).UTC()
since := time.Now().Sub(tm)
since := time.Since(tm)
if secs := since.Seconds(); secs > 0 && secs < 60 {
val := math.Floor(secs)
out := fmt.Sprintf("%.0f second", val)
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/nomad/events.go
Expand Up @@ -100,7 +100,7 @@ func getEventsFromJQQuery(event string, code *gojq.Code) ([]Event, error) {
if err != nil {
events = append(events, Event{event, fmt.Sprintf("events jq json error: %s", err)})
}
events = append(events, Event{event, fmt.Sprintf("%s", j)})
events = append(events, Event{event, string(j)})
}
return events, nil
}
53 changes: 30 additions & 23 deletions internal/tui/nomad/exec.go
Expand Up @@ -53,7 +53,7 @@ func AllocExec(client *api.Client, allocID, task string, args []string) (int, er
if len(foundAllocs) > 0 {
if len(foundAllocs) == 1 && len(maps.Values(foundAllocs)[0]) == 1 && maps.Values(foundAllocs)[0][0] != nil {
// only one job with one allocation found, use that
alloc, _, err = client.Allocations().Info(maps.Values(foundAllocs)[0][0].ID, nil)
alloc, _, _ = client.Allocations().Info(maps.Values(foundAllocs)[0][0].ID, nil)
} else {
// multiple jobs and/or allocations found, print them and exit
for job, jobAllocs := range foundAllocs {
Expand All @@ -78,7 +78,7 @@ func AllocExec(client *api.Client, allocID, task string, args []string) (int, er
}
return 1, err
} else if len(shortIDAllocs) == 1 {
alloc, _, err = client.Allocations().Info(shortIDAllocs[0].ID, nil)
alloc, _, _ = client.Allocations().Info(shortIDAllocs[0].ID, nil)
} else {
return 1, fmt.Errorf("no allocations found for alloc id %s", allocID)
}
Expand Down Expand Up @@ -108,14 +108,21 @@ func AllocExec(client *api.Client, allocID, task string, args []string) (int, er
}

// execImpl invokes the Alloc Exec api call, it also prepares and restores terminal states as necessary.
func execImpl(client *api.Client, alloc *api.Allocation, task string,
command []string, escapeChar string, stdin io.Reader, stdout, stderr io.WriteCloser) (int, error) {

func execImpl(
client *api.Client,
alloc *api.Allocation,
task string,
command []string,
escapeChar string,
stdin io.Reader,
stdout,
stderr io.WriteCloser,
) (int, error) {
// attempt to clear screen
time.Sleep(10 * time.Millisecond)
os.Stdout.Write([]byte("\033c"))
_, _ = os.Stdout.Write([]byte("\033c"))

fmt.Println(fmt.Sprintf("Exec session for %s (%s), task %s", alloc.Name, formatter.ShortAllocID(alloc.ID), task))
fmt.Printf("Exec session for %s (%s), task %s\n", alloc.Name, formatter.ShortAllocID(alloc.ID), task)

sizeCh := make(chan api.TerminalSize, 1)

Expand Down Expand Up @@ -183,7 +190,7 @@ func setRawTerminal(stream interface{}) (cleanup func(), err error) {
}

return func() {
term.RestoreTerminal(fd, state)
_ = term.RestoreTerminal(fd, state)
}, nil
}

Expand All @@ -200,7 +207,7 @@ func setRawTerminalOutput(stream interface{}) (cleanup func(), err error) {
}

return func() {
term.RestoreTerminal(fd, state)
_ = term.RestoreTerminal(fd, state)
}, nil
}

Expand Down Expand Up @@ -276,22 +283,22 @@ func (r *reader) pipe() {

if n > 0 {
state = r.processBuf(bw, rb, n, state)
bw.Flush()
_ = bw.Flush()
if state == sLookChar {
// terminated with ~ - let's read one more character
n, err = r.impl.Read(rb[:1])
if n == 1 {
state = sLookNewLine
if rb[0] == r.escapeChar {
// only emit escape character once
bw.WriteByte(rb[0])
bw.Flush()
_ = bw.WriteByte(rb[0])
_ = bw.Flush()
} else if r.handler(rb[0]) {
// skip if handled
} else {
bw.WriteByte(r.escapeChar)
bw.WriteByte(rb[0])
bw.Flush()
_ = bw.WriteByte(r.escapeChar)
_ = bw.WriteByte(rb[0])
_ = bw.Flush()
if rb[0] == '\n' || rb[0] == '\r' {
state = sLookEscapeChar
}
Expand All @@ -303,10 +310,10 @@ func (r *reader) pipe() {
if err != nil {
// write ~ if it's the last thing
if state == sLookChar {
bw.WriteByte(r.escapeChar)
_ = bw.WriteByte(r.escapeChar)
}
bw.Flush()
r.pw.CloseWithError(err)
_ = bw.Flush()
_ = r.pw.CloseWithError(err)
break
}
}
Expand All @@ -324,19 +331,19 @@ START:
if s == sLookEscapeChar && buf[i] == r.escapeChar {
if i+1 >= n {
// buf terminates with ~ - write all before
bw.Write(buf[wi:i])
_, _ = bw.Write(buf[wi:i])
return sLookChar
}

nc := buf[i+1]
if nc == r.escapeChar {
// skip one escape char
bw.Write(buf[wi:i])
_, _ = bw.Write(buf[wi:i])
i++
wi = i
} else if r.handler(nc) {
// skip both characters
bw.Write(buf[wi:i])
_, _ = bw.Write(buf[wi:i])
i = i + 2
wi = i
} else if nc == '\n' || nc == '\r' {
Expand All @@ -353,14 +360,14 @@ START:
for {
if i >= n {
// got to end without new line, write and return
bw.Write(buf[wi:n])
_, _ = bw.Write(buf[wi:n])
return sLookNewLine
}

if buf[i] == '\n' || buf[i] == '\r' {
// buf terminated at new line
if i+1 >= n {
bw.Write(buf[wi:n])
_, _ = bw.Write(buf[wi:n])
return sLookEscapeChar
}

Expand Down
1 change: 0 additions & 1 deletion internal/tui/nomad/jobs.go
Expand Up @@ -101,5 +101,4 @@ func toJobsKey(jobResponseEntry *api.JobListStub) string {
func JobIDAndNamespaceFromKey(key string) (string, string) {
split := strings.Split(key, " ")
return split[0], split[1]

}

0 comments on commit 8c8d671

Please sign in to comment.