1 Star 0 Fork 1

Collin / leaf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
agent.go 1021 Bytes
一键复制 编辑 原始数据 按行查看 历史
Collin 提交于 2021-10-09 16:08 . adjust
package console
import (
"bufio"
"strings"
"gitee.com/nbcx/leaf/conf"
"gitee.com/nbcx/leaf/network"
"gitee.com/nbcx/leaf/network/tcp"
)
type Agent struct {
conn *tcp.TCPConn
reader *bufio.Reader
}
func newAgent(conn *tcp.TCPConn) network.Agent {
a := new(Agent)
a.conn = conn
a.reader = bufio.NewReader(conn)
return a
}
func (a *Agent) Run() {
for {
if conf.ConsolePrompt != "" {
a.conn.Write([]byte(conf.ConsolePrompt))
}
line, err := a.reader.ReadString('\n')
if err != nil {
break
}
line = strings.TrimSuffix(line[:len(line)-1], "\r")
args := strings.Fields(line)
if len(args) == 0 {
continue
}
if args[0] == "quit" {
break
}
var c Command
for _, _c := range commands {
if _c.name() == args[0] {
c = _c
break
}
}
if c == nil {
a.conn.Write([]byte("command not found, try `help` for help\r\n"))
continue
}
output := c.run(args[1:])
if output != "" {
a.conn.Write([]byte(output + "\r\n"))
}
}
}
func (a *Agent) OnClose() {}
1
https://gitee.com/nbcx/leaf.git
git@gitee.com:nbcx/leaf.git
nbcx
leaf
leaf
78613ecb5808

搜索帮助