Add command "list properties" so we can get all internal properties to sort, filter, or set jail properties

This commit is contained in:
yo 2022-04-02 15:40:04 +02:00
parent 8d18bfe55d
commit f1c4fd960d
2 changed files with 49 additions and 2 deletions

View File

@ -296,6 +296,42 @@ func displayStructFields(jails []Jail, valsToDisplay []string) {
fmt.Printf("\n")
}
/*
* Display struct attributes name for a given struct.
* Recurse into struct attributes of type struct
* Used to show user jail properties
*/
func getStructFieldNames(parentStruct interface{}, result []string, prefix string) []string {
v := reflect.ValueOf(parentStruct)
for i := 0 ; i < v.NumField() ; i++ {
if v.Type().Field(i).Type.Kind() == reflect.Struct {
result = getStructFieldNames(v.Field(i).Interface(), result, v.Type().Field(i).Name)
} else {
if len(prefix) > 0 {
result = append(result, fmt.Sprintf("%s.%s", prefix, v.Type().Field(i).Name))
} else {
result = append(result, v.Type().Field(i).Name)
}
}
}
return result
}
func ListJailsProps(args []string) {
var conf Jail
var jailconf JailConfig
var result []string
conf.Config = jailconf
result = getStructFieldNames(conf, result, "")
for _, f := range result {
fmt.Printf("%s\n", f)
}
}
/* Get Jails from datastores. Store config and running metadata into gJails global var */
func ListJails(args []string, display bool) {

View File

@ -12,7 +12,7 @@ import (
)
const (
gVersion = "0.022a"
gVersion = "0.022b"
)
var (
@ -63,10 +63,20 @@ ex: gocage list srv-db srv-web`,
},
}
listPropsCmd = &cobra.Command{
Use: "properties",
Short: "Print jails properties",
Long: "Display jails properties. You can use properties to filter, get or set them.",
Run: func(cmd *cobra.Command, args []string) {
ListJailsProps(args)
cleanAfterRun()
},
}
stopCmd = &cobra.Command{
Use: "stop",
Short: "stop jail",
Long: `shutdown jail`,
Long: "shutdown jail",
Run: func(cmd *cobra.Command, args []string) {
// Get the inventory
ListJails(args, false)
@ -105,6 +115,7 @@ func init() {
// Now declare commands
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(listCmd)
listCmd.AddCommand(listPropsCmd)
rootCmd.AddCommand(stopCmd)
rootCmd.AddCommand(startCmd)