Convert setJailProperty to setStructFieldValue
This commit is contained in:
parent
4f85f2e6ac
commit
f9ce3601df
@ -104,53 +104,21 @@ func SetJailProperties(args []string) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, p := range props {
|
||||
err := setJailProperty(&jail, p.name, p.value)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// setJailProperty takes a string as propValue, whatever the real property type is.
|
||||
// It will be converted.
|
||||
func setJailProperty(jail *Jail, propName string, propValue string) error {
|
||||
for i, j := range gJails {
|
||||
if j.Name == jail.Name {
|
||||
val, _, err := getStructFieldValue(&gJails[i], propName)
|
||||
//val, _, err := getStructFieldValue(&gJails[i].Config, strings.Split(propName, ".")[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if val.CanSet() {
|
||||
switch val.Kind() {
|
||||
case reflect.String:
|
||||
val.SetString(propValue)
|
||||
case reflect.Int:
|
||||
ival, err := strconv.ParseInt(propValue, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
val.SetInt(ival)
|
||||
case reflect.Bool:
|
||||
bval, err := strconv.ParseBool(propValue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
val.SetBool(bval)
|
||||
default:
|
||||
return errors.New(fmt.Sprintf("Field is an unkown type: %s: %s", propName, val.Kind().String()))
|
||||
// Get jail by index to modify it
|
||||
for i, _ := range gJails {
|
||||
if gJails[i].Name == jail.Name {
|
||||
for _, p := range props {
|
||||
err := setStructFieldValue(&gJails[i], p.name, p.value)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %s\n", err.Error())
|
||||
return
|
||||
} else {
|
||||
fmt.Printf("%s: %s set to %s\n", gJails[i].Name, p.name, p.value)
|
||||
gJails[i].ConfigUpdated = true
|
||||
}
|
||||
fmt.Printf("%s: %s set to %s\n", jail.Name, propName, propValue)
|
||||
gJails[i].ConfigUpdated = true
|
||||
} else {
|
||||
return errors.New(fmt.Sprintf("Field is not writable : %s", propName))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
38
cmd/utils.go
38
cmd/utils.go
@ -23,7 +23,7 @@ func NewJailConfig() (JailConfig, error) {
|
||||
|
||||
hostid, err := ioutil.ReadFile("/etc/hostid")
|
||||
if err != nil {
|
||||
return err
|
||||
return jc, err
|
||||
} else {
|
||||
hostid = []byte(strings.Replace(string(hostid), "\n", "", -1))
|
||||
}
|
||||
@ -157,7 +157,7 @@ func NewJailConfig() (JailConfig, error) {
|
||||
jc.Writebps = "off"
|
||||
jc.Writeiops = "off"
|
||||
|
||||
return jc
|
||||
return jc, nil
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
@ -331,6 +331,7 @@ func getStructFieldNames(parentStruct interface{}, result []string, prefix strin
|
||||
/********************************************************************************
|
||||
* Recurse into structure, returning reflect.Value of wanted field.
|
||||
* Nested fields are named with a dot (ex "MyStruct.MyField")
|
||||
* Returns (value, field_name, error)
|
||||
*******************************************************************************/
|
||||
func getStructFieldValue(parentStruct interface{}, fieldName string) (*reflect.Value, string, error) {
|
||||
v := reflect.ValueOf(parentStruct)
|
||||
@ -431,6 +432,39 @@ func getStructField(parentStruct interface{}, fieldName string) (reflect.Value,
|
||||
return v, fieldName
|
||||
}
|
||||
|
||||
// setStructFieldValue takes a string as propValue, whatever the real property type is.
|
||||
// It will be converted.
|
||||
func setStructFieldValue(parentStruct interface{}, propName string, propValue string) error {
|
||||
val, _, err := getStructFieldValue(parentStruct, propName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if val.CanSet() {
|
||||
switch val.Kind() {
|
||||
case reflect.String:
|
||||
val.SetString(propValue)
|
||||
case reflect.Int:
|
||||
ival, err := strconv.ParseInt(propValue, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
val.SetInt(ival)
|
||||
case reflect.Bool:
|
||||
bval, err := strconv.ParseBool(propValue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
val.SetBool(bval)
|
||||
default:
|
||||
return errors.New(fmt.Sprintf("Field is an unkown type: %s: %s", propName, val.Kind().String()))
|
||||
}
|
||||
} else {
|
||||
return errors.New(fmt.Sprintf("Field is not writable : %s", propName))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/********************************************************************************
|
||||
* Pretty display of jails field
|
||||
|
Loading…
Reference in New Issue
Block a user