Set property K for int type + write config to disk
This commit is contained in:
parent
f40db69b9d
commit
da74456d6a
10
cmd/root.go
10
cmd/root.go
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
gVersion = "0.022b"
|
gVersion = "0.023"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -188,12 +188,16 @@ func cleanAfterRun() {
|
|||||||
for _, j := range gJails {
|
for _, j := range gJails {
|
||||||
if j.ConfigUpdated {
|
if j.ConfigUpdated {
|
||||||
// TODO : Marshall to disk
|
// TODO : Marshall to disk
|
||||||
fmt.Printf("Config for jail %s will be updated\n", j.Name)
|
//fmt.Printf("Config for jail %s will be updated\n", j.Name)
|
||||||
marshaled, err := json.MarshalIndent(j.Config, "", " ")
|
marshaled, err := json.MarshalIndent(j.Config, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
|
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
|
||||||
}
|
}
|
||||||
fmt.Printf(string(marshaled))
|
//fmt.Printf(string(marshaled))
|
||||||
|
if os.WriteFile(j.ConfigPath, []byte(marshaled), 0644); err != nil {
|
||||||
|
fmt.Printf("Error writing config file %s: %v\n", j.ConfigPath, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
29
cmd/start.go
29
cmd/start.go
@ -9,7 +9,7 @@ import (
|
|||||||
// "os/exec"
|
// "os/exec"
|
||||||
// "reflect"
|
// "reflect"
|
||||||
"strings"
|
"strings"
|
||||||
// "strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetJailProperties(args []string) {
|
func SetJailProperties(args []string) {
|
||||||
@ -55,6 +55,7 @@ func SetJailProperties(args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO : Support types. For now we just update int, other types will crash
|
||||||
// WIP. Not working now (need to address the real struct field, not a copy of it)
|
// WIP. Not working now (need to address the real struct field, not a copy of it)
|
||||||
// setJailProperty takes a string as propValue, whatever the real property type is.
|
// setJailProperty takes a string as propValue, whatever the real property type is.
|
||||||
// It will be converted.
|
// It will be converted.
|
||||||
@ -88,23 +89,24 @@ func setJailProperty(jail *Jail, propName string, propValue string) error {
|
|||||||
return errors.New(fmt.Sprintf("Property %s have an unsupported type in setJailProperty!\n", propName))
|
return errors.New(fmt.Sprintf("Property %s have an unsupported type in setJailProperty!\n", propName))
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
// panic: reflect: reflect.Value.Set using unaddressable value
|
if val.CanSet() {
|
||||||
//val.Set(reflect.ValueOf(propValue).Elem())
|
ival, err := strconv.Atoi(propValue)
|
||||||
// ...Because val settability is false :-(
|
if err != nil {
|
||||||
fmt.Printf("settability of val %s: %v\n", val.Interface(), val.CanSet())
|
return err
|
||||||
|
}
|
||||||
// This is OK, using the index to get the real jail object
|
val.SetInt(int64(ival))
|
||||||
//gJails[i].Config.Allow_mlock = 1
|
fmt.Printf("%s: %s set to %s\n", jail.Name, propName, propValue)
|
||||||
|
gJails[i].ConfigUpdated = true
|
||||||
|
} else {
|
||||||
// TODO : integrate this function
|
return errors.New(fmt.Sprintf("Field is not writable : %s", propName))
|
||||||
//setJailConfigUpdated(jail)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME : Do not work?!
|
||||||
// We cant use internalName as the value exist only when jail is running
|
// We cant use internalName as the value exist only when jail is running
|
||||||
func setJailConfigUpdated(jail *Jail) error {
|
func setJailConfigUpdated(jail *Jail) error {
|
||||||
if len(jail.ConfigPath) == 0 {
|
if len(jail.ConfigPath) == 0 {
|
||||||
@ -112,7 +114,8 @@ func setJailConfigUpdated(jail *Jail) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, j := range gJails {
|
for i, j := range gJails {
|
||||||
if jail.ConfigPath == j.ConfigPath {
|
if jail.Name == j.Name {
|
||||||
|
fmt.Printf("Tag %s as configUpdated\n", jail.Name)
|
||||||
gJails[i].ConfigUpdated = true
|
gJails[i].ConfigUpdated = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
138
cmd/utils.go
138
cmd/utils.go
@ -206,116 +206,44 @@ func getStructFieldValue(parentStruct interface{}, fieldName string) (*reflect.V
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.Contains(fieldName, ".") {
|
var f reflect.Value
|
||||||
|
var found bool
|
||||||
|
|
||||||
// Le code degueulasse mais qui marche
|
fs := strings.Split(fieldName, ".")
|
||||||
legacy := false
|
// Loop through properties
|
||||||
|
for i, curF := range fs {
|
||||||
if legacy {
|
found = false
|
||||||
fs := strings.Split(fieldName, ".")
|
for j := 0 ; j < v.NumField() ; j++ {
|
||||||
fmt.Printf("Dealing with field \"%s\" of struct \"%s\"\n", strings.Join(fs[1:], "."), fs[0])
|
if typeOfV.Field(j).Name == curF {
|
||||||
var f reflect.Value
|
f = v.Field(j)
|
||||||
var found bool
|
found = true
|
||||||
for i := 0 ; i < v.NumField() ; i++ {
|
break
|
||||||
if typeOfV.Field(i).Name == fs[0] {
|
|
||||||
f = v.Field(i)
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
|
||||||
}
|
|
||||||
|
|
||||||
for ; f.Kind() == reflect.Ptr ; {
|
|
||||||
fmt.Printf("We got a pointer as a nested field, get Elem form it\n")
|
|
||||||
f = f.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("v.kind() = %v\n", v.Kind().String())
|
|
||||||
fmt.Printf("v = %v\n", v)
|
|
||||||
fmt.Printf("f.kind() = %v\n", f.Kind().String())
|
|
||||||
fmt.Printf("f = %v\n", f)
|
|
||||||
/*if f.Kind() == reflect.Ptr {
|
|
||||||
fmt.Printf("We got a pointer as a nested field, get Elem form it\n")
|
|
||||||
f = f.Elem()
|
|
||||||
}*/
|
|
||||||
//f := v.Elem().FieldByName(fs[0])
|
|
||||||
if f.Kind() == reflect.Struct {
|
|
||||||
//return getStructFieldValue(&f, strings.Join(fs[1:], "."))
|
|
||||||
//return getStructFieldValue(f, strings.Join(fs[1:], "."))
|
|
||||||
|
|
||||||
|
|
||||||
/* GRUIIIK */
|
|
||||||
|
|
||||||
g := f.FieldByName(fs[1])
|
|
||||||
fmt.Printf("get substruct field, f.kind() = %v\n", f.Kind().String())
|
|
||||||
fmt.Printf("get substruct field, g.kind() = %v\n", g.Kind().String())
|
|
||||||
fmt.Printf("get substruct field, f = %v\n", f)
|
|
||||||
fmt.Printf("get substruct field, g = %v\n", g)
|
|
||||||
if g.IsValid() {
|
|
||||||
fmt.Printf("Return g = %v of Kind %s\n", g, g.Kind().String())
|
|
||||||
return &g, fieldName, nil
|
|
||||||
} else {
|
|
||||||
return &f, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
|
||||||
}
|
|
||||||
|
|
||||||
/* FIN DE GRUIIIIK */
|
|
||||||
} else {
|
|
||||||
log.Fatalln(fmt.Sprintf("%s is not a struct: %s\n", fs[0], f.Kind().String()))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
var f reflect.Value
|
|
||||||
var found bool
|
|
||||||
fs := strings.Split(fieldName, ".")
|
|
||||||
// Loop through properties
|
|
||||||
for i, curF := range fs {
|
|
||||||
found = false
|
|
||||||
for j := 0 ; j < v.NumField() ; j++ {
|
|
||||||
if typeOfV.Field(j).Name == curF {
|
|
||||||
f = v.Field(j)
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
|
||||||
}
|
|
||||||
|
|
||||||
for ; f.Kind() == reflect.Ptr ; f = f.Elem() {}
|
|
||||||
|
|
||||||
/*fmt.Printf("v.kind() = %v\n", v.Kind().String())
|
|
||||||
fmt.Printf("v = %v\n", v)
|
|
||||||
fmt.Printf("f.kind() = %v\n", f.Kind().String())
|
|
||||||
fmt.Printf("f = %v\n", f)*/
|
|
||||||
|
|
||||||
// If this is the last loop, return result even if it's a struct
|
|
||||||
// FIXME : What if we got interface?
|
|
||||||
if f.Kind() != reflect.Struct && i < len(fs)-1 {
|
|
||||||
if f.IsValid() {
|
|
||||||
//fmt.Printf("Return f = %v of Kind %s\n", f, f.Kind().String())
|
|
||||||
return &f, fieldName, nil
|
|
||||||
} else {
|
|
||||||
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
v = f
|
|
||||||
typeOfV = v.Type()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
if !found {
|
||||||
f := v.FieldByName(fieldName)
|
|
||||||
fmt.Printf("No substruct, v.kind() = %v\n", v.Kind().String())
|
|
||||||
fmt.Printf("No substruct, f.kind() = %v\n", f.Kind().String())
|
|
||||||
fmt.Printf("No substruct, v = %v\n", v)
|
|
||||||
fmt.Printf("No substruct, f = %v\n", f)
|
|
||||||
if f.IsValid() {
|
|
||||||
fmt.Printf("Return f = %v of Kind %s\n", f, f.Kind().String())
|
|
||||||
return &f, fieldName, nil
|
|
||||||
} else {
|
|
||||||
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for ; f.Kind() == reflect.Ptr ; f = f.Elem() {}
|
||||||
|
|
||||||
|
/*fmt.Printf("v.kind() = %v\n", v.Kind().String())
|
||||||
|
fmt.Printf("v = %v\n", v)
|
||||||
|
fmt.Printf("f.kind() = %v\n", f.Kind().String())
|
||||||
|
fmt.Printf("f = %v\n", f)*/
|
||||||
|
|
||||||
|
// If this is the last loop, return result even if it's a struct
|
||||||
|
// FIXME : What if we got interface?
|
||||||
|
if f.Kind() != reflect.Struct && i < len(fs)-1 {
|
||||||
|
if f.IsValid() {
|
||||||
|
//fmt.Printf("Return f = %v of Kind %s\n", f, f.Kind().String())
|
||||||
|
return &f, fieldName, nil
|
||||||
|
} else {
|
||||||
|
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
v = f
|
||||||
|
typeOfV = v.Type()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &v, fieldName, nil
|
return &v, fieldName, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user