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