MCPcopy
hub / github.com/cli/cli / listRun

Function listRun

pkg/cmd/gpg-key/list/list.go:45–95  ·  view source on GitHub ↗
(opts *ListOptions)

Source from the content-addressed store, hash-verified

43}
44
45func listRun(opts *ListOptions) error {
46 apiClient, err := opts.HTTPClient()
47 if err != nil {
48 return err
49 }
50
51 cfg, err := opts.Config()
52 if err != nil {
53 return err
54 }
55
56 host, _ := cfg.Authentication().DefaultHost()
57
58 gpgKeys, err := userKeys(apiClient, host, "")
59 if err != nil {
60 if errors.Is(err, errScopes) {
61 cs := opts.IO.ColorScheme()
62 fmt.Fprint(opts.IO.ErrOut, "Error: insufficient OAuth scopes to list GPG keys\n")
63 fmt.Fprintf(opts.IO.ErrOut, "Run the following to grant scopes: %s\n", cs.Bold("gh auth refresh -s read:gpg_key"))
64 return cmdutil.SilentError
65 }
66 return err
67 }
68
69 if len(gpgKeys) == 0 {
70 return cmdutil.NewNoResultsError("no GPG keys present in the GitHub account")
71 }
72
73 t := tableprinter.New(opts.IO, tableprinter.WithHeader("EMAIL", "KEY ID", "PUBLIC KEY", "ADDED", "EXPIRES"))
74 cs := opts.IO.ColorScheme()
75 now := time.Now()
76
77 for _, gpgKey := range gpgKeys {
78 t.AddField(gpgKey.Emails.String())
79 t.AddField(gpgKey.KeyID)
80 t.AddField(gpgKey.PublicKey, tableprinter.WithTruncate(truncateMiddle))
81 t.AddTimeField(now, gpgKey.CreatedAt, cs.Muted)
82 expiresAt := gpgKey.ExpiresAt.Format(time.RFC3339)
83 if t.IsTTY() {
84 if gpgKey.ExpiresAt.IsZero() {
85 expiresAt = "Never"
86 } else {
87 expiresAt = gpgKey.ExpiresAt.Format("2006-01-02")
88 }
89 }
90 t.AddField(expiresAt, tableprinter.WithColor(cs.Muted))
91 t.EndRow()
92 }
93
94 return t.Render()
95}
96
97func truncateMiddle(maxWidth int, t string) string {
98 if len(t) <= maxWidth {

Callers 2

Test_listRunFunction · 0.70
NewCmdListFunction · 0.70

Calls 13

NewNoResultsErrorFunction · 0.92
NewFunction · 0.92
WithHeaderFunction · 0.92
userKeysFunction · 0.85
ColorSchemeMethod · 0.80
BoldMethod · 0.80
AddTimeFieldMethod · 0.80
IsTTYMethod · 0.80
ConfigMethod · 0.65
DefaultHostMethod · 0.65
AuthenticationMethod · 0.65
RenderMethod · 0.65

Tested by 1

Test_listRunFunction · 0.56