-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_status.go
More file actions
70 lines (58 loc) · 1.57 KB
/
cmd_status.go
File metadata and controls
70 lines (58 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/urfave/cli/v3"
)
func newStatusCommand() *cli.Command {
return &cli.Command{
Name: "status",
Usage: "Check authentication status for services",
Action: runStatus,
}
}
func runStatus(ctx context.Context, cmd *cli.Command) error {
configPath := cmd.String("config")
config, err := loadConfigFromFile(configPath)
if err != nil {
return fmt.Errorf("error loading config: %w", err)
}
log.Println("Authentication Status:")
log.Println("======================")
// Check AniList
oauthAnilist, err := NewAnilistOAuthWithoutInit(ctx, config)
if err != nil {
log.Printf("AniList: Error - %v\n", err)
} else {
printServiceStatus("AniList", oauthAnilist)
}
// Check MyAnimeList
oauthMAL, err := NewMyAnimeListOAuthWithoutInit(ctx, config)
if err != nil {
log.Printf("MyAnimeList: Error - %v\n", err)
} else {
printServiceStatus("MyAnimeList", oauthMAL)
}
log.Println()
log.Printf("Token file: %s\n", config.TokenFilePath)
return nil
}
func printServiceStatus(serviceName string, oauth *OAuth) {
if oauth.NeedInit() {
log.Printf("%-13s Not authenticated\n", serviceName+":")
return
}
if oauth.IsTokenValid() {
expiry := oauth.TokenExpiry()
if expiry.IsZero() {
log.Printf("%-13s Authenticated (no expiry)\n", serviceName+":")
} else {
remaining := time.Until(expiry).Round(time.Minute)
log.Printf("%-13s Authenticated (expires in %v)\n", serviceName+":", remaining)
}
} else {
log.Printf("%-13s Token expired (refresh may be attempted on use)\n", serviceName+":")
}
}