Skip to content

Commit

Permalink
Address various linter complains
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Oct 15, 2023
1 parent 6319731 commit ac956b3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
linters-settings:
exhaustive:
default-signifies-exhaustive: true
errcheck:
check-type-assertions: true
goconst:
Expand All @@ -24,7 +26,6 @@ linters:
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
Expand Down
15 changes: 8 additions & 7 deletions cmd/wsget/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/ksysoev/wsget/pkg/ws"
)

var wsUrl string
var wsURL string
var OutputFH *os.File
var InputFH *os.File

Expand All @@ -27,7 +27,7 @@ func init() {
os.Exit(0)
}

wsUrl = *url
wsURL = *url

if outputFile != nil && *outputFile != "" {
var err error
Expand All @@ -39,15 +39,16 @@ func init() {
}

func main() {
fmt.Println("Connecting to", wsUrl, "...")
wsInsp, err := ws.NewWS(wsUrl)
fmt.Println("Connecting to", wsURL, "...")
wsInsp, err := ws.NewWS(wsURL)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected")
defer wsInsp.Close()

cli := cli.NewCLI(wsInsp)
fmt.Println("Connected")

client := cli.NewCLI(wsInsp)

if InputFH != nil {
go func() {
Expand All @@ -61,7 +62,7 @@ func main() {
}()
}

err = cli.Run(OutputFH)
err = client.Run(OutputFH)
if err != nil {
log.Fatal(err)

Check failure on line 67 in cmd/wsget/main.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

exitAfterDefer: log.Fatal will exit, and `defer wsInsp.Close()` will not run (gocritic)
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const (

HISTORY_FILE = ".wsget_history"
HISTORY_LIMIT = 100

MACOS_DELETE_KEY = 127

KEYBOARD_BUFFER_SIZE = 10
)

type CLI struct {
Expand Down Expand Up @@ -47,7 +51,7 @@ func (c *CLI) Run(outputFile *os.File) error {
defer keyboard.Close()
defer c.history.SaveToFile()

Check failure on line 52 in pkg/cli/cli.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

Error return value of `c.history.SaveToFile` is not checked (errcheck)

keysEvents, err := keyboard.GetKeys(10)
keysEvents, err := keyboard.GetKeys(KEYBOARD_BUFFER_SIZE)
if err != nil {
return err
}
Expand All @@ -57,6 +61,7 @@ func (c *CLI) Run(outputFile *os.File) error {
for {
select {
case event := <-keysEvents:
//nolint:gomnd

Check failure on line 64 in pkg/cli/cli.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

whyNoLint: include an explanation for nolint directive (gocritic)
switch event.Key {
case keyboard.KeyCtrlC, keyboard.KeyCtrlD:
return nil
Expand All @@ -81,6 +86,8 @@ func (c *CLI) Run(outputFile *os.File) error {
}

fmt.Println("Connection Mode: Press ESC to enter Request mode")
default:
continue
}

case msg := <-c.wsConn.Messages:
Expand Down Expand Up @@ -113,6 +120,7 @@ func (c *CLI) requestMode(keyStream <-chan keyboard.KeyEvent) (string, error) {
return buffer, e.Err
}

//nolint:gomnd

Check failure on line 123 in pkg/cli/cli.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

whyNoLint: include an explanation for nolint directive (gocritic)
switch e.Key {
case keyboard.KeyCtrlC, keyboard.KeyCtrlD:
return buffer, fmt.Errorf("interrupted")
Expand All @@ -136,7 +144,7 @@ func (c *CLI) requestMode(keyStream <-chan keyboard.KeyEvent) (string, error) {
buffer += "\n"
continue

case keyboard.KeyBackspace, keyboard.KeyDelete, 127:
case keyboard.KeyBackspace, keyboard.KeyDelete, MACOS_DELETE_KEY:
if len(buffer) == 0 {

Check failure on line 148 in pkg/cli/cli.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

emptyStringTest: replace `len(buffer) == 0` with `buffer == ""` (gocritic)
continue
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/cli/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
)

type History struct {
file string
limit uint
requests []string
fileName string
limit uint
}

func NewHistory(file string, limit uint) *History {
func NewHistory(fileName string, limit uint) *History {
h := &History{
file: file,
fileName: fileName,
limit: limit,
requests: make([]string, 0),
}
Expand All @@ -26,7 +26,7 @@ func NewHistory(file string, limit uint) *History {
}

func (h *History) loadFromFile() error {
fileHandler, err := os.OpenFile(h.file, os.O_RDONLY|os.O_CREATE, 0644)
fileHandler, err := os.OpenFile(h.fileName, os.O_RDONLY|os.O_CREATE, 0644)

Check failure on line 29 in pkg/cli/history.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

octalLiteral: use new octal literal style, 0o644 (gocritic)
if err != nil {
log.Println("Error opening history file:", err)
return err
Expand Down Expand Up @@ -57,7 +57,7 @@ func (h *History) loadFromFile() error {
}

func (h *History) SaveToFile() error {
fileHandler, err := os.OpenFile(h.file, os.O_WRONLY|os.O_CREATE, 0644)
fileHandler, err := os.OpenFile(h.fileName, os.O_WRONLY|os.O_CREATE, 0644)

Check failure on line 60 in pkg/cli/history.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

octalLiteral: use new octal literal style, 0o644 (gocritic)
if err != nil {
log.Println("Error opening history file:", err)
return err
Expand Down
8 changes: 6 additions & 2 deletions pkg/formater/formater.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ func (f *Formater) formatTestMessage(msgType ws.MessageType, data string) (strin
return f.text.FormatRequest(data)
case ws.Response:
return f.text.FormatResponse(data)
default:
case ws.NotDefined:
return "", fmt.Errorf("unknown message type")
default:
panic("Unexpected message type: " + string(msgType))
}
}

Expand All @@ -70,8 +72,10 @@ func (f *Formater) formatJsonMessage(msgType ws.MessageType, data any) (string,
return f.json.FormatRequest(data)
case ws.Response:
return f.json.FormatResponse(data)
default:
case ws.NotDefined:
return "", fmt.Errorf("unknown message type")
default:
panic("Unexpected message type: " + string(msgType))
}
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ const (
Response
)

const (
WS_MESSAGE_BUFFER_SIZE = 100

Check warning on line 18 in pkg/ws/ws.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

var-naming: don't use ALL_CAPS in Go names; use CamelCase (revive)
)

type Message struct {
Type MessageType `json:"type"`
Data string `json:"data"`
Type MessageType `json:"type"`
}

type WSConnection struct {

Check warning on line 26 in pkg/ws/ws.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

exported: type name will be used as ws.WSConnection by other packages, and that stutters; consider calling this Connection (revive)
Expand All @@ -31,7 +35,7 @@ func NewWS(url string) (*WSConnection, error) {
return nil, err
}

messages := make(chan Message, 100)
messages := make(chan Message, WS_MESSAGE_BUFFER_SIZE)

go func(messages chan Message) {
for {
Expand Down

0 comments on commit ac956b3

Please sign in to comment.