-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
cli.go
280 lines (241 loc) · 6.95 KB
/
cli.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//go:build cli
/*
* SPDX-License-Identifier: GPL-3.0
* Vencord Installer, a cross platform gui/cli app for installing Vencord
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
package main
import (
"errors"
"flag"
"fmt"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
"os"
"runtime"
"strings"
"vencordinstaller/buildinfo"
)
var discords []any
var interactive = false
func isValidBranch(branch string) bool {
switch branch {
case "", "stable", "ptb", "canary", "auto":
return true
default:
return false
}
}
func die(msg string) {
Log.Error(msg)
exitFailure()
}
func main() {
InitGithubDownloader()
discords = FindDiscords()
// Used by log.go init func
flag.Bool("debug", false, "Enable debug info")
var helpFlag = flag.Bool("help", false, "View usage instructions")
var versionFlag = flag.Bool("version", false, "View the program version")
var updateSelfFlag = flag.Bool("update-self", false, "Update me to the latest version")
var installFlag = flag.Bool("install", false, "Install Vencord")
var updateFlag = flag.Bool("repair", false, "Repair Vencord")
var uninstallFlag = flag.Bool("uninstall", false, "Uninstall Vencord")
var installOpenAsarFlag = flag.Bool("install-openasar", false, "Install OpenAsar")
var uninstallOpenAsarFlag = flag.Bool("uninstall-openasar", false, "Uninstall OpenAsar")
var locationFlag = flag.String("location", "", "The location of the Discord install to modify")
var branchFlag = flag.String("branch", "", "The branch of Discord to modify [auto|stable|ptb|canary]")
flag.Parse()
if *helpFlag {
flag.Usage()
return
}
if *versionFlag {
fmt.Println("Vencord Installer Cli", buildinfo.InstallerTag, "("+buildinfo.InstallerGitHash+")")
fmt.Println("Copyright (C) 2023 Vendicated and Vencord contributors")
fmt.Println("License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.")
return
}
if *updateSelfFlag {
if !<-SelfUpdateCheckDoneChan {
die("Can't update self because checking for updates failed")
}
if err := UpdateSelf(); err != nil {
Log.Error("Failed to update self:", err)
exitFailure()
}
exitSuccess()
}
if *locationFlag != "" && *branchFlag != "" {
die("The 'location' and 'branch' flags are mutually exclusive.")
}
if !isValidBranch(*branchFlag) {
die("The 'branch' flag must be one of the following: [auto|stable|ptb|canary]")
}
if *installFlag || *updateFlag {
if !<-GithubDoneChan {
die("Not " + Ternary(*installFlag, "installing", "updating") + " as fetching release data failed")
}
}
install, uninstall, update, installOpenAsar, uninstallOpenAsar := *installFlag, *uninstallFlag, *updateFlag, *installOpenAsarFlag, *uninstallOpenAsarFlag
switches := []*bool{&install, &update, &uninstall, &installOpenAsar, &uninstallOpenAsar}
if !SliceContainsFunc(switches, func(b *bool) bool { return *b }) {
interactive = true
go func() {
<-SelfUpdateCheckDoneChan
if IsSelfOutdated {
Log.Warn("Your installer is outdated.")
Log.Warn("To update, select the 'Update Vencord Installer' option to update, or run with --update-self")
}
}()
choices := []string{
"Install Vencord",
"Repair Vencord",
"Uninstall Vencord",
"Install OpenAsar",
"Uninstall OpenAsar",
"View Help Menu",
"Update Vencord Installer",
"Quit",
}
_, choice, err := (&promptui.Select{
Label: "What would you like to do? (Press Enter to confirm)",
Items: choices,
}).Run()
handlePromptError(err)
switch choice {
case "View Help Menu":
flag.Usage()
return
case "Quit":
return
case "Update Vencord Installer":
if err := UpdateSelf(); err != nil {
Log.Error("Failed to update self:", err)
exitFailure()
}
exitSuccess()
}
*switches[SliceIndex(choices, choice)] = true
}
var err error
var errSilent error
if install {
errSilent = PromptDiscord("patch", *locationFlag, *branchFlag).patch()
} else if uninstall {
errSilent = PromptDiscord("unpatch", *locationFlag, *branchFlag).unpatch()
} else if update {
Log.Info("Downloading latest Vencord files...")
err := installLatestBuilds()
Log.Info("Done!")
if err == nil {
errSilent = PromptDiscord("repair", *locationFlag, *branchFlag).patch()
}
} else if installOpenAsar {
discord := PromptDiscord("patch", *locationFlag, *branchFlag)
if !discord.IsOpenAsar() {
err = discord.InstallOpenAsar()
} else {
die("OpenAsar already installed")
}
} else if uninstallOpenAsar {
discord := PromptDiscord("patch", *locationFlag, *branchFlag)
if discord.IsOpenAsar() {
err = discord.UninstallOpenAsar()
} else {
die("OpenAsar not installed")
}
}
if err != nil {
Log.Error(err)
exitFailure()
}
if errSilent != nil {
exitFailure()
}
exitSuccess()
}
func exit(status int) {
if runtime.GOOS == "windows" && IsDoubleClickRun() && interactive {
fmt.Print("Press Enter to exit")
var b byte
_, _ = fmt.Scanf("%v", &b)
}
os.Exit(status)
}
func exitSuccess() {
color.HiGreen("✔ Success!")
exit(0)
}
func exitFailure() {
color.HiRed("❌ Failed!")
exit(1)
}
func handlePromptError(err error) {
if errors.Is(err, promptui.ErrInterrupt) {
exit(0)
}
Log.FatalIfErr(err)
}
func PromptDiscord(action, dir, branch string) *DiscordInstall {
if branch == "auto" {
for _, b := range []string{"stable", "canary", "ptb"} {
for _, discord := range discords {
install := discord.(*DiscordInstall)
if install.branch == b {
return install
}
}
}
die("No Discord install found. Try manually specifying it with the --dir flag. Hint: snap is not supported")
}
if branch != "" {
for _, discord := range discords {
install := discord.(*DiscordInstall)
if install.branch == branch {
return install
}
}
die("Discord " + branch + " not found")
}
if dir != "" {
if discord := ParseDiscord(dir, branch); discord != nil {
return discord
} else {
die(dir + " is not a valid Discord install. Hint: snap is not supported")
}
}
items := SliceMap(discords, func(d any) string {
install := d.(*DiscordInstall)
//goland:noinspection GoDeprecation
return fmt.Sprintf("%s - %s%s", strings.Title(install.branch), install.path, Ternary(install.isPatched, " [PATCHED]", ""))
})
items = append(items, "Custom Location")
_, choice, err := (&promptui.Select{
Label: "Select Discord install to " + action + " (Press Enter to confirm)",
Items: items,
}).Run()
handlePromptError(err)
if choice != "Custom Location" {
return discords[SliceIndex(items, choice)].(*DiscordInstall)
}
for {
custom, err := (&promptui.Prompt{
Label: "Custom Discord Location",
}).Run()
handlePromptError(err)
if di := ParseDiscord(custom, ""); di != nil {
return di
}
Log.Error("Invalid Discord install!")
}
}
func InstallLatestBuilds() error {
return installLatestBuilds()
}
func HandleScuffedInstall() {
fmt.Println("Hold On!")
fmt.Println("You have a broken Discord Install.")
fmt.Println("Please reinstall Discord before proceeding!")
fmt.Println("Otherwise, Vencord will likely not work.")
}