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