From 4eeb6d0d99ca1e3fef53c632eae5c999a5fe6b89 Mon Sep 17 00:00:00 2001 From: yo Date: Sun, 19 Dec 2021 13:05:30 +0100 Subject: [PATCH] Choose fields to display with -o flag --- cmd/list.go | 3 ++- cmd/root.go | 21 +++++++++++++++------ gocage.conf.yml | 4 ++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index 1728b86..e7d1b31 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -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) diff --git a/cmd/root.go b/cmd/root.go index 0cc0ec5..6cd6629 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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() { diff --git a/gocage.conf.yml b/gocage.conf.yml index 35175c9..3ee03fa 100644 --- a/gocage.conf.yml +++ b/gocage.conf.yml @@ -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' +