Address PR comments
This commit is contained in:
parent
952ebc8eb2
commit
600c387bd2
4 changed files with 20 additions and 8 deletions
|
|
@ -81,7 +81,7 @@ func runAdd(opts *AddOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = GPGKeyUpload(httpClient, hostname, keyReader)
|
||||
err = gpgKeyUpload(httpClient, hostname, keyReader)
|
||||
if err != nil {
|
||||
if errors.Is(err, scopesError) {
|
||||
cs := opts.IO.ColorScheme()
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import (
|
|||
|
||||
var scopesError = errors.New("insufficient OAuth scopes")
|
||||
|
||||
func GPGKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader) error {
|
||||
func gpgKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader) error {
|
||||
url := ghinstance.RESTPrefix(hostname) + "user/gpg_keys"
|
||||
|
||||
keyBytes, err := ioutil.ReadAll(keyFile)
|
||||
|
|
|
|||
|
|
@ -79,8 +79,8 @@ func listRun(opts *ListOptions) error {
|
|||
|
||||
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() {
|
||||
|
|
@ -98,11 +98,23 @@ func listRun(opts *ListOptions) error {
|
|||
}
|
||||
t.AddField(expiresAt, nil, cs.Gray)
|
||||
|
||||
if !t.IsTTY() {
|
||||
t.AddField(gpgKey.PublicKey, nil, nil)
|
||||
}
|
||||
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:]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,14 +25,14 @@ func TestListRun(t *testing.T) {
|
|||
name: "list tty",
|
||||
opts: ListOptions{HTTPClient: mockGPGResponse},
|
||||
isTTY: true,
|
||||
wantStdout: "johnny@test.com ABCDEF1234567890 Created Jun 11, 2020 Expires 2099-01-01\nmonalisa@github.com 1234567890ABCDEF Created Jan 11, 2021 Never expires\n",
|
||||
wantStdout: "johnny@test.com ABCDEF12345... xJMEW...oofoo Created Ju... Expires 20...\nmonalisa@github.com 1234567890A... xJMEW...arbar Created Ja... Never expires\n",
|
||||
wantStderr: "",
|
||||
},
|
||||
{
|
||||
name: "list non-tty",
|
||||
opts: ListOptions{HTTPClient: mockGPGResponse},
|
||||
isTTY: false,
|
||||
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",
|
||||
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",
|
||||
wantStderr: "",
|
||||
},
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue