From 0434a784cc2d8a734ef0febd134af50ea6651e54 Mon Sep 17 00:00:00 2001 From: Matthew R Kasun Date: Sun, 30 Jan 2022 18:11:22 -0500 Subject: [PATCH] prevent fatal stop of mq daemon when netclient config dir contains unexpected files --- netclient/ncutils/netclientutils.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index a7f3e956..6bbc37d4 100644 --- a/netclient/ncutils/netclientutils.go +++ b/netclient/ncutils/netclientutils.go @@ -14,6 +14,7 @@ import ( "net/http" "os" "os/exec" + "path/filepath" "regexp" "runtime" "strconv" @@ -449,18 +450,19 @@ func PrintLog(message string, loglevel int) { // GetSystemNetworks - get networks locally func GetSystemNetworks() ([]string, error) { - var networks []string - files, err := os.ReadDir(GetNetclientPathSpecific()) + files, err := filepath.Glob(GetNetclientPathSpecific() + "netconfig-*") if err != nil { - return networks, err + return nil, err } - for _, f := range files { - if strings.Contains(f.Name(), "netconfig-") && !strings.Contains(f.Name(), "backup") { - networkname := stringAfter(f.Name(), "netconfig-") - networks = append(networks, networkname) + i := 0 + for _, file := range files { + //don't want files such as *.bak, *.swp + if filepath.Ext(file) == "" { + files[i] = filepath.Base(file) + i++ } } - return networks, err + return files[:i], nil } func stringAfter(original string, substring string) string {