Skip to content

Commit

Permalink
Add header and id column to the gh ssh-key list output (cli#6270)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsmag authored Sep 22, 2022
1 parent 55edf2a commit 1a759ec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/ssh-key/list/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

type sshKey struct {
ID int
Key string
Title string
CreatedAt time.Time `json:"created_at"`
Expand Down
26 changes: 21 additions & 5 deletions pkg/cmd/ssh-key/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package list

import (
"net/http"
"strconv"
"time"

"github.com/cli/cli/v2/internal/config"
Expand Down Expand Up @@ -67,15 +68,30 @@ func listRun(opts *ListOptions) error {
cs := opts.IO.ColorScheme()
now := time.Now()

for _, sshKey := range sshKeys {
t.AddField(sshKey.Title, nil, nil)
t.AddField(sshKey.Key, truncateMiddle, nil)
if t.IsTTY() {
t.AddField("TITLE", nil, nil)
t.AddField("ID", nil, nil)
t.AddField("KEY", nil, nil)
t.AddField("ADDED", nil, nil)
t.EndRow()
}

for _, sshKey := range sshKeys {
id := strconv.Itoa(sshKey.ID)
createdAt := sshKey.CreatedAt.Format(time.RFC3339)

if t.IsTTY() {
createdAt = text.FuzzyAgoAbbr(now, sshKey.CreatedAt)
t.AddField(sshKey.Title, nil, nil)
t.AddField(id, nil, nil)
t.AddField(sshKey.Key, truncateMiddle, nil)
t.AddField(text.FuzzyAgoAbbr(now, sshKey.CreatedAt), nil, cs.Gray)
} else {
t.AddField(sshKey.Title, nil, nil)
t.AddField(sshKey.Key, nil, nil)
t.AddField(createdAt, nil, nil)
t.AddField(id, nil, nil)
}
t.AddField(createdAt, nil, cs.Gray)

t.EndRow()
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/cmd/ssh-key/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func TestListRun(t *testing.T) {
},
isTTY: true,
wantStdout: heredoc.Doc(`
Mac ssh-rsa AAAABbBB123 1d
hubot@Windows ssh-rsa EEEEEEEK247 1d
TITLE ID KEY ADDED
Mac 1234 ssh-rsa AAAABbBB123 1d
hubot@Windows 5678 ssh-rsa EEEEEEEK247 1d
`),
wantStderr: "",
},
Expand Down Expand Up @@ -82,8 +83,8 @@ func TestListRun(t *testing.T) {
},
isTTY: false,
wantStdout: heredoc.Doc(`
Mac ssh-rsa AAAABbBB123 2020-08-31T15:44:24+02:00
hubot@Windows ssh-rsa EEEEEEEK247 2020-08-31T15:44:24+02:00
Mac ssh-rsa AAAABbBB123 2020-08-31T15:44:24+02:00 1234
hubot@Windows ssh-rsa EEEEEEEK247 2020-08-31T15:44:24+02:00 5678
`),
wantStderr: "",
},
Expand Down Expand Up @@ -126,8 +127,8 @@ func TestListRun(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ios, _, stdout, stderr := iostreams.Test()
ios.SetStdoutTTY(tt.isTTY)
ios.SetStdinTTY(tt.isTTY)
ios.SetStdoutTTY(tt.isTTY)
ios.SetStderrTTY(tt.isTTY)

opts := tt.opts
Expand Down

0 comments on commit 1a759ec

Please sign in to comment.