Add ability to set title for ssh key in auth login (#5524)

This commit is contained in:
Andrew Senetar 2022-05-23 03:06:42 -05:00 committed by GitHub
parent 3574240fa7
commit ff8aa8a555
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 6 deletions

View file

@ -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
}

View file

@ -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")

View file

@ -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)
}