-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
316 lines (262 loc) · 6.12 KB
/
main.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"strings"
"github.com/AlecAivazis/survey/v2"
)
var qs = &survey.MultiSelect{
Message: "What do you want to install?",
Options: []string{"starship config", "bashrc extension", "gitconfig", "motd-diskspace", "alacritty config", "atuin config",
"vim config", "tmux config"},
}
func main() {
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
pwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
var configIndexes []int
survey.AskOne(qs, &configIndexes)
for _, index := range configIndexes {
fmt.Println("Installing: " + qs.Options[index])
installStatus := false
switch index {
case 0:
installStatus = installStarship(pwd, dirname)
case 1:
installStatus = installBashrc(pwd, dirname)
case 2:
installStatus = installGitconfig(pwd, dirname)
case 3:
installStatus = installMotdDiskspace(pwd, dirname)
case 4:
installStatus = installAlacritty(pwd, dirname)
case 5:
installStatus = installAutin(pwd, dirname)
case 6:
installStatus = installVimConfig(pwd, dirname)
case 7:
installStatus = installTmuxConfig(pwd, dirname)
}
if installStatus {
fmt.Println("Success")
} else {
fmt.Println("Failed")
}
fmt.Println()
}
}
func isFileExist(file string) bool {
if _, err := os.Stat(file); os.IsNotExist(err) {
return false
}
return true
}
func CheckHandleFileExist(file string, isFolder bool) bool {
if !isFileExist(file) {
return true
}
delete := false
prompt := &survey.Confirm{
Message: "File: " + file + " already exists, do you want to delete it?",
}
if isFolder {
prompt.Message = "Folder: " + file + " is not empty, are you sure you want to delete it?"
}
survey.AskOne(prompt, &delete)
if delete {
var err error
if isFolder {
err = os.RemoveAll(file)
} else {
err = os.Remove(file)
}
if err != nil {
if strings.Contains(err.Error(), "directory not empty") {
CheckHandleFileExist(file, true)
} else {
fmt.Println(err)
return false
}
}
}
return delete
}
func installStarship(pwd string, homedir string) bool {
target := path.Join(homedir, ".config", "starship.toml")
file := path.Join(pwd, ".config", "starship.toml")
err := os.MkdirAll(path.Dir(target), 0755)
if err != nil {
fmt.Println(err)
return false
}
continueInstall := CheckHandleFileExist(target, false)
if !continueInstall {
return false
}
err = os.Symlink(file, target)
if err != nil {
fmt.Println(err)
return false
}
return true
}
func installAlacritty(pwd string, homedir string) bool {
target := path.Join(homedir, ".config", "alacritty")
file := path.Join(pwd, ".config", "alacritty")
err := os.MkdirAll(path.Dir(target), 0755)
if err != nil {
fmt.Println(err)
return false
}
continueInstall := CheckHandleFileExist(target, false)
if !continueInstall {
return false
}
err = os.Symlink(file, target)
if err != nil {
fmt.Println(err)
return false
}
return true
}
func installAutin(pwd string, homedir string) bool {
target := path.Join(homedir, ".config", "atuin")
file := path.Join(pwd, ".config", "atuin")
err := os.MkdirAll(path.Dir(target), 0755)
if err != nil {
fmt.Println(err)
return false
}
continueInstall := CheckHandleFileExist(target, false)
if !continueInstall {
return false
}
err = os.Symlink(file, target)
if err != nil {
fmt.Println(err)
return false
}
return true
}
func installBashrc(pwd string, homedir string) bool {
target := path.Join(homedir, ".bashrc_ext")
file := path.Join(pwd, ".bashrc_ext")
continueInstall := CheckHandleFileExist(target, false)
if !continueInstall {
return false
}
err := os.Symlink(file, target)
if err != nil {
fmt.Println(err)
return false
}
f, err := os.OpenFile(path.Join(homedir, ".bashrc"), os.O_RDWR, 0644)
if err != nil {
fmt.Println(err)
return false
}
defer f.Close()
// Check if source is already in bashrc
bashrcLine := "source ~/.bashrc_ext"
scanner := bufio.NewScanner(f)
found := false
for scanner.Scan() {
if strings.TrimSpace(scanner.Text()) == strings.TrimSpace(bashrcLine) {
found = true
break // Found it, no need to keep scanning
}
}
// Seek back to the end for appending
_, err = f.Seek(0, io.SeekEnd)
if err != nil {
fmt.Println("Error seeking:", err)
return false
}
if !found {
_, err = f.WriteString(fmt.Sprintf("\n%s\n", bashrcLine))
if err != nil {
fmt.Println(err)
return false
}
}
return true
}
func installGitconfig(pwd string, homedir string) bool {
target := path.Join(homedir, ".gitconfig")
file := path.Join(pwd, ".gitconfig")
continueInstall := CheckHandleFileExist(target, false)
if !continueInstall {
return false
}
err := os.Symlink(file, target)
if err != nil {
fmt.Println(err)
return false
}
return true
}
func installMotdDiskspace(pwd string, homedir string) bool {
folder := "/etc/update-motd.d"
if !isFileExist(folder) {
fmt.Println("Folder: " + folder + " does not exist\nCan't install motd-diskspace")
return false
}
target := path.Join(folder, "30-diskspace")
file := path.Join(pwd, "30-diskspace")
continueInstall := CheckHandleFileExist(target, false)
if !continueInstall {
return false
}
err := os.Symlink(file, target)
if err != nil {
if !os.IsPermission(err) {
fmt.Println(err)
return false
}
fmt.Println("Permission error, escalating permissions")
err := exec.Command("sudo", "ln", "-s", file, target).Run()
if err != nil {
fmt.Println(err)
return false
}
}
return true
}
func installVimConfig(pwd string, homedir string) bool {
target := path.Join(homedir, ".vimrc")
file := path.Join(pwd, ".vimrc")
continueInstall := CheckHandleFileExist(target, false)
if !continueInstall {
return false
}
err := os.Symlink(file, target)
if err != nil {
fmt.Println(err)
return false
}
return true
}
func installTmuxConfig(pwd string, homedir string) bool {
target := path.Join(homedir, ".tmux.conf")
file := path.Join(pwd, ".tmux.conf")
continueInstall := CheckHandleFileExist(target, false)
if !continueInstall {
return false
}
err := os.Symlink(file, target)
if err != nil {
fmt.Println(err)
return false
}
return true
}