gocage/cmd/properties.go

126 lines
2.4 KiB
Go
Raw Normal View History

package cmd
import (
"os"
2022-04-24 16:49:54 +02:00
"fmt"
"errors"
"reflect"
"strconv"
2022-04-24 16:49:54 +02:00
"strings"
)
func GetJailProperties(args []string) {
var props []string
var jail *Jail
var err error
2022-04-24 16:49:54 +02:00
if len(args) > 0 {
for i, a := range args {
// Last arg is the jail name
if i == len(args)-1 {
2023-08-05 19:49:50 +02:00
jail, err = getJailFromArray(a, []string{""}, gJails)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
os.Exit(1)
}
} else {
props = append(props, a)
}
}
2022-04-24 16:49:54 +02:00
}
if len(jail.Name) == 0 || len(args) == 0 {
// TODO : Show help
fmt.Printf("Error\n")
return
}
2022-04-03 11:04:01 +02:00
if isStringInArray(props, "all") {
var result []string
result = getStructFieldNames(*jail, result, "")
2022-04-03 11:04:01 +02:00
props = result
}
2022-04-24 16:49:54 +02:00
for _, p := range props {
v, err := getJailProperty(jail, p)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return
} else {
fmt.Printf("%s = %s\n", p, v)
}
}
}
func getJailProperty(jail *Jail, propName string) (string, error) {
val, _, err := getStructFieldValue(jail, propName)
if err != nil {
return "", err
}
2022-04-24 16:49:54 +02:00
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()))
}
2022-04-24 16:49:54 +02:00
return "", errors.New("Unknown error in getJailProperty")
}
func SetJailProperties(args []string) {
type properties struct {
2022-04-24 16:49:54 +02:00
name string
value string
}
2022-04-24 16:49:54 +02:00
var jail Jail
var props []properties
2022-04-24 16:49:54 +02:00
if len(args) > 0 {
for i, a := range args {
// This is the jail name
if i == len(args)-1 {
jail.Name = a
} else {
kv := strings.Split(a, "=")
if len(kv) != 2 {
// TODO : Show help
fmt.Printf("Error parsing args: %s\n", a)
return
} else {
p := properties{name: kv[0], value: kv[1]}
props = append(props, p)
}
}
}
2022-04-24 16:49:54 +02:00
}
if len(jail.Name) == 0 || len(args) == 0 {
// TODO : Show help
fmt.Printf("Error\n")
return
}
2022-04-24 16:49:54 +02:00
cj, err := getJailFromArray(jail.Name, []string{""}, gJails)
if err != nil {
fmt.Printf("Error getting jail %s: %v\n", jail.Name, err)
return
}
for _, p := range props {
err := setStructFieldValue(cj, p.name, p.value)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return
} else {
fmt.Printf("%s: %s set to %s\n", cj.Name, p.name, p.value)
}
}
2023-08-26 19:36:36 +02:00
cj.WriteConfigToDisk(false)
}