| 53 | } |
| 54 | |
| 55 | func Login(opts *LoginOptions) error { |
| 56 | cfg := opts.Config |
| 57 | hostname := opts.Hostname |
| 58 | httpClient := opts.HTTPClient |
| 59 | cs := opts.IO.ColorScheme() |
| 60 | |
| 61 | gitProtocol := strings.ToLower(opts.GitProtocol) |
| 62 | if opts.Interactive && gitProtocol == "" { |
| 63 | options := []string{ |
| 64 | "HTTPS", |
| 65 | "SSH", |
| 66 | } |
| 67 | result, err := opts.Prompter.Select( |
| 68 | "What is your preferred protocol for Git operations on this host?", |
| 69 | options[0], |
| 70 | options) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | proto := options[result] |
| 75 | gitProtocol = strings.ToLower(proto) |
| 76 | } |
| 77 | |
| 78 | var additionalScopes []string |
| 79 | |
| 80 | if opts.Interactive && gitProtocol == "https" { |
| 81 | if err := opts.CredentialFlow.Prompt(hostname); err != nil { |
| 82 | return err |
| 83 | } |
| 84 | additionalScopes = append(additionalScopes, opts.CredentialFlow.Scopes()...) |
| 85 | } |
| 86 | |
| 87 | var keyToUpload string |
| 88 | keyTitle := defaultSSHKeyTitle |
| 89 | if opts.Interactive && !opts.SkipSSHKeyPrompt && gitProtocol == "ssh" { |
| 90 | pubKeys, err := opts.sshContext.LocalPublicKeys() |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | |
| 95 | if len(pubKeys) > 0 { |
| 96 | options := append(pubKeys, "Skip") |
| 97 | keyChoice, err := opts.Prompter.Select( |
| 98 | "Upload your SSH public key to your GitHub account?", |
| 99 | options[0], |
| 100 | options) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | if keyChoice < len(pubKeys) { |
| 105 | keyToUpload = pubKeys[keyChoice] |
| 106 | } |
| 107 | } else if opts.sshContext.HasKeygen() { |
| 108 | sshChoice, err := opts.Prompter.Confirm("Generate a new SSH key to add to your GitHub account?", true) |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | |