From 9cd17572b23ff93861752f08ed1fda544d2bb082 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sun, 26 Apr 2026 15:28:17 +0500 Subject: [PATCH] Add BatchBuilder for constructing and managing nftables batch commands --- batch.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 batch.go diff --git a/batch.go b/batch.go new file mode 100644 index 0000000..9dc87ad --- /dev/null +++ b/batch.go @@ -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() +}