From 139ea1842228c85ad3b1fb0d6ee783764739c306 Mon Sep 17 00:00:00 2001 From: yo Date: Sun, 3 Apr 2022 10:35:48 +0200 Subject: [PATCH] add "gocage get property jail" + "gocage set property jail" now support string, int and bool types --- cmd/root.go | 15 +++++++- cmd/start.go | 98 ---------------------------------------------------- 2 files changed, 14 insertions(+), 99 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 614b025..8be0fa8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -107,6 +107,19 @@ Multiples properties can be specified, separated with space (Ex: gocage set allo cleanAfterRun() }, } + + getCmd = &cobra.Command{ + Use: "get", + Short: "Get a jail property", + Long: `Get jail property value. Specify property, end command with jail name. + Multiples properties can be specified, separated with space (Ex: gocage get allow_mlock boot myjail)`, + Run: func(cmd *cobra.Command, args []string) { + // Get the inventory + ListJails(args, false) + GetJailProperties(args) + cleanAfterRun() + }, + } ) @@ -130,6 +143,7 @@ func init() { listCmd.AddCommand(listPropsCmd) rootCmd.AddCommand(stopCmd) rootCmd.AddCommand(startCmd) + rootCmd.AddCommand(getCmd) rootCmd.AddCommand(setCmd) // Get FreeBSD version @@ -187,7 +201,6 @@ func initConfig() { func cleanAfterRun() { for _, j := range gJails { if j.ConfigUpdated { - // TODO : Marshall to disk //fmt.Printf("Config for jail %s will be updated\n", j.Name) marshaled, err := json.MarshalIndent(j.Config, "", " ") if err != nil { diff --git a/cmd/start.go b/cmd/start.go index c38089e..593e311 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -3,109 +3,11 @@ package cmd import ( "os" "fmt" -// "log" "errors" "regexp" -// "os/exec" -// "reflect" "strings" - "strconv" ) -func SetJailProperties(args []string) { - type properties struct { - name string - value string - } - - var jail Jail - var props []properties - - 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) - } - } - } - } - - if len(jail.Name) == 0 || len(args) == 0 { - // TODO : Show help - fmt.Printf("Error\n") - return - } - - for _, p := range props { - err := setJailProperty(&jail, p.name, p.value) - if err != nil { - fmt.Printf("Error: %s\n", err.Error()) - return - } - } -} - -// 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) -// 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 { - // First get propName type, to convert propValue -/* k, _, err := getStructFieldKind(jail, propName) - if err != nil { - return err - } - kind := k.String() -*/ - - 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 kind == "string" { - // WIll the affectation be done in source object or a copy? - val.Set(propValue) - } else if kind == "int" { - v, err := strconv.Atoi(propValue) - if err != nil { - return errors.New(fmt.Sprintf("propValue have wrong type: %s\n", err.Error())) - } - val.Set(v) - } else { - return errors.New(fmt.Sprintf("Property %s have an unsupported type in setJailProperty!\n", propName)) - }*/ - - if val.CanSet() { - ival, err := strconv.Atoi(propValue) - if err != nil { - return err - } - val.SetInt(int64(ival)) - 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 -} - // FIXME : Do not work?! // We cant use internalName as the value exist only when jail is running func setJailConfigUpdated(jail *Jail) error {