Simplify List()

This commit is contained in:
Bryan Boreham authored and Marc Carré committed 2017-03-14 17:56:59 +00:00
1 parent f9f3b18ce4
commit 6ddbc5cb57
1 file changed
+8 -12
+8 -12
View File
@@ -1,7 +1,6 @@
package ipset package ipset
import ( import (
"bytes"
"os/exec" "os/exec"
"strings" "strings"
@@ -83,22 +82,19 @@ func (i *ipset) DestroyAll() error {
return err return err
} }
// Fetch a list of all existing sets // 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") err, output := doExec("list","-name","-output","plain")
if err != nil {
return nil, err
}
var selected []Name var selected []Name
if err == nil && len(output) > 0 { sets := strings.Split(string(output), "\n")
output = bytes.TrimRight(output, "\n") for _, v := range sets {
sets := strings.Split(string(output[:]), "\n") if strings.HasPrefix(v, prefix) {
selected = append(selected, Name(v))
plen := len(prefix)
for _, v := range sets {
if (plen <= len(v)) && (prefix == v[:len(prefix)]) {
selected = append(selected, Name(v))
}
} }
} }
return selected, err return selected, err