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