| 270 | } |
| 271 | |
| 272 | func statusRun(opts *StatusOptions) error { |
| 273 | cfg, err := opts.Config() |
| 274 | if err != nil { |
| 275 | return err |
| 276 | } |
| 277 | authCfg := cfg.Authentication() |
| 278 | |
| 279 | stderr := opts.IO.ErrOut |
| 280 | stdout := opts.IO.Out |
| 281 | cs := opts.IO.ColorScheme() |
| 282 | |
| 283 | hostnames := authCfg.Hosts() |
| 284 | if len(hostnames) == 0 { |
| 285 | fmt.Fprintf(stderr, |
| 286 | "You are not logged into any GitHub hosts. To log in, run: %s\n", cs.Bold("gh auth login")) |
| 287 | if opts.Exporter != nil { |
| 288 | // In machine-friendly mode, we always exit with no error. |
| 289 | opts.Exporter.Write(opts.IO, newAuthStatus()) |
| 290 | return nil |
| 291 | } |
| 292 | return cmdutil.SilentError |
| 293 | } |
| 294 | |
| 295 | if opts.Hostname != "" && !slices.Contains(hostnames, opts.Hostname) { |
| 296 | fmt.Fprintf(stderr, |
| 297 | "You are not logged into any accounts on %s\n", opts.Hostname) |
| 298 | if opts.Exporter != nil { |
| 299 | // In machine-friendly mode, we always exit with no error. |
| 300 | opts.Exporter.Write(opts.IO, newAuthStatus()) |
| 301 | return nil |
| 302 | } |
| 303 | return cmdutil.SilentError |
| 304 | } |
| 305 | |
| 306 | httpClient, err := opts.HttpClient() |
| 307 | if err != nil { |
| 308 | return err |
| 309 | } |
| 310 | |
| 311 | var finalErr error |
| 312 | statuses := newAuthStatus() |
| 313 | |
| 314 | for _, hostname := range hostnames { |
| 315 | if opts.Hostname != "" && opts.Hostname != hostname { |
| 316 | continue |
| 317 | } |
| 318 | |
| 319 | var activeUser string |
| 320 | gitProtocol := cfg.GitProtocol(hostname).Value |
| 321 | activeCred, activeRefreshStatus := resolveCredential(authCfg, hostname, "", opts.NoRefresh) |
| 322 | if authTokenWriteable(activeCred.Source) { |
| 323 | activeUser, _ = authCfg.ActiveUser(hostname) |
| 324 | } |
| 325 | entry := buildEntry(httpClient, buildEntryOptions{ |
| 326 | active: true, |
| 327 | gitProtocol: gitProtocol, |
| 328 | hostname: hostname, |
| 329 | credential: activeCred, |