From f1c4fd960d98b4d539ec169c9b46a79169cca043 Mon Sep 17 00:00:00 2001 From: yo Date: Sat, 2 Apr 2022 15:40:04 +0200 Subject: [PATCH] Add command "list properties" so we can get all internal properties to sort, filter, or set jail properties --- cmd/list.go | 36 ++++++++++++++++++++++++++++++++++++ cmd/root.go | 15 +++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index 18f55e6..376f137 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -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) { diff --git a/cmd/root.go b/cmd/root.go index d5518f5..2a38156 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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)