status subcommand
()
| 122 | // status subcommand |
| 123 | |
| 124 | func newAuthStatusCommand() *cobra.Command { |
| 125 | return &cobra.Command{ |
| 126 | Use: "status", |
| 127 | Short: "Show authentication status", |
| 128 | RunE: func(cmd *cobra.Command, args []string) error { |
| 129 | status := map[string]any{ |
| 130 | "base_url": cfg.BaseURL, |
| 131 | "authenticated": false, |
| 132 | } |
| 133 | |
| 134 | if os.Getenv("HEY_TOKEN") != "" { |
| 135 | status["authenticated"] = true |
| 136 | status["method"] = "env_var" |
| 137 | |
| 138 | if writer.IsStyled() { |
| 139 | w := cmd.OutOrStdout() |
| 140 | fmt.Fprintf(w, "Base URL: %s\n", cfg.BaseURL) |
| 141 | fmt.Fprintln(w, "Status: Logged in (via HEY_TOKEN env var)") |
| 142 | return nil |
| 143 | } |
| 144 | return writeOK(status, output.WithSummary("Logged in via HEY_TOKEN")) |
| 145 | } |
| 146 | |
| 147 | store := authMgr.GetStore() |
| 148 | creds, err := store.Load(authMgr.CredentialKey()) |
| 149 | if err != nil || (creds.AccessToken == "" && creds.SessionCookie == "") { |
| 150 | if writer.IsStyled() { |
| 151 | w := cmd.OutOrStdout() |
| 152 | fmt.Fprintf(w, "Base URL: %s\n", cfg.BaseURL) |
| 153 | fmt.Fprintln(w, "Status: Not logged in") |
| 154 | return nil |
| 155 | } |
| 156 | return writeOK(status, output.WithSummary("Not logged in"), |
| 157 | output.WithBreadcrumbs(output.Breadcrumb{ |
| 158 | Action: "login", |
| 159 | Command: "hey auth login", |
| 160 | Description: "Authenticate with HEY", |
| 161 | }), |
| 162 | ) |
| 163 | } |
| 164 | |
| 165 | status["authenticated"] = true |
| 166 | if creds.OAuthType != "" { |
| 167 | status["auth_type"] = creds.OAuthType |
| 168 | } |
| 169 | if store.UsingKeyring() { |
| 170 | status["storage"] = "keyring" |
| 171 | } else { |
| 172 | status["storage"] = "file" |
| 173 | } |
| 174 | if creds.ExpiresAt > 0 { |
| 175 | expiry := time.Unix(creds.ExpiresAt, 0) |
| 176 | status["expires_at"] = expiry.Format(time.RFC3339) |
| 177 | status["expired"] = time.Now().After(expiry) |
| 178 | } |
| 179 | if creds.RefreshToken != "" { |
| 180 | status["refresh_available"] = true |
| 181 | } |
no test coverage detected