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

Function logoutRun

pkg/cmd/auth/logout/logout.go:102–216  ·  view source on GitHub ↗
(opts *LogoutOptions)

Source from the content-addressed store, hash-verified

100}
101
102func logoutRun(opts *LogoutOptions) error {
103 hostname := opts.Hostname
104 username := opts.Username
105
106 cfg, err := opts.Config()
107 if err != nil {
108 return err
109 }
110 authCfg := cfg.Authentication()
111
112 knownHosts := authCfg.Hosts()
113 if len(knownHosts) == 0 {
114 return fmt.Errorf("not logged in to any hosts")
115 }
116
117 if hostname != "" {
118 if !slices.Contains(knownHosts, hostname) {
119 return fmt.Errorf("not logged in to %s", hostname)
120 }
121
122 if username != "" {
123 knownUsers := cfg.Authentication().UsersForHost(hostname)
124 if !slices.Contains(knownUsers, username) {
125 return fmt.Errorf("not logged in to %s account %s", hostname, username)
126 }
127 }
128 }
129
130 type hostUser struct {
131 host string
132 user string
133 }
134 var candidates []hostUser
135
136 for _, host := range knownHosts {
137 if hostname != "" && host != hostname {
138 continue
139 }
140 knownUsers := cfg.Authentication().UsersForHost(host)
141 for _, user := range knownUsers {
142 if username != "" && user != username {
143 continue
144 }
145 candidates = append(candidates, hostUser{host: host, user: user})
146 }
147 }
148
149 if len(candidates) == 0 {
150 return errors.New("no accounts matched that criteria")
151 } else if len(candidates) == 1 {
152 hostname = candidates[0].host
153 username = candidates[0].user
154 } else if !opts.IO.CanPrompt() {
155 return errors.New("unable to determine which account to log out of, please specify `--hostname` and `--user`")
156 } else {
157 prompts := make([]string, len(candidates))
158 for i, c := range candidates {
159 prompts[i] = fmt.Sprintf("%s (%s)", c.user, c.host)

Callers 3

Test_logoutRun_ttyFunction · 0.85
Test_logoutRun_nonttyFunction · 0.85
NewCmdLogoutFunction · 0.85

Calls 15

RejectExistingMethod · 0.95
AuthTokenWriteableFunction · 0.92
logoutWithRevocationFunction · 0.85
ContainsMethod · 0.80
CanPromptMethod · 0.80
ColorSchemeMethod · 0.80
WarningIconMethod · 0.80
SuccessIconMethod · 0.80
BoldMethod · 0.80
ConfigMethod · 0.65
AuthenticationMethod · 0.65
HostsMethod · 0.65

Tested by 2

Test_logoutRun_ttyFunction · 0.68
Test_logoutRun_nonttyFunction · 0.68