48 lines
795 B
Go
48 lines
795 B
Go
package command
|
|
|
|
import (
|
|
"errors"
|
|
"os/exec"
|
|
|
|
"git.kor-elf.net/kor-elf-shield/go-nftables-client/contract"
|
|
)
|
|
|
|
type execNFT struct {
|
|
nftPath string
|
|
}
|
|
|
|
func New(path string) (contract.Command, 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
|
|
}
|