Include `RunWithOutput` support in the command package and introduce version parsing logic, enabling retrieval of the nftables version and options.
51 lines
815 B
Go
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
|
|
}
|