Merge pull request #52983 from corhere/nftables-fluent-api

d/libn/i/nftables: add fluent chain builder API
This commit is contained in:
Sebastiaan van Stijn
2026-07-02 20:24:16 +02:00
committed by GitHub
2 changed files with 35 additions and 1 deletions

View File

@@ -0,0 +1,30 @@
package nftables
type chainBuilder struct {
chain string
tm *Modifier
}
func (b BaseChain) Builder() chainBuilder {
tm := &Modifier{}
tm.create(b, 1)
return chainBuilder{chain: b.Name, tm: tm}
}
func (c Chain) Builder() chainBuilder {
tm := &Modifier{}
tm.create(c, 1)
return chainBuilder{chain: c.Name, tm: tm}
}
func (b chainBuilder) Rule(rule ...string) chainBuilder {
b.tm.create(Rule{
Chain: b.chain,
Rule: rule,
}, 1)
return b
}
func (b chainBuilder) Create(tm *Modifier) {
tm.cmds = append(tm.cmds, b.tm.cmds...)
}

View File

@@ -490,7 +490,11 @@ type Modifier struct {
// Create enqueues creation of object o, to be applied by tm.Apply.
func (tm *Modifier) Create(o Obj) {
_, f, l, _ := runtime.Caller(1)
tm.create(o, 1)
}
func (tm *Modifier) create(o Obj, skipFrames int) {
_, f, l, _ := runtime.Caller(skipFrames + 1)
tm.cmds = append(tm.cmds, command{
obj: o,
callerFile: f,