[WIP] Initial commit
This commit is contained in:
commit
549f752b6f
6 changed files with 156 additions and 0 deletions
28
internal/cmd/cmd.go
Normal file
28
internal/cmd/cmd.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var CommandList = map[string]Command{
|
||||
"test": Test,
|
||||
}
|
||||
|
||||
type Command struct {
|
||||
Name string
|
||||
Usage string
|
||||
Description string
|
||||
Action func([]string)
|
||||
MinArg int
|
||||
MaxArg int
|
||||
}
|
||||
|
||||
func Usage(cmd *Command) {
|
||||
fmt.Println("Usage: ", cmd.Usage)
|
||||
}
|
||||
|
||||
func Execute(cmd *Command, line string) {
|
||||
arg := strings.Split(line, " ")[1:]
|
||||
cmd.Action(arg)
|
||||
}
|
16
internal/cmd/test.go
Normal file
16
internal/cmd/test.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var Test = Command {
|
||||
Name: "test",
|
||||
Usage: "TEST",
|
||||
Description: "Test",
|
||||
Action: Testfunc,
|
||||
}
|
||||
|
||||
func Testfunc(param []string) {
|
||||
fmt.Printf("test %s\n", param)
|
||||
}
|
10
internal/setting/setting.go
Normal file
10
internal/setting/setting.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package setting
|
||||
|
||||
type Status struct {
|
||||
Key string
|
||||
Curr_user string
|
||||
Last_ret int
|
||||
Last_id int
|
||||
Auth bool
|
||||
Connection bool
|
||||
}
|
20
internal/shell/completer.go
Normal file
20
internal/shell/completer.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package shell
|
||||
|
||||
import (
|
||||
"github.com/chzyer/readline"
|
||||
"gitlab.com/neonsea/iopshell/internal/cmd"
|
||||
)
|
||||
|
||||
var completer = readline.NewPrefixCompleter()
|
||||
|
||||
func updateCompleter() {
|
||||
commands := make([]string, len(cmd.CommandList))
|
||||
i := 0
|
||||
for c := range cmd.CommandList {
|
||||
commands[i] = c
|
||||
i++
|
||||
}
|
||||
for _, c := range commands {
|
||||
completer.Children = append(completer.Children, readline.PcItem(c))
|
||||
}
|
||||
}
|
73
internal/shell/shell.go
Normal file
73
internal/shell/shell.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package shell
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/chzyer/readline"
|
||||
"gitlab.com/neonsea/iopshell/internal/setting"
|
||||
"gitlab.com/neonsea/iopshell/internal/cmd"
|
||||
)
|
||||
|
||||
func filterInput(r rune) (rune, bool) {
|
||||
switch r {
|
||||
case readline.CharCtrlZ:
|
||||
return r, false
|
||||
}
|
||||
return r, true
|
||||
}
|
||||
|
||||
func UpdatePrompt(l *readline.Instance, s setting.Status) {
|
||||
var prompt string
|
||||
if !s.Connection {
|
||||
prompt = "\033[91miop\033[0;1m$\033[0m "
|
||||
} else {
|
||||
if !s.Auth {
|
||||
prompt = "\033[32miop\033[0;1m$\033[0m "
|
||||
} else {
|
||||
prompt = fmt.Sprintf("\033[32miop\033[0m %s\033[0;1m$\033[0m ", s.Curr_user)
|
||||
}
|
||||
}
|
||||
l.SetPrompt(prompt)
|
||||
l.Refresh()
|
||||
}
|
||||
|
||||
func Shell() {
|
||||
l, err := readline.NewEx(&readline.Config{
|
||||
Prompt: "\033[91miop\033[0;1m$\033[0m ",
|
||||
HistoryFile: "/tmp/iop.tmp",
|
||||
AutoComplete: completer,
|
||||
InterruptPrompt: "^C",
|
||||
EOFPrompt: "^D",
|
||||
|
||||
HistorySearchFold: true,
|
||||
FuncFilterInputRune: filterInput,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
updateCompleter()
|
||||
|
||||
log.SetOutput(l.Stderr())
|
||||
|
||||
for {
|
||||
line, err := l.Readline()
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err == readline.ErrInterrupt {
|
||||
continue
|
||||
}
|
||||
|
||||
line = strings.TrimSpace(line)
|
||||
command := strings.Split(line, " ")[0]
|
||||
if val, k := cmd.CommandList[command]; k {
|
||||
cmd.Execute(&val, line)
|
||||
} else {
|
||||
fmt.Printf("Unknown command '%s'\n", line)
|
||||
}
|
||||
}
|
||||
}
|
9
iopshell.go
Normal file
9
iopshell.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"gitlab.com/neonsea/iopshell/internal/shell"
|
||||
)
|
||||
|
||||
func main() {
|
||||
shell.Shell()
|
||||
}
|
Loading…
Reference in a new issue