-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredplane.nim
120 lines (103 loc) · 2.79 KB
/
redplane.nim
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
#Import necessary libraries
import os, strformat, strutils, parseopt
#Declare variables
var
p = initOptParser()
yesToAll = false
yn: char
multiplePkgs = false
allPkgs: seq[string]
usageString = """
-h: return help
-u: update packages in to-update file
-i: install package (value required) (can install multiple if separated by a comma, example: obs,telegram-desktop,linux-lqx)
-y: answer yes to all prompts
tip: use the flags that requite a value like -flag:value, not -flag value
"""
#Create the required procedures
proc errorPrompt(err: string) =
echo fmt"KABOOM! plane crash: {err}"
quit(1)
proc pacmanInstall(pkg: string): int =
if execShellCmd(fmt"sudo pacman -S {pkg}") != 0:
return 1
proc mkfiles() =
discard execShellCmd("touch to-update")
proc clonePkg(pkg: string) =
discard execShellCmd(fmt"git clone https://aur.archlinux.org/{pkg}")
proc makePkg(pkg: string) =
discard execShellCmd(fmt"(cd {pkg} && makepkg PKGBUILD)")
proc cleanUp(pkg: string) =
discard execShellCmd(fmt"rm -rf {pkg}")
proc addToUpdateScript(pkg: string) =
discard execShellCmd(fmt"tee -a {pkg} to-update")
proc installPkg(pkg: string) =
if pacmanInstall(pkg) != 0:
clonePkg(pkg)
makePkg(pkg)
cleanUp(pkg)
else:
echo "pacman has successfully installed the package."
#Create required files
mkfiles()
#Check if there are parameters
if paramCount() == 0:
errorPrompt("no option specified")
#Parse options and execute
while true:
p.next
case p.key
of "t":
putEnv("MAKEFLAGS", fmt"-j{p.val}")
of "y":
yesToAll = true
of "i":
for i in p.val:
if i == ',':
echo "multiple packages detected! attempting parse"
multiplePkgs = true
allPkgs = p.val.split(",")
if multiplePkgs:
if yesToAll:
for i in allPkgs:
installPkg(i)
addToUpdateScript(i)
else:
echo "would you like to install multiple packages? [y/n]: "
yn = stdin.readChar()
if yn == 'y':
for i in allPkgs:
installPkg(i)
addToUpdateScript(i)
else:
echo yn
quit(0)
else:
if yesToAll:
installPkg(p.val)
addToUpdateScript(p.val)
else:
echo fmt"would you like to install {p.val}? [y/n]: "
yn = stdin.readChar()
if yn == 'y':
installPkg(p.val)
addToUpdateScript(p.val)
else:
echo yn
quit(0)
of "u":
if not yesToAll:
echo "would you like to update? [y/n]: "
yn = stdin.readChar()
if yn == 'y':
for line in lines("to-update"):
installPkg(line)
else:
quit(0)
else:
for line in lines("to-update"):
installPkg(line)
of "h":
echo usageString
else:
errorPrompt("invalid option.")