Add Version method to NFT interface and implementation

Include `RunWithOutput` support in the command package and introduce version parsing logic, enabling retrieval of the nftables version and options.
This commit is contained in:
2025-10-20 22:44:40 +05:00
parent c3a513f92c
commit 9210448f16
3 changed files with 83 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import (
type NFT interface { type NFT interface {
Run(arg ...string) error Run(arg ...string) error
RunWithOutput(arg ...string) (string, error)
} }
type execNFT struct { type execNFT struct {
@@ -35,3 +36,15 @@ func (r *execNFT) Run(arg ...string) error {
return nil return nil
} }
func (r *execNFT) RunWithOutput(arg ...string) (string, error) {
cmd := exec.Command(r.nftPath, arg...)
out, err := cmd.CombinedOutput()
if err != nil {
if len(out) > 0 {
return string(out), err
}
return "", err
}
return string(out), nil
}

49
nft.go
View File

@@ -2,7 +2,10 @@ package nft
import ( import (
"errors" "errors"
"regexp"
"strings"
"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/command"
"git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/table" "git.kor-elf.net/kor-elf-shield/go-nftables-client/internal/table"
) )
@@ -15,6 +18,12 @@ type NFT interface {
// nft flush ruleset // nft flush ruleset
Clear() error 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 returns an API for working with tables.
Table() table.API Table() table.API
} }
@@ -22,6 +31,7 @@ type NFT interface {
type nft struct { type nft struct {
command command.NFT command command.NFT
table table.API table table.API
chain chain.API
} }
// New Returns a client for working with nftables. // New Returns a client for working with nftables.
@@ -48,6 +58,7 @@ func NewWithPath(path string) (NFT, error) {
return &nft{ return &nft{
command: nftCommand, command: nftCommand,
table: table.New(nftCommand), table: table.New(nftCommand),
chain: chain.New(nftCommand),
}, nil }, nil
} }
@@ -56,6 +67,44 @@ func (n *nft) Clear() error {
return n.command.Run(args...) return n.command.Run(args...)
} }
func (n *nft) Version() (Version, error) {
args := []string{"-V"}
out, err := n.command.RunWithOutput(args...)
if err != nil {
return nil, err
}
vers := ""
opts := make(map[string]string)
lines := regexp.MustCompile("\r?\n").Split(strings.TrimSpace(string(out)), -1)
for index, line := range lines {
line = strings.TrimSpace(line)
if index == 0 {
vers = line
continue
}
values := strings.Split(line, ":")
if len(values) != 2 {
continue
}
name := strings.TrimSpace(values[0])
value := strings.TrimSpace(values[1])
opts[name] = value
}
return &version{
version: vers,
opts: opts,
}, nil
}
func (n *nft) Table() table.API { func (n *nft) Table() table.API {
return n.table return n.table
} }
func (n *nft) Chain() chain.API {
return n.chain
}

21
version.go Normal file
View File

@@ -0,0 +1,21 @@
package nft
type Version interface {
// Version returns the version of the nftables client.
Version() string
// Opts returns the options of the nftables client.
Opts() map[string]string
}
type version struct {
version string
opts map[string]string
}
func (v version) Version() string {
return v.version
}
func (v version) Opts() map[string]string {
return v.opts
}