Unwind changes to doExec and handle List() error differently

exec.Output() already wraps the stderr output, so we don't need to do that.
This commit is contained in:
Bryan Boreham authored and Marc Carré committed 2017-03-14 17:56:59 +00:00
1 parent 6ddbc5cb57
commit 5f30846092
1 file changed
+12 -20
+12 -20
View File
@@ -38,53 +38,46 @@ func New() Interface {
} }
func (i *ipset) Create(ipsetName Name, ipsetType Type) error { func (i *ipset) Create(ipsetName Name, ipsetType Type) error {
err, _ := doExec("create", string(ipsetName), string(ipsetType)) return doExec("create", string(ipsetName), string(ipsetType))
return err
} }
func (i *ipset) AddEntry(ipsetName Name, entry string) error { func (i *ipset) AddEntry(ipsetName Name, entry string) error {
if i.inc(ipsetName, entry) > 1 { // already in the set if i.inc(ipsetName, entry) > 1 { // already in the set
return nil return nil
} }
err, _ := doExec("add", string(ipsetName), entry) return doExec("add", string(ipsetName), entry)
return err
} }
func (i *ipset) DelEntry(ipsetName Name, entry string) error { func (i *ipset) DelEntry(ipsetName Name, entry string) error {
if i.dec(ipsetName, entry) > 0 { // still needed if i.dec(ipsetName, entry) > 0 { // still needed
return nil return nil
} }
err, _ := doExec("del", string(ipsetName), entry) return doExec("del", string(ipsetName), entry)
return err
} }
func (i *ipset) Flush(ipsetName Name) error { func (i *ipset) Flush(ipsetName Name) error {
i.removeSet(ipsetName) i.removeSet(ipsetName)
err, _ := doExec("flush", string(ipsetName)) return doExec("flush", string(ipsetName))
return err
} }
func (i *ipset) FlushAll() error { func (i *ipset) FlushAll() error {
i.refCount = newRefCount() i.refCount = newRefCount()
err, _ := doExec("flush") return doExec("flush")
return err
} }
func (i *ipset) Destroy(ipsetName Name) error { func (i *ipset) Destroy(ipsetName Name) error {
i.removeSet(ipsetName) i.removeSet(ipsetName)
err, _ := doExec("destroy", string(ipsetName)) return doExec("destroy", string(ipsetName))
return err
} }
func (i *ipset) DestroyAll() error { func (i *ipset) DestroyAll() error {
i.refCount = newRefCount() i.refCount = newRefCount()
err, _ := doExec("destroy") return doExec("destroy")
return err
} }
// Fetch a list of all existing sets with a given prefix // Fetch a list of all existing sets with a given prefix
func (i *ipset) List(prefix string) ([]Name, error) { func (i *ipset) List(prefix string) ([]Name, error) {
err, output := doExec("list","-name","-output","plain") output, err := exec.Command("ipset", "list", "-name", "-output", "plain").Output()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -100,12 +93,11 @@ func (i *ipset) List(prefix string) ([]Name, error) {
return selected, err return selected, err
} }
func doExec(args ...string) (error, []byte) { func doExec(args ...string) error {
output, err := exec.Command("ipset", args...).CombinedOutput() if output, err := exec.Command("ipset", args...).CombinedOutput(); err != nil {
if err != nil { return errors.Wrapf(err, "ipset %v failed: %s", args, output)
return errors.Wrapf(err, "ipset %v failed: %s", args, output), output
} }
return nil, output return nil
} }
// Reference-counting // Reference-counting