Skip to content

implement COMMIT at the end of iptables-save #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func (d Policy) String() string {
return fmt.Sprintf("%s%s %s", prefix, d.Chain, d.Action)
}

type Commit struct{}

func (c Commit) String() string {
return "COMMIT"
}

// Rule represents a rule in an iptables dump. Normally the start with -A.
// The parser treats the -A flag like any other flag, thus does not require
// the -A flag as the leading flag.
Expand Down Expand Up @@ -373,6 +379,8 @@ func (p *Parser) Parse() (l Line, err error) {
return p.parseRule()
case COLON:
return p.parseDefault(p.s.scanLine())
case COMMIT:
return Commit{}, nil
case EOF:
return nil, io.EOF // ErrEOF
case NEWLINE:
Expand Down
6 changes: 4 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ func TestParser_ParseMore(t *testing.T) {
},
},
{
name: "Parse some rules from iptables -S",
name: "Parse some rules from iptables -S as well as iptables-save",
s: `-P INPUT ACCEPT
-P FORWARD DROP
-P OUTPUT ACCEPT
Expand All @@ -1220,7 +1220,8 @@ func TestParser_ParseMore(t *testing.T) {
-N DOCKER-ISOLATION-STAGE-2
-N DOCKER-USER
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION-STAGE-1`,
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
COMMIT`,
r: []interface{}{
Policy{
UserDefined: &_false,
Expand Down Expand Up @@ -1265,6 +1266,7 @@ func TestParser_ParseMore(t *testing.T) {
Name: "DOCKER-ISOLATION-STAGE-1",
},
},
Commit{},
},
},
} {
Expand Down
6 changes: 5 additions & 1 deletion scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func (s *scanner) scan() (tok Token, lit string) {
return s.scanWhitespace()
case isLetter(ch) || isDigit(ch):
s.unread()
return s.scanIdent()
tok, lit := s.scanIdent()
if lit == "COMMIT" {
return COMMIT, "COMMIT"
}
return tok, lit
}

// Otherwise read the individual character.
Expand Down
1 change: 1 addition & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (

// Literals
IDENT // main
COMMIT

// Misc characters
COLON // :
Expand Down
Loading