From 1a759ecd4bd796ce4b0cb5a2f037f75eb01603b6 Mon Sep 17 00:00:00 2001 From: Natthakit Susanthitanon Date: Thu, 22 Sep 2022 17:07:57 +0700 Subject: [PATCH] Add header and id column to the `gh ssh-key list` output (#6270) --- pkg/cmd/ssh-key/list/http.go | 1 + pkg/cmd/ssh-key/list/list.go | 26 +++++++++++++++++++++----- pkg/cmd/ssh-key/list/list_test.go | 11 ++++++----- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/ssh-key/list/http.go b/pkg/cmd/ssh-key/list/http.go index 07f17e7acdb..7275615e0dd 100644 --- a/pkg/cmd/ssh-key/list/http.go +++ b/pkg/cmd/ssh-key/list/http.go @@ -12,6 +12,7 @@ import ( ) type sshKey struct { + ID int Key string Title string CreatedAt time.Time `json:"created_at"` diff --git a/pkg/cmd/ssh-key/list/list.go b/pkg/cmd/ssh-key/list/list.go index bc4fcb6261c..5bde29b9e66 100644 --- a/pkg/cmd/ssh-key/list/list.go +++ b/pkg/cmd/ssh-key/list/list.go @@ -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() } diff --git a/pkg/cmd/ssh-key/list/list_test.go b/pkg/cmd/ssh-key/list/list_test.go index f19a600cb26..91466ad8f75 100644 --- a/pkg/cmd/ssh-key/list/list_test.go +++ b/pkg/cmd/ssh-key/list/list_test.go @@ -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