Skip to content

Commit 019a77d

Browse files
committed
make linter happy
Signed-off-by: Florian Lehner <[email protected]>
1 parent caafb91 commit 019a77d

File tree

4 files changed

+76
-74
lines changed

4 files changed

+76
-74
lines changed

goNetViz-reconstruct.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package main
33
import (
44
"bufio"
55
"fmt"
6-
"github.com/google/gopacket"
7-
"github.com/google/gopacket/layers"
8-
"github.com/google/gopacket/pcapgo"
9-
"golang.org/x/sync/errgroup"
106
"os"
117
"reflect"
128
"regexp"
139
"strconv"
10+
11+
"github.com/google/gopacket"
12+
"github.com/google/gopacket/layers"
13+
"github.com/google/gopacket/pcapgo"
14+
"golang.org/x/sync/errgroup"
1415
)
1516

1617
// reconstructOptions represents all options for reconstruction
@@ -66,7 +67,7 @@ func createPacket(ch chan<- []byte, packet []int, bpP int) error {
6667
buf = append(buf, byte(tmp))
6768
}
6869
default:
69-
return fmt.Errorf("This format is not supported so far")
70+
return fmt.Errorf("this format is not supported so far")
7071
}
7172

7273
ch <- buf
@@ -87,7 +88,7 @@ func checkVersion(parse *[]svgOptions, version string) (string, error) {
8788
*parse = append([]svgOptions{lValue}, *parse...)
8889
lGate := svgOptions{regex: "\\s+LogicGate=\"([a-zA-Z]+)\"", reconstructOption: "LogicGate"}
8990
*parse = append([]svgOptions{lGate}, *parse...)
90-
return "", fmt.Errorf("Can't decode version 0.0.4 at the moment.")
91+
return "", fmt.Errorf("can't decode version 0.0.4 at the moment")
9192
case "0.0.3":
9293
filter := svgOptions{regex: "\\s+Filter=\"(\\w+)\"", reconstructOption: "Filter"}
9394
*parse = append([]svgOptions{filter}, *parse...)
@@ -96,7 +97,7 @@ func checkVersion(parse *[]svgOptions, version string) (string, error) {
9697
dtg := svgOptions{regex: "\\s+DTG=\"([0-9. :a-zA-Z]+)\"", reconstructOption: "Dtg"}
9798
*parse = append([]svgOptions{dtg}, *parse...)
9899
default:
99-
return "", fmt.Errorf("Unrecognized version: %s", version)
100+
return "", fmt.Errorf("unrecognized version: %s", version)
100101
}
101102
return version, nil
102103
}
@@ -128,13 +129,13 @@ func checkHeader(svg *bufio.Scanner) (reconstructOptions, error) {
128129
for svg.Scan() {
129130
line := svg.Text()
130131
switch {
131-
case options.LimitX == 0 && options.LimitY == 0 && header == false:
132+
case options.LimitX == 0 && options.LimitY == 0 && !header:
132133
matches := limits.FindStringSubmatch(line)
133134
if len(matches) == 3 {
134135
options.LimitX, _ = strconv.Atoi(matches[1])
135136
options.LimitY, _ = strconv.Atoi(matches[2])
136137
}
137-
case header == false:
138+
case !header:
138139
if headerStart.MatchString(line) {
139140
header = true
140141
}
@@ -148,7 +149,7 @@ func checkHeader(svg *bufio.Scanner) (reconstructOptions, error) {
148149
}
149150
default:
150151
if optionIndex > len(parseOptions) {
151-
return options, fmt.Errorf("Option index is out of range")
152+
return options, fmt.Errorf("option index is out of range")
152153
}
153154
regex, err := regexp.Compile(parseOptions[optionIndex].regex)
154155
if err != nil {
@@ -165,7 +166,7 @@ func checkHeader(svg *bufio.Scanner) (reconstructOptions, error) {
165166
case reflect.String:
166167
option.SetString(matches[1])
167168
default:
168-
return options, fmt.Errorf("Unhandeld option type")
169+
return options, fmt.Errorf("unhandeld option type")
169170
}
170171
optionIndex++
171172
} else {
@@ -175,13 +176,13 @@ func checkHeader(svg *bufio.Scanner) (reconstructOptions, error) {
175176
}
176177
}
177178
}
178-
return options, fmt.Errorf("No end of header found")
179+
return options, fmt.Errorf("no end of header found")
179180
}
180181

181182
func extractInformation(g *errgroup.Group, ch chan []byte, cfg configs) error {
182183
inputfile, err := os.Open(cfg.input)
183184
if err != nil {
184-
return fmt.Errorf("Could not open file %s: %s\n", cfg.input, err.Error())
185+
return fmt.Errorf("could not open file %s: %s", cfg.input, err.Error())
185186
}
186187
defer inputfile.Close()
187188
svg := bufio.NewScanner(inputfile)
@@ -217,7 +218,7 @@ func extractInformation(g *errgroup.Group, ch chan []byte, cfg configs) error {
217218
packet = packet[:0]
218219
}
219220
if pixelX >= opt.LimitX {
220-
return fmt.Errorf("x-coordinate (%d) is bigger than the limit (%d)\n", pixelX, opt.LimitX)
221+
return fmt.Errorf("x-coordinate (%d) is bigger than the limit (%d)", pixelX, opt.LimitX)
221222
}
222223
r, _ := strconv.Atoi(matches[3])
223224
g, _ := strconv.Atoi(matches[4])
@@ -237,7 +238,7 @@ func createPcap(g *errgroup.Group, ch chan []byte, cfg configs) error {
237238
filename += ".pcap"
238239
output, err := os.Create(filename)
239240
if err != nil {
240-
return fmt.Errorf("Could not create file %s: %s\n", filename, err.Error())
241+
return fmt.Errorf("could not create file %s: %s", filename, err.Error())
241242
}
242243
defer output.Close()
243244
w := pcapgo.NewWriter(output)

goNetViz-reconstruct_test.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"golang.org/x/sync/errgroup"
87
"io/ioutil"
98
"os"
109
"regexp"
1110
"sync"
1211
"testing"
12+
13+
"golang.org/x/sync/errgroup"
1314
)
1415

1516
func TestReconstruct(t *testing.T) {
@@ -41,7 +42,7 @@ func TestReconstruct(t *testing.T) {
4142
cfg configs
4243
err string
4344
}{
44-
{name: "solder", cfg: configs{1, 2, 0, 0, solder, 1, 1500, "", fmt.Sprintf("%s", fakePcap.Name()), fmt.Sprintf("%s/solder", tdir), logic}},
45+
{name: "solder", cfg: configs{1, 2, 0, 0, solder, 1, 1500, "", fakePcap.Name(), fmt.Sprintf("%s/solder", tdir), logic}},
4546
}
4647
for _, tc := range tests {
4748
t.Run(tc.name, func(t *testing.T) {
@@ -122,7 +123,7 @@ func TestCreatePacket(t *testing.T) {
122123
{name: "24 BitsPerPixel", recv: []byte{8, 16, 32, 64, 128}, packet: []int{8, 16, 32, 64, 128}, bpP: 24},
123124
{name: "12 BitPerPixel", recv: []byte{1, 2, 5, 13, 18, 57, 153}, packet: []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233}, bpP: 12},
124125
{name: "3 BitPerPixel", recv: []byte{5, 49, 14}, packet: []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1, 1, 0}, bpP: 3},
125-
{name: "2 BitsPerPixel", recv: []byte{}, packet: []int{8, 16, 32, 64, 128}, bpP: 2, err: "This format is not supported so far"},
126+
{name: "2 BitsPerPixel", recv: []byte{}, packet: []int{8, 16, 32, 64, 128}, bpP: 2, err: "this format is not supported so far"},
126127
{name: "1 BitPerPixel", recv: []byte{1, 8}, packet: []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0}, bpP: 1},
127128
}
128129

@@ -134,10 +135,8 @@ func TestCreatePacket(t *testing.T) {
134135
var recv []byte
135136
go func() {
136137
defer wg.Done()
137-
select {
138-
case v, _ := <-ch:
139-
recv = append(recv, v...)
140-
}
138+
v := <-ch
139+
recv = append(recv, v...)
141140
close(ch)
142141
}()
143142
err := createPacket(ch, tc.packet, tc.bpP)
@@ -153,7 +152,7 @@ func TestCreatePacket(t *testing.T) {
153152
t.Fatalf("Expected error, got none")
154153
}
155154
wg.Wait()
156-
if bytes.Compare(recv, tc.recv) != 0 {
155+
if !bytes.Equal(recv, tc.recv) {
157156
t.Fatalf("Expected: %v \t Got: %v", tc.recv, recv)
158157
}
159158
})
@@ -234,12 +233,12 @@ func TestExtractInformation(t *testing.T) {
234233
recv []byte
235234
err string
236235
}{
237-
{name: "No file", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", "noFile", fmt.Sprintf("%s/noFile", dir), logic}, err: "Could not open file"},
238-
{name: "Not a svg", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", fmt.Sprintf("%s", notSvgFile.Name()), fmt.Sprintf("%s/not_a_svg", dir), logic}, err: "No end of header found"},
239-
{name: "Without Comment", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", fmt.Sprintf("%s", withoutCommentFile.Name()), fmt.Sprintf("%s/without_comment", dir), logic}, err: "No end of header found"},
240-
{name: "Valid003 svg", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", fmt.Sprintf("%s", validSvgFile003.Name()), fmt.Sprintf("%s/valid_003_svg", dir), logic}, recv: []byte{0, 0}},
241-
{name: "Valid004 svg", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", fmt.Sprintf("%s", validSvgFile004.Name()), fmt.Sprintf("%s/valid_004_svg", dir), logic}, err: "Can't decode version"},
242-
{name: "Invalid version", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", fmt.Sprintf("%s", invalidVersionFile.Name()), fmt.Sprintf("%s/invalid_version", dir), logic}, err: "Unrecognized version"},
236+
{name: "No file", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", "noFile", fmt.Sprintf("%s/noFile", dir), logic}, err: "could not open file"},
237+
{name: "Not a svg", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", notSvgFile.Name(), fmt.Sprintf("%s/not_a_svg", dir), logic}, err: "no end of header found"},
238+
{name: "Without Comment", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", withoutCommentFile.Name(), fmt.Sprintf("%s/without_comment", dir), logic}, err: "no end of header found"},
239+
{name: "Valid003 svg", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", validSvgFile003.Name(), fmt.Sprintf("%s/valid_003_svg", dir), logic}, recv: []byte{0, 0}},
240+
{name: "Valid004 svg", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", validSvgFile004.Name(), fmt.Sprintf("%s/valid_004_svg", dir), logic}, err: "can't decode version"},
241+
{name: "Invalid version", cfg: configs{1, 0, 0, 0, 0, 1, 1500, "", invalidVersionFile.Name(), fmt.Sprintf("%s/invalid_version", dir), logic}, err: "unrecognized version"},
243242
}
244243

245244
for _, tc := range tests {
@@ -267,7 +266,7 @@ func TestExtractInformation(t *testing.T) {
267266
t.Fatalf("Expected error, got none")
268267
}
269268
wg.Wait()
270-
if bytes.Compare(recv, tc.recv) != 0 {
269+
if !bytes.Equal(recv, tc.recv) {
271270
t.Fatalf("Expected: %v \t Got: %v", tc.recv, recv)
272271
}
273272
})

goNetViz.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -194,39 +194,39 @@ func createTerminalVisualization(pkt1, pkt2 data, cfg configs) {
194194

195195
func createImage(filename string, width, height int, content string, cfg configs) error {
196196
if len(content) == 0 {
197-
return fmt.Errorf("No content to write")
197+
return fmt.Errorf("no content to write")
198198
}
199199

200200
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0644)
201201
if err != nil {
202-
return fmt.Errorf("Could not open file %s: %s", filename, err.Error())
202+
return fmt.Errorf("could not open file %s: %s", filename, err.Error())
203203
}
204204

205205
if _, err := f.WriteString(fmt.Sprintf("<?xml version=\"1.0\"?>\n<svg width=\"%d\" height=\"%d\">\n", width, height)); err != nil {
206206
f.Close()
207-
return fmt.Errorf("Could not write header: %s", err.Error())
207+
return fmt.Errorf("could not write header: %s", err.Error())
208208
}
209209

210210
var source = cfg.input
211211

212212
if _, err := f.WriteString(fmt.Sprintf("<!--\n\tgoNetViz \"%s\"\n\tScale=%d\n\tBitsPerPixel=%d\n\tDTG=\"%s\"\n\tSource=\"%s\"\n\tFilter=\"%s\"\n\tLogicGate=\"%s\"\n\tLogicValue=0x%X\n-->\n",
213213
Version, cfg.scale, cfg.bpP, time.Now().UTC(), source, cfg.filter, cfg.logicOp.name, cfg.logicOp.value)); err != nil {
214214
f.Close()
215-
return fmt.Errorf("Could not write additional information: %s", err.Error())
215+
return fmt.Errorf("could not write additional information: %s", err.Error())
216216
}
217217

218218
if _, err := f.WriteString(content); err != nil {
219219
f.Close()
220-
return fmt.Errorf("Could not write content: %s", err.Error())
220+
return fmt.Errorf("could not write content: %s", err.Error())
221221
}
222222

223223
if _, err := f.WriteString("</svg>"); err != nil {
224224
f.Close()
225-
return fmt.Errorf("Could not write closing information: %s", err.Error())
225+
return fmt.Errorf("could not write closing information: %s", err.Error())
226226
}
227227

228228
if err := f.Close(); err != nil {
229-
return fmt.Errorf("Could not close file %s: %s", filename, err.Error())
229+
return fmt.Errorf("could not close file %s: %s", filename, err.Error())
230230
}
231231
return nil
232232
}
@@ -314,7 +314,6 @@ func handlePackets(g *errgroup.Group, input source, cfg configs, ch chan<- data)
314314

315315
ch <- data{len: plen, toa: toa, payload: logicGate(bytes, logicValue)}
316316
}
317-
return
318317
}
319318

320319
func initPcapSource(input, filter string, device bool) (source, error) {
@@ -355,17 +354,17 @@ func initSource(input, filter string, pcap bool) (handle source, err error) {
355354
device = true
356355
}
357356

358-
if len(filter) > 0 || pcap == true {
357+
if len(filter) > 0 || pcap {
359358
return initPcapSource(input, filter, device)
360359
}
361360

362361
if device {
363-
return nil, fmt.Errorf("Please open networking interface with pcap support")
362+
return nil, fmt.Errorf("please open networking interface with pcap support")
364363
}
365364

366365
fi, err := os.Lstat(input)
367366
if err != nil {
368-
return nil, fmt.Errorf("Could not get file information")
367+
return nil, fmt.Errorf("could not get file information")
369368
}
370369
mode := fi.Mode()
371370

@@ -401,7 +400,7 @@ func getOperand(val string) (byte, error) {
401400
}
402401

403402
if err != nil {
404-
return 0x00, fmt.Errorf("Could not convert %s", val)
403+
return 0x00, fmt.Errorf("could not convert %s", val)
405404
}
406405

407406
if i < 0 || i > 255 {

0 commit comments

Comments
 (0)