From ff8aa8a555fa95367ef90dba32cbe52690d0971a Mon Sep 17 00:00:00 2001 From: Andrew Senetar Date: Mon, 23 May 2022 03:06:42 -0500 Subject: [PATCH] Add ability to set title for ssh key in auth login (#5524) --- pkg/cmd/auth/shared/login_flow.go | 20 ++++++++++++++++---- pkg/cmd/auth/shared/login_flow_test.go | 1 + pkg/cmd/auth/shared/ssh_keys.go | 4 ++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/auth/shared/login_flow.go b/pkg/cmd/auth/shared/login_flow.go index 4f14e2ea6..f2c0fd6fb 100644 --- a/pkg/cmd/auth/shared/login_flow.go +++ b/pkg/cmd/auth/shared/login_flow.go @@ -14,6 +14,8 @@ import ( "github.com/cli/cli/v2/pkg/prompt" ) +const defaultSSHKeyTitle = "GitHub CLI" + type iconfig interface { Get(string, string) (string, error) Set(string, string, string) error @@ -68,6 +70,7 @@ func Login(opts *LoginOptions) error { } var keyToUpload string + keyTitle := defaultSSHKeyTitle if opts.Interactive && gitProtocol == "ssh" { pubKeys, err := opts.sshContext.localPublicKeys() if err != nil { @@ -93,9 +96,18 @@ func Login(opts *LoginOptions) error { return err } } - } - if keyToUpload != "" { - additionalScopes = append(additionalScopes, "admin:public_key") + + if keyToUpload != "" { + err := prompt.SurveyAskOne(&survey.Input{ + Message: "Title for your SSH key:", + Default: defaultSSHKeyTitle, + }, &keyTitle) + if err != nil { + return fmt.Errorf("could not prompt: %w", err) + } + + additionalScopes = append(additionalScopes, "admin:public_key") + } } var authMode int @@ -187,7 +199,7 @@ func Login(opts *LoginOptions) error { } if keyToUpload != "" { - err := sshKeyUpload(httpClient, hostname, keyToUpload) + err := sshKeyUpload(httpClient, hostname, keyToUpload, keyTitle) if err != nil { return err } diff --git a/pkg/cmd/auth/shared/login_flow_test.go b/pkg/cmd/auth/shared/login_flow_test.go index ea814d918..60a19fe03 100644 --- a/pkg/cmd/auth/shared/login_flow_test.go +++ b/pkg/cmd/auth/shared/login_flow_test.go @@ -56,6 +56,7 @@ func TestLogin_ssh(t *testing.T) { ask.StubPrompt("What is your preferred protocol for Git operations?").AnswerWith("SSH") ask.StubPrompt("Generate a new SSH key to add to your GitHub account?").AnswerWith(true) ask.StubPrompt("Enter a passphrase for your new SSH key (Optional)").AnswerWith("monkey") + ask.StubPrompt("Title for your SSH key:").AnswerWith("Test Key") ask.StubPrompt("How would you like to authenticate GitHub CLI?").AnswerWith("Paste an authentication token") ask.StubPrompt("Paste your authentication token:").AnswerWith("ATOKEN") diff --git a/pkg/cmd/auth/shared/ssh_keys.go b/pkg/cmd/auth/shared/ssh_keys.go index 97f5174e4..4a8914a2c 100644 --- a/pkg/cmd/auth/shared/ssh_keys.go +++ b/pkg/cmd/auth/shared/ssh_keys.go @@ -108,12 +108,12 @@ func (c *sshContext) generateSSHKey() (string, error) { return keyFile + ".pub", run.PrepareCmd(keygenCmd).Run() } -func sshKeyUpload(httpClient *http.Client, hostname, keyFile string) error { +func sshKeyUpload(httpClient *http.Client, hostname, keyFile string, title string) error { f, err := os.Open(keyFile) if err != nil { return err } defer f.Close() - return add.SSHKeyUpload(httpClient, hostname, f, "GitHub CLI") + return add.SSHKeyUpload(httpClient, hostname, f, title) }