Skip to content

Commit 18a518c

Browse files
committed
Apply gofmt formatting
- format code - add gofmt in CI Signed-off-by: leonnicolas <[email protected]>
1 parent 29d4904 commit 18a518c

File tree

5 files changed

+139
-137
lines changed

5 files changed

+139
-137
lines changed

.github/workflows/test.yaml

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
---
12
name: Test
23

34
on:
4-
push:
55
pull_request:
66
jobs:
77
test:
88
runs-on: ubuntu-latest
99
steps:
10-
- uses: actions/checkout@main
11-
- uses: actions/setup-go@v1
12-
with:
13-
go-version: '1.16'
14-
- run: go test ./...
10+
- uses: actions/checkout@main
11+
- uses: actions/setup-go@v1
12+
with:
13+
go-version: '1.16'
14+
- run: go test ./...
15+
fmt:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@main
19+
- uses: actions/setup-go@v1
20+
with:
21+
go-version: '1.16'
22+
- run: '[ -z "$(gofmt -e -d ./)" ]'

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2021 Leon Löchner
1+
Copyright (c) 2022 Leon Löchner
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

match_modules.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (p *Parser) parseStatistic(f *map[string]Flag) (state, error) {
8080
// unscanIgnoreWhitespace twice (this can fail
8181
// because of a fixed sized buffer, that is full
8282
// of Whitespaces).
83-
p.unscan(1) //IgnoreWhitespace(2) // unscan 2
83+
p.unscan(1) // IgnoreWhitespace(2) // unscan 2
8484
return sNot, nil
8585
}
8686
case sIF:
@@ -160,7 +160,7 @@ func (p *Parser) parseUdp(f *map[string]Flag) (state, error) {
160160
// unscanIgnoreWhitespace twice (this can fail
161161
// because of a fixed sized buffer, that is full
162162
// of Whitespaces).
163-
p.unscan(1) //IgnoreWhitespace(2) // unscan 2
163+
p.unscan(1) // IgnoreWhitespace(2) // unscan 2
164164
return sNot, nil
165165
}
166166
case sIF:
@@ -188,10 +188,10 @@ func (p *Parser) parseUdp(f *map[string]Flag) (state, error) {
188188
}
189189
return sStart, nil
190190
}
191+
191192
func (p *Parser) parseAddrtype(f *map[string]Flag) (state, error) {
192193
s := sStart
193194
for tok, lit := p.scanIgnoreWhitespace(); tok != EOF; tok, lit = p.scanIgnoreWhitespace() {
194-
195195
for nextValue := false; !nextValue; {
196196
nextValue = true
197197
switch s {
@@ -228,7 +228,7 @@ func (p *Parser) parseAddrtype(f *map[string]Flag) (state, error) {
228228
// unscanIgnoreWhitespace twice (this can fail
229229
// because of a fixed sized buffer, that is full
230230
// of Whitespaces).
231-
p.unscan(1) //IgnoreWhitespace(2) // unscan 2
231+
p.unscan(1) // IgnoreWhitespace(2) // unscan 2
232232
return sNot, nil
233233
}
234234
case sIF:
@@ -334,7 +334,7 @@ func (p *Parser) parseTcp(f *map[string]Flag) (state, error) {
334334
// unscanIgnoreWhitespace twice (this can fail
335335
// because of a fixed sized buffer, that is full
336336
// of Whitespaces).
337-
p.unscan(1) //IgnoreWhitespace(2) // unscan 2
337+
p.unscan(1) // IgnoreWhitespace(2) // unscan 2
338338
return sNot, nil
339339
}
340340
case sIF:

parser.go

+31-44
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ func (d Policy) String() string {
6060
}
6161
if d.Counter != nil {
6262
return fmt.Sprintf("%s%s %s %s", prefix, d.Chain, d.Action, d.Counter.String())
63-
} else {
64-
return fmt.Sprintf("%s%s %s", prefix, d.Chain, d.Action)
6563
}
64+
return fmt.Sprintf("%s%s %s", prefix, d.Chain, d.Action)
6665
}
6766

6867
// Rule represents a rule in an iptables dump. Normally the start with -A.
@@ -144,7 +143,6 @@ func (r Rule) Spec() (ret []string) {
144143
} else {
145144
ret = append(ret, "!", "-f")
146145
}
147-
148146
}
149147
if r.IPv4 {
150148
ret = append(ret, "-4")
@@ -154,7 +152,6 @@ func (r Rule) Spec() (ret []string) {
154152
}
155153
if len(r.Matches) > 0 {
156154
for _, m := range r.Matches {
157-
158155
ret = append(ret, m.Spec()...)
159156
}
160157
}
@@ -169,8 +166,8 @@ func (r Rule) Spec() (ret []string) {
169166

170167
// EqualTo returns true, if the rules are
171168
// equal to each other.
172-
func (r1 Rule) EqualTo(r2 Rule) bool {
173-
return reflect.DeepEqual(r1, r2)
169+
func (r Rule) EqualTo(r2 Rule) bool {
170+
return reflect.DeepEqual(r, r2)
174171
}
175172

176173
// DNSOrIPPair either holds an IP or DNS and a flag.
@@ -187,6 +184,7 @@ func (d DNSOrIPPair) String(f string) string {
187184
return strings.Join(d.Spec(f), " ")
188185
}
189186

187+
// Spec returns a DNSOrIPPair how coreos' iptables package would expect it.
190188
func (d DNSOrIPPair) Spec(f string) []string {
191189
s := []string{"!", f, d.Value.String()}
192190
if !d.Not {
@@ -256,6 +254,7 @@ func (sp StringPair) String(f string) string {
256254
return strings.Join(sp.Spec(f), " ")
257255
}
258256

257+
// Spec returns a StringPair how coreos' iptables package would expect it.
259258
func (sp StringPair) Spec(f string) []string {
260259
ret := []string{"!", f, sp.Value}
261260
if !sp.Not {
@@ -285,6 +284,7 @@ func (m Match) String() string {
285284
return strings.Join(m.Spec(), " ")
286285
}
287286

287+
// Spec returns a Match how coreos' iptables package would expect it.
288288
func (m Match) Spec() []string {
289289
ret := make([]string, 2, 2+len(m.Flags)*2)
290290
ret[0], ret[1] = "-m", m.Type
@@ -305,6 +305,7 @@ func (fl Flag) String(f string) string {
305305
return strings.Join(fl.Spec(f), " ")
306306
}
307307

308+
// Spec returns a Flag how coreos' iptables package would expect it.
308309
func (fl Flag) Spec(f string) []string {
309310
ret := []string{"!", f}
310311
ret = append(ret, fl.Values...)
@@ -314,6 +315,7 @@ func (fl Flag) Spec(f string) []string {
314315
return ret
315316
}
316317

318+
// Target represents a Target Extension. See iptables-extensions(8).
317319
type Target struct {
318320
Name string
319321
Flags map[string]Flag
@@ -323,6 +325,7 @@ func (t Target) String(name string) string {
323325
return strings.Join(t.Spec(name), " ")
324326
}
325327

328+
// Spec returns a Target how coreos' iptables package would expect it.
326329
func (t Target) Spec(f string) []string {
327330
ret := make([]string, 2, 2+len(t.Flags)*2)
328331
ret[0], ret[1] = f, t.Name
@@ -332,17 +335,17 @@ func (t Target) Spec(f string) []string {
332335
return ret
333336
}
334337

335-
// Max buffer size of the ring buffer in the parser.
336-
const BUF_SIZE = 10
338+
// BUFSIZE is the max buffer size of the ring buffer in the parser.
339+
const BUFSIZE = 16
337340

338341
// Parser represents a parser.
339342
type Parser struct {
340343
s *scanner
341344
buf struct {
342-
toks [BUF_SIZE]Token // token buffer
343-
lits [BUF_SIZE]string // literal buffer
344-
p int // current position in the buffer (max=BUF_SIZE)
345-
n int // offset (max=BUF_SIZE)
345+
toks [BUFSIZE]Token // token buffer
346+
lits [BUFSIZE]string // literal buffer
347+
p int // current position in the buffer (max=BUF_SIZE)
348+
n int // offset (max=BUF_SIZE)
346349
}
347350
}
348351

@@ -371,7 +374,7 @@ func (p *Parser) Parse() (l Line, err error) {
371374
case COLON:
372375
return p.parseDefault(p.s.scanLine())
373376
case EOF:
374-
return nil, io.EOF //ErrEOF
377+
return nil, io.EOF // ErrEOF
375378
case NEWLINE:
376379
return nil, errors.New("empty line")
377380
default:
@@ -392,8 +395,10 @@ func (p *Parser) ParseRule() (*Rule, error) {
392395
}
393396
}
394397

395-
var matchModules map[string]struct{}
396-
var targetExtensions map[string]struct{}
398+
var (
399+
matchModules map[string]struct{}
400+
targetExtensions map[string]struct{}
401+
)
397402

398403
func init() {
399404
matchModules = make(map[string]struct{})
@@ -430,17 +435,17 @@ func (p *Parser) parseDefault(lit string) (Line, error) {
430435
func parseCounter(bytes []byte) (Counter, error) {
431436
var c Counter
432437
pc := regCounter.ReplaceAll(bytes, []byte("$1"))
433-
if i, err := strconv.ParseUint(string(pc), 10, 0); err != nil {
438+
i, err := strconv.ParseUint(string(pc), 10, 0)
439+
if err != nil {
434440
return c, fmt.Errorf("Could not parse counter: %w", err)
435-
} else {
436-
c.packets = i
437441
}
442+
c.packets = i
438443
pc = regCounter.ReplaceAll(bytes, []byte("$2"))
439-
if i, err := strconv.ParseUint(string(pc), 10, 0); err != nil {
444+
i, err = strconv.ParseUint(string(pc), 10, 0)
445+
if err != nil {
440446
return c, fmt.Errorf("Could not parse counter: %w", err)
441-
} else {
442-
c.bytes = i
443447
}
448+
c.bytes = i
444449
return c, nil
445450
}
446451

@@ -579,7 +584,6 @@ func (p *Parser) parseRule() (Line, error) {
579584
// Avoid scanning the next token, if an error occured.
580585
nextValue = nextValue && err == nil
581586
}
582-
583587
}
584588
return r, nil
585589
}
@@ -608,7 +612,7 @@ func (p *Parser) parsePolicy(d bool) (Line, error) {
608612
return ret, nil
609613
}
610614
if tok, lit := p.scanIgnoreWhitespace(); tok != EOF && tok != NEWLINE {
611-
return nil, fmt.Errorf("found %q, expected EOF or newline.", lit)
615+
return nil, fmt.Errorf("found %q, expected EOF or newline", lit)
612616
}
613617
return ret, nil
614618
}
@@ -648,9 +652,8 @@ func (p *Parser) parseStringPair(sp *StringPair, not bool) (state, error) {
648652
*sp = StringPair{Value: "", Not: not}
649653
p.unscan(1)
650654
return sStart, errors.New("unexpected token, expected IDENT")
651-
} else {
652-
*sp = StringPair{Value: lit, Not: not}
653655
}
656+
*sp = StringPair{Value: lit, Not: not}
654657
return sStart, nil
655658
}
656659

@@ -666,14 +669,14 @@ func (p *Parser) scan() (tok Token, lit string) {
666669
// If we have a token on the buffer, return it.
667670
if p.buf.n != 0 {
668671
p.buf.n--
669-
return p.buf.toks[mod(p.buf.p-p.buf.n-1, BUF_SIZE)], p.buf.lits[mod(p.buf.p-p.buf.n-1, BUF_SIZE)]
672+
return p.buf.toks[mod(p.buf.p-p.buf.n-1, BUFSIZE)], p.buf.lits[mod(p.buf.p-p.buf.n-1, BUFSIZE)]
670673
}
671674
// Otherwise read the next token from the scanner.
672675
tok, lit = p.s.scan()
673676
// Save it to the buffer in case we unscan later.
674677
p.buf.toks[p.buf.p], p.buf.lits[p.buf.p] = tok, lit
675678
p.buf.p++ // increase the pointer of the ring buffer.
676-
p.buf.p %= BUF_SIZE
679+
p.buf.p %= BUFSIZE
677680
return
678681
}
679682

@@ -689,28 +692,12 @@ func (p *Parser) scanIgnoreWhitespace() (tok Token, lit string) {
689692
// unscan reverts the pointer on the buffer, callers should not unscan more then what was
690693
// previously read, or values larger then BUF_SIZE.
691694
func (p *Parser) unscan(n int) {
692-
if p.buf.n+n >= BUF_SIZE {
695+
if p.buf.n+n >= BUFSIZE {
693696
panic("size exceeds buffer")
694697
}
695698
p.buf.n += n
696699
}
697700

698-
func (p *Parser) unscanIgnoreWhitespace(n int) error {
699-
for i := 0; i < BUF_SIZE; i++ {
700-
if p.buf.toks[p.buf.n] == ILLEGAL {
701-
break
702-
}
703-
if p.buf.toks[p.buf.n] == WS {
704-
p.unscan(1)
705-
} else {
706-
if n--; n == 0 {
707-
return nil
708-
}
709-
}
710-
}
711-
return errors.New("buffer has no none whitespace characters")
712-
}
713-
714701
var hasWS *regexp.Regexp = regexp.MustCompile(`\s`)
715702

716703
func enquoteIfWS(s []string) []string {

0 commit comments

Comments
 (0)