77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
package nft
|
|
|
|
import (
|
|
"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"
|
|
nftCommand "git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/pkg/nft"
|
|
)
|
|
|
|
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) Command() contract.CommandRun {
|
|
return b.command
|
|
}
|
|
|
|
func (b *batchBuilder) Clear() error {
|
|
args := nftCommand.Clear()
|
|
return b.command.Run(args...)
|
|
}
|
|
|
|
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()
|
|
}
|