Files
go-garth/cmd/garth/cmd/tokens.go
2025-09-21 18:56:33 -07:00

56 lines
1.3 KiB
Go

package cmd
import (
"encoding/json"
"fmt"
"log"
"github.com/sstent/go-garth/internal/auth/credentials"
"github.com/sstent/go-garth/pkg/garmin"
"github.com/spf13/cobra"
)
var tokensCmd = &cobra.Command{
Use: "tokens",
Short: "Output OAuth tokens in JSON format",
Long: `Output the OAuth1 and OAuth2 tokens in JSON format after a successful login.`,
Run: func(cmd *cobra.Command, args []string) {
// Load credentials from .env file
_, _, domain, err := credentials.LoadEnvCredentials()
if err != nil {
log.Fatalf("Failed to load credentials: %v", err)
}
// Create client
garminClient, err := garmin.NewClient(domain)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Try to load existing session first
sessionFile := "garmin_session.json"
if err := garminClient.LoadSession(sessionFile); err != nil {
log.Fatalf("No existing session found. Please run 'garth login' first.")
}
tokens := struct {
OAuth1 *garmin.OAuth1Token `json:"oauth1"`
OAuth2 *garmin.OAuth2Token `json:"oauth2"`
}{
OAuth1: garminClient.OAuth1Token(),
OAuth2: garminClient.OAuth2Token(),
}
jsonBytes, err := json.MarshalIndent(tokens, "", " ")
if err != nil {
log.Fatalf("Failed to marshal tokens: %v", err)
}
fmt.Println(string(jsonBytes))
},
}
func init() {
rootCmd.AddCommand(tokensCmd)
}