Add BatchBuilder for constructing and managing nftables batch commands
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package nft
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.kor-elf.net/kor-elf-shield/go-nftables-client/contract"
|
||||
contractBatch "git.kor-elf.net/kor-elf-shield/go-nftables-client/contract/batch"
|
||||
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/batch"
|
||||
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/batch/chain"
|
||||
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/batch/rule"
|
||||
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/batch/table"
|
||||
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/pkg"
|
||||
)
|
||||
|
||||
type batchBuilder struct {
|
||||
command contract.CommandRun
|
||||
file *pkg.File
|
||||
|
||||
table contractBatch.Table
|
||||
chain contractBatch.Chain
|
||||
rule contractBatch.Rule
|
||||
|
||||
isBuildBatch bool
|
||||
}
|
||||
|
||||
func NewBatchBuilder(dir string) (contract.BatchBuilder, error) {
|
||||
file, err := pkg.CreateRandomTmpFile(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
command := batch.NewCommand(file)
|
||||
|
||||
return &batchBuilder{
|
||||
command: command,
|
||||
file: file,
|
||||
|
||||
table: table.New(command),
|
||||
chain: chain.New(command),
|
||||
rule: rule.New(command),
|
||||
|
||||
isBuildBatch: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (b *batchBuilder) Clear() error {
|
||||
return fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (b *batchBuilder) Table() contractBatch.Table {
|
||||
return b.table
|
||||
}
|
||||
|
||||
func (b *batchBuilder) Chain() contractBatch.Chain {
|
||||
return b.chain
|
||||
}
|
||||
|
||||
func (b *batchBuilder) Rule() contractBatch.Rule {
|
||||
return b.rule
|
||||
}
|
||||
|
||||
func (b *batchBuilder) Build() contract.Batch {
|
||||
b.isBuildBatch = true
|
||||
return batch.NewBatch(b.file)
|
||||
}
|
||||
|
||||
func (b *batchBuilder) Close() error {
|
||||
if b.isBuildBatch {
|
||||
return nil
|
||||
}
|
||||
return b.file.Remove()
|
||||
}
|
||||
Reference in New Issue
Block a user