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

Function buildEntry

pkg/cmd/auth/status/status.go:376–425  ·  view source on GitHub ↗
(httpClient *http.Client, opts buildEntryOptions)

Source from the content-addressed store, hash-verified

374}
375
376func buildEntry(httpClient *http.Client, opts buildEntryOptions) authEntry {
377 tokenSource := opts.tokenSource
378 if tokenSource == "oauth_token" {
379 // The go-gh function TokenForHost returns this value as source for tokens read from the
380 // config file, but we want the file path instead. This attempts to reconstruct it.
381 tokenSource = filepath.Join(config.ConfigDir(), "hosts.yml")
382 }
383 entry := authEntry{
384 Active: opts.active,
385 Host: opts.hostname,
386 Login: opts.username,
387 TokenSource: tokenSource,
388 Token: opts.token,
389 GitProtocol: opts.gitProtocol,
390 }
391
392 // If token is not writeable, then it came from an environment variable and
393 // we need to fetch the username as it won't be stored in the config.
394 if !authTokenWriteable(tokenSource) {
395 // The httpClient will automatically use the correct token here as
396 // the token from the environment variable take highest precedence.
397 apiClient := api.NewClientFromHTTP(httpClient)
398 var err error
399 entry.Login, err = api.CurrentLoginName(apiClient, opts.hostname)
400 if err != nil {
401 entry.State = authEntryStateError
402 entry.Error = err.Error()
403 return entry
404 }
405 }
406
407 // Get scopes for token.
408 scopesHeader, err := shared.GetScopes(httpClient, opts.hostname, opts.token)
409 if err != nil {
410 var networkError net.Error
411 if errors.As(err, &networkError) && networkError.Timeout() {
412 entry.State = authEntryStateTimeout
413 entry.Error = err.Error()
414 return entry
415 }
416
417 entry.State = authEntryStateError
418 entry.Error = err.Error()
419 return entry
420 }
421 entry.Scopes = scopesHeader
422
423 entry.State = authEntryStateSuccess
424 return entry
425}
426
427func authTokenWriteable(src string) bool {
428 return !strings.HasSuffix(src, "_TOKEN")

Callers 1

statusRunFunction · 0.85

Calls 7

ConfigDirFunction · 0.92
NewClientFromHTTPFunction · 0.92
CurrentLoginNameFunction · 0.92
GetScopesFunction · 0.92
authTokenWriteableFunction · 0.85
JoinMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected