/
ishell.go
325 lines (291 loc) · 7.33 KB
/
ishell.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
// Package ishell implements an interactive shell.
package ishell
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"sort"
"strings"
"sync"
"github.com/FlorianOtel/gonuageshell/Godeps/_workspace/src/github.com/flynn/go-shlex"
"github.com/FlorianOtel/gonuageshell/Godeps/_workspace/src/github.com/howeyc/gopass"
)
const (
defaultPrompt = ">> "
)
type Shell struct {
prompt string
showPrompt bool
functions map[string]CmdFunc
generic CmdFunc
reader *shellReader
writer io.Writer
active bool
activeMutex sync.RWMutex
ignoreCase bool
haltChan chan struct{}
}
// NewShell creates a new shell with default settings. Uses standard output and default prompt ">>".
func NewShell() *Shell {
shell := &Shell{
prompt: defaultPrompt,
showPrompt: true,
functions: make(map[string]CmdFunc),
reader: &shellReader{
scanner: bufio.NewReader(os.Stdin),
},
writer: os.Stdout,
haltChan: make(chan struct{}),
}
addDefaultFuncs(shell)
return shell
}
// Start starts the shell. It reads inputs from standard input and calls registered functions
// accordingly. This function blocks until the shell is stopped.
func (s *Shell) Start() {
s.start()
}
func (s *Shell) start() {
if s.Active() {
return
}
s.activeMutex.Lock()
s.active = true
s.activeMutex.Unlock()
shell:
for s.Active() {
var line string
var err error
read := make(chan struct{})
go func() {
line, err = s.readLine()
read <- struct{}{}
}()
select {
case <-read:
break
case <-s.haltChan:
continue shell
}
if err == io.EOF {
fmt.Println("EOF")
break
} else if err != nil {
s.Println("Error:", err)
break
}
if line == "" {
continue
}
line = strings.TrimSpace(line)
err = handleInput(s, line)
if err1, ok := err.(shellError); ok && err != nil {
switch err1.level {
case LevelWarn:
s.Println("Warning:", err)
continue shell
case LevelStop:
s.Println(err)
break shell
case LevelExit:
s.Println(err)
os.Exit(1)
case LevelPanic:
panic(err)
}
} else if !ok && err != nil {
s.Println("Error:", err)
}
}
}
// Active tells if the shell is active. i.e. Start is previously called.
func (s *Shell) Active() bool {
s.activeMutex.RLock()
defer s.activeMutex.RUnlock()
return s.active
}
func handleInput(s *Shell, line string) error {
handled, err := s.handleCommand(line)
if handled || err != nil {
return err
}
// Generic handler
if s.generic == nil {
return errNoHandler
}
output, err := s.generic(line)
if err != nil {
return err
}
if output != "" {
s.Println(output)
}
return nil
}
func (s *Shell) handleCommand(line string) (bool, error) {
str := strings.SplitN(line, " ", 2)
cmd := str[0]
if s.ignoreCase {
cmd = strings.ToLower(cmd)
}
var args []string
if _, ok := s.functions[cmd]; !ok {
return false, nil
}
if len(str) > 1 {
args1, err := shlex.Split(str[1])
if err != nil {
return false, err
}
args = args1
}
output, err := s.functions[cmd](args...)
if err != nil {
return true, err
}
if output != "" {
s.Println(output)
}
return true, nil
}
// Stop stops the shell. This will stop the shell from auto reading inputs and calling
// registered functions. A stopped shell is only inactive but totally functional.
// Its functions can still be called.
func (s *Shell) Stop() {
if !s.Active() {
return
}
s.activeMutex.Lock()
s.active = false
s.activeMutex.Unlock()
go func() {
s.haltChan <- struct{}{}
}()
}
// ReadLine reads a line from standard input.
func (s *Shell) ReadLine() string {
line, _ := s.readLine()
return line
}
func (s *Shell) readLine() (line string, err error) {
if s.showPrompt {
s.Print(s.prompt)
}
consumer := make(chan lineString)
s.reader.ReadLine(consumer)
ls := <-consumer
return ls.line, ls.err
}
// ReadMultiLinesFunc reads multiple lines from standard input. It passes each read line to
// f and stops reading when f returns false.
func (s *Shell) ReadMultiLinesFunc(f func(string) bool) string {
lines := bytes.NewBufferString("")
for {
line := s.ReadLine()
fmt.Fprint(lines, line)
if !f(line) {
break
}
fmt.Fprintln(lines)
}
return lines.String()
}
// ReadMultiLines reads multiple lines from standard input. It stops reading when terminator
// is encountered at the end of the line. It returns the lines read including terminator.
// For more control, use ReadMultiLinesFunc.
func (s *Shell) ReadMultiLines(terminator string) string {
return s.ReadMultiLinesFunc(func(line string) bool {
if strings.HasSuffix(strings.TrimSpace(line), terminator) {
return false
}
return true
})
}
// ReadPassword reads password from standard input without echoing the characters.
// If mask is true, each character will be represented with asterisks '*'. Note that
// this only works as expected when the standard input is a terminal.
func (s *Shell) ReadPassword(mask bool) string {
if s.showPrompt {
s.Print(s.prompt)
}
if mask {
return string(gopass.GetPasswdMasked())
}
return string(gopass.GetPasswd())
}
// Println prints to output and ends with newline character.
func (s *Shell) Println(val ...interface{}) {
fmt.Fprintln(s.writer, val...)
}
// Print prints to output.
func (s *Shell) Print(val ...interface{}) {
fmt.Fprint(s.writer, val...)
}
// Register registers a function for command. It overwrites existing function, if any.
func (s *Shell) Register(command string, function CmdFunc) {
s.functions[command] = function
}
// Unregister unregisters a function for a command
func (s *Shell) Unregister(command string) {
delete(s.functions, command)
}
// RegisterGeneric registers a generic function for all inputs.
// It is called if the shell input could not be handled by any of the
// registered functions. Unlike Register, the entire line is passed as
// first argument to CmdFunc.
func (s *Shell) RegisterGeneric(function CmdFunc) {
s.generic = function
}
// SetPrompt sets the prompt string. The string to be displayed before the cursor.
func (s *Shell) SetPrompt(prompt string) {
s.prompt = prompt
}
// ShowPrompt sets whether prompt should show when requesting input for ReadLine and ReadPassword.
// Defaults to true.
func (s *Shell) ShowPrompt(show bool) {
s.showPrompt = show
}
// SetOut sets the writer to write outputs to.
func (s *Shell) SetOut(writer io.Writer) {
s.writer = writer
}
// PrintCommands prints a space separated list of registered commands to the shell.
func (s *Shell) PrintCommands() {
out := strings.Join(s.Commands(), " ")
if out != "" {
s.Println("Commands:")
s.Println(out)
}
}
// Commands returns a sorted list of all registered commands.
func (s *Shell) Commands() []string {
var commands []string
for command := range s.functions {
commands = append(commands, command)
}
sort.Strings(commands)
return commands
}
// IgnoreCase specifies whether commands should not be case sensitive.
// Defaults to false i.e. commands are case sensitive.
// If true, commands must be registered in lower cases. e.g. shell.Register("cmd", ...)
func (s *Shell) IgnoreCase(ignore bool) {
s.ignoreCase = ignore
}
// ClearScreen clears the screen. Same behaviour as running 'clear' in unix terminal or 'cls' in windows cmd.
func (s *Shell) ClearScreen() error {
return clearScreen(s)
}
func clearScreen(s *Shell) error {
c := "clear"
if runtime.GOOS == "windows" {
c = "cls"
}
cmd := exec.Command(c)
cmd.Stdout = s.writer
return cmd.Run()
}