Files
go-nftables-client/internal/command/command.go
Leonid Nikitin 9210448f16 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.
2025-10-20 22:44:40 +05:00

51 lines
815 B
Go

package command
import (
"errors"
"os/exec"
)
type NFT interface {
Run(arg ...string) error
RunWithOutput(arg ...string) (string, error)
}
type execNFT struct {
nftPath string
}
func New(path string) (NFT, error) {
if err := checkingNFT(path); err != nil {
return nil, err
}
return &execNFT{
nftPath: path,
}, nil
}
func (r *execNFT) Run(arg ...string) error {
cmd := exec.Command(r.nftPath, arg...)
out, err := cmd.CombinedOutput()
if err != nil {
if len(out) > 0 {
return errors.New(string(out))
}
return err
}
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
}