-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_logout.go
More file actions
92 lines (78 loc) · 2.02 KB
/
cmd_logout.go
File metadata and controls
92 lines (78 loc) · 2.02 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"fmt"
"log"
"github.com/urfave/cli/v3"
)
func newLogoutCommand() *cli.Command {
return &cli.Command{
Name: "logout",
Usage: "Remove authentication tokens",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "service",
Aliases: []string{"s"},
Usage: "service to logout (anilist, myanimelist, all)",
Value: ServiceAll,
},
},
Action: runLogout,
}
}
func runLogout(ctx context.Context, cmd *cli.Command) error {
configPath := cmd.String("config")
service := cmd.String("service")
config, err := loadConfigFromFile(configPath)
if err != nil {
return fmt.Errorf("error loading config: %w", err)
}
switch service {
case ServiceAnilist:
return logoutAnilist(ctx, config)
case ServiceMyAnimeList:
return logoutMyAnimeList(ctx, config)
case ServiceAll:
err := logoutMyAnimeList(ctx, config)
if err != nil {
log.Printf("Warning: %v", err)
}
err = logoutAnilist(ctx, config)
if err != nil {
log.Printf("Warning: %v", err)
}
return nil
default:
return fmt.Errorf("unknown service: %s (use: anilist, myanimelist, all)", service)
}
}
func logoutAnilist(ctx context.Context, config Config) error {
oauth, err := NewAnilistOAuthWithoutInit(ctx, config)
if err != nil {
return fmt.Errorf("error creating anilist oauth: %w", err)
}
if oauth.NeedInit() {
log.Println("AniList: Not logged in")
return nil
}
if err := oauth.DeleteToken(); err != nil {
return fmt.Errorf("error removing anilist token: %w", err)
}
log.Println("AniList: Logged out successfully")
return nil
}
func logoutMyAnimeList(ctx context.Context, config Config) error {
oauth, err := NewMyAnimeListOAuthWithoutInit(ctx, config)
if err != nil {
return fmt.Errorf("error creating myanimelist oauth: %w", err)
}
if oauth.NeedInit() {
log.Println("MyAnimeList: Not logged in")
return nil
}
if err := oauth.DeleteToken(); err != nil {
return fmt.Errorf("error removing myanimelist token: %w", err)
}
log.Println("MyAnimeList: Logged out successfully")
return nil
}