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
126 lines (100 loc) · 2.81 KB
/
list.go
File metadata and controls
126 lines (100 loc) · 2.81 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package list
import (
"fmt"
"net/http"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/label/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/utils"
"github.com/spf13/cobra"
)
type browser interface {
Browse(string) error
}
type ListOptions struct {
BaseRepo func() (ghrepo.Interface, error)
Browser browser
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
Limit int
WebMode bool
}
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
opts := ListOptions{
Browser: f.Browser,
HttpClient: f.HttpClient,
IO: f.IOStreams,
}
cmd := &cobra.Command{
Use: "list",
Short: "List labels in a repository",
Long: "Display labels in a GitHub repository.",
Args: cobra.NoArgs,
Aliases: []string{"ls"},
RunE: func(c *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
if opts.Limit < 1 {
return cmdutil.FlagErrorf("invalid limit: %v", opts.Limit)
}
if runF != nil {
return runF(&opts)
}
return listRun(&opts)
},
}
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "List labels in the web browser")
cmd.Flags().IntVarP(&opts.Limit, "limit", "L", 30, "Maximum number of items to fetch")
return cmd
}
func listRun(opts *ListOptions) error {
httpClient, err := opts.HttpClient()
if err != nil {
return err
}
baseRepo, err := opts.BaseRepo()
if err != nil {
return err
}
if opts.WebMode {
labelListURL := ghrepo.GenerateRepoURL(baseRepo, "labels")
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(labelListURL))
}
return opts.Browser.Browse(labelListURL)
}
opts.IO.StartProgressIndicator()
labels, totalCount, err := listLabels(httpClient, baseRepo, opts.Limit)
opts.IO.StopProgressIndicator()
if err != nil {
return err
}
if opts.IO.IsStdoutTTY() {
title := listHeader(ghrepo.FullName(baseRepo), len(labels), totalCount)
fmt.Fprintf(opts.IO.Out, "\n%s\n\n", title)
}
return printLabels(opts.IO, labels)
}
func printLabels(io *iostreams.IOStreams, labels []shared.Label) error {
cs := io.ColorScheme()
table := utils.NewTablePrinter(io)
for _, label := range labels {
labelName := ""
if table.IsTTY() {
labelName = cs.HexToRGB(label.Color, label.Name)
} else {
labelName = label.Name
}
table.AddField(labelName, nil, nil)
table.AddField(label.Description, nil, nil)
table.EndRow()
}
return table.Render()
}
func listHeader(repoName string, count int, totalCount int) string {
if totalCount == 0 {
return fmt.Sprintf("There are no %ss in %s", "label", repoName)
}
return fmt.Sprintf("Showing %d of %s in %s", count, utils.Pluralize(totalCount, "label"), repoName)
}