Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/quantcli/withings-export-cli/internal/auth"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -84,8 +85,35 @@ var refreshCmd = &cobra.Command{
},
}

var statusCmd = &cobra.Command{
Use: "status",
Short: "Print one-line auth readiness state and exit 0 if usable",
Long: `Print a one-line summary of whether the CLI has a usable token. Exit 0
if a saved token is present and not yet expired, 1 otherwise.

This is a local check — no network call and no refresh is attempted, even
when the saved token is expired. Use 'auth refresh' (or any export
subcommand) to actually refresh.

Per the quantcli shared contract:
https://github.com/quantcli/common/blob/main/CONTRACT.md#5-auth`,
RunE: func(cmd *cobra.Command, args []string) error {
store, err := auth.Load()
if err != nil {
return fmt.Errorf("not logged in — run: withings-export auth login")
}
exp := store.ExpiresAt.Local().Format(time.RFC3339)
if time.Now().After(store.ExpiresAt) {
return fmt.Errorf("token expired %s — run: withings-export auth refresh", exp)
}
fmt.Fprintf(cmd.OutOrStdout(), "logged in as user %s (token expires %s)\n", store.UserID, exp)
return nil
},
}

func init() {
authCmd.AddCommand(loginCmd)
authCmd.AddCommand(logoutCmd)
authCmd.AddCommand(refreshCmd)
authCmd.AddCommand(statusCmd)
}
7 changes: 7 additions & 0 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@ func load() (*TokenStore, error) {
return &store, nil
}

// Load reads the saved token store without attempting a refresh. Returns
// (nil, error) when no token has been saved yet. Suitable for non-mutating
// status checks; use GetToken when an action needs a usable access token.
func Load() (*TokenStore, error) {
return load()
}

func openBrowser(target string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
Expand Down
Loading