-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_functions.go
858 lines (690 loc) · 22.6 KB
/
shell_functions.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
package shellframework
import (
"bufio"
"fmt"
"strings"
//"github.com/eiannone/keyboard"
"github.com/eshu0/shellframework/interfaces"
"github.com/eshu0/simplelogger/interfaces"
)
//
// SHELL Printing
//
// these function provide printing to the Out
//
func (shell *Shell) Println(msg string) {
if !PointerInvalid(shell.out) {
shell.out.WriteString(msg + "\n")
}
}
func (shell *Shell) Printlnf(msg string, a ...interface{}) {
shell.Println(fmt.Sprintf(msg, a...))
}
func (shell *Shell) Printf(msg string, a ...interface{}) {
shell.Print(fmt.Sprintf(msg, a...))
}
func (shell *Shell) Print(msg string) {
if !PointerInvalid(shell.out) {
shell.out.WriteString(msg)
}
}
//
// SHELL Extra Print functions
//
func (shell *Shell) PrintDetails() {
shell.Println("*****************************")
shell.Printlnf("Framework Version: %s", shell.GetVersion())
session := shell.GetSession()
imeth := session.GetInteractiveMethod()
shell.Printlnf("Session: %s", session.ID())
shell.Printlnf("Iteractive: %t", imeth(session))
shell.Println("*****************************")
shell.Println("")
}
func (shell *Shell) PrintInputMessage() {
sess := shell.GetSession()
if !PointerInvalid(sess) {
shell.Printf("[%s]: ", sess.ID())
} else {
shell.Print("[Invalid session]: ")
}
}
//
// SHELL Processing
//
func (shell *Shell) ParseInput(input string) []sfinterfaces.ICommandInput {
var ecs []sfinterfaces.ICommandInput
ecs = []sfinterfaces.ICommandInput{}
log := *shell.GetLog()
log.LogDebugf("ParseInput()", "Parsing '%s' with length %d", input, len(input))
var ecsposition int
var commandfound bool
var commandfoundat int
//var pargs []string
commandfound = false
ecsposition = 0
commandfoundat = 0
var openqoute bool
var lastargat int
//var argstart int
//var argend int
openqoute = false
lastargat = 0
//argstart = 0
//argend = 0
var rawpos int
rawpos = 0
textr := []rune(input)
for pos, char := range textr {
if pos == 0 && char == '#' {
log.LogDebug("ParseInput()", "Comment found at the beggining this whole input is a comment finish parsing ")
break
} else {
// first position let's create an command input
if pos == 0 {
ecsposition = 0
commandfound = false
commandfoundat = 0
ci := CommandInput{}
ecs = append(ecs, &ci)
log.LogDebug("ParseInput()", "appended first command")
}
//shell.LogPrintlnf("character %c at position %d", char, pos)
if char == '#' {
log.LogDebugf("ParseInput()", "'%c' - Comment indentifier found at '%d' parsing finished", char, pos)
break
}
// run out of string
if len(input)-1 == pos {
log.LogDebug("ParseInput()", "run out of string to parse")
if !commandfound {
cmndname := string(textr[commandfoundat : pos+1])
log.LogDebugf("ParseInput()", "Parsed command '%s' from '%s'", cmndname, input)
ecs[ecsposition].SetCommandName(cmndname)
} else {
s := lastargat
e := pos
if textr[lastargat] == '"' {
s = s + 1
}
if textr[pos] == '"' {
e = e - 1
}
arg := string(textr[s : e+1])
log.LogDebugf("ParseInput()", "Found argument terminator: argument read: %s", arg)
pargs := ecs[ecsposition].GetArgs()
pargs = append(pargs, arg)
ecs[ecsposition].SetArgs(pargs)
}
rawi := string(textr[rawpos : pos+1])
log.LogDebugf("ParseInput()", "Parsed rawinput '%s' from '%s'", rawi, input)
ecs[ecsposition].SetRawInput(rawi)
break
} else {
// we are looking for a command
if !commandfound {
if char == ' ' {
log.LogDebugf("ParseInput()", "Final character %c at position %d is end of command", char, pos)
cmndname := string(textr[commandfoundat : pos+1])
log.LogDebugf("ParseInput()", "Parsed command '%s' from '%s'", cmndname, input)
ecs[ecsposition].SetCommandName(cmndname)
commandfoundat = pos
commandfound = true
lastargat = pos + 1
} else {
// let's keep looking
continue
}
} else { // we are parsing arguements
if char == '"' {
if openqoute {
log.LogDebugf("ParseInput()", "Open qoute found at '%d' this is closing", pos)
openqoute = false
} else {
log.LogDebugf("ParseInput()", "Open qoute found at '%d' this is opening", pos)
openqoute = true
}
continue
}
// we keep going till it is closed
if !openqoute {
if char == ' ' {
s := lastargat
e := pos
log.LogDebugf("ParseInput()", "e = %d", e)
if textr[lastargat] == '"' {
s = s + 1
}
log.LogDebugf("ParseInput()", "textr[e] = %s", string(textr[e]))
if textr[e-1] == '"' {
log.LogDebugf("ParseInput()", "minus e = %d", e)
e = e - 1
}
log.LogDebugf("ParseInput()", "e = %d", e)
arg := string(textr[s:e])
log.LogDebugf("ParseInput()", "Found argument terminator: argument read: %s", arg)
pargs := ecs[ecsposition].GetArgs()
pargs = append(pargs, arg)
ecs[ecsposition].SetArgs(pargs)
lastargat = pos + 1
}
if char == '|' {
log.LogDebugf("ParseInput()", "Pipe found at '%d' - new command input created", pos)
log.LogDebugf("ParseInput()", "Append %s ", ecs[ecsposition].GetCommandName())
ci := CommandInput{}
ecs = append(ecs, &ci)
ecsposition++
commandfound = false
rawi := string(textr[rawpos : pos+1])
log.LogDebugf("ParseInput()", "Parsed rawinput '%s' from '%s'", rawi, input)
ecs[ecsposition].SetRawInput(rawi)
//this is zero on the first run so we need it to be past the pipe
commandfoundat = pos + 1 // pos is the pipe command is at the next item
lastargat = pos + 1
rawpos = pos + 1
log.LogDebugf("ParseInput()", "Created new command and incremented to %d ", ecsposition)
}
}
}
}
}
}
log.LogDebug("ParseInput()", "following command input parsed and will be executed in order 0> ")
for epos, cmdi := range ecs {
log.LogDebugf("ParseInput()", "%d - Command: %s", epos, cmdi.GetCommandName())
log.LogDebugf("ParseInput()", "%d - Raw Input: %s", epos, cmdi.GetRawInput())
log.LogDebugf("ParseInput()", "%d - Input with out name: %s", epos, cmdi.GetInputWithOutCommand())
for apos, arg := range cmdi.GetArgs() {
log.LogDebugf("ParseInput()", "Args[%d]: %s", apos, arg)
}
}
/*
shell.LogPrintlnf("ParseInput(): Splitting '%s' by the pipe |", input)
commands := strings.Split(input, "|")
shell.LogPrintlnf("ParseInput(): Found '%d' commands ", len(commands))
//args := strings.Split(text, " ")
//shouldcontinue = cmd.Process(args[1:])
for _, text := range commands {
commentindex := strings.Index(text, "#")
if commentindex > -1 {
shell.LogPrintlnf("ParseInput(): Comment found at '%d' stripping after this ", commentindex)
shell.LogPrintlnf("ParseInput(): string before parsing was %s ", text)
textr := []rune(text)
text = string(textr[:commentindex])
shell.LogPrintlnf("ParseInput(): string after parsing was %s ", text)
}
// we have removed the comment
// if the whole line was a comment we can ignore it
if text == "" {
shell.LogPrintln("ParseInput(): After removing comment line was empty - skipping ")
} else {
// filter out any silly caps lock mistakes
lowerinput := strings.ToLower(text)
// not sure this is the best thing to do
// this could be made more comperhensive
shell.LogPrintlnf("ParseInput(): Command '%s' matched '%s'", cmd.GetName(), lowercmd)
runes := []rune(text)
commandlength := len(cmd.GetName())
shell.LogPrintlnf("ParseInput(): lowercmd length: %d ", commandlength)
withoutcommand := string(runes[commandlength:])
shell.LogPrintlnf("ParseInput(): without command %s", withoutcommand)
withoutcommand = strings.TrimPrefix(withoutcommand, " ")
}
}
/*
if i > -1 {
chars := x[:i]
arefun := x[i+1:]
fmt.Println(chars)
fmt.Println(arefun)
} else {
fmt.Println("Index not found")
fmt.Println(x)
}
*/
return ecs
}
func (shell *Shell) Run() {
// grab the environment
env := shell.GetEnvironment()
// pointer is valid?
if !PointerInvalid(env) {
env.LoadFile(sfinterfaces.EnvironmentFilename)
}
log := *shell.GetLog()
session := shell.GetSession()
// first call
session.CallBuildIDMethod(shell)
var interactiveMethod func(ss sfinterfaces.ISession) bool
interactiveMethod = session.GetInteractiveMethod()
if interactiveMethod(session) {
shell.PrintDetails()
log.LogDebug("Run()", "Interactive Session")
shell.InteractiveSession(env, log)
} else {
log.LogDebug("Run()", "Non-Interactive Session")
shell.NonInteractiveSession(env, log)
}
}
func (shell *Shell) NonInteractiveSession(env sfinterfaces.IEnvironment, log slinterfaces.ISimpleLogger) {
shouldcontinue := true
reader := bufio.NewReader(shell.in)
for {
// this keeps updating so let's keep it syncd
env = shell.GetEnvironment()
// get the session and build the ID
// it could be out of sync
session := shell.GetSession()
session.CallBuildIDMethod(shell)
// pointer is valid?
if !PointerInvalid(reader) {
// read the string input
text, readerr := reader.ReadString('\n')
if readerr != nil {
log.LogDebugf("NonInteractiveSession()", "Reading input has provided following err '%s'", readerr.Error())
break
// break out for loop
}
// convert CRLF to LF
text = strings.Replace(text, "\n", "", -1)
// pointer is valid?
if !PointerInvalid(env) && text != "" && (len(text) > 0 && text[0] != '#') {
env.AddStringValue(sfinterfaces.LastCommands, text)
/*
envvar, exists := env.GetVariable(sfinterfaces.LastCommands)
if !exists {
var cmds []string
cmds = append(cmds, text)
env.SetVariable(env.MakeMultiVariable(sfinterfaces.LastCommands, cmds))
} else {
wc := envvar
lc := wc.GetValues()
lc = append(lc, text)
wc.SetValues(lc)
env.SetVariable(wc)
}
*/
}
executionorder := shell.ParseInput(text)
var cmdres string
endexecution := false
for _, ec := range executionorder {
log.LogDebugf("NonInteractiveSession()", "Found '%s' execution command ", ec.GetCommandName())
if endexecution {
break
}
if cmdres != "" {
log.LogDebugf("NonInteractiveSession()", "Previous command finished with result %s override the args", cmdres)
var pargs []string
pargs = append(pargs, cmdres)
ec.SetArgs(pargs)
}
// walk commands in shell
for _, cmd := range shell.GetCommands() {
// This command matched
if cmd.Match(ec) {
// not sure this is the best thing to do
// this could be made more comperhensive
// we set this here so prasing doesn;t affect the input
cmd.SetCommandInput(ec)
res := cmd.Process()
if res.ExitShell() {
shouldcontinue = false
} else {
if res.Sucessful() {
cmdres = res.Result()
} else {
err := res.Err()
if err != nil {
shell.Printlnf("'%s' failed: %s ", cmd.GetName(), err.Error())
log.LogDebugf("NonInteractiveSession()", "Error with command '%s' following error provided: %s ", cmd.GetName(), err.Error())
} else {
shell.Printlnf("Error with command '%s' no error provided ", cmd.GetName())
log.LogDebugf("NonInteractiveSession()", "Error with command '%s' no error provided ", cmd.GetName())
}
endexecution = true
}
}
break
}
}
}
if !shouldcontinue {
shell.Println("Exiting")
log.LogDebug("NonInteractiveSession()", "Exiting")
break
}
} else {
log.LogDebug("NonInteractiveSession()", "Reader is nil")
shouldcontinue = false
}
if !PointerInvalid(env) {
env.SaveToFile(sfinterfaces.EnvironmentFilename)
}
} // for loop
}
func (shell *Shell) InteractiveSession(env sfinterfaces.IEnvironment, log slinterfaces.ISimpleLogger) {
shouldcontinue := true
reader := bufio.NewReader(shell.in)
for {
// this keeps updating so let's keep it syncd
env = shell.GetEnvironment()
// get the session and build the ID
// it could be out of sync
session := shell.GetSession()
session.CallBuildIDMethod(shell)
// print the input message
shell.PrintInputMessage()
// pointer is valid?
if !PointerInvalid(reader) {
// read the string input
text, readerr := reader.ReadString('\n')
if readerr != nil {
log.LogDebugf("InteractiveSession()", "Reading input has provided following err '%s'", readerr.Error())
break
// break out for loop
}
// convert CRLF to LF
text = strings.Replace(text, "\n", "", -1)
// pointer is valid?
if !PointerInvalid(env) && text != "" && (len(text) > 0 && text[0] != '#') {
env.AddStringValue(sfinterfaces.LastCommands, text)
/*
envvar, exists := env.GetVariable(sfinterfaces.LastCommands)
if !exists {
var cmds []string
cmds = append(cmds, text)
env.SetVariable(env.MakeMultiVariable(sfinterfaces.LastCommands, cmds))
} else {
wc := envvar
lc := wc.GetValues()
lc = append(lc, text)
wc.SetValues(lc)
env.SetVariable(wc)
}
*/
}
executionorder := shell.ParseInput(text)
var cmdres string
endexecution := false
for _, ec := range executionorder {
log.LogDebugf("InteractiveSession()", "Found '%s' execution command ", ec.GetCommandName())
if endexecution {
break
}
if cmdres != "" {
log.LogDebugf("InteractiveSession()", "Previous command finished with result %s override the args", cmdres)
var pargs []string
pargs = append(pargs, cmdres)
ec.SetArgs(pargs)
}
// walk commands in shell
for _, cmd := range shell.GetCommands() {
// This command matched
if cmd.Match(ec) {
// not sure this is the best thing to do
// this could be made more comperhensive
// we set this here so prasing doesn;t affect the input
cmd.SetCommandInput(ec)
res := cmd.Process()
if res.ExitShell() {
shouldcontinue = false
} else {
if res.Sucessful() {
cmdres = res.Result()
} else {
err := res.Err()
if err != nil {
shell.Printlnf("'%s' failed: %s ", cmd.GetName(), err.Error())
log.LogDebugf("InteractiveSession()", "Error with command '%s' following error provided: %s ", cmd.GetName(), err.Error())
} else {
shell.Printlnf("Error with command '%s' no error provided ", cmd.GetName())
log.LogDebugf("InteractiveSession()", "Error with command '%s' no error provided ", cmd.GetName())
}
endexecution = true
}
}
break
}
}
}
if !shouldcontinue {
shell.Println("Exiting")
log.LogDebug("InteractiveSession()", "Exiting")
break
}
} else {
log.LogDebug("InteractiveSession()", "Reader is nil")
shouldcontinue = false
}
if !PointerInvalid(env) {
env.SaveToFile(sfinterfaces.EnvironmentFilename)
}
} // for loop
}
/*
func (shell *Shell) InteractiveSession(env sfinterfaces.IEnvironment, log sfinterfaces.IShellLogger) {
lastcommandpos := 0
shouldcontinue := true
for {
// this keeps updating so let's keep it syncd
env = shell.GetEnvironment()
// print the input message
shell.PrintInputMessage()
kerr := keyboard.Open()
if kerr != nil {
shell.Println("Opening Keyboard caused an error")
log.LogDebugf("InteractiveSession()", "Opening Keyboard: %s", kerr.Error())
return
}
defer keyboard.Close()
text := ""
for {
char, key, err := keyboard.GetKey() //GetSingleKey()
if err != nil {
shell.Println("Getting Keyboard key caused an error")
log.LogDebugf("InteractiveSession()", "GetSingleKey: %s", err.Error())
return
} else {
log.LogDebugf("InteractiveSession()", "key: %d char %d", key, char)
if(key != 0){
switch(key){
case keyboard.KeyArrowUp {
envvar, exists := env.GetVariable(sfinterfaces.LastCommands)
if exists {
wc := envvar
lc := wc.GetValues()
if lastcommandpos >= len(lc)-1 {
shell.PrintInputMessage()
shell.Printf("%s", lc[lastcommandpos])
lastcommandpos = 0
} else {
shell.PrintInputMessage()
shell.Printf(" %s", lc[lastcommandpos])
lastcommandpos = lastcommandpos + 1
}
}
break
}
case keyboard.KeyArrowDown {
envvar, exists := env.GetVariable(sfinterfaces.LastCommands)
if exists {
wc := envvar
lc := wc.GetValues()
if lastcommandpos >= len(lc)-1 {
shell.PrintInputMessage()
shell.Printf("%s", lc[lastcommandpos])
lastcommandpos = 0
} else {
shell.PrintInputMessage()
shell.Printf("%s", lc[lastcommandpos])
lastcommandpos = lastcommandpos - 1
}
}
break
}
case keyboard.KeyEsc {
shell.Println("Exiting")
log.LogDebug("InteractiveSession()", "Exiting")
return
}
case keyboard.KeyEsc {
shell.Print("\n")
break
}
}
} else {
shell.Print(string(char))
text = text + string(char)
}
}
}
env.AddStringValue(sfinterfaces.LastCommands, text)
executionorder := shell.ParseInput(text)
var cmdres string
endexecution := false
for _, ec := range executionorder {
log.LogDebugf("InteractiveSession()", "Found '%s' execution command ", ec.GetCommandName())
if endexecution {
break
}
if cmdres != "" {
log.LogDebugf("InteractiveSession()", "Previous command finished with result %s override the args", cmdres)
var pargs []string
pargs = append(pargs, cmdres)
ec.SetArgs(pargs)
}
// walk commands in shell
for _, cmd := range shell.GetCommands() {
// This command matched
if cmd.Match(ec) {
// not sure this is the best thing to do
// this could be made more comperhensive
// we set this here so prasing doesn;t affect the input
log.LogDebugf("InteractiveSession()", "Started SetCommandInput for '%s' ", cmd.GetName())
cmd.SetCommandInput(ec)
log.LogDebugf("InteractiveSession()", "Finished SetCommandInput for '%s' ", cmd.GetName())
log.LogDebugf("InteractiveSession()", "Started command '%s' ", cmd.GetName())
res := cmd.Process()
log.LogDebugf("InteractiveSession()", "Finished command '%s' ", cmd.GetName())
if res.ExitShell() {
shouldcontinue = false
} else {
if res.Sucessful() {
log.LogDebugf("InteractiveSession()", "Command '%s' was sucessful ", cmd.GetName())
cmdres = res.Result()
} else {
err := res.Err()
if err != nil {
shell.Printlnf("'%s' failed: %s ", cmd.GetName(), err.Error())
log.LogDebugf("InteractiveSession()", "Error with command '%s' following error provided: %s ", cmd.GetName(), err.Error())
} else {
shell.Printlnf("Error with command '%s' no error provided ", cmd.GetName())
log.LogDebugf("InteractiveSession()", "Error with command '%s' no error provided ", cmd.GetName())
}
endexecution = true
}
}
break
}
}
}
if !shouldcontinue {
shell.Println("Exiting")
log.LogDebug("Run()", "Exiting")
break
}
if !PointerInvalid(env) {
env.SaveToFile(sfinterfaces.EnvironmentFilename)
}
} // for loop
}
*/
//
// Commands adding etc
//
func (shell *Shell) NewCommand(name string, description string, operator func(command sfinterfaces.ICommand) sfinterfaces.ICommandResult, flags []sfinterfaces.IFlag) sfinterfaces.ICommand {
sc := &Command{}
sc.name = name
sc.operator = operator //
sc.description = description
sc.shell = shell
flgs := &CommandFlags{}
flgs.SetCommand(sc)
flgs.SetFlags(flags)
sc.SetCommandFlags(flgs)
//sc.flags = flags
return sc
}
func (shell *Shell) AddCommand(cmd sfinterfaces.ICommand) {
// append the command to the shell
shell.commands = append(shell.commands, cmd)
}
//Adds a Simple Command to the Shell
func (shell *Shell) AddCommands(commands []sfinterfaces.ICommand) {
// walk thoguh the commands passed in
for _, cmd := range commands {
// make sure this pointer is valid
if !PointerInvalid(cmd) {
// use this method to add the simple command
shell.AddCommand(cmd)
}
}
}
func (shell *Shell) RegisterNewCommandWithFlags(name string, description string, operator func(command sfinterfaces.ICommand) sfinterfaces.ICommandResult, flags []sfinterfaces.IFlag) {
shell.AddCommand(shell.NewCommand(name, description, operator, flags))
}
func (shell *Shell) RegisterNewCommand(name string, description string, operator func(command sfinterfaces.ICommand) sfinterfaces.ICommandResult) {
flags := []sfinterfaces.IFlag{}
shell.AddCommand(shell.NewCommand(name, description, operator, flags))
}
func (shell *Shell) RegisterCommandNewBoolFlag(cmd string, name string, defaultvalue bool, usage string) {
sf := &CommandFlag{}
sf.name = name
sf.defaultbvalue = defaultvalue
sf.usage = usage
sf.flagtype = 2
shell.RegisterCommandFlag(cmd, sf)
}
func (shell *Shell) RegisterCommandNewIntFlag(cmd string, name string, defaultvalue int, usage string) {
sf := &CommandFlag{}
sf.name = name
sf.defaultivalue = defaultvalue
sf.usage = usage
sf.flagtype = 3
shell.RegisterCommandFlag(cmd, sf)
}
func (shell *Shell) RegisterCommandNewStringFlag(cmd string, name string, defaultvalue string, usage string) {
sf := &CommandFlag{}
sf.name = name
sf.defaultsvalue = defaultvalue
sf.usage = usage
sf.flagtype = 1
shell.RegisterCommandFlag(cmd, sf)
}
func (shell *Shell) RegisterCommandFlag(cmd string, flag sfinterfaces.IFlag) {
log := *shell.GetLog()
for i, _ := range shell.commands {
// make sure this pointer is valid
if strings.ToLower(shell.commands[i].GetName()) == strings.ToLower(cmd) {
log.LogDebugf("RegisterCommandFlag()", "'%s' macthed '%s'", shell.commands[i].GetName(), cmd)
flgsbefore := shell.commands[i].GetCommandFlags().GetFlags()
for p, flg := range flgsbefore {
log.LogDebugf("RegisterCommandFlag()", "flgsbefore - commands[%d][%d] has '%s' set to type %d", i, p, flg.GetName(), flg.GetFlagType())
}
// get the iflags from the command
flgs := shell.commands[i].GetCommandFlags()
// now get the underlying array list
flags := flgs.GetFlags()
flags = append(flags, flag)
flgs.SetFlags(flags)
log.LogDebugf("RegisterCommandFlag()", "commands[%d] had it's flags set", i)
shell.commands[i].SetCommandFlags(flgs)
flgsafter := shell.commands[i].GetCommandFlags().GetFlags()
for j, flg := range flgsafter {
log.LogDebugf("RegisterCommandFlag()", "flgsafter - commands[%d][%d] has '%s' set to type %d", i, j, flg.GetName(), flg.GetFlagType())
}
return
}
}
}