MCPcopy Create free account
hub / github.com/cli/cli / loginRun

Function loginRun

pkg/cmd/auth/login/login.go:201–297  ·  view source on GitHub ↗
(opts *LoginOptions)

Source from the content-addressed store, hash-verified

199}
200
201func loginRun(opts *LoginOptions) error {
202 cfg, err := opts.Config()
203 if err != nil {
204 return err
205 }
206 authCfg := cfg.Authentication()
207 copyToClipboard := shared.ShouldCopyToClipboard(cfg, opts.Clipboard)
208
209 hostname := opts.Hostname
210 if opts.Interactive && hostname == "" {
211 var err error
212 hostname, err = promptForHostname(opts)
213 if err != nil {
214 return err
215 }
216 }
217
218 // The go-gh Config object currently does not support case-insensitive lookups for host names,
219 // so normalize the host name case here before performing any lookups with it or persisting it.
220 // https://github.com/cli/go-gh/pull/105
221 hostname = strings.ToLower(hostname)
222
223 if src, writeable := shared.AuthTokenWriteable(authCfg, hostname); !writeable {
224 fmt.Fprintf(opts.IO.ErrOut, "The value of the %s environment variable is being used for authentication.\n", src)
225 fmt.Fprint(opts.IO.ErrOut, "To have GitHub CLI store credentials instead, first clear the value from the environment.\n")
226 return cmdutil.SilentError
227 }
228
229 plainHTTPClient, err := opts.PlainHttpClient()
230 if err != nil {
231 return err
232 }
233
234 httpClient, err := opts.HttpClient()
235 if err != nil {
236 return err
237 }
238
239 if opts.Token != "" {
240 if err := shared.HasMinimumScopes(httpClient, hostname, opts.Token); err != nil {
241 return fmt.Errorf("error validating token: %w", err)
242 }
243 username, err := shared.GetCurrentLogin(httpClient, hostname, opts.Token)
244 if err != nil {
245 return fmt.Errorf("error retrieving current user: %w", err)
246 }
247
248 // Adding a user key ensures that a nonempty host section gets written to the config file.
249 _, loginErr := authCfg.Login(hostname, username, opts.Token, opts.GitProtocol, !opts.InsecureStorage)
250 return loginErr
251 }
252
253 // Nudge users towards expiring tokens unless they have already expressed a preference, either with the flag or
254 // the config. Enterprise Server hosts are skipped because they may not support expiring tokens.
255 if opts.IO.IsStderrTTY() && opts.Expiring == nil && !ghauth.IsEnterprise(hostname) && !shared.ShouldUseExpiring(cfg, nil, hostname) {
256 cs := opts.IO.ColorScheme()
257 fmt.Fprintf(opts.IO.ErrOut, "%s Did you know that expiring tokens can be requested with %s? Read more at %s\n",
258 cs.WarningIcon(), cs.Bold("--expiring"), "https://gh.io/gh-expiring-tokens")

Callers 4

Test_loginRun_nonttyFunction · 0.85
Test_loginRun_SurveyFunction · 0.85
NewCmdLoginFunction · 0.85

Calls 15

ShouldCopyToClipboardFunction · 0.92
AuthTokenWriteableFunction · 0.92
HasMinimumScopesFunction · 0.92
GetCurrentLoginFunction · 0.92
ShouldUseExpiringFunction · 0.92
LoginFunction · 0.92
promptForHostnameFunction · 0.85
IsStderrTTYMethod · 0.80
ColorSchemeMethod · 0.80
WarningIconMethod · 0.80
BoldMethod · 0.80
ConfigMethod · 0.65

Tested by 3

Test_loginRun_nonttyFunction · 0.68
Test_loginRun_SurveyFunction · 0.68