Choose fields to display with -o flag

This commit is contained in:
yo 2021-12-19 13:05:30 +01:00
parent eebc7da169
commit 4eeb6d0d99
3 changed files with 21 additions and 7 deletions

View File

@ -189,7 +189,8 @@ func displayStructFields(jails []Jail, valsToDisplay []string) {
/* Get Jails from datastores. Store config and running metadata into gJails global var */
func ListJails(args []string, display bool) {
//fields := []string{"JID", "Name", "Config.Release", "Config.Ip4_addr", "RootPath", "Running", "Config.Jail_zfs", "Config.Jail_zfs_dataset", "Zpool"}
fields := []string{"JID", "Name", "Config.Release", "Config.Ip4_addr", "Running", "Zpool"}
//fields := []string{"JID", "Name", "Config.Release", "Config.Ip4_addr", "Running", "Zpool"}
fields := strings.Split(gDisplayColumns, ",")
for _, d := range viper.GetStringSlice("datastore") {
listJailsFromDatastore(d)

View File

@ -14,11 +14,13 @@ const (
)
var (
gJails []Jail
gJails []Jail
gUseSudo bool
gUseSudo bool
gConfigFile string
gDisplayColumns string
gConfigFile string
rootCmd = & cobra.Command{
Use: "gocage",
@ -73,8 +75,8 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&gUseSudo, "sudo", "s", false, "Use sudo to run commands")
// Command dependant switches
/* listComputerCmd.PersistentFlags().BoolVarP(&gDisplayAsCSV, "csv-format", "v", false, "Show results in CSV (separator = ';')")
searchComputerCmd.PersistentFlags().BoolVarP(&gShowAccount, "show-account", "c", false, "Show account when listing computer")
listCmd.PersistentFlags().StringVarP(&gDisplayColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output")
/* searchComputerCmd.PersistentFlags().BoolVarP(&gShowAccount, "show-account", "c", false, "Show account when listing computer")
searchComputerCmd.PersistentFlags().BoolVarP(&gDisplayAsCSV, "csv-format", "v", false, "Show results in CSV (separator = ';')")
*/
@ -102,7 +104,14 @@ func initConfig() {
// fmt.Println("Using config file:", viper.ConfigFileUsed())
// fmt.Printf("datastore in config : %s\n", viper.GetStringSlice("datastore"))
// fmt.Printf("datastore.0 in config : %s\n", viper.GetStringSlice("datastore.0"))
gUseSudo = viper.GetBool("sudo")
// Command line flags have priority on config file
if false == rootCmd.Flags().Lookup("sudo").Changed {
gUseSudo = viper.GetBool("sudo")
}
if false == listCmd.Flags().Lookup("outcol").Changed {
gDisplayColumns = viper.GetString("outcol")
}
}
func Execute() {

View File

@ -3,3 +3,7 @@ datastore:
- /iocage
sudo: false
# Columns to display when "gocage list". Column names are struct fields, see cmd/struct.go
outcol: 'JID,Name,Config.Release,Config.Ip4_addr,Running'