Skip to content

Commit 71e16bd

Browse files
Andre SantosAndre Santos
Andre Santos
authored and
Andre Santos
committed
Add flag support w/ new features (#1)
Added ability to use a flag to select a specific build type and make it non-interactive (skips requirement to press 'enter').
1 parent 270e6b8 commit 71e16bd

File tree

1 file changed

+91
-30
lines changed

1 file changed

+91
-30
lines changed

main.go

Lines changed: 91 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313
"time"
1414

1515
"github.com/h2non/filetype"
16+
"github.com/jessevdk/go-flags"
1617
"github.com/ricochet2200/go-disk-usage/du"
1718
)
1819

1920
const (
20-
softVersion = "1.1"
21+
softVersion = "1.2"
2122
dumpDir = "dump"
2223
)
2324

@@ -198,19 +199,76 @@ func freeStorage(path string) {
198199
return
199200
}
200201

202+
// Slice search
203+
func searchSlice(y []string, z string) bool {
204+
for _, n := range y {
205+
if z == n {
206+
return true
207+
}
208+
}
209+
return false
210+
}
211+
201212
// Initialisation of system information variables
202213
var uid int
203214
var userName string
204215
var homePath string
205216
var sudoerUID int
206217

207218
func main() {
208-
fmt.Print("\n")
219+
220+
fmt.Println()
221+
222+
// Discord client build names
223+
discordBuildName := map[int]string{
224+
0: "Stable",
225+
1: "PTB",
226+
2: "Canary",
227+
3: "Development",
228+
}
229+
// Discord client build folder names
230+
discordBuildDir := map[int]string{
231+
0: "discord",
232+
1: "discordptb",
233+
2: "discordcanary",
234+
3: "discorddevelopment",
235+
}
236+
// Cache paths for each platform
237+
cachePath := map[string]string{
238+
"linux": "%s/.config/%s/Cache/",
239+
"darwin": "%s/Library/Application Support/%s/Cache/",
240+
"windows": "%s\\AppData\\Roaming\\%s\\Cache\\",
241+
}
242+
243+
// Initialise a few maps
244+
pathStatus := make(map[int]bool)
245+
cachedFile := make(map[int]map[int]string)
246+
247+
// Initialise flags
248+
var discordBuildListSlice []string
249+
var discordBuildListOption string
250+
var discordBuildListEntryName string
251+
var discordBuildListEntryDir string
252+
253+
for i := 0; i < len(discordBuildName); i++ {
254+
discordBuildListSlice = append(discordBuildListSlice, strings.ToLower(discordBuildName[i]))
255+
}
256+
257+
var opts struct {
258+
Build string `short:"b" long:"build" description:"Select build type: stable, ptb, canary, development"`
259+
Noninteractive bool `short:"n" long:"noninteractive" description:"Non-interactive -- no 'enter' key required"`
260+
}
261+
262+
_, err := flags.Parse(&opts)
263+
264+
if err != nil {
265+
os.Exit(0)
266+
}
209267

210268
// Banner
211-
fmt.Print("#######################################\n")
269+
fmt.Print("#####################################\n")
212270
fmt.Printf("# Discord Cache Dump :: Version %s #\n", softVersion)
213-
fmt.Print("#######################################\n\n")
271+
fmt.Print("#####################################\n\n")
214272

215273
user, err := user.Current()
216274
if err != nil {
@@ -275,6 +333,29 @@ func main() {
275333
}
276334

277335
fmt.Printf("Logged in as: %s\n\n", userName)
336+
// Build flag
337+
if opts.Build != "" {
338+
discordBuildListOption = strings.ToLower(opts.Build)
339+
if !searchSlice(discordBuildListSlice, discordBuildListOption) {
340+
fmt.Printf("[ERROR] Build type does not exist %s", exitNewLine())
341+
os.Exit(1)
342+
} else {
343+
fmt.Printf("Build selected: %s\n\n", discordBuildListOption)
344+
for i := 0; i < len(discordBuildDir); i++ {
345+
if discordBuildListOption == strings.ToLower(discordBuildName[i]) {
346+
discordBuildListEntryName = discordBuildName[i]
347+
discordBuildListEntryDir = discordBuildDir[i]
348+
break
349+
}
350+
}
351+
discordBuildName = map[int]string{
352+
0: discordBuildListEntryName,
353+
}
354+
discordBuildDir = map[int]string{
355+
0: discordBuildListEntryDir,
356+
}
357+
}
358+
}
278359

279360
fmt.Print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
280361
fmt.Print("!! A few files (index, data_0-3) are in use by Discord while !!\n")
@@ -284,34 +365,11 @@ func main() {
284365
fmt.Print("!! while copying the files over. !!\n")
285366
fmt.Print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n")
286367

287-
fmt.Print("Press enter to continue ...\n")
288-
fmt.Scanln()
289-
290-
// Discord client build names
291-
discordBuildName := map[int]string{
292-
0: "Stable",
293-
1: "PTB",
294-
2: "Canary",
295-
3: "Development",
296-
}
297-
// Discord client build folder names
298-
discordBuildDir := map[int]string{
299-
0: "discord",
300-
1: "discordptb",
301-
2: "discordcanary",
302-
3: "discorddevelopment",
303-
}
304-
// Cache paths for each platform
305-
cachePath := map[string]string{
306-
"linux": "%s/.config/%s/Cache/",
307-
"darwin": "%s/Library/Application Support/%s/Cache/",
308-
"windows": "%s\\AppData\\Roaming\\%s\\Cache\\",
368+
if opts.Noninteractive == false {
369+
fmt.Print("Press enter to continue ...\n")
370+
fmt.Scanln()
309371
}
310372

311-
// Initialise a few maps
312-
pathStatus := make(map[int]bool)
313-
cachedFile := make(map[int]map[int]string)
314-
315373
fmt.Print("Checking for existing cache directories ...\n\n")
316374

317375
// Check if directories exist
@@ -420,6 +478,9 @@ func main() {
420478
fmt.Printf("[NOTICE] Cannot read client-critial cache while Discord %s is running\n", discordBuildName[i])
421479
fmt.Printf("[...] Unable to read %d client-critial cache file(s)\n", unreadableRes)
422480
unreadableResCount := int64(len(cachedFile[i])) - unreadableRes
481+
if unreadableResCount < 0 {
482+
unreadableResCount = 0
483+
}
423484
fmt.Printf("[...] Actually copied %d cache files from Discord %s\n", unreadableResCount, discordBuildName[i])
424485
}
425486
}

0 commit comments

Comments
 (0)