Skip to content

Commit ee350cd

Browse files
committed
Initial commit.
0 parents  commit ee350cd

File tree

7 files changed

+503
-0
lines changed

7 files changed

+503
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example/example

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Abiola Ibrahim
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# ishell
2+
ishell is an interactive shell library for creating interactive cli applications.
3+
4+
[![Documentation](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/abiosoft/ishell)
5+
6+
### Usage
7+
8+
```go
9+
import "github.com/abiosoft/ishell"
10+
11+
func main(){
12+
// create new shell.
13+
shell := ishell.NewShell()
14+
15+
// display welcome info.
16+
shell.Println("Sample Interactive Shell")
17+
18+
// register a function for "exit" command.
19+
shell.Register("exit", func(cmd string, args []string) (string, error) {
20+
shell.Stop()
21+
return "bye!", nil
22+
})
23+
24+
// start shell
25+
shell.Start()
26+
}
27+
```
28+
Execution
29+
```
30+
Sample Interactive Shell
31+
>> exit
32+
bye!
33+
```
34+
35+
#### Let's do more.
36+
```go
37+
// simulate an authentication
38+
shell.Register("login", func(cmd string, args []string) (string, error) {
39+
// get username
40+
shell.Println("Username:")
41+
username, _ := shell.ReadLine()
42+
43+
// get password. Does not echo characters.
44+
shell.Println("Password:")
45+
password := shell.ReadPassword(false)
46+
47+
... // do something with username and password
48+
49+
return "Authentication Successful.", nil
50+
})
51+
```
52+
Execution
53+
```
54+
Username:
55+
>> someusername
56+
Password:
57+
>>
58+
Authentication Successful.
59+
```
60+
Check example code for more.
61+
62+
### Note
63+
ishell is in active development and can still change significantly.
64+
65+
### Roadmap (in no particular order)
66+
* Support multiline inputs.
67+
* Handle ^C interrupts.
68+
* Support coloured outputs.
69+
* Testing, testing, testing.
70+
71+
### Contribution
72+
1. Create an issue to discuss it.
73+
2. Send in Pull Request.
74+
75+
### License
76+
MIT
77+
78+
### Credits
79+
* github.com/flynn/go-shlex for splitting input into command and args.
80+
* github.com/howeyc/gopass for reading passwords.

errors.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ishell
2+
3+
type ErrLevel int
4+
5+
const (
6+
LevelWarn ErrLevel = iota + 1
7+
LevelStop
8+
LevelExit
9+
LevelPanic
10+
)
11+
12+
var (
13+
errNoHandler = WarnErr("No handler registered for input.")
14+
)
15+
16+
// ShellError is an interractive shell error
17+
type shellError struct {
18+
err string
19+
level ErrLevel
20+
}
21+
22+
func (s shellError) Error() string {
23+
return s.err
24+
}
25+
26+
// NewErr creates a new error with specified level
27+
func NewErr(err string, level ErrLevel) error {
28+
return shellError{
29+
err: err,
30+
level: LevelWarn,
31+
}
32+
}
33+
34+
// WarnErr creates a Warn level error
35+
func WarnErr(err string) error {
36+
return shellError{
37+
err: err,
38+
level: LevelWarn,
39+
}
40+
}
41+
42+
// StopErr creates a Stop level error. Shell stops if encountered.
43+
func StopErr(err string) error {
44+
return shellError{
45+
err: err,
46+
level: LevelStop,
47+
}
48+
}
49+
50+
// ExitErr creates a Exit level error. Program terminates if encountered.
51+
func ExitErr(err string) error {
52+
return shellError{
53+
err: err,
54+
level: LevelExit,
55+
}
56+
}
57+
58+
// PanicErr creates a Panic level error. Program panics if encountered.
59+
func PanicErr(err string) error {
60+
return shellError{
61+
err: err,
62+
level: LevelPanic,
63+
}
64+
}

example/main.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
6+
"github.com/abiosoft/ishell"
7+
)
8+
9+
func main() {
10+
shell := ishell.NewShell()
11+
12+
// display info
13+
shell.Println("Sample Interactive Shell")
14+
15+
// handle exit
16+
shell.Register("exit", func(cmd string, args []string) (string, error) {
17+
shell.Println("Do you want to do more ? y/n:")
18+
line, _ := shell.ReadLine()
19+
if strings.ToLower(line) == "y" {
20+
doMore(shell)
21+
}
22+
shell.Stop()
23+
return "bye!", nil
24+
})
25+
26+
// start shell
27+
shell.Start()
28+
}
29+
30+
func doMore(shell *ishell.Shell) {
31+
shell.Stop()
32+
33+
// prompt for input
34+
shell.Println("Username:")
35+
username, _ := shell.ReadLine()
36+
shell.Println("Password:")
37+
password := shell.ReadPassword(false)
38+
39+
// do something with username and password
40+
shell.Println("Your inputs were", username, "and", password+".")
41+
42+
}

0 commit comments

Comments
 (0)