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