MCPcopy Index your code
hub / github.com/cli/cli / buildEntry

Function buildEntry

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

Source from the content-addressed store, hash-verified

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