| 190 | } |
| 191 | |
| 192 | func statusRun(opts *StatusOptions) error { |
| 193 | cfg, err := opts.Config() |
| 194 | if err != nil { |
| 195 | return err |
| 196 | } |
| 197 | authCfg := cfg.Authentication() |
| 198 | |
| 199 | stderr := opts.IO.ErrOut |
| 200 | stdout := opts.IO.Out |
| 201 | cs := opts.IO.ColorScheme() |
| 202 | |
| 203 | hostnames := authCfg.Hosts() |
| 204 | if len(hostnames) == 0 { |
| 205 | fmt.Fprintf(stderr, |
| 206 | "You are not logged into any GitHub hosts. To log in, run: %s\n", cs.Bold("gh auth login")) |
| 207 | if opts.Exporter != nil { |
| 208 | // In machine-friendly mode, we always exit with no error. |
| 209 | opts.Exporter.Write(opts.IO, newAuthStatus()) |
| 210 | return nil |
| 211 | } |
| 212 | return cmdutil.SilentError |
| 213 | } |
| 214 | |
| 215 | if opts.Hostname != "" && !slices.Contains(hostnames, opts.Hostname) { |
| 216 | fmt.Fprintf(stderr, |
| 217 | "You are not logged into any accounts on %s\n", opts.Hostname) |
| 218 | if opts.Exporter != nil { |
| 219 | // In machine-friendly mode, we always exit with no error. |
| 220 | opts.Exporter.Write(opts.IO, newAuthStatus()) |
| 221 | return nil |
| 222 | } |
| 223 | return cmdutil.SilentError |
| 224 | } |
| 225 | |
| 226 | httpClient, err := opts.HttpClient() |
| 227 | if err != nil { |
| 228 | return err |
| 229 | } |
| 230 | |
| 231 | var finalErr error |
| 232 | statuses := newAuthStatus() |
| 233 | |
| 234 | for _, hostname := range hostnames { |
| 235 | if opts.Hostname != "" && opts.Hostname != hostname { |
| 236 | continue |
| 237 | } |
| 238 | |
| 239 | var activeUser string |
| 240 | gitProtocol := cfg.GitProtocol(hostname).Value |
| 241 | activeUserToken, activeUserTokenSource := authCfg.ActiveToken(hostname) |
| 242 | if authTokenWriteable(activeUserTokenSource) { |
| 243 | activeUser, _ = authCfg.ActiveUser(hostname) |
| 244 | } |
| 245 | entry := buildEntry(httpClient, buildEntryOptions{ |
| 246 | active: true, |
| 247 | gitProtocol: gitProtocol, |
| 248 | hostname: hostname, |
| 249 | token: activeUserToken, |