Skip to content

Commit a26cf71

Browse files
authored
Merge pull request #14 from MEschenbacher/commit
implement COMMIT at the end of iptables-save
2 parents 275b7c9 + 344f02b commit a26cf71

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

parser.go

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ func (d Policy) String() string {
6464
return fmt.Sprintf("%s%s %s", prefix, d.Chain, d.Action)
6565
}
6666

67+
type Commit struct{}
68+
69+
func (c Commit) String() string {
70+
return "COMMIT"
71+
}
72+
6773
// Rule represents a rule in an iptables dump. Normally the start with -A.
6874
// The parser treats the -A flag like any other flag, thus does not require
6975
// the -A flag as the leading flag.
@@ -373,6 +379,8 @@ func (p *Parser) Parse() (l Line, err error) {
373379
return p.parseRule()
374380
case COLON:
375381
return p.parseDefault(p.s.scanLine())
382+
case COMMIT:
383+
return Commit{}, nil
376384
case EOF:
377385
return nil, io.EOF // ErrEOF
378386
case NEWLINE:

parser_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ func TestParser_ParseMore(t *testing.T) {
12111211
},
12121212
},
12131213
{
1214-
name: "Parse some rules from iptables -S",
1214+
name: "Parse some rules from iptables -S as well as iptables-save",
12151215
s: `-P INPUT ACCEPT
12161216
-P FORWARD DROP
12171217
-P OUTPUT ACCEPT
@@ -1220,7 +1220,8 @@ func TestParser_ParseMore(t *testing.T) {
12201220
-N DOCKER-ISOLATION-STAGE-2
12211221
-N DOCKER-USER
12221222
-A FORWARD -j DOCKER-USER
1223-
-A FORWARD -j DOCKER-ISOLATION-STAGE-1`,
1223+
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
1224+
COMMIT`,
12241225
r: []interface{}{
12251226
Policy{
12261227
UserDefined: &_false,
@@ -1265,6 +1266,7 @@ func TestParser_ParseMore(t *testing.T) {
12651266
Name: "DOCKER-ISOLATION-STAGE-1",
12661267
},
12671268
},
1269+
Commit{},
12681270
},
12691271
},
12701272
} {

scanner.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ func (s *scanner) scan() (tok Token, lit string) {
2727
return s.scanWhitespace()
2828
case isLetter(ch) || isDigit(ch):
2929
s.unread()
30-
return s.scanIdent()
30+
tok, lit := s.scanIdent()
31+
if lit == "COMMIT" {
32+
return COMMIT, "COMMIT"
33+
}
34+
return tok, lit
3135
}
3236

3337
// Otherwise read the individual character.

token.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010

1111
// Literals
1212
IDENT // main
13+
COMMIT
1314

1415
// Misc characters
1516
COLON // :

0 commit comments

Comments
 (0)