This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
input.go
216 lines (161 loc) · 3.49 KB
/
input.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
package main
import (
"os"
"strings"
)
const version = "0.39-beta"
var (
purge bool = false
debug bool = false
assumeYes bool = false
force bool = false
noDeps bool = false
ignoreRoot bool = false
)
var optionToOthers, optionToOther bool = false, false
func checkFlag(flag string) {
switch flag {
case "-p", "--purge":
purge = true
case "-d", "--debug":
debug = true
case "-y", "--assumeyes":
assumeYes = true
case "-f", "--force":
force = true
case "-n", "--nodeps":
noDeps = true
case "-r", "--ignoreroot":
ignoreRoot = true
case "-help", "--help":
rawLog(helpMsg)
os.Exit(0)
case "-version", "--version":
rawLog(version)
os.Exit(0)
default:
errorLogRaw("Flag %s not found", bolden(flag))
os.Exit(1)
}
}
func checkCommand(other string, others []string, index int, args []string) {
checkForOptions := func(errSpecify string, commandPartsCount int) {
if len(others[index+commandPartsCount:]) < 1 {
errorLogRaw("No %s specified", errSpecify)
os.Exit(1)
}
}
switch other {
case "install":
checkForOptions("package names", 1)
optionToOthers = true
installPkgs(others[index+1:])
case "re-compile":
checkForOptions("package names", 1)
optionToOthers = true
reCompile(others[index+1:])
case "uninstall":
checkForOptions("package names", 1)
optionToOthers = true
uninstallPkgs(others[index+1:])
case "remove-data":
checkForOptions("package names", 1)
optionToOthers = true
rmData(others[index+1:])
case "upgrade":
if len(others) <= index+1 {
upgradeAllPackages()
} else {
optionToOthers = true
upgradePackage(others[index+1:])
}
case "update":
if len(others) <= index+1 {
updateAllPackages()
} else {
optionToOthers = true
updatePackage(others[index+1:])
}
case "info":
checkForOptions("package name", 1)
optionToOther = true
infoPkg(others[index+1])
case "list-all":
listAll()
case "env-add":
envAdd()
case "re-clone":
reClone()
case "version":
log(1, "Indiepkg Version %s", bolden(version))
case "raw-version":
rawLog(version)
case "help":
rawLog(helpMsg)
case "list":
listPkgs()
case "init":
initDirs(true)
case "repo":
checkForOptions("sub-command", 1)
switch others[index+1] {
case "add":
checkForOptions("url", 2)
addRepo(others[index+2])
case "remove":
checkForOptions("url", 2)
rmRepo(others[index+2])
case "list":
listRepos()
default:
errorLogRaw("Sub-command %s not found", bolden(others[index+1]))
os.Exit(1)
}
case "search":
checkForOptions("query", 1)
optionToOther = true
search(others[index+1])
case "indiepkg-update":
updateIndiePKG()
case "setup":
setup()
case "fetch":
fetch()
case "github-gen":
optionToOthers = true
checkForOptions("author", 1)
checkForOptions("repo", 2)
getRepoInfo(others[index+1], others[index+2])
case "help2man":
help2man()
default:
errorLogRaw("Command %s not found", bolden(other))
os.Exit(1)
}
}
func parseInput() {
args := os.Args[1:]
var (
flags []string
others []string
)
for _, arg := range args {
if strings.HasPrefix(arg, "-") {
flags = append(flags, arg)
} else {
others = append(others, arg)
}
}
for _, flag := range flags {
checkFlag(flag)
}
for i, other := range others {
if !optionToOthers && !optionToOther {
checkCommand(other, others, i, args)
}
optionToOther = true
}
if len(others) < 1 {
log(1, "Indiepkg Version %s, run %s for usage.", bolden(version), bolden("indiepkg help"))
}
}