Refactor to consolidate APIs into contract package and introduce NFT interface for better modularity and maintainability.

This commit is contained in:
2026-04-22 21:54:59 +05:00
parent 92803286f5
commit 3c47e7566b
12 changed files with 182 additions and 154 deletions
+13 -39
View File
@@ -5,50 +5,24 @@ import (
"regexp"
"strings"
"git.kor-elf.net/kor-elf-shield/go-nftables-client/contract"
nftContract "git.kor-elf.net/kor-elf-shield/go-nftables-client/contract/nft"
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/chain"
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/command"
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/rule"
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/table"
)
// NFT A client for working with nftables
type NFT interface {
// Command returns the command used to execute nft.
// You can execute your raw request.
Command() command.NFT
// Clear clears all rules.
//
// This command is equivalent to:
// nft flush ruleset
Clear() error
// Version returns the version of nftables.
//
// This command is equivalent to:
// nft -V
Version() (Version, error)
// Table returns an API for working with tables.
Table() table.API
// Chain returns an API for working with chains.
Chain() chain.API
// Rule returns an API for working with rules.
Rule() rule.API
}
type nft struct {
command command.NFT
table table.API
chain chain.API
rule rule.API
command contract.Command
table nftContract.Table
chain nftContract.Chain
rule nftContract.Rule
}
// New Returns a client for working with nftables.
// Searches for nft in paths: nft, /usr/sbin/nft, /sbin/nft
func New() (NFT, error) {
func New() (contract.NFT, error) {
paths := []string{"nft", "/usr/sbin/nft", "/sbin/nft"}
for _, path := range paths {
nftClient, err := NewWithPath(path)
@@ -61,7 +35,7 @@ func New() (NFT, error) {
}
// NewWithPath Returns the client for working with nftables with its path specified.
func NewWithPath(path string) (NFT, error) {
func NewWithPath(path string) (contract.NFT, error) {
nftCommand, err := command.New(path)
if err != nil {
return nil, err
@@ -80,7 +54,7 @@ func (n *nft) Clear() error {
return n.command.Run(args...)
}
func (n *nft) Version() (Version, error) {
func (n *nft) Version() (nftContract.Version, error) {
args := []string{"-V"}
out, err := n.command.RunWithOutput(args...)
if err != nil {
@@ -114,18 +88,18 @@ func (n *nft) Version() (Version, error) {
}, nil
}
func (n *nft) Table() table.API {
func (n *nft) Table() nftContract.Table {
return n.table
}
func (n *nft) Chain() chain.API {
func (n *nft) Chain() nftContract.Chain {
return n.chain
}
func (n *nft) Rule() rule.API {
func (n *nft) Rule() nftContract.Rule {
return n.rule
}
func (n *nft) Command() command.NFT {
func (n *nft) Command() contract.Command {
return n.command
}