-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.go
More file actions
177 lines (158 loc) · 3.49 KB
/
Copy pathrunner.go
File metadata and controls
177 lines (158 loc) · 3.49 KB
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
package runner
import (
"bufio"
"errors"
"os"
"reflect"
"strconv"
"unicode"
)
type Shell struct {
Commands map[string]Command
}
type Command struct {
Cmd string
Callback Callback
Helptext string
}
var prompt string = ":|: "
// dangerous...
type Callback interface{}
func (s *Shell) Add_command(cmds ...Command) {
for _, c := range cmds {
if _, ok := s.Commands[c.Cmd]; ok {
panic("Dupplicate commands not supported")
}
s.Commands[c.Cmd] = c
}
}
func (s *Shell) Flags() {
if len(os.Args) == 1 {
println("Must specify command in Flag mode")
s.print_help()
return
}
cmd, ok := s.Commands[os.Args[1]]
if !ok {
println(os.Args[1], "Command not found")
return
}
if len(os.Args) > 2 {
s.call_command(cmd, os.Args[2:])
} else {
s.call_command(cmd, nil)
}
}
func (s *Shell) Start() {
println("Entering Runner")
s.Add_command(Command{"exit", func() { os.Exit(0) }, "exit runner"})
reader := bufio.NewReader(os.Stdin)
var history []string // Not yet implimented
for {
print(prompt)
// readsting error connot occur for my use case
text, _ := reader.ReadString('\n')
history = append(history, text)
tokens := parseLine(text)
if len(tokens) == 0 {
s.print_help()
continue
}
cmd, ok := s.Commands[tokens[0]]
if !ok {
println(tokens[0], "Command not found")
continue
}
s.call_command(cmd, tokens[1:])
}
}
func (s *Shell) call_command(cmd Command, strargs []string) {
// Scary
f := reflect.TypeOf(cmd.Callback)
method := reflect.ValueOf(cmd.Callback)
args := make([]reflect.Value, f.NumIn())
if len(strargs) < f.NumIn() {
println("Error calling, invalid paramaters", cmd.Cmd)
return
}
for i := 0; i < f.NumIn(); i++ {
t := f.In(i)
v, err := convert_types(t, strargs[i])
if err != nil {
println(err.Error())
continue
}
args[i] = v
}
method.Call(args)
}
func convert_types(to reflect.Type, arg string) (reflect.Value, error) {
switch to.Kind().String() {
case "bool", "Bool":
arg, err := strconv.ParseBool(arg)
if err != nil {
goto Err
}
return reflect.ValueOf(arg).Convert(to), nil
case "int", "Int", "Int8", "Int16", "Int32", "Int64":
arg, err := strconv.ParseInt(arg, 0, 0) // need to change to size
if err != nil {
goto Err
}
// needs to be changed
return reflect.ValueOf(int(arg)), nil
case "uint", "Uint", "Uint8", "Uint16", "Uint32", "Uint64":
arg, err := strconv.ParseUint(arg, 0, 0)
if err != nil {
goto Err
}
return reflect.ValueOf(arg).Convert(to), nil
case "float", "Float32", "Float64":
arg, err := strconv.ParseFloat(arg, 0)
if err != nil {
goto Err
}
return reflect.ValueOf(arg).Convert(to), nil
default:
// panics on failure, probably not good
return reflect.ValueOf(arg).Convert(to), nil
}
Err:
return reflect.Value{}, errors.New("Connot convert type to argument")
}
func (s *Shell) print_help() {
for _, command := range s.Commands {
println(command.Cmd+":", command.Helptext)
}
}
func NewShell() Shell {
m := make(map[string]Command)
s := Shell{m}
s.Add_command(
Command{"help", s.print_help, "print list of commands"})
return s
}
func parseLine(str string) []string {
parsed := make([]string, 0)
token := ""
quoted := false
for _, char := range str {
if char == '"' {
if quoted {
parsed = append(parsed, token)
} else {
quoted = true
continue
}
}
if unicode.IsSpace(rune(char)) {
if !quoted && token != "" {
parsed = append(parsed, token)
token = ""
continue
}
}
token = token + string(char)
}
return parsed
}