-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
128 lines (112 loc) · 2.79 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strings"
"time"
)
func containsCharacterSet(s, charset string) bool {
for _, char := range charset {
if !strings.ContainsRune(s, char) {
return false
}
}
return true
}
func LoadingSpinner(task func()) {
done := make(chan struct{})
go func() {
// Define a set of Unicode spinner characters
spinChars := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠶", "⠦", "⠇", "⠏"}
for {
select {
case <-done:
return
default:
for _, char := range spinChars {
fmt.Printf("\r%s Loading...", char)
time.Sleep(100 * time.Millisecond)
}
}
}
}()
// Execute the provided task in a separate goroutine
go func() {
task()
close(done) // Signal the loader to stop once the task is done
}()
// Wait until the task is complete
<-done // Block until the task is complete
fmt.Println("\rDone! ") // Clear line after done
}
func getConfigPath(target string) string {
targetUser := os.Getenv("SUDO_USER")
var configHome string
if targetUser == "" {
fmt.Println("ERROR: SUDO_USER empty. this could be because you are using an unsupported authenticator. Please use sudo.")
panic("Could not get config.")
} else {
configHome = filepath.Join("/home/", targetUser, "/.config/")
}
if target == "" {
return filepath.Join(configHome, "dill") // make this so you can just retrieve the directory, for writing.
} else {
return filepath.Join(configHome, "dill", target)
}
}
func getTMP(target string) string {
_, err := os.Open("/tmp/dill")
if err != nil {
os.Mkdir("/tmp/dill/", 01411)
}
if target == "" {
return filepath.Join("/tmp/", "dill")
} else {
return filepath.Join("/tmp/", "dill", target)
}
}
func check(err error) {
if err != nil {
panic(err)
}
}
func UnmarshalJSON(filepath string, storageStruct any) any {
file, err := os.Open(filepath)
check(err)
defer file.Close() // Ensure the file is closed after reading
decoder := json.NewDecoder(file)
err = decoder.Decode(storageStruct)
check(err)
fmt.Println(storageStruct)
return storageStruct // Return the unmarshaled struct
}
func remove_pac(s []Pac_Out, i int) []Pac_Out {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
func isRoot() bool {
currentUser, err := user.Current()
if err != nil {
log.Fatalf("[isRoot] Unable to get current user: %s", err)
}
return currentUser.Username == "root"
}
func confirm_choice() {
fmt.Printf("\n ")
fmt.Println("Would you like to make these changes to your installation? Y/N")
var input string
fmt.Scanln(&input)
if input == "Y" {
fmt.Println("Okay, continuing.")
} else if input == "N" {
fmt.Println("Okay, exiting gracefully.")
os.Exit(0)
} else {
fmt.Println("Invalid choice, try again.")
confirm_choice()
}
}