Skip to content

Commit

Permalink
chore: enable github actions validation (#36)
Browse files Browse the repository at this point in the history
Enable github actions validation of code and remove travis build status
from README.md as travis is no longer in use.

Fix lint errors from gofumpt, this includes switching from []struct
with a name to map[string]struct for tests, which results in clearer
 subtest definition.

Also:
* Remove deprecated linters
* Enabling new useful linters ones applying the needed fixes.
* Use bash for cache path step to prevent different commands from
  being needed on Windows.
  • Loading branch information
stevenh authored Nov 30, 2022
1 parent cfdf6fe commit 5ab781c
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 97 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.go text eol=lf
74 changes: 74 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Go build, test and lint

on:
push:
tags:
- v*
branches:
- main
pull_request:
permissions:
contents: read

jobs:
go:
strategy:
matrix:
go: [1.19]
golangcli: [v1.50.1]
os: [ubuntu-latest, macos-latest, windows-latest]
name: lint
runs-on: ${{ matrix.os }}
steps:
- name: Setup
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}

- name: Checkout
uses: actions/checkout@v3

- name: Go Cache Paths
id: go-cache-paths
shell: bash
run: |
echo "GOCACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT
echo "GOMODCACHE=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT
- name: Go Build Cache
uses: actions/cache@v3
with:
path: ${{ steps.go-cache-paths.outputs.GOCACHE }}
key: ${{ runner.os }}-GOCACHE-${{ hashFiles('**/go.sum') }}

- name: Go Mod Cache
uses: actions/cache@v3
with:
path: ${{ steps.go-cache-paths.outputs.GOMODCACHE }}
key: ${{ runner.os }}-GOMODCACHE-${{ hashFiles('**/go.sum') }}

- name: Go Lint Standard
if: runner.os != 'Windows'
uses: golangci/golangci-lint-action@v3
with:
version: ${{ matrix.golangci }}
args: "--out-${NO_FUTURE}format colored-line-number"
skip-pkg-cache: true
skip-build-cache: true

- name: Go Lint Windows
if: runner.os == 'Windows'
uses: golangci/golangci-lint-action@v3
env:
outformat: out-format
with:
version: ${{ matrix.golangci }}
args: "--%outformat% colored-line-number"
skip-pkg-cache: true
skip-build-cache: true

- name: Go Build
run: go build ./...

- name: Go Test
run: go test -race -v ./...
17 changes: 11 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
linters:
enable:
- deadcode
- errcheck
- ineffassign
- structcheck
- unused
- typecheck
- varcheck
- bodyclose
- depguard
- dogsled
Expand All @@ -16,11 +14,9 @@ linters:
- gocyclo
- gofmt
- goimports
- golint
- revive
- gosec
- gosimple
- interfacer
- maligned
- misspell
- nakedret
- prealloc
Expand All @@ -32,3 +28,12 @@ linters:
- whitespace
- govet
- gocognit
- dupword
- errorlint
- godot
- gofumpt
- nolintlint
- nosprintfhostport
- tenv
- thelper
- wrapcheck
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TeamSpeak 3 [![Go Report Card](https://goreportcard.com/badge/github.com/multiplay/go-ts3)](https://goreportcard.com/report/github.com/multiplay/go-ts3) [![License](https://img.shields.io/badge/license-BSD-blue.svg)](https://github.com/multiplay/go-ts3/blob/master/LICENSE) [![GoDoc](https://godoc.org/github.com/multiplay/go-ts3?status.svg)](https://godoc.org/github.com/multiplay/go-ts3) [![Build Status](https://travis-ci.org/multiplay/go-ts3.svg?branch=master)](https://travis-ci.org/multiplay/go-ts3)
# TeamSpeak 3 [![Go Report Card](https://goreportcard.com/badge/github.com/multiplay/go-ts3)](https://goreportcard.com/report/github.com/multiplay/go-ts3) [![License](https://img.shields.io/badge/license-BSD-blue.svg)](https://github.com/multiplay/go-ts3/blob/master/LICENSE) [![GoDoc](https://godoc.org/github.com/multiplay/go-ts3?status.svg)](https://godoc.org/github.com/multiplay/go-ts3)

go-ts3 is a [Go](http://golang.org/) client for the [TeamSpeak 3 ServerQuery Protocol](http://media.teamspeak.com/ts3_literature/TeamSpeak%203%20Server%20Query%20Manual.pdf).

Expand Down
2 changes: 1 addition & 1 deletion basic_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *Client) Whoami() (*ConnectionInfo, error) {
return i, nil
}

// Properties that can be changed with ClientUpdate
// Properties that can be changed with ClientUpdate.
const (
ClientNickname = "client_nickname"
ClientIsTalker = "client_is_talker"
Expand Down
5 changes: 5 additions & 0 deletions basic_cmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestCmdsBasic(t *testing.T) {
}()

auth := func(t *testing.T) {
t.Helper()
if err = c.Login("user", "pass"); !assert.NoError(t, err) {
return
}
Expand All @@ -36,6 +37,7 @@ func TestCmdsBasic(t *testing.T) {
}

version := func(t *testing.T) {
t.Helper()
v, err := c.Version()
if !assert.NoError(t, err) {
return
Expand All @@ -47,14 +49,17 @@ func TestCmdsBasic(t *testing.T) {
}

useID := func(t *testing.T) {
t.Helper()
assert.NoError(t, c.Use(1))
}

usePort := func(t *testing.T) {
t.Helper()
assert.NoError(t, c.UsePort(1024))
}

whoami := func(t *testing.T) {
t.Helper()
info, err := c.Whoami()
if !assert.NoError(t, err) {
return
Expand Down
19 changes: 11 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ts3

import (
"bufio"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -146,15 +147,15 @@ func NewClient(addr string, options ...func(c *Client) error) (*Client, error) {

var err error
if c.conn, err = net.DialTimeout("tcp", addr, c.timeout); err != nil {
return nil, err
return nil, fmt.Errorf("client: dial timeout: %w", err)
}

c.scanner = bufio.NewScanner(bufio.NewReader(c.conn))
c.scanner.Buffer(c.buf, c.maxBufSize)
c.scanner.Split(ScanLines)

if err := c.conn.SetDeadline(time.Now().Add(c.timeout)); err != nil {
return nil, err
return nil, fmt.Errorf("client: set deadline: %w", err)
}

// Read the connection header
Expand All @@ -163,7 +164,7 @@ func NewClient(addr string, options ...func(c *Client) error) (*Client, error) {
}

if l := c.scanner.Text(); l != c.connectHeader {
return nil, fmt.Errorf("invalid connection header %q", l)
return nil, fmt.Errorf("client: invalid connection header %q", l)
}

// Slurp the banner
Expand All @@ -172,7 +173,7 @@ func NewClient(addr string, options ...func(c *Client) error) (*Client, error) {
}

if err := c.conn.SetReadDeadline(time.Time{}); err != nil {
return nil, err
return nil, fmt.Errorf("client: set read deadline: %w", err)
}

// Start handlers
Expand All @@ -187,7 +188,7 @@ func (c *Client) messageHandler() {
for {
if c.scanner.Scan() {
line := c.scanner.Text()
// nolint: gocritic
//nolint: gocritic
if line == "error id=0 msg=ok" {
c.err <- nil
} else if matches := respTrailerRe.FindStringSubmatch(line); len(matches) == 4 {
Expand All @@ -206,7 +207,7 @@ func (c *Client) messageHandler() {
} else {
err := c.scanErr()
c.err <- err
if err == io.ErrUnexpectedEOF {
if errors.Is(err, io.ErrUnexpectedEOF) {
close(c.disconnect)
return
}
Expand Down Expand Up @@ -290,16 +291,18 @@ func (c *Client) Close() error {

if err != nil {
return err
} else if err2 != nil {
return fmt.Errorf("client: close: %w", err2)
}

return err2
return nil
}

// scanError returns the error from the scanner if non-nil,
// `io.ErrUnexpectedEOF` otherwise.
func (c *Client) scanErr() error {
if err := c.scanner.Err(); err != nil {
return err
return fmt.Errorf("client: scan: %w", err)
}
return io.ErrUnexpectedEOF
}
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func NewArg(key string, val interface{}) *Arg {
return &Arg{key: key, val: fmt.Sprint(val)}
}

// ArgString implements CmdArg
// ArgString implements CmdArg.
func (a *Arg) ArgString() string {
return fmt.Sprintf("%v=%v", encoder.Replace(a.key), encoder.Replace(a.val))
}
81 changes: 44 additions & 37 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,73 @@ import (
)

func TestCmd(t *testing.T) {
tests := []struct {
name string
tests := map[string]struct {
cmd *Cmd
expect string
}{
{"cmd", NewCmd("version"),
"cmd": {
NewCmd("version"),
"version",
},
{"arg", NewCmd("use").
WithArgs(NewArg("sid", 1)),
"arg": {
NewCmd("use").
WithArgs(NewArg("sid", 1)),
"use sid=1",
},
{"args", NewCmd("use").
WithArgs(NewArg("sid", 1), NewArg("port", 1234)),
"args": {
NewCmd("use").
WithArgs(NewArg("sid", 1), NewArg("port", 1234)),
"use sid=1 port=1234",
},
{"args-option", NewCmd("use").
WithArgs(NewArg("sid", 1), NewArg("port", 1234)).
WithOptions("-virtual"),
"args-option": {
NewCmd("use").
WithArgs(NewArg("sid", 1), NewArg("port", 1234)).
WithOptions("-virtual"),
"use sid=1 port=1234 -virtual",
},
{"options", NewCmd("serverlist").
WithOptions("-uid", "-short"),
"options": {
NewCmd("serverlist").
WithOptions("-uid", "-short"),
"serverlist -uid -short",
},
{"arg-group-single", NewCmd("servergroupdelperm").
WithArgs(
NewArg("sgid", 1),
NewArgGroup(NewArg("permid", 1), NewArg("permid", 2)),
),
"arg-group-single": {
NewCmd("servergroupdelperm").
WithArgs(
NewArg("sgid", 1),
NewArgGroup(NewArg("permid", 1), NewArg("permid", 2)),
),
"servergroupdelperm sgid=1 permid=1|permid=2",
},
{"arg-group-multi", NewCmd("servergroupaddperm").
WithArgs(
NewArg("sgid", 1),
NewArgGroup(
NewArgSet(
NewArg("permid", 1),
NewArg("permvalue", 1),
NewArg("permnegated", 0),
NewArg("permskip", 0),
),
NewArgSet(
NewArg("permid", 2),
NewArg("permvalue", 2),
NewArg("permnegated", 1),
NewArg("permskip", 1),
"arg-group-multi": {
NewCmd("servergroupaddperm").
WithArgs(
NewArg("sgid", 1),
NewArgGroup(
NewArgSet(
NewArg("permid", 1),
NewArg("permvalue", 1),
NewArg("permnegated", 0),
NewArg("permskip", 0),
),
NewArgSet(
NewArg("permid", 2),
NewArg("permvalue", 2),
NewArg("permnegated", 1),
NewArg("permskip", 1),
),
),
),
),
"servergroupaddperm sgid=1 permid=1 permvalue=1 permnegated=0 permskip=0|permid=2 permvalue=2 permnegated=1 permskip=1",
},
{"escaped-chars", NewCmd("servergroupadd").
WithArgs(NewArg("name", "Chars:\\/ |\a\b\f\n\r\t\v")),
"escaped-chars": {
NewCmd("servergroupadd").
WithArgs(NewArg("name", "Chars:\\/ |\a\b\f\n\r\t\v")),
`servergroupadd name=Chars:\\\/\s\p\a\b\f\n\r\t\v`,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.expect+"\n", tc.cmd.String())
})
}
Expand Down
Loading

0 comments on commit 5ab781c

Please sign in to comment.