From 9b90f9c8125a84026166b5f5a9d20ae9d2f13680 Mon Sep 17 00:00:00 2001 From: yo Date: Sun, 10 Jul 2022 14:17:12 +0200 Subject: [PATCH] Use getJailFromArray so we can handle same name jails --- cmd/properties.go | 49 ++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/cmd/properties.go b/cmd/properties.go index bb9c903..670e0b8 100644 --- a/cmd/properties.go +++ b/cmd/properties.go @@ -10,13 +10,18 @@ import ( func GetJailProperties(args []string) { var props []string - var jail Jail + var jail *Jail + var err error if len(args) > 0 { for i, a := range args { // Last arg is the jail name if i == len(args)-1 { - jail.Name = a + jail, err = getJailFromArray(a, gJails) + if err != nil { + fmt.Printf("Error: %s\n", err.Error()) + return + } } else { props = append(props, a) } @@ -28,15 +33,15 @@ func GetJailProperties(args []string) { fmt.Printf("Error\n") return } - + if isStringInArray(props, "all") { var result []string - result = getStructFieldNames(jail, result, "") + result = getStructFieldNames(*jail, result, "") props = result } for _, p := range props { - v, err := getJailProperty(&jail, p) + v, err := getJailProperty(jail, p) if err != nil { fmt.Printf("Error: %s\n", err.Error()) return @@ -47,27 +52,23 @@ func GetJailProperties(args []string) { } func getJailProperty(jail *Jail, propName string) (string, error) { - for i, j := range gJails { - if j.Name == jail.Name { - val, _, err := getStructFieldValue(&gJails[i], propName) - if err != nil { - return "", err - } - - switch val.Kind() { - case reflect.String: - return val.String(), nil - case reflect.Int: - return strconv.FormatInt(val.Int(), 10), nil - case reflect.Bool: - return strconv.FormatBool(val.Bool()), nil - default: - return "", errors.New(fmt.Sprintf("Error: Unknown type for property %s: %s\n", propName, val.Kind())) - } - } + val, _, err := getStructFieldValue(jail, propName) + if err != nil { + return "", err } - return "", errors.New(fmt.Sprintf("Jail not found: %s", jail.Name)) + switch val.Kind() { + case reflect.String: + return val.String(), nil + case reflect.Int: + return strconv.FormatInt(val.Int(), 10), nil + case reflect.Bool: + return strconv.FormatBool(val.Bool()), nil + default: + return "", errors.New(fmt.Sprintf("Error: Unknown type for property %s: %s\n", propName, val.Kind())) + } + + return "", errors.New("Unknown error in getJailProperty") } func SetJailProperties(args []string) {