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

Method Logout

internal/config/config.go:491–521  ·  view source on GitHub ↗

Logout will remove user, git protocol, and auth token for the given hostname. It will remove the auth token from the encrypted storage if it exists there.

(hostname, username string)

Source from the content-addressed store, hash-verified

489// Logout will remove user, git protocol, and auth token for the given hostname.
490// It will remove the auth token from the encrypted storage if it exists there.
491func (c *AuthConfig) Logout(hostname, username string) error {
492 users := c.UsersForHost(hostname)
493
494 // If there is only one (or zero) users, then we remove the host
495 // and unset the keyring tokens.
496 if len(users) < 2 {
497 _ = c.cfg.Remove([]string{hostsKey, hostname})
498 _ = keyring.Delete(keyringServiceName(hostname), "")
499 _ = keyring.Delete(keyringServiceName(hostname), username)
500 return ghConfig.Write(c.cfg)
501 }
502
503 // Otherwise, we remove the user from this host
504 _ = c.cfg.Remove([]string{hostsKey, hostname, usersKey, username})
505
506 // This error is ignorable because we already know there is an active user for the host
507 activeUser, _ := c.ActiveUser(hostname)
508
509 // If the user we're removing isn't active, then we just write the config
510 if activeUser != username {
511 return ghConfig.Write(c.cfg)
512 }
513
514 // Otherwise we get the first user in the slice that isn't the user we're removing
515 switchUserIdx := slices.IndexFunc(users, func(n string) bool {
516 return n != username
517 })
518
519 // And activate them
520 return c.activateUser(hostname, users[switchUserIdx])
521}
522
523func (c *AuthConfig) activateUser(hostname, user string) error {
524 // We first need to idempotently clear out any set tokens for the host

Callers

nothing calls this directly

Calls 7

UsersForHostMethod · 0.95
ActiveUserMethod · 0.95
activateUserMethod · 0.95
DeleteFunction · 0.92
keyringServiceNameFunction · 0.70
RemoveMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected