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
121 lines (101 loc) · 2.64 KB
/
list.go
File metadata and controls
121 lines (101 loc) · 2.64 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
package list
import (
"errors"
"fmt"
"net/http"
"time"
"github.com/cli/cli/v2/internal/config"
"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 ListOptions struct {
IO *iostreams.IOStreams
Config func() (config.Config, error)
HTTPClient func() (*http.Client, error)
}
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
opts := &ListOptions{
IO: f.IOStreams,
Config: f.Config,
HTTPClient: f.HttpClient,
}
cmd := &cobra.Command{
Use: "list",
Short: "Lists GPG keys in your GitHub account",
Aliases: []string{"ls"},
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if runF != nil {
return runF(opts)
}
return listRun(opts)
},
}
return cmd
}
func listRun(opts *ListOptions) error {
apiClient, err := opts.HTTPClient()
if err != nil {
return err
}
cfg, err := opts.Config()
if err != nil {
return err
}
host, err := cfg.DefaultHost()
if err != nil {
return err
}
gpgKeys, err := userKeys(apiClient, host, "")
if err != nil {
if errors.Is(err, scopesError) {
cs := opts.IO.ColorScheme()
fmt.Fprint(opts.IO.ErrOut, "Error: insufficient OAuth scopes to list GPG keys\n")
fmt.Fprintf(opts.IO.ErrOut, "Run the following to grant scopes: %s\n", cs.Bold("gh auth refresh -s read:gpg_key"))
return cmdutil.SilentError
}
return err
}
if len(gpgKeys) == 0 {
fmt.Fprintln(opts.IO.ErrOut, "No GPG keys present in GitHub account.")
return cmdutil.SilentError
}
t := utils.NewTablePrinter(opts.IO)
cs := opts.IO.ColorScheme()
now := time.Now()
for _, gpgKey := range gpgKeys {
t.AddField(gpgKey.Emails.String(), nil, nil)
t.AddField(gpgKey.KeyId, nil, nil)
t.AddField(gpgKey.PublicKey, truncateMiddle, nil)
createdAt := gpgKey.CreatedAt.Format(time.RFC3339)
if t.IsTTY() {
createdAt = "Created " + utils.FuzzyAgoAbbr(now, gpgKey.CreatedAt)
}
t.AddField(createdAt, nil, cs.Gray)
expiresAt := gpgKey.ExpiresAt.Format(time.RFC3339)
if t.IsTTY() {
if gpgKey.ExpiresAt.IsZero() {
expiresAt = "Never expires"
} else {
expiresAt = "Expires " + gpgKey.ExpiresAt.Format("2006-01-02")
}
}
t.AddField(expiresAt, nil, cs.Gray)
t.EndRow()
}
return t.Render()
}
func truncateMiddle(maxWidth int, t string) string {
if len(t) <= maxWidth {
return t
}
ellipsis := "..."
if maxWidth < len(ellipsis)+2 {
return t[0:maxWidth]
}
halfWidth := (maxWidth - len(ellipsis)) / 2
remainder := (maxWidth - len(ellipsis)) % 2
return t[0:halfWidth+remainder] + ellipsis + t[len(t)-halfWidth:]
}