|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/cli/cli/v2/internal/config" |
| 7 | + "github.com/cli/cli/v2/pkg/cmdutil" |
| 8 | + "github.com/cli/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 | + } |
| 23 | + |
| 24 | + cmd := &cobra.Command{ |
| 25 | + Use: "list", |
| 26 | + Short: "Print a list of configuration keys and values", |
| 27 | + Args: cobra.ExactArgs(0), |
| 28 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 29 | + opts.Config = f.Config |
| 30 | + |
| 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 setting") |
| 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 | + //cs := opts.IO.ColorScheme() |
| 51 | + isTTY := opts.IO.IsStdoutTTY() |
| 52 | + |
| 53 | + host, err := cfg.DefaultHost() |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + if isTTY { |
| 59 | + fmt.Printf("Settings configured for %s\n\n", host) |
| 60 | + } |
| 61 | + |
| 62 | + configOptions := config.ConfigOptions() |
| 63 | + |
| 64 | + for _, key := range configOptions { |
| 65 | + val, err := cfg.Get(opts.Hostname, key.Key) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + fmt.Printf("%s: %s\n", key.Key, val) |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
0 commit comments