Add header and id column to the gh ssh-key list output (#6270)

This commit is contained in:
Natthakit Susanthitanon 2022-09-22 17:07:57 +07:00 committed by GitHub
parent 55edf2ae7b
commit 1a759ecd4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 10 deletions

View file

@ -12,6 +12,7 @@ import (
)
type sshKey struct {
ID int
Key string
Title string
CreatedAt time.Time `json:"created_at"`

View file

@ -2,6 +2,7 @@ package list
import (
"net/http"
"strconv"
"time"
"github.com/cli/cli/v2/internal/config"
@ -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()
}

View file

@ -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: "",
},
@ -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: "",
},
@ -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