github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/config/list/list.go (about) 1 package list 2 3 import ( 4 "fmt" 5 6 "github.com/ungtb10d/cli/v2/internal/config" 7 "github.com/ungtb10d/cli/v2/pkg/cmdutil" 8 "github.com/ungtb10d/cli/v2/pkg/iostreams" 9 "github.com/spf13/cobra" 10 ) 11 12 type ListOptions struct { 13 IO *iostreams.IOStreams 14 Config func() (config.Config, error) 15 16 Hostname string 17 } 18 19 func NewCmdConfigList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command { 20 opts := &ListOptions{ 21 IO: f.IOStreams, 22 Config: f.Config, 23 } 24 25 cmd := &cobra.Command{ 26 Use: "list", 27 Short: "Print a list of configuration keys and values", 28 Aliases: []string{"ls"}, 29 Args: cobra.ExactArgs(0), 30 RunE: func(cmd *cobra.Command, args []string) error { 31 if runF != nil { 32 return runF(opts) 33 } 34 35 return listRun(opts) 36 }, 37 } 38 39 cmd.Flags().StringVarP(&opts.Hostname, "host", "h", "", "Get per-host configuration") 40 41 return cmd 42 } 43 44 func listRun(opts *ListOptions) error { 45 cfg, err := opts.Config() 46 if err != nil { 47 return err 48 } 49 50 var host string 51 if opts.Hostname != "" { 52 host = opts.Hostname 53 } else { 54 host, _ = cfg.DefaultHost() 55 } 56 57 configOptions := config.ConfigOptions() 58 59 for _, key := range configOptions { 60 val, err := cfg.GetOrDefault(host, key.Key) 61 if err != nil { 62 return err 63 } 64 fmt.Fprintf(opts.IO.Out, "%s=%s\n", key.Key, val) 65 } 66 67 return nil 68 }