forked from metafates/mangal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration.go
144 lines (123 loc) · 3.37 KB
/
integration.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package cmd
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/metafates/mangal/icon"
"github.com/metafates/mangal/integration/anilist"
"github.com/metafates/mangal/key"
"github.com/metafates/mangal/log"
"github.com/metafates/mangal/open"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
rootCmd.AddCommand(integrationCmd)
integrationCmd.AddCommand(integrationAnilistCmd)
integrationAnilistCmd.Flags().BoolP("disable", "d", false, "Disable Anilist integration")
}
var integrationCmd = &cobra.Command{
Use: "integration",
Short: "Integration with other services",
Long: `Integration with other services`,
}
var integrationAnilistCmd = &cobra.Command{
Use: "anilist",
Short: "Integration with Anilist",
Long: `Integration with Anilist.
See https://github.com/metafates/mangal/wiki/Anilist-Integration for more information`,
Run: func(cmd *cobra.Command, args []string) {
if lo.Must(cmd.Flags().GetBool("disable")) {
viper.Set(key.AnilistEnable, false)
viper.Set(key.AnilistCode, "")
viper.Set(key.AnilistSecret, "")
viper.Set(key.AnilistID, "")
log.Info("Anilist integration disabled")
handleErr(viper.WriteConfig())
}
if !viper.GetBool(key.AnilistEnable) {
confirm := survey.Confirm{
Message: "Anilist is disabled. Enable?",
Default: false,
}
var response bool
err := survey.AskOne(&confirm, &response)
handleErr(err)
if !response {
return
}
viper.Set(key.AnilistEnable, response)
err = viper.WriteConfig()
if err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
err = viper.SafeWriteConfig()
handleErr(err)
default:
handleErr(err)
log.Error(err)
}
}
}
if viper.GetString(key.AnilistID) == "" {
input := survey.Input{
Message: "Anilsit client ID is not set. Please enter it:",
Help: "",
}
var response string
err := survey.AskOne(&input, &response)
handleErr(err)
if response == "" {
return
}
viper.Set(key.AnilistID, response)
err = viper.WriteConfig()
handleErr(err)
}
if viper.GetString(key.AnilistSecret) == "" {
input := survey.Input{
Message: "Anilsit client secret is not set. Please enter it:",
Help: "",
}
var response string
err := survey.AskOne(&input, &response)
handleErr(err)
if response == "" {
return
}
viper.Set(key.AnilistSecret, response)
err = viper.WriteConfig()
handleErr(err)
}
if viper.GetString(key.AnilistCode) == "" {
authURL := anilist.New().AuthURL()
confirmOpenInBrowser := survey.Confirm{
Message: "Open browser to authenticate with Anilist?",
Default: false,
}
var openInBrowser bool
err := survey.AskOne(&confirmOpenInBrowser, &openInBrowser)
if err == nil && openInBrowser {
err = open.Start(authURL)
}
if err != nil || !openInBrowser {
fmt.Println("Please open the following URL in your browser:")
fmt.Println(authURL)
}
input := survey.Input{
Message: "Anilsit code is not set. Please copy it from the link and paste in here:",
Help: "",
}
var response string
err = survey.AskOne(&input, &response)
handleErr(err)
if response == "" {
return
}
viper.Set(key.AnilistCode, response)
err = viper.WriteConfig()
handleErr(err)
}
fmt.Printf("%s Anilist integration was set up\n", icon.Get(icon.Success))
},
}