Skip to content

Commit 600c387

Browse files
committed
Address PR comments
1 parent 952ebc8 commit 600c387

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

pkg/cmd/gpg-key/add/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func runAdd(opts *AddOptions) error {
8181
return err
8282
}
8383

84-
err = GPGKeyUpload(httpClient, hostname, keyReader)
84+
err = gpgKeyUpload(httpClient, hostname, keyReader)
8585
if err != nil {
8686
if errors.Is(err, scopesError) {
8787
cs := opts.IO.ColorScheme()

pkg/cmd/gpg-key/add/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
var scopesError = errors.New("insufficient OAuth scopes")
1616

17-
func GPGKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader) error {
17+
func gpgKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader) error {
1818
url := ghinstance.RESTPrefix(hostname) + "user/gpg_keys"
1919

2020
keyBytes, err := ioutil.ReadAll(keyFile)

pkg/cmd/gpg-key/list/list.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func listRun(opts *ListOptions) error {
7979

8080
for _, gpgKey := range gpgKeys {
8181
t.AddField(gpgKey.Emails.String(), nil, nil)
82-
8382
t.AddField(gpgKey.KeyId, nil, nil)
83+
t.AddField(gpgKey.PublicKey, truncateMiddle, nil)
8484

8585
createdAt := gpgKey.CreatedAt.Format(time.RFC3339)
8686
if t.IsTTY() {
@@ -98,11 +98,23 @@ func listRun(opts *ListOptions) error {
9898
}
9999
t.AddField(expiresAt, nil, cs.Gray)
100100

101-
if !t.IsTTY() {
102-
t.AddField(gpgKey.PublicKey, nil, nil)
103-
}
104101
t.EndRow()
105102
}
106103

107104
return t.Render()
108105
}
106+
107+
func truncateMiddle(maxWidth int, t string) string {
108+
if len(t) <= maxWidth {
109+
return t
110+
}
111+
112+
ellipsis := "..."
113+
if maxWidth < len(ellipsis)+2 {
114+
return t[0:maxWidth]
115+
}
116+
117+
halfWidth := (maxWidth - len(ellipsis)) / 2
118+
remainder := (maxWidth - len(ellipsis)) % 2
119+
return t[0:halfWidth+remainder] + ellipsis + t[len(t)-halfWidth:]
120+
}

pkg/cmd/gpg-key/list/list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ func TestListRun(t *testing.T) {
2525
name: "list tty",
2626
opts: ListOptions{HTTPClient: mockGPGResponse},
2727
isTTY: true,
28-
wantStdout: "johnny@test.com ABCDEF1234567890 Created Jun 11, 2020 Expires 2099-01-01\nmonalisa@github.com 1234567890ABCDEF Created Jan 11, 2021 Never expires\n",
28+
wantStdout: "johnny@test.com ABCDEF12345... xJMEW...oofoo Created Ju... Expires 20...\nmonalisa@github.com 1234567890A... xJMEW...arbar Created Ja... Never expires\n",
2929
wantStderr: "",
3030
},
3131
{
3232
name: "list non-tty",
3333
opts: ListOptions{HTTPClient: mockGPGResponse},
3434
isTTY: false,
35-
wantStdout: "johnny@test.com\tABCDEF1234567890\t2020-06-11T15:44:24+01:00\t2099-01-01T15:44:24+01:00\txJMEWfoofoofoo\nmonalisa@github.com\t1234567890ABCDEF\t2021-01-11T15:44:24+01:00\t0001-01-01T00:00:00Z\txJMEWbarbarbar\n",
35+
wantStdout: "johnny@test.com\tABCDEF1234567890\txJMEWfoofoofoo\t2020-06-11T15:44:24+01:00\t2099-01-01T15:44:24+01:00\nmonalisa@github.com\t1234567890ABCDEF\txJMEWbarbarbar\t2021-01-11T15:44:24+01:00\t0001-01-01T00:00:00Z\n",
3636
wantStderr: "",
3737
},
3838
{

0 commit comments

Comments
 (0)