Refactor table management to use a dedicated API and improve command handling.

This commit is contained in:
2025-10-19 22:50:14 +05:00
parent d056f5dbf8
commit 5b19993343
5 changed files with 107 additions and 52 deletions

48
nft.go
View File

@@ -2,6 +2,9 @@ package nft
import (
"errors"
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/command"
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/table"
)
// NFT A client for working with nftables
@@ -12,27 +15,13 @@ type NFT interface {
// nft flush ruleset
Clear() error
// AddTable adds a new table.
//
// This command is equivalent to:
// nft add table (ip|ip6|inet|arp|bridge) {table_name}
AddTable(family FamilyType, tableName string) error
// DeleteTable deletes a table.
//
// This command is equivalent to:
// nft delete table (ip|ip6|inet|arp|bridge) {table_name}
DeleteTable(family FamilyType, tableName string) error
// ClearTable clears all rules in a table.
//
// This command is equivalent to:
// nft flush table (ip|ip6|inet|arp|bridge) {table_name}
ClearTable(family FamilyType, tableName string) error
// Table returns an API for working with tables.
Table() table.API
}
type nft struct {
path string
command command.NFT
table table.API
}
// New Returns a client for working with nftables.
@@ -51,31 +40,22 @@ func New() (NFT, error) {
// NewWithPath Returns the client for working with nftables with its path specified.
func NewWithPath(path string) (NFT, error) {
if err := checkingNFT(path); err != nil {
nftCommand, err := command.New(path)
if err != nil {
return nil, err
}
return &nft{
path: path,
command: nftCommand,
table: table.New(nftCommand),
}, nil
}
func (n *nft) Clear() error {
args := []string{"flush", "ruleset"}
return executeCommand(n.path, args...)
return n.command.Run(args...)
}
func (n *nft) AddTable(family FamilyType, tableName string) error {
args := []string{"add", "table", family.String(), tableName}
return executeCommand(n.path, args...)
}
func (n *nft) DeleteTable(family FamilyType, tableName string) error {
args := []string{"delete", "table", family.String(), tableName}
return executeCommand(n.path, args...)
}
func (n *nft) ClearTable(family FamilyType, tableName string) error {
args := []string{"flush", "table", family.String(), tableName}
return executeCommand(n.path, args...)
func (n *nft) Table() table.API {
return n.table
}