Browse Source

prevent fatal stop of mq daemon when netclient config dir contains unexpected files

Matthew R Kasun 3 years ago
parent
commit
0434a784cc
1 changed files with 10 additions and 8 deletions
  1. 10 8
      netclient/ncutils/netclientutils.go

+ 10 - 8
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 {