-
Notifications
You must be signed in to change notification settings - Fork 3
/
menu.go
executable file
·343 lines (279 loc) · 6.65 KB
/
menu.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package pitchfork
import (
"bufio"
"encoding/csv"
"errors"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
)
/* Mutex needed to only allow one batch script at a time */
var batchmutex = &sync.Mutex{}
const ERR_UNKNOWN_CMDPFX = "Unknown command: "
type PfFunc func(ctx PfCtx, args []string) (err error)
type PfMEntry struct {
Cmd string
Fun PfFunc
Args_min int
Args_max int
Args []string
Perms Perm
Desc string
}
type PfMenu struct {
M []PfMEntry
}
func NewPfMenu(m []PfMEntry) PfMenu {
return PfMenu{M: m}
}
func NewPfMEntry(Cmd string, Fun PfFunc, Args_min int, Args_max int, Args []string, Perms Perm, Desc string) PfMEntry {
return PfMEntry{Cmd, Fun, Args_min, Args_max, Args, Perms, Desc}
}
func (menu *PfMenu) Add(m ...PfMEntry) {
menu.M = append(menu.M, m...)
}
func (menu *PfMenu) Replace(cmd string, fun PfFunc) {
for i, m := range menu.M {
if m.Cmd == cmd {
menu.M[i].Fun = fun
return
}
}
}
func (menu *PfMenu) Remove(cmd string) {
for i, m := range menu.M {
if cmd == m.Cmd {
menu.M = append(menu.M[:i], menu.M[i+1:]...)
return
}
}
}
/* Or new permissions into it, useful to mark a menu item hidden */
func (menu *PfMenu) AddPerms(cmd string, perms Perm) {
for i, m := range menu.M {
if cmd == m.Cmd {
menu.M[i].Perms |= perms
return
}
}
}
func (menu *PfMenu) DelPerms(cmd string, perms Perm) {
for i, m := range menu.M {
if cmd == m.Cmd {
menu.M[i].Perms &^= perms
return
}
}
}
func (menu *PfMenu) SetPerms(cmd string, perms Perm) {
for i, m := range menu.M {
if cmd == m.Cmd {
menu.M[i].Perms = perms
return
}
}
}
func (ctx *PfCtxS) Menu(args []string, menu PfMenu) (err error) {
err = nil
ok := false
arg := "help"
ctx.MenuOverride(&menu)
if len(args) > 0 && args[0] != "" {
arg = strings.ToLower(args[0])
}
if arg == "help" {
/* Walk only, thus don't show help */
if ctx.menu_walkonly {
err = errors.New("help not allowed during menuwalk")
return
}
if ctx.loc == "" {
ctx.OutLn(AppName + " Help")
} else {
ctx.OutLn(AppName + " Help for: \"" + ctx.loc + "\"")
}
if ctx.IsLoggedIn() {
ss := ""
if ctx.TheUser().IsSysAdmin() {
ss = " [sysadmin]"
} else if ctx.TheUser().CanBeSysAdmin() {
ss = " [NOT sysadmin]"
}
ctx.OutLn("User: %s%s", ctx.TheUser().GetUserName(), ss)
} else {
ctx.OutLn("User: [Not authenticated]")
}
ctx.OutLn("")
/* Special introdoctcuary header at the top menu */
if ctx.loc == "" {
ctx.Out("" +
"Welcome to the " + AppName + " menu system which is command line interface (CLI) based.\n" +
"Note that when a command is not in the help menu the selected user might not have permissions for it.\n" +
"\n" +
"Each section, items marked [SUB], has its own 'help' command.\n" +
"\n" +
"The following commands are available on the root level:\n")
}
for _, m := range menu.M {
opts := ""
/* Skip menu items that are not allowed */
ok, _ = ctx.CheckPerms("Menu("+m.Cmd+")/help", m.Perms)
if !ok {
continue
}
if m.Args != nil {
for o := range m.Args {
opt := strings.Split(m.Args[o], "#")
opts += "<" + opt[0] + "> "
}
opts = strings.TrimSpace(opts)
} else if m.Args_max == -1 {
opts = "[SUB]"
}
ctx.OutLn(" %-20s %-20s %-20s", m.Cmd, opts, m.Desc)
}
return
}
for _, m := range menu.M {
if m.Cmd != arg {
continue
}
nargs := args[1:]
if ctx.loc != "" {
ctx.loc += " "
}
ctx.loc += arg
_, err = ctx.CheckPerms("Menu("+m.Cmd+")", m.Perms)
if err != nil {
user := "<<notloggedin>>"
if ctx.IsLoggedIn() {
user = ctx.TheUser().GetUserName()
}
ctx.Log("User " + user + " tried access to command '" + ctx.loc + "': " + err.Error())
ctx.SetStatus(StatusUnauthorized)
return
}
/* Walk Only & command & return the menu? */
if m.Args != nil && ctx.menu_walkonly {
ctx.menu_menu = &m
return
}
if m.Args_min > len(nargs) {
err = errors.New("Not enough arguments for '" + ctx.loc + "' (got " + strconv.Itoa(len(nargs)) + ", need at least " + strconv.Itoa(m.Args_min) + ")")
return
}
if m.Args_max != -1 {
if len(nargs) > m.Args_max {
err = errors.New("Too many arguments for '" + ctx.loc + "' (got " + strconv.Itoa(len(nargs)) + ", but want a maximum of " + strconv.Itoa(m.Args_min) + ")")
return
}
}
/* Execute the menu */
err = m.Fun(ctx, nargs)
return
}
msg := ERR_UNKNOWN_CMDPFX
if ctx.loc != "" {
msg += ctx.loc + " "
}
msg += arg
err = errors.New(msg)
return
}
func ErrIsUnknownCommand(err error) bool {
s := err.Error()
sl := len(s)
el := len(ERR_UNKNOWN_CMDPFX)
return sl > el && s[:el] == ERR_UNKNOWN_CMDPFX
}
func (ctx *PfCtxS) Cmd(args []string) (err error) {
ctx.loc = ""
return ctx.Menu(args, MainMenu)
}
func (ctx *PfCtxS) CmdOut(cmd string, args []string) (msg string, err error) {
cmds := []string{}
if cmd != "" {
cmds = strings.Split(cmd, " ")
cmds = append(cmds, args...)
} else {
cmds = args
}
err = ctx.Cmd(cmds)
msg = ctx.Buffered()
return
}
func (ctx *PfCtxS) Batch(filename string) (err error) {
/* Only allow one batch at a time */
batchmutex.Lock()
defer batchmutex.Unlock()
/* Restrict to only .cli files */
lf := len(filename)
if lf <= 4 || filename[lf-4:] != ".cli" {
err = errors.New("Not a .cli batch file")
return
}
/* Open the batch file */
ctx.OutLn("Opening batch file: " + filename)
f, err := os.Open(filename)
if err != nil {
return
}
/* Remember the current working directory */
oldwd, err := os.Getwd()
if err != nil {
return
}
/* And return to it when we exit this */
defer os.Chdir(oldwd)
/* If we didn't do this, things would be true magic */
/*
* Change location to the directory of the file
* This helps with relative paths inside the batch files
*/
dir := filepath.Dir(filename)
ctx.OutLn("Changing work-directory to " + dir)
err = os.Chdir(dir)
if err != nil {
return
}
r := csv.NewReader(bufio.NewReader(f))
/* Space is the separator */
r.Comma = ' '
/* Ignore Comments */
r.Comment = '#'
/* Allow any number of records */
r.FieldsPerRecord = -1
var line []string
for {
line, err = r.Read()
if err == io.EOF {
err = nil
break
}
if err != nil {
err = errors.New("Problem while reading CSV command file: " + err.Error())
break
}
ctx.OutLn("Command: %#q", line)
/* Skip empty lines */
if len(line) == 0 {
continue
}
err = ctx.Cmd(line)
if err != nil {
break
}
}
ctx.OutLn("Batch processing done")
return
}
func (ctx *PfCtxS) WalkMenu(args []string) (menu *PfMEntry, err error) {
ctx.menu_menu = nil
ctx.menu_walkonly = true
err = ctx.Cmd(args)
ctx.menu_walkonly = false
return ctx.menu_menu, err
}