forked from StupidityDB/VencordPlusInstaller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
89 lines (76 loc) · 1.69 KB
/
util.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
/*
* 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"
"fmt"
"os"
"runtime"
"strings"
"syscall"
)
func ArrayIncludes[T comparable](arr []T, v T) bool {
for _, e := range arr {
if e == v {
return true
}
}
return false
}
func ExistsFile(path string) bool {
_, err := os.Stat(path)
fmt.Println("Checking if", path, "exists:", Ternary(err == nil, "Yes", "No"))
return err == nil
}
func IsDirectory(path string) bool {
s, err := os.Stat(path)
if err != nil {
fmt.Println("Error while checking if", path, "is directory:", err)
return false
}
fmt.Println("Checking if", path, "is directory:", Ternary(s.IsDir(), "Yes", "No"))
return s.IsDir()
}
func Ternary[T any](b bool, ifTrue, ifFalse T) T {
if b {
return ifTrue
}
return ifFalse
}
var branches = []string{"canary", "development", "ptb"}
func GetBranch(name string) string {
name = strings.ToLower(name)
for _, branch := range branches {
if strings.HasSuffix(name, branch) {
return branch
}
}
return "stable"
}
func Ptr[T any](v T) *T {
return &v
}
func CheckIfErrIsCauseItsBusyRn(err error) error {
if runtime.GOOS != "windows" {
return err
}
// bruhhhh
if linkError, ok := err.(*os.LinkError); ok {
if errno, ok := linkError.Err.(syscall.Errno); ok && errno == 32 /* ERROR_SHARING_VIOLATION */ {
return errors.New(
"Cannot patch because Discord's files are used by a different process." +
"\nMake sure you close Discord before trying to patch!",
)
}
}
return err
}
func Unwrap[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}