From 6ddbc5cb572ca4424d7be337714f2b3270b906d5 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 6 Mar 2017 17:31:09 +0000 Subject: [PATCH] Simplify List() --- npc/ipset/ipset.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/npc/ipset/ipset.go b/npc/ipset/ipset.go index 1a197bb1..a30f799c 100644 --- a/npc/ipset/ipset.go +++ b/npc/ipset/ipset.go @@ -1,7 +1,6 @@ package ipset import ( - "bytes" "os/exec" "strings" @@ -83,22 +82,19 @@ func (i *ipset) DestroyAll() error { 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) { err, output := doExec("list","-name","-output","plain") + if err != nil { + return nil, err + } var selected []Name - if err == nil && len(output) > 0 { - output = bytes.TrimRight(output, "\n") - sets := strings.Split(string(output[:]), "\n") - - plen := len(prefix) - for _, v := range sets { - if (plen <= len(v)) && (prefix == v[:len(prefix)]) { - selected = append(selected, Name(v)) - } + sets := strings.Split(string(output), "\n") + for _, v := range sets { + if strings.HasPrefix(v, prefix) { + selected = append(selected, Name(v)) } - } return selected, err