-
Notifications
You must be signed in to change notification settings - Fork 5
/
check_cmd.go
139 lines (113 loc) · 3.14 KB
/
check_cmd.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
package cmd
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/KevinGong2013/apkgo/cmd/shared"
"github.com/KevinGong2013/apkgo/cmd/storage"
"github.com/KevinGong2013/apkgo/cmd/utils"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra"
)
var checkCommand = &cobra.Command{
Use: "check",
Short: "预检查各个平台的授权信息",
Run: runCheck,
}
func init() {
rootCmd.AddCommand(checkCommand)
checkCommand.Flags().Bool("refresh-cookie", true, "如果认证失败是否打开浏览器重新登陆以刷新cookie")
}
func runCheck(cmd *cobra.Command, args []string) {
// 1. 读取配置文件
c, _ := LoadConfig()
if c == nil {
fatalErr("请先完成初始化配置")
}
// 初始化存储
s, err := storage.New(c.Storage, filepath.Join(apkgoHome, SecretDirName))
if err != nil {
fatalErr("storage配置不正确")
}
// 更新本地的认证信息到最新
if err := s.UpToDate(); err != nil {
fatalErr(err.Error())
}
// 解析config文件
sc, err := ParseStoreSecretFile(args)
if err != nil {
fatalErr(err.Error())
}
// 检查没有插件,如果有插件检查一下插件有没有成功配置
refreshCookie, err := cmd.Flags().GetBool("refresh-cookie")
if err != nil {
refreshCookie = true
}
if utils.IsRunningInDockerContainer() {
refreshCookie = false
}
fmt.Println("Initial publishers ...")
curls, browsers, plugins, err := InitPublishers(sc)
if err != nil {
fatalErr(err.Error())
}
fmt.Printf("curls(%d), browsers(%d), plugins(%d)\n", len(curls), len(browsers), len(plugins))
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetAutoIndex(true)
t.AppendHeader(table.Row{"name", "category", "status"})
var rows []table.Row
for _, plugin := range plugins {
rows = append(rows, table.Row{plugin.Name(), "plugin", text.FgGreen.Sprint("正常")})
}
if len(rows) > 0 {
t.AppendRows(rows, table.RowConfig{
AutoMerge: false,
})
t.AppendSeparator()
rows = nil
}
for _, curl := range curls {
rows = append(rows, table.Row{curl.Name(), "api", text.FgGreen.Sprint("正常")})
}
if len(rows) > 0 {
t.AppendRows(rows, table.RowConfig{
AutoMerge: false,
})
t.AppendSeparator()
rows = nil
}
// 测试用
for _, p := range browsers {
if c, ok := p.(shared.Checker); ok {
status := text.FgGreen.Sprint("正常")
if err := c.CheckAuth(refreshCookie); err != nil {
if refreshCookie {
status = text.FgRed.Sprintf("重新登陆失败: %s", err.Error())
} else {
status = text.FgYellow.Sprint("需要重新登陆")
}
}
if post, ok := p.(shared.PublishCleaner); ok {
if err := post.Clean(); err != nil {
fmt.Println(text.FgRed.Sprintf("清理资源出错. %s", err.Error()))
}
}
rows = append(rows, table.Row{p.Name(), "browser", status})
}
}
t.AppendRows(rows)
// 同步认证信息
fmt.Println(text.FgYellow.Sprint("等待浏览器保存数据 2s"))
time.Sleep(time.Second * 2)
if err := s.Sync(); err != nil {
fatalErr(err.Error())
}
t.Render()
}
func fatalErr(message string) {
fmt.Println(text.FgRed.Sprintf("%s\n帮助文档: https://apkgo.com.cn", message))
os.Exit(1)
}