Skip to content

Commit 72c2634

Browse files
committed
linting
1 parent 80858c9 commit 72c2634

File tree

16 files changed

+284
-145
lines changed

16 files changed

+284
-145
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
.env
44
*.log
55
*.out
6+
cmd/fs_event_log/fs_event_log
7+
cmd/fs_event_log/fs_events_view

.golangci.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
linters-settings:
2+
errcheck:
3+
check-type-assertions: true
4+
forbidigo:
5+
forbid:
6+
# Unlike most projects, we're allowing the standard library's log
7+
# package: this is a demo service, so let's keep it simple.
8+
- '^fmt\.Print'
9+
- '^print$'
10+
- '^println$'
11+
- '^panic$'
12+
- '^log\.'
13+
gci:
14+
sections:
15+
- standard # Standard section: captures all standard packages.
16+
- default # Default section: contains all imports that could not be matched to another section type.
17+
godox:
18+
# TODO, OPT, etc. comments are fine to commit. Use FIXME comments for
19+
# temporary hacks, and use godox to prevent committing them.
20+
keywords: [FIXME]
21+
varnamelen:
22+
ignore-decls:
23+
- f *os.File
24+
- rw io.ReadWriter
25+
- w io.Writer
26+
- n int
27+
linters:
28+
enable-all: true
29+
disable:
30+
- cyclop # covered by gocyclo
31+
- deadcode # abandoned
32+
- depguard # in golangci-lint v1.53.0+ default requires only stdlib deps
33+
- exhaustivestruct # replaced by exhaustruct
34+
- funlen # rely on code review to limit function length
35+
- gocognit # dubious "cognitive overhead" quantification
36+
- gofumpt # prefer standard gofmt
37+
- goimports # rely on gci instead
38+
- golint # deprecated by Go team
39+
- gomnd # some unnamed constants are okay
40+
- ifshort # deprecated by author
41+
- interfacer # deprecated by author
42+
- ireturn # "accept interfaces, return structs" isn't ironclad
43+
# - lll # don't want hard limits for line length
44+
- maintidx # covered by gocyclo
45+
- maligned # readability trumps efficient struct packing
46+
# - nlreturn # generous whitespace violates house style
47+
- nosnakecase # deprecated in https://github.com/golangci/golangci-lint/pull/3065
48+
- paralleltest # in this project, it's not worth making all tests parallel
49+
- scopelint # deprecated by author
50+
- structcheck # abandoned
51+
- testpackage # internal tests are fine
52+
- tparallel # in this project, it's not worth making all tests parallel
53+
- varcheck # abandoned
54+
# - wrapcheck # don't _always_ need to wrap errors
55+
# - wsl # generous whitespace violates house style
56+
issues:
57+
exclude:
58+
# Don't ban use of fmt.Errorf to create new errors, but the remaining
59+
# checks from err113 are useful.
60+
- "err113: do not define dynamic errors.*"

client.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type Client struct {
2121
}
2222

2323
// Default timeout options.
24+
//
25+
//nolint:gochecknoglobals
2426
var (
2527
DialTimeout = time.Second * 5
2628
AuthTimeout = time.Second * 2
@@ -61,6 +63,7 @@ func NewClient(rwc io.ReadWriteCloser, password string, opts ...Option) (*Client
6163

6264
if err := conn.AuthTimeout(password, AuthTimeout); err != nil {
6365
rwc.Close()
66+
6467
return nil, fmt.Errorf("failed to auth: %w", err)
6568
}
6669

@@ -80,7 +83,8 @@ func NewClient(rwc io.ReadWriteCloser, password string, opts ...Option) (*Client
8083
// Close closes the client connection.
8184
func (c *Client) Close() error {
8285
c.sendRecv(cmd("exit")) //nolint:errcheck // ignore send error
83-
return c.closer.Close()
86+
87+
return c.closer.Close() //nolint:wrapcheck
8488
}
8589

8690
// API sends a command to the API and returns the response body or an error.
@@ -107,7 +111,7 @@ func (c *Client) API(command string) (string, error) {
107111
// When the command is done executing, FreeSWITCH fires an event with the result
108112
// and you can compare that to the Job-UUID to see what the result was. In order
109113
// to receive this event, you will need to subscribe to BACKGROUND_JOB events.
110-
func (c *Client) Job(command string) (id string, err error) {
114+
func (c *Client) Job(command string) (id string, err error) { //nolint:nonamedreturns
111115
resp, err := c.sendRecv(cmd("bgapi", command))
112116
if err != nil {
113117
return "", err
@@ -127,6 +131,7 @@ func (c *Client) Job(command string) (id string, err error) {
127131
// to receive this event, you will need to subscribe to BACKGROUND_JOB events.
128132
func (c *Client) JobWithID(command, id string) error {
129133
_, err := c.sendRecv(cmd("bgapi", command).WithJobUUID(id))
134+
130135
return err
131136
}
132137

@@ -138,14 +143,17 @@ func (c *Client) JobWithID(command, id string) error {
138143
func (c *Client) Subscribe(names ...string) error {
139144
cmdNames := buildEventNamesCmd(names...)
140145
_, err := c.sendRecv(cmd("event", cmdNames))
146+
141147
return err
142148
}
143149

144150
// Unsubscribe unsubscribes the client from one or more events.
145151
//
146152
// Suppress the specified type of event.
147153
// If name is empty then all events will be suppressed.
148-
func (c *Client) Unsubscribe(names ...string) (err error) {
154+
func (c *Client) Unsubscribe(names ...string) error {
155+
var err error
156+
149157
cmdNames := buildEventNamesCmd(names...)
150158
if cmdNames == eventAll {
151159
_, err = c.sendRecv(cmd("noevents"))
@@ -176,6 +184,7 @@ func (c *Client) Unsubscribe(names ...string) (err error) {
176184
// events for multiple users on a particular conference.
177185
func (c *Client) Filter(eventHeader, valueToFilter string) error {
178186
_, err := c.sendRecv(cmd("filter", eventHeader, valueToFilter))
187+
179188
return err
180189
}
181190

@@ -186,6 +195,7 @@ func (c *Client) Filter(eventHeader, valueToFilter string) error {
186195
// is no use of the filter.
187196
func (c *Client) FilterDelete(eventHeader, valueToFilter string) error {
188197
_, err := c.sendRecv(cmd("filter delete", eventHeader, valueToFilter))
198+
189199
return err
190200
}
191201

@@ -202,6 +212,7 @@ func (c *Client) FilterDelete(eventHeader, valueToFilter string) error {
202212
// use a filter.
203213
func (c *Client) MyEvent(uuid string) error {
204214
_, err := c.sendRecv(cmd("myevents", uuid))
215+
205216
return err
206217
}
207218

@@ -220,13 +231,15 @@ func (c *Client) DivertEvents(on ...bool) error {
220231
}
221232

222233
_, err := c.sendRecv(cmd("divert_events", val))
234+
223235
return err
224236
}
225237

226238
// Send an event into the event system.
227239
func (c *Client) SendEvent(name string, headers map[string]string, body string) error {
228240
_, err := c.sendRecv(
229241
cmd("sendevent", name).WithMessage(headers, body))
242+
230243
return err
231244
}
232245

@@ -235,33 +248,44 @@ func (c *Client) SendEvent(name string, headers map[string]string, body string)
235248
func (c *Client) SendMsg(uuid string, headers map[string]string, body string) error {
236249
_, err := c.sendRecv(
237250
cmd("sendmsg", uuid).WithMessage(headers, body))
251+
238252
return err
239253
}
240254

255+
const (
256+
commandReply = "command/reply"
257+
disconnectNotice = "text/disconnect-notice"
258+
eventPlain = "text/event-plain"
259+
)
260+
241261
// runReader is a method of the Client struct that reads responses from the connection and handles them accordingly.
242262
func (c *Client) runReader(events chan<- Event, autoClose bool) {
243263
c.conn.log.Info("esl: run response reading")
264+
244265
defer func() {
245266
close(c.chResp)
246267
close(c.chErr)
268+
247269
if autoClose && events != nil {
248270
close(events)
249271
}
272+
250273
c.conn.log.Info("esl: response reader stopped")
251274
}()
252275

253276
for {
254277
resp, err := c.conn.Read()
255278
if err != nil {
256279
c.chErr <- err
280+
257281
return // break on read error
258282
}
259283

260-
switch ct := resp.ContentType(); ct {
261-
case "api/response", "command/reply":
284+
switch contentType := resp.ContentType(); contentType {
285+
case "api/response", commandReply:
262286
c.chResp <- resp
263287

264-
case "text/event-plain":
288+
case eventPlain:
265289
if events == nil {
266290
continue // ignore events if no events channel is provided
267291
}
@@ -270,18 +294,19 @@ func (c *Client) runReader(events chan<- Event, autoClose bool) {
270294
if err != nil {
271295
c.conn.log.Error("esl: failed to parse event",
272296
slog.String("err", err.Error()))
297+
273298
continue // ignore bad event
274299
}
275300

276301
c.conn.log.Info("esl: handle", slog.Any("event", event))
277302
events <- event
278303

279-
case "text/disconnect-notice":
304+
case disconnectNotice:
280305
return // disconnect
281306

282307
default:
283308
c.conn.log.Warn("esl: unexpected response",
284-
slog.String("content-type", ct))
309+
slog.String("content-type", contentType))
285310
}
286311
}
287312
}
@@ -302,12 +327,14 @@ func (c *Client) read() (response, error) {
302327
if ok {
303328
return response{}, err
304329
}
330+
305331
return response{}, io.EOF // connection closed
306332

307333
case resp := <-c.chResp:
308334
if err := resp.AsErr(); err != nil {
309335
return response{}, err // response with error message
310336
}
337+
311338
return resp, nil
312339
}
313340
}

client_test.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ func TestClient(t *testing.T) {
1717
go func() {
1818
for ev := range events {
1919
t.Logf("event %s [%d]", ev.Name(), ev.Sequence())
20+
2021
if ev.ContentLength() > 0 {
2122
t.Logf("body: %s", ev.Body())
2223
}
2324
}
25+
2426
t.Log("events channel closed")
2527
}()
2628

2729
client, err := Connect(addr, password,
2830
WithEvents(events, true),
2931
WithLog(slog.Default()),
30-
// WithDumpIn(os.Stdout),
31-
// WithDumpOut(os.Stderr),
3232
)
3333
if err != nil {
3434
t.Skip("FreeSWITCH not running:", err)
@@ -43,6 +43,7 @@ func TestClient(t *testing.T) {
4343
if err != nil {
4444
t.Error(err)
4545
}
46+
4647
_ = msg
4748
// t.Log(msg)
4849

@@ -56,38 +57,21 @@ func TestClient(t *testing.T) {
5657
if err != nil {
5758
t.Error(err)
5859
}
60+
5961
if jobid == "" {
6062
t.Error("incorrect job id:", jobid)
6163
}
6264

6365
time.Sleep(time.Second * 5)
6466
}
6567

66-
// for _, cmd := range []string{
67-
// // spell-checker:disable
68-
// "fsctl debug_level 9",
69-
// "fsctl loglevel 7",
70-
// "fsctl debug_sql",
71-
// "fsctl last_sps",
72-
// `json {"command" : "status", "data" : ""}`,
73-
// "md5 freeswitch-is-awesome",
74-
// "module_exists mod_callcenter",
75-
// "show channels as json",
76-
// "status",
77-
// // spell-checker:enable
78-
// } {
79-
// _, err := client.Job(cmd)
80-
// if err != nil {
81-
// return err
82-
// }
83-
// }
84-
8568
func TestClientDefault(t *testing.T) {
8669
client, err := Connect("", "ClueCon",
8770
WithLog(slog.Default()),
8871
)
8972
if err != nil {
9073
t.Skip("FreeSWITCH not running:", err)
9174
}
75+
9276
client.Close()
9377
}

0 commit comments

Comments
 (0)