mirror of
https://shylinux.com/x/ContextOS
synced 2025-04-25 16:58:06 +08:00
Merge branch '0.1.0' into 0.2.0
This commit is contained in:
commit
4cb2b90da5
@ -5,13 +5,11 @@
|
||||
~ssh dial chat.shylinux.com:9090 true
|
||||
sleep 1
|
||||
~host1
|
||||
ifneq load load end
|
||||
remote context mpa register terminal shhylinux term term term 1
|
||||
$sessid $result
|
||||
remote cache sessid $sessid
|
||||
~nfs save usr/sess.txt "terminal: " $sessid
|
||||
~nfs genqr usr/sess.png "terminal: " $sessid
|
||||
end
|
||||
return
|
||||
# ~cli
|
||||
# remote slaver listen ":9393" tcp
|
||||
|
@ -57,7 +57,7 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
return time.Unix(int64(n), 0).Format("15:03:04")
|
||||
}}
|
||||
|
||||
if m.Target == Index {
|
||||
if m.Target() == Index {
|
||||
Pulse = m
|
||||
}
|
||||
return aaa
|
||||
@ -74,17 +74,17 @@ func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool {
|
||||
aaa.Group = arg[0]
|
||||
}
|
||||
|
||||
m.Log("info", m.Source, "%s login %s %s", Pulse.Cap("nuser"), m.Cap("group"), m.Cap("username"))
|
||||
m.Log("info", m.Source(), "%s login %s %s", Pulse.Cap("nuser"), m.Cap("group"), m.Cap("username"))
|
||||
return false
|
||||
}
|
||||
|
||||
func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool {
|
||||
switch aaa.Context {
|
||||
case m.Target:
|
||||
root := Pulse.Target.Server.(*AAA)
|
||||
case m.Target():
|
||||
root := Pulse.Target().Server.(*AAA)
|
||||
delete(root.sessions, m.Cap("sessid"))
|
||||
m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username"))
|
||||
case m.Source:
|
||||
case m.Source():
|
||||
}
|
||||
|
||||
return true
|
||||
@ -101,26 +101,26 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||
},
|
||||
Commands: map[string]*ctx.Command{
|
||||
"login": &ctx.Command{Name: "login [sessid]|[[group] username password]]", Help: "用户登录", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Target, m.Master = c, c
|
||||
m.Target(c)
|
||||
aaa := c.Server.(*AAA)
|
||||
|
||||
switch len(arg) {
|
||||
case 0:
|
||||
m.Travel(c, func(m *ctx.Message) bool {
|
||||
m.Echo("%s(%s): %s\n", m.Target.Name, m.Cap("group"), m.Cap("time"))
|
||||
m.Echo("%s(%s): %s\n", m.Target().Name, m.Cap("group"), m.Cap("time"))
|
||||
if int64(m.Capi("expire")) < time.Now().Unix() {
|
||||
m.Target.Close(m)
|
||||
m.Target().Close(m)
|
||||
}
|
||||
return true
|
||||
})
|
||||
case 1:
|
||||
s, ok := aaa.sessions[arg[0]]
|
||||
m.Assert(ok, "会话失败")
|
||||
m.Target = s
|
||||
m.Target(s)
|
||||
m.Assert(int64(m.Capi("expire")) > time.Now().Unix(), "会话失败")
|
||||
|
||||
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
||||
m.Log("info", m.Source, "logon %s %s", m.Cap("username"), m.Cap("group"))
|
||||
m.Source().Group, m.Source().Owner = m.Cap("group"), m.Target()
|
||||
m.Log("info", m.Source(), "logon %s %s", m.Cap("username"), m.Cap("group"))
|
||||
m.Echo(m.Cap("username"))
|
||||
case 2, 3:
|
||||
group, username, password := arg[0], arg[0], arg[1]
|
||||
@ -129,16 +129,16 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||
}
|
||||
|
||||
if username == Pulse.Conf("rootname") {
|
||||
m.Set("detail", group, username).Target.Start(m)
|
||||
m.Set("detail", group, username).Target().Start(m)
|
||||
} else if msg := m.Find(username, false); msg == nil {
|
||||
m.Start(username, "认证用户", group, username)
|
||||
} else {
|
||||
m.Target = msg.Target
|
||||
m.Target(msg.Target())
|
||||
}
|
||||
|
||||
m.Cap("password", password)
|
||||
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
||||
aaa.sessions[m.Cap("sessid")] = m.Target
|
||||
m.Source().Group, m.Source().Owner = m.Cap("group"), m.Target()
|
||||
aaa.sessions[m.Cap("sessid")] = m.Target()
|
||||
m.Echo(m.Cap("sessid"))
|
||||
}
|
||||
}},
|
||||
|
@ -17,87 +17,42 @@ import ( // {{{
|
||||
// }}}
|
||||
|
||||
type CLI struct {
|
||||
in io.ReadCloser
|
||||
ins []io.ReadCloser
|
||||
bio *bufio.Reader
|
||||
bios []*bufio.Reader
|
||||
bufs [][]byte
|
||||
|
||||
bio *bufio.Reader
|
||||
out io.WriteCloser
|
||||
|
||||
alias map[string]string
|
||||
back string
|
||||
next string
|
||||
exit bool
|
||||
|
||||
lex *ctx.Message
|
||||
target *ctx.Context
|
||||
wait *ctx.Context
|
||||
|
||||
*ctx.Message
|
||||
*ctx.Context
|
||||
}
|
||||
|
||||
func (cli *CLI) push(f io.ReadCloser) { // {{{
|
||||
if cli.ins == nil || cli.bios == nil {
|
||||
cli.ins = make([]io.ReadCloser, 0, 3)
|
||||
cli.bios = make([]*bufio.Reader, 0, 3)
|
||||
}
|
||||
|
||||
cli.in = f
|
||||
cli.ins = append(cli.ins, cli.in)
|
||||
cli.bio = bufio.NewReader(f)
|
||||
cli.bios = append(cli.bios, cli.bio)
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (cli *CLI) echo(str string, arg ...interface{}) { // {{{
|
||||
if len(cli.ins) == 1 || cli.Conf("slient") != "yes" {
|
||||
func (cli *CLI) echo(str string, arg ...interface{}) bool {
|
||||
if cli.out != nil {
|
||||
fmt.Fprintf(cli.out, str, arg...)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (cli *CLI) parse(m *ctx.Message) bool { // {{{
|
||||
line := ""
|
||||
if cli.next == "" {
|
||||
func (cli *CLI) parse(m *ctx.Message) bool {
|
||||
line := m.Cap("next")
|
||||
if line == "" {
|
||||
cli.echo(m.Conf("PS1"))
|
||||
ls, e := cli.bio.ReadString('\n')
|
||||
if e == io.EOF {
|
||||
l := len(cli.ins)
|
||||
if l > 1 {
|
||||
cli.ins = cli.ins[:l-1]
|
||||
cli.bios = cli.bios[:l-1]
|
||||
cli.in = cli.ins[l-2]
|
||||
cli.bio = cli.bios[l-2]
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
l, e := cli.bio.ReadString('\n')
|
||||
m.Assert(e)
|
||||
line = ls
|
||||
|
||||
if len(cli.ins) > 1 {
|
||||
cli.echo(line)
|
||||
cli.echo("\n")
|
||||
}
|
||||
|
||||
if len(line) == 1 {
|
||||
if len(cli.ins) > 1 {
|
||||
return true
|
||||
}
|
||||
line, cli.back = cli.back, ""
|
||||
}
|
||||
} else {
|
||||
line, cli.next = cli.next, ""
|
||||
|
||||
if m.Conf("slient") != "yes" {
|
||||
cli.echo(m.Conf("PS1"))
|
||||
cli.echo(line)
|
||||
cli.echo("\n")
|
||||
}
|
||||
line = l
|
||||
}
|
||||
m.Cap("next", "")
|
||||
|
||||
line = strings.TrimSpace(line)
|
||||
if line = strings.TrimSpace(line); len(line) == 0 {
|
||||
line, cli.back = cli.back, ""
|
||||
}
|
||||
if len(line) == 0 || line[0] == '#' {
|
||||
return true
|
||||
}
|
||||
@ -106,21 +61,15 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
|
||||
if cli.lex == nil {
|
||||
ls = strings.Split(line, " ")
|
||||
} else {
|
||||
lex := m.Spawn(cli.lex.Target)
|
||||
lex := m.Spawn(cli.lex.Target())
|
||||
m.Assert(lex.Cmd("split", line, "void"))
|
||||
|
||||
ls = lex.Meta["result"]
|
||||
}
|
||||
|
||||
if len(ls) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
msg := m.Spawn(cli.target)
|
||||
|
||||
for i := 0; i < len(ls); i++ {
|
||||
ls[i] = strings.TrimSpace(ls[i])
|
||||
if ls[i] == "" {
|
||||
if ls[i] = strings.TrimSpace(ls[i]); ls[i] == "" {
|
||||
continue
|
||||
}
|
||||
if ls[i][0] == '#' {
|
||||
@ -135,38 +84,37 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
|
||||
}
|
||||
ls[i] = ls[i][1:]
|
||||
} else if len(ls[i]) > 1 {
|
||||
mm := m.Spawn(cli.target)
|
||||
m.Assert(mm.Exec(c, ls[i][1:]))
|
||||
ls[i] = mm.Get("result")
|
||||
msg := m.Spawn(cli.target)
|
||||
m.Assert(msg.Exec(c, ls[i][1:]))
|
||||
ls[i] = msg.Get("result")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cli.lex != nil && len(ls[i]) > 1 {
|
||||
switch ls[i][0] {
|
||||
case '"', '\'':
|
||||
ls[i] = ls[i][1 : len(ls[i])-1]
|
||||
}
|
||||
}
|
||||
|
||||
msg.Add("detail", ls[i])
|
||||
}
|
||||
|
||||
msg.Wait = make(chan bool)
|
||||
msg.Post(cli.Context)
|
||||
msg.Post(cli.Context.Master())
|
||||
|
||||
result := strings.TrimRight(strings.Join(msg.Meta["result"], ""), "\n") + "\n"
|
||||
if len(result) > 1 {
|
||||
cli.echo(m.Cap("result", result))
|
||||
if result := strings.TrimRight(strings.Join(msg.Meta["result"], ""), "\n"); len(result) > 0 {
|
||||
cli.echo(m.Cap("result", result) + "\n")
|
||||
}
|
||||
cli.target = msg.Target
|
||||
cli.back = line
|
||||
return true
|
||||
m.Cap("target", cli.target.Name)
|
||||
m.Cap("back", line)
|
||||
m.Log("fuck", nil, "over")
|
||||
if cli.wait != nil {
|
||||
msg.Log("fuck", nil, "wait 1")
|
||||
<-cli.wait.Exit
|
||||
cli.wait = nil
|
||||
msg.Log("fuck", nil, "wait 2")
|
||||
} else {
|
||||
cli.target = msg.Target()
|
||||
}
|
||||
return !cli.exit
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
||||
func (cli *CLI) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
||||
func (cli *CLI) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||
c.Caches = map[string]*ctx.Cache{}
|
||||
c.Configs = map[string]*ctx.Config{}
|
||||
|
||||
@ -175,10 +123,12 @@ func (cli *CLI) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
||||
return s
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
cli.Caches["target"] = &ctx.Cache{Name: "操作目标", Value: "", Help: "操作目标"}
|
||||
cli.Caches["result"] = &ctx.Cache{Name: "前一条指令执行结果", Value: "", Help: "前一条指令执行结果"}
|
||||
cli.Configs["slient"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息,yes:屏蔽,no:不屏蔽"}
|
||||
cli.Caches["back"] = &ctx.Cache{Name: "前一条指令", Value: "", Help: "前一条指令"}
|
||||
cli.Caches["next"] = &ctx.Cache{Name: "下一条指令", Value: "", Help: "下一条指令"}
|
||||
|
||||
cli.Configs["PS1"] = &ctx.Config{Name: "命令行提示符(target/detail)", Value: "target", Help: "命令行提示符,target:显示当前模块,detail:显示详细信息", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
||||
if len(arg) > 0 { // {{{
|
||||
return arg[0]
|
||||
@ -186,7 +136,7 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
|
||||
ps := make([]string, 0, 3)
|
||||
|
||||
if cli, ok := m.Target.Server.(*CLI); ok && cli.target != nil {
|
||||
if cli, ok := m.Target().Server.(*CLI); ok && cli.target != nil {
|
||||
ps = append(ps, "[")
|
||||
ps = append(ps, time.Now().Format("15:04:05"))
|
||||
ps = append(ps, "]")
|
||||
@ -222,18 +172,18 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
}}
|
||||
cli.Configs["lex"] = &ctx.Config{Name: "词法解析器", Value: "", Help: "命令行词法解析器", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
||||
if len(arg) > 0 { // {{{
|
||||
cli, ok := m.Target.Server.(*CLI)
|
||||
cli, ok := m.Target().Server.(*CLI)
|
||||
m.Assert(ok, "模块类型错误")
|
||||
|
||||
lex := m.Find(arg[0], true)
|
||||
m.Assert(lex != nil, "词法解析模块不存在")
|
||||
if lex.Cap("status") != "start" {
|
||||
lex.Target.Start(lex)
|
||||
m.Spawn(lex.Target).Cmd("train", "'[^']*'")
|
||||
m.Spawn(lex.Target).Cmd("train", "\"[^\"]*\"")
|
||||
m.Spawn(lex.Target).Cmd("train", "[^ \t\n]+")
|
||||
m.Spawn(lex.Target).Cmd("train", "[ \n\t]+", "void", "void")
|
||||
m.Spawn(lex.Target).Cmd("train", "#[^\n]*\n", "void", "void")
|
||||
lex.Target().Start(lex)
|
||||
m.Spawn(lex.Target()).Cmd("train", "'[^']*'")
|
||||
m.Spawn(lex.Target()).Cmd("train", "\"[^\"]*\"")
|
||||
m.Spawn(lex.Target()).Cmd("train", "[^ \t\n]+")
|
||||
m.Spawn(lex.Target()).Cmd("train", "[ \n\t]+", "void", "void")
|
||||
m.Spawn(lex.Target()).Cmd("train", "#[^\n]*\n", "void", "void")
|
||||
}
|
||||
cli.lex = lex
|
||||
}
|
||||
@ -245,10 +195,6 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
cli.Configs["init.sh"] = &ctx.Config{Name: "启动脚本", Value: arg[0], Help: "模块启动时自动运行的脚本"}
|
||||
}
|
||||
|
||||
cli.Context.Master = cli.Context
|
||||
if cli.Context != Index {
|
||||
cli.Owner = nil
|
||||
}
|
||||
if cli.Context == Index {
|
||||
Pulse = m
|
||||
}
|
||||
@ -266,69 +212,75 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
return cli
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
cli.Message = m
|
||||
func (cli *CLI) Start(m *ctx.Message, arg ...string) bool {
|
||||
|
||||
if stream, ok := m.Data["io"]; ok {
|
||||
io := stream.(io.ReadWriteCloser)
|
||||
cli.bio = bufio.NewReader(io)
|
||||
cli.out = io
|
||||
cli.push(io)
|
||||
cli.Context.Master(cli.Context)
|
||||
|
||||
if m.Has("master") {
|
||||
m.Log("info", nil, "master terminal")
|
||||
if cli.bufs == nil {
|
||||
cli.bufs = make([][]byte, 0, 10)
|
||||
}
|
||||
for {
|
||||
b := make([]byte, 128)
|
||||
n, e := cli.bio.Read(b)
|
||||
m.Log("info", nil, "read %d", n)
|
||||
m.Assert(e)
|
||||
cli.bufs = append(cli.bufs, b)
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
if cli.Owner == nil {
|
||||
if msg := m.Find("aaa", true); msg != nil {
|
||||
username := ""
|
||||
cli.echo("username>")
|
||||
fmt.Fscanln(cli.in, &username)
|
||||
if msg := m.Find("aaa", true); msg != nil {
|
||||
cli.echo("username>")
|
||||
username, e := cli.bio.ReadString('\n')
|
||||
msg.Assert(e)
|
||||
username = strings.TrimSpace(username)
|
||||
|
||||
password := ""
|
||||
cli.echo("password>")
|
||||
fmt.Fscanln(cli.in, &password)
|
||||
cli.echo("password>")
|
||||
password, e := cli.bio.ReadString('\n')
|
||||
msg.Assert(e)
|
||||
password = strings.TrimSpace(password)
|
||||
|
||||
msg.Name = "aaa"
|
||||
msg.Wait = make(chan bool)
|
||||
if msg.Cmd("login", username, password) == "" {
|
||||
cli.echo("登录失败")
|
||||
m.Cmd("exit")
|
||||
cli.out.Close()
|
||||
cli.in.Close()
|
||||
return false
|
||||
}
|
||||
|
||||
if cli.Sessions == nil {
|
||||
cli.Sessions = make(map[string]*ctx.Message)
|
||||
}
|
||||
cli.Sessions["aaa"] = msg
|
||||
}
|
||||
} else {
|
||||
m.Cap("stream", "stdout")
|
||||
msg.Name = "aaa"
|
||||
msg.Wait = make(chan bool)
|
||||
if msg.Cmd("login", username, password) == "" {
|
||||
cli.echo("登录失败")
|
||||
io.Close()
|
||||
return true
|
||||
}
|
||||
|
||||
m.Log("info", nil, "slaver terminal")
|
||||
m.Log("info", nil, "open %s", m.Conf("init.sh"))
|
||||
if f, e := os.Open(m.Conf("init.sh")); e == nil {
|
||||
cli.push(f)
|
||||
if cli.Sessions == nil {
|
||||
cli.Sessions = make(map[string]*ctx.Message)
|
||||
}
|
||||
|
||||
go m.AssertOne(m, true, func(m *ctx.Message) {
|
||||
for cli.parse(m) {
|
||||
}
|
||||
})
|
||||
cli.Sessions["aaa"] = msg
|
||||
}
|
||||
} else if stream, ok := m.Data["bio"]; ok {
|
||||
cli.Context.Exit = make(chan bool)
|
||||
cli.bio = stream.(*bufio.Reader)
|
||||
m.AssertOne(m, true, func(m *ctx.Message) {
|
||||
for cli.parse(m) {
|
||||
}
|
||||
})
|
||||
m.Log("fuck", nil, "done 1")
|
||||
cli.Context.Exit <- true
|
||||
m.Log("fuck", nil, "done 2")
|
||||
return true
|
||||
|
||||
} else if stream, ok := m.Data["file"]; ok {
|
||||
cli.Context.Exit = make(chan bool)
|
||||
io := stream.(io.ReadWriteCloser)
|
||||
cli.bio = bufio.NewReader(io)
|
||||
m.AssertOne(m, true, func(m *ctx.Message) {
|
||||
for cli.parse(m) {
|
||||
}
|
||||
})
|
||||
m.Log("fuck", nil, "done 1")
|
||||
cli.Context.Exit <- true
|
||||
m.Log("fuck", nil, "done 2")
|
||||
return true
|
||||
} else if len(arg) > 0 {
|
||||
cli.Context.Master(cli.Context)
|
||||
cli.bio = bufio.NewReader(os.Stdin)
|
||||
cli.out = os.Stdout
|
||||
m.Cap("stream", "stdout")
|
||||
}
|
||||
|
||||
if cli.bio != nil {
|
||||
go m.AssertOne(m, true, func(m *ctx.Message) {
|
||||
m.Cap("next", "source "+m.Conf("init.sh"))
|
||||
for cli.parse(m) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
m.Capi("nterm", 1)
|
||||
@ -343,8 +295,8 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
msg.Set("append")
|
||||
c := exec.Command(msg.Meta["detail"][0], msg.Meta["detail"][1:]...)
|
||||
|
||||
if len(cli.ins) == 1 && cli.Context == Index {
|
||||
c.Stdin, c.Stdout, c.Stderr = cli.in, cli.out, cli.out
|
||||
if cli.out == os.Stdout {
|
||||
c.Stdin, c.Stdout, c.Stderr = os.Stdin, os.Stdout, os.Stderr
|
||||
msg.Assert(c.Start())
|
||||
msg.Assert(c.Wait())
|
||||
} else {
|
||||
@ -358,28 +310,23 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
}
|
||||
}
|
||||
|
||||
cli.target = msg.Target
|
||||
cli.target = msg.Target()
|
||||
return cli.exit == false
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (cli *CLI) Close(m *ctx.Message, arg ...string) bool { // {{{
|
||||
switch cli.Context {
|
||||
case m.Target:
|
||||
case m.Target():
|
||||
if cli.Context == Index {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(cli.Context.Requests) == 0 {
|
||||
m.Log("info", nil, "%s close %v", Pulse.Cap("nterm"), arg)
|
||||
}
|
||||
case m.Source:
|
||||
case m.Source():
|
||||
if m.Name == "aaa" {
|
||||
msg := m.Spawn(cli.Context)
|
||||
msg.Master = cli.Context
|
||||
msg.Master(cli.Context)
|
||||
if !cli.Context.Close(msg, arg...) {
|
||||
return false
|
||||
}
|
||||
@ -399,26 +346,32 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
Configs: map[string]*ctx.Config{},
|
||||
Commands: map[string]*ctx.Command{
|
||||
"source": &ctx.Command{Name: "source file", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
cli := c.Server.(*CLI) // {{{
|
||||
switch len(arg) {
|
||||
case 1:
|
||||
f, e := os.Open(arg[0])
|
||||
m.Assert(e)
|
||||
cli.push(f)
|
||||
}
|
||||
|
||||
// }}}
|
||||
cli := m.Master().Master().Server.(*CLI)
|
||||
f, e := os.Open(arg[0])
|
||||
m.Assert(e)
|
||||
m.Put("option", "file", f).Start(arg[0], "脚本文件", key)
|
||||
m.Log("fuck", nil, "source")
|
||||
cli.wait = m.Target()
|
||||
}},
|
||||
"return": &ctx.Command{Name: "return", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
cli := m.Master().Master().Server.(*CLI)
|
||||
cli.exit = true
|
||||
}},
|
||||
"sleep": &ctx.Command{Name: "sleep time", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
t, e := strconv.Atoi(arg[0])
|
||||
m.Assert(e)
|
||||
m.Log("info", nil, "sleep %ds", t)
|
||||
time.Sleep(time.Second * time.Duration(t))
|
||||
m.Log("info", nil, "sleep %ds done", t)
|
||||
}},
|
||||
"return": &ctx.Command{Name: "return", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
cli := c.Server.(*CLI) // {{{
|
||||
cli.bio.Discard(cli.bio.Buffered())
|
||||
// }}}
|
||||
"if": &ctx.Command{Name: "if a [ = | != ] b", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
cli := m.Master().Master().Server.(*CLI)
|
||||
m.Put("option", "bio", cli.bio).Start(strings.Join(arg, " "), "条件语句", key)
|
||||
cli.wait = m.Target()
|
||||
}},
|
||||
"end": &ctx.Command{Name: "end", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
cli := m.Master().Master().Server.(*CLI)
|
||||
cli.exit = true
|
||||
}},
|
||||
"alias": &ctx.Command{Name: "alias [short [long]]", Help: "查看日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
cli := c.Server.(*CLI) // {{{
|
||||
@ -444,22 +397,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
"remote": &ctx.Command{Name: "remote [send args...]|[[master|slaver] listen|dial address protocol]", Help: "建立远程连接",
|
||||
Formats: map[string]int{"send": -1, "master": 0, "slaver": 0, "listen": 1, "dial": 1},
|
||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if m.Has("send") { // {{{
|
||||
cli := m.Target.Server.(*CLI)
|
||||
|
||||
cli.out.Write([]byte(strings.Join(m.Meta["args"], " ") + "\n"))
|
||||
m.Echo("~~~remote~~~\n")
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
for _, b := range cli.bufs {
|
||||
m.Echo("%s", string(b))
|
||||
}
|
||||
cli.bufs = cli.bufs[0:0]
|
||||
m.Echo("\n~~~remote~~~\n")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
action := "dial"
|
||||
action := "dial" // {{{
|
||||
if m.Has("listen") {
|
||||
action = "listen"
|
||||
}
|
||||
@ -467,12 +405,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
msg := m.Find(m.Get("args"), true)
|
||||
|
||||
if m.Has("master") {
|
||||
msg.Template = msg.Spawn(msg.Source).Add("option", "master")
|
||||
msg.Template = msg.Spawn(msg.Source()).Add("option", "master")
|
||||
}
|
||||
msg.Cmd(action, m.Get(action))
|
||||
|
||||
}},
|
||||
// }}}
|
||||
}}, // }}}
|
||||
"open": &ctx.Command{Name: "open [master|slaver] [script [log]]", Help: "建立远程连接",
|
||||
Options: map[string]string{"master": "主控终端", "slaver": "被控终端", "args": "启动参数", "io": "读写流"},
|
||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
@ -484,11 +421,10 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
cli, ok := c.Server.(*CLI) // {{{
|
||||
m.Assert(ok, "模块类型错误")
|
||||
m.Assert(m.Target != c, "模块是主控模块")
|
||||
m.Assert(m.Target() != c, "模块是主控模块")
|
||||
|
||||
msg := m.Spawn(c)
|
||||
msg.Start(fmt.Sprintf("PTS%d", cli.Capi("nterm")), arg[0], arg[1:]...)
|
||||
m.Target.Master = msg.Target
|
||||
// }}}
|
||||
}},
|
||||
},
|
||||
|
@ -1,10 +1,13 @@
|
||||
package ctx // {{{
|
||||
// }}}
|
||||
import ( // {{{
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
@ -59,7 +62,7 @@ type Context struct {
|
||||
context *Context
|
||||
contexts map[string]*Context
|
||||
|
||||
Master *Context
|
||||
master *Context
|
||||
messages chan *Message
|
||||
|
||||
Pulse *Message
|
||||
@ -67,15 +70,24 @@ type Context struct {
|
||||
Historys []*Message
|
||||
Sessions map[string]*Message
|
||||
|
||||
Index map[string]*Context
|
||||
Groups map[string]*Context
|
||||
Owner *Context
|
||||
Group string
|
||||
Index map[string]*Context
|
||||
Groups map[string]*Context
|
||||
Owner *Context
|
||||
Group string
|
||||
password string
|
||||
|
||||
Server
|
||||
Exit chan bool
|
||||
}
|
||||
|
||||
func (c *Context) Register(s *Context, x Server) *Context { // {{{
|
||||
func (c *Context) Password(meta string) string { // {{{
|
||||
bs := md5.Sum([]byte(fmt.Sprintln("%d%d%s", time.Now().Unix(), rand.Int(), meta)))
|
||||
sessid := hex.EncodeToString(bs[:])
|
||||
return sessid
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (c *Context) Register(s *Context, x Server) (password string) { // {{{
|
||||
if c.contexts == nil {
|
||||
c.contexts = make(map[string]*Context)
|
||||
}
|
||||
@ -86,21 +98,22 @@ func (c *Context) Register(s *Context, x Server) *Context { // {{{
|
||||
c.contexts[s.Name] = s
|
||||
s.context = c
|
||||
s.Server = x
|
||||
return s
|
||||
s.password = c.Password(s.Name)
|
||||
return s.password
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (c *Context) Spawn(m *Message, name string, help string) *Context { // {{{
|
||||
s := &Context{Name: name, Help: help, root: c.root, context: c}
|
||||
|
||||
if m.Target = s; c.Server != nil {
|
||||
if m.target = s; c.Server != nil {
|
||||
c.Register(s, c.Server.Spawn(m, s, m.Meta["detail"]...))
|
||||
} else {
|
||||
c.Register(s, nil)
|
||||
}
|
||||
|
||||
if m.Template != nil {
|
||||
m.Template.Source = s
|
||||
m.Template.source = s
|
||||
}
|
||||
|
||||
return s
|
||||
@ -116,9 +129,9 @@ func (c *Context) Begin(m *Message) *Context { // {{{
|
||||
c.Requests = []*Message{m}
|
||||
c.Historys = []*Message{m}
|
||||
|
||||
c.Master = m.Master.Master
|
||||
c.Owner = m.Master.Owner
|
||||
c.Group = m.Master.Group
|
||||
c.master = m.master.master
|
||||
c.Owner = m.master.Owner
|
||||
c.Group = m.master.Group
|
||||
|
||||
m.Log("begin", nil, "%d context %v", m.root.Capi("ncontext", 1), m.Meta["detail"])
|
||||
for k, x := range c.Configs {
|
||||
@ -136,7 +149,11 @@ func (c *Context) Begin(m *Message) *Context { // {{{
|
||||
|
||||
// }}}
|
||||
func (c *Context) Start(m *Message) bool { // {{{
|
||||
if c.Requests, m.Index = append(c.Requests, m), len(c.Requests)+1; m.Cap("status") != "start" {
|
||||
if m != c.Requests[0] {
|
||||
c.Requests, m.Index = append(c.Requests, m), len(c.Requests)+1
|
||||
}
|
||||
|
||||
if m.Cap("status") != "start" {
|
||||
running := make(chan bool)
|
||||
go m.AssertOne(m, true, func(m *Message) {
|
||||
m.Log(m.Cap("status", "start"), nil, "%d server %v", m.root.Capi("nserver", 1), m.Meta["detail"])
|
||||
@ -154,11 +171,11 @@ func (c *Context) Start(m *Message) bool { // {{{
|
||||
func (c *Context) Close(m *Message, arg ...string) bool { // {{{
|
||||
m.Log("close", c, "%v", arg)
|
||||
|
||||
if m.Target == c {
|
||||
if m.target == c {
|
||||
if m.Index == 0 {
|
||||
for i := len(c.Requests) - 1; i >= 0; i-- {
|
||||
v := c.Requests[i]
|
||||
if v.Index = -1; v.Source != c && !v.Source.Close(v, arg...) {
|
||||
if v.Index = -1; v.source != c && !v.source.Close(v, arg...) {
|
||||
v.Index = i
|
||||
return false
|
||||
}
|
||||
@ -176,7 +193,7 @@ func (c *Context) Close(m *Message, arg ...string) bool { // {{{
|
||||
return false
|
||||
}
|
||||
|
||||
if m.Source == c && m.Target != c {
|
||||
if m.source == c && m.target != c {
|
||||
if _, ok := c.Sessions[m.Name]; ok {
|
||||
delete(c.Sessions, m.Name)
|
||||
}
|
||||
@ -190,13 +207,14 @@ func (c *Context) Close(m *Message, arg ...string) bool { // {{{
|
||||
if m.Cap("status") == "start" {
|
||||
m.Log(m.Cap("status", "close"), nil, "%d server %v", m.root.Capi("nserver", -1)+1, arg)
|
||||
for _, v := range c.Sessions {
|
||||
if v.Target != c {
|
||||
v.Target.Close(v, arg...)
|
||||
if v.target != c {
|
||||
v.target.Close(v, arg...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if m.Index == 0 && c.context != nil && len(c.contexts) == 0 {
|
||||
// if m.Index == 0 && c.context != nil && len(c.contexts) == 0 {
|
||||
if c.context != nil {
|
||||
m.Log("close", nil, "%d context %v", m.root.Capi("ncontext", -1)+1, arg)
|
||||
delete(c.context.contexts, c.Name)
|
||||
}
|
||||
@ -205,6 +223,20 @@ func (c *Context) Close(m *Message, arg ...string) bool { // {{{
|
||||
|
||||
// }}}
|
||||
|
||||
func (c *Context) Context() *Context { // {{{
|
||||
return c.context
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (c *Context) Master(s ...*Context) *Context { // {{{
|
||||
if len(s) > 0 && s[0] == c {
|
||||
c.master = c
|
||||
}
|
||||
return c.master
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
||||
func (c *Context) Add(group string, arg ...string) { // {{{
|
||||
if c.Index == nil {
|
||||
c.Index = make(map[string]*Context)
|
||||
@ -393,14 +425,36 @@ type Message struct {
|
||||
root *Message
|
||||
|
||||
Name string
|
||||
Source *Context
|
||||
Master *Context
|
||||
Target *Context
|
||||
source *Context
|
||||
master *Context
|
||||
target *Context
|
||||
Index int
|
||||
|
||||
Template *Message
|
||||
}
|
||||
|
||||
func (m *Message) Source() *Context { // {{{
|
||||
return m.source
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Master(s ...*Context) *Context { // {{{
|
||||
if len(s) > 0 && s[0] == m.source {
|
||||
m.master = m.source
|
||||
}
|
||||
return m.master
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Target(s ...*Context) *Context { // {{{
|
||||
if len(s) > 0 {
|
||||
m.target = s[0]
|
||||
}
|
||||
return m.target
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
||||
func (m *Message) Log(action string, ctx *Context, str string, arg ...interface{}) { // {{{
|
||||
color := 0
|
||||
switch action {
|
||||
@ -421,13 +475,14 @@ func (m *Message) Log(action string, ctx *Context, str string, arg ...interface{
|
||||
}
|
||||
|
||||
if ctx == nil {
|
||||
ctx = m.Target
|
||||
ctx = m.target
|
||||
}
|
||||
|
||||
info := fmt.Sprintf("%s", ctx.Name)
|
||||
name := fmt.Sprintf("%s->%s", m.Source.Name, m.Target.Name)
|
||||
// name := fmt.Sprintf("%s->%s", m.source.Name, m.target.Name)
|
||||
name := fmt.Sprintf("%s:%s->%s.%d", m.source.Name, m.Name, m.target.Name, m.Index)
|
||||
if m.Name != "" {
|
||||
name = fmt.Sprintf("%s:%s->%s.%d", m.Source.Name, m.Name, m.Target.Name, m.Index)
|
||||
name = fmt.Sprintf("%s:%s->%s.%d", m.source.Name, m.Name, m.target.Name, m.Index)
|
||||
}
|
||||
|
||||
log.Printf("\033[%dm%d %s(%s) %s: %s\033[0m", color, m.code, action, name, info, fmt.Sprintf(str, arg...))
|
||||
@ -438,14 +493,14 @@ func (m *Message) Check(s *Context, arg ...string) bool { // {{{
|
||||
if s.Owner == nil {
|
||||
return true
|
||||
}
|
||||
if m.Master.Owner == s.Owner {
|
||||
if m.master.Owner == s.Owner {
|
||||
return true
|
||||
}
|
||||
if m.Master.Owner == s.root.Owner {
|
||||
if m.master.Owner == s.root.Owner {
|
||||
return true
|
||||
}
|
||||
|
||||
g, ok := s.Index[m.Master.Group]
|
||||
g, ok := s.Index[m.master.Group]
|
||||
gg, gok := s.Index["void"]
|
||||
|
||||
if len(arg) < 2 {
|
||||
@ -453,12 +508,12 @@ func (m *Message) Check(s *Context, arg ...string) bool { // {{{
|
||||
return true
|
||||
}
|
||||
|
||||
m.Log("debug", s, "not auth: %s(%s)", m.Master.Name, m.Master.Group)
|
||||
m.Log("debug", s, "not auth: %s(%s)", m.master.Name, m.master.Group)
|
||||
if gok && gg != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
m.Log("debug", s, "not auth: %s(void)", m.Master.Name)
|
||||
m.Log("debug", s, "not auth: %s(void)", m.master.Name)
|
||||
return false
|
||||
}
|
||||
|
||||
@ -491,12 +546,12 @@ func (m *Message) Check(s *Context, arg ...string) bool { // {{{
|
||||
return true
|
||||
}
|
||||
if g != nil {
|
||||
m.Log("debug", s, "%s:%s not auth: %s(%s)", arg[0], arg[1], m.Master.Name, m.Master.Group)
|
||||
m.Log("debug", s, "%s:%s not auth: %s(%s)", arg[0], arg[1], m.master.Name, m.master.Group)
|
||||
}
|
||||
if gok {
|
||||
return true
|
||||
}
|
||||
m.Log("debug", s, "%s:%s not auth: %s(void)", arg[0], arg[1], m.Master.Name)
|
||||
m.Log("debug", s, "%s:%s not auth: %s(void)", arg[0], arg[1], m.master.Name)
|
||||
return false
|
||||
}
|
||||
|
||||
@ -547,9 +602,9 @@ func (m *Message) AssertOne(msg *Message, safe bool, hand ...func(msg *Message))
|
||||
|
||||
msg.Log("error", nil, "error: %v", e)
|
||||
if msg.root.Conf("debug") == "on" && e != io.EOF {
|
||||
fmt.Printf("\n\033[31m%s error: %v\033[0m\n", msg.Target.Name, e)
|
||||
fmt.Printf("\n\033[31m%s error: %v\033[0m\n", msg.target.Name, e)
|
||||
debug.PrintStack()
|
||||
fmt.Printf("\033[31m%s error: %v\033[0m\n\n", msg.Target.Name, e)
|
||||
fmt.Printf("\033[31m%s error: %v\033[0m\n\n", msg.target.Name, e)
|
||||
}
|
||||
|
||||
if e == io.EOF {
|
||||
@ -577,9 +632,9 @@ func (m *Message) Spawn(c *Context, key ...string) *Message { // {{{
|
||||
time: time.Now(),
|
||||
message: m,
|
||||
root: m.root,
|
||||
Source: m.Target,
|
||||
Master: m.Target,
|
||||
Target: c,
|
||||
source: m.target,
|
||||
master: m.target,
|
||||
target: c,
|
||||
}
|
||||
|
||||
if m.messages == nil {
|
||||
@ -591,10 +646,10 @@ func (m *Message) Spawn(c *Context, key ...string) *Message { // {{{
|
||||
return msg
|
||||
}
|
||||
|
||||
if msg.Source.Sessions == nil {
|
||||
msg.Source.Sessions = make(map[string]*Message)
|
||||
if msg.source.Sessions == nil {
|
||||
msg.source.Sessions = make(map[string]*Message)
|
||||
}
|
||||
msg.Source.Sessions[key[0]] = msg
|
||||
msg.source.Sessions[key[0]] = msg
|
||||
msg.Name = key[0]
|
||||
return msg
|
||||
}
|
||||
@ -602,7 +657,7 @@ func (m *Message) Spawn(c *Context, key ...string) *Message { // {{{
|
||||
// }}}
|
||||
func (m *Message) Reply(key ...string) *Message { // {{{
|
||||
if m.Template == nil {
|
||||
m.Template = m.Spawn(m.Source, key...)
|
||||
m.Template = m.Spawn(m.source, key...)
|
||||
}
|
||||
|
||||
msg := m.Template
|
||||
@ -610,19 +665,19 @@ func (m *Message) Reply(key ...string) *Message { // {{{
|
||||
return msg
|
||||
}
|
||||
|
||||
if msg.Source.Sessions == nil {
|
||||
msg.Source.Sessions = make(map[string]*Message)
|
||||
if msg.source.Sessions == nil {
|
||||
msg.source.Sessions = make(map[string]*Message)
|
||||
}
|
||||
msg.Source.Sessions[key[0]] = msg
|
||||
msg.source.Sessions[key[0]] = msg
|
||||
msg.Name = key[0]
|
||||
return msg
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Format() string { // {{{
|
||||
name := fmt.Sprintf("%s->%s", m.Source.Name, m.Target.Name)
|
||||
name := fmt.Sprintf("%s->%s", m.source.Name, m.target.Name)
|
||||
if m.Name != "" {
|
||||
name = fmt.Sprintf("%s.%s->%s.%d", m.Source.Name, m.Name, m.Target.Name, m.Index)
|
||||
name = fmt.Sprintf("%s.%s->%s.%d", m.source.Name, m.Name, m.target.Name, m.Index)
|
||||
}
|
||||
return fmt.Sprintf("%d(%s): %s %v", m.code, name, m.time.Format("15:04:05"), m.Meta["detail"])
|
||||
}
|
||||
@ -630,25 +685,25 @@ func (m *Message) Format() string { // {{{
|
||||
// }}}
|
||||
|
||||
func (m *Message) BackTrace(hand func(m *Message) bool) { // {{{
|
||||
target := m.Target
|
||||
target := m.target
|
||||
for s := target; s != nil; s = s.context {
|
||||
if m.Target = s; m.Check(s) && !hand(m) {
|
||||
if m.target = s; m.Check(s) && !hand(m) {
|
||||
break
|
||||
}
|
||||
}
|
||||
m.Target = target
|
||||
m.target = target
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Travel(c *Context, hand func(m *Message) bool) { // {{{
|
||||
if c == nil {
|
||||
c = m.Target
|
||||
c = m.target
|
||||
}
|
||||
target := m.Target
|
||||
target := m.target
|
||||
|
||||
cs := []*Context{c}
|
||||
for i := 0; i < len(cs); i++ {
|
||||
if m.Target = cs[i]; m.Check(cs[i]) && !hand(m) {
|
||||
if m.target = cs[i]; m.Check(cs[i]) && !hand(m) {
|
||||
break
|
||||
}
|
||||
|
||||
@ -657,7 +712,7 @@ func (m *Message) Travel(c *Context, hand func(m *Message) bool) { // {{{
|
||||
}
|
||||
}
|
||||
|
||||
m.Target = target
|
||||
m.target = target
|
||||
}
|
||||
|
||||
// }}}
|
||||
@ -665,16 +720,16 @@ func (m *Message) Search(key string, root ...bool) []*Message { // {{{
|
||||
reg, e := regexp.Compile(key)
|
||||
m.Assert(e)
|
||||
|
||||
target := m.Target
|
||||
target := m.target
|
||||
if len(root) > 0 && root[0] {
|
||||
target = m.Target.root
|
||||
target = m.target.root
|
||||
}
|
||||
|
||||
cs := make([]*Context, 0, 3)
|
||||
m.Travel(target, func(m *Message) bool {
|
||||
if reg.MatchString(m.Target.Name) || reg.FindString(m.Target.Help) != "" {
|
||||
if reg.MatchString(m.target.Name) || reg.FindString(m.target.Help) != "" {
|
||||
m.Log("search", nil, "%d match [%s]", len(cs)+1, key)
|
||||
cs = append(cs, m.Target)
|
||||
cs = append(cs, m.target)
|
||||
}
|
||||
return true
|
||||
})
|
||||
@ -689,9 +744,9 @@ func (m *Message) Search(key string, root ...bool) []*Message { // {{{
|
||||
|
||||
// }}}
|
||||
func (m *Message) Find(name string, root ...bool) *Message { // {{{
|
||||
target := m.Target.root
|
||||
target := m.target.root
|
||||
if len(root) > 0 && !root[0] {
|
||||
target = m.Target
|
||||
target = m.target
|
||||
}
|
||||
|
||||
cs := target.contexts
|
||||
@ -709,7 +764,7 @@ func (m *Message) Find(name string, root ...bool) *Message { // {{{
|
||||
|
||||
// }}}
|
||||
func (m *Message) Start(name string, help string, arg ...string) bool { // {{{
|
||||
return m.Set("detail", arg...).Target.Spawn(m, name, help).Begin(m).Start(m)
|
||||
return m.Set("detail", arg...).target.Spawn(m, name, help).Begin(m).Start(m)
|
||||
}
|
||||
|
||||
// }}}
|
||||
@ -836,10 +891,10 @@ func (m *Message) End(s bool) { // {{{
|
||||
|
||||
func (m *Message) Exec(key string, arg ...string) string { // {{{
|
||||
|
||||
for _, c := range []*Context{m.Target, m.Target.Master, m.Target.Owner, m.Source, m.Source.Master, m.Source.Owner} {
|
||||
for _, c := range []*Context{m.target, m.target.master, m.target.Owner, m.source, m.source.master, m.source.Owner} {
|
||||
for s := c; s != nil; s = s.context {
|
||||
|
||||
m.Master = m.Source
|
||||
m.master = m.source
|
||||
if x, ok := s.Commands[key]; ok && x.Hand != nil && m.Check(c, "commands", key) {
|
||||
m.AssertOne(m, true, func(m *Message) {
|
||||
m.Log("cmd", s, "%s %v %v", key, arg, m.Meta["option"])
|
||||
@ -902,12 +957,12 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
|
||||
|
||||
// }}}
|
||||
func (m *Message) Deal(pre func(msg *Message, arg ...string) bool, post func(msg *Message, arg ...string) bool) { // {{{
|
||||
if m.Target.messages == nil {
|
||||
m.Target.messages = make(chan *Message, m.Confi("MessageQueueSize"))
|
||||
if m.target.messages == nil {
|
||||
m.target.messages = make(chan *Message, m.Confi("MessageQueueSize"))
|
||||
}
|
||||
|
||||
for run := true; run; {
|
||||
m.AssertOne(<-m.Target.messages, true, func(msg *Message) {
|
||||
m.AssertOne(<-m.target.messages, true, func(msg *Message) {
|
||||
defer msg.End(true)
|
||||
|
||||
if len(msg.Meta["detail"]) == 0 {
|
||||
@ -932,7 +987,7 @@ func (m *Message) Deal(pre func(msg *Message, arg ...string) bool, post func(msg
|
||||
// }}}
|
||||
func (m *Message) Post(s *Context) string { // {{{
|
||||
if s == nil {
|
||||
s = m.Target.Master
|
||||
s = m.target.master
|
||||
}
|
||||
|
||||
m.Assert(s.messages != nil, s.Name+" 没有开启消息处理")
|
||||
@ -951,7 +1006,7 @@ func (m *Message) Cmd(arg ...string) string { // {{{
|
||||
m.Set("detail", arg...)
|
||||
}
|
||||
|
||||
if s := m.Target.Master; s != nil && s != m.Source.Master {
|
||||
if s := m.target.master; s != nil && s != m.source.master {
|
||||
return m.Post(s)
|
||||
}
|
||||
|
||||
@ -974,7 +1029,7 @@ func (m *Message) Confi(key string, arg ...int) int { // {{{
|
||||
// }}}
|
||||
func (m *Message) Conf(key string, arg ...string) string { // {{{
|
||||
var hand func(m *Message, x *Config, arg ...string) string
|
||||
for s := m.Target; s != nil; s = s.context {
|
||||
for s := m.target; s != nil; s = s.context {
|
||||
if x, ok := s.Configs[key]; ok {
|
||||
if !m.Check(s, "configs", key) {
|
||||
continue
|
||||
@ -1005,12 +1060,12 @@ func (m *Message) Conf(key string, arg ...string) string { // {{{
|
||||
}
|
||||
}
|
||||
|
||||
if len(arg) == 3 && m.Check(m.Target, "configs", key) {
|
||||
if m.Target.Configs == nil {
|
||||
m.Target.Configs = make(map[string]*Config)
|
||||
if len(arg) == 3 && m.Check(m.target, "configs", key) {
|
||||
if m.target.Configs == nil {
|
||||
m.target.Configs = make(map[string]*Config)
|
||||
}
|
||||
|
||||
m.Target.Configs[key] = &Config{Name: arg[0], Value: arg[1], Help: arg[2], Hand: hand}
|
||||
m.target.Configs[key] = &Config{Name: arg[0], Value: arg[1], Help: arg[2], Hand: hand}
|
||||
m.Log("conf", nil, "%s %v", key, arg)
|
||||
return m.Conf(key, arg[1])
|
||||
}
|
||||
@ -1035,7 +1090,7 @@ func (m *Message) Capi(key string, arg ...int) int { // {{{
|
||||
// }}}
|
||||
func (m *Message) Cap(key string, arg ...string) string { // {{{
|
||||
var hand func(m *Message, x *Cache, arg ...string) string
|
||||
for s := m.Target; s != nil; s = s.context {
|
||||
for s := m.target; s != nil; s = s.context {
|
||||
if x, ok := s.Caches[key]; ok {
|
||||
if !m.Check(s, "caches", key) {
|
||||
continue
|
||||
@ -1064,12 +1119,12 @@ func (m *Message) Cap(key string, arg ...string) string { // {{{
|
||||
}
|
||||
}
|
||||
|
||||
if len(arg) == 3 && m.Check(m.Target, "caches", key) {
|
||||
if m.Target.Caches == nil {
|
||||
m.Target.Caches = make(map[string]*Cache)
|
||||
if len(arg) == 3 && m.Check(m.target, "caches", key) {
|
||||
if m.target.Caches == nil {
|
||||
m.target.Caches = make(map[string]*Cache)
|
||||
}
|
||||
|
||||
m.Target.Caches[key] = &Cache{Name: arg[0], Value: arg[1], Help: arg[2], Hand: hand}
|
||||
m.target.Caches[key] = &Cache{Name: arg[0], Value: arg[1], Help: arg[2], Hand: hand}
|
||||
m.Log("cap", nil, "%s %v", key, arg)
|
||||
return m.Cap(key, arg[1])
|
||||
}
|
||||
@ -1081,7 +1136,7 @@ func (m *Message) Cap(key string, arg ...string) string { // {{{
|
||||
|
||||
// }}}
|
||||
|
||||
var Pulse = &Message{code: 0, time: time.Now(), Wait: make(chan bool), Source: Index, Master: Index, Target: Index}
|
||||
var Pulse = &Message{code: 0, time: time.Now(), Wait: make(chan bool), source: Index, master: Index, target: Index}
|
||||
var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
Caches: map[string]*Cache{
|
||||
"nserver": &Cache{Name: "服务数量", Value: "0", Help: "显示已经启动运行模块的数量"},
|
||||
@ -1142,19 +1197,19 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
switch { // {{{
|
||||
case m.Has("add"):
|
||||
m.Target.Add(m.Source.Group, m.Meta["add"]...)
|
||||
m.target.Add(m.source.Group, m.Meta["add"]...)
|
||||
case m.Has("del"):
|
||||
m.Target.Del(m.Meta["del"]...)
|
||||
m.target.Del(m.Meta["del"]...)
|
||||
default:
|
||||
target := m.Target
|
||||
m.Target = target.Owner
|
||||
if m.Target != nil && m.Check(m.Target) {
|
||||
target := m.target
|
||||
m.target = target.Owner
|
||||
if m.target != nil && m.Check(m.target) {
|
||||
m.Echo("%s %s\n", m.Cap("username"), m.Cap("group"))
|
||||
}
|
||||
m.Target = target
|
||||
m.target = target
|
||||
|
||||
if len(m.Meta["args"]) > 0 {
|
||||
if g, ok := m.Target.Index[m.Get("args")]; ok {
|
||||
if g, ok := m.target.Index[m.Get("args")]; ok {
|
||||
for k, _ := range g.Commands {
|
||||
m.Echo("cmd: %s\n", k)
|
||||
}
|
||||
@ -1166,7 +1221,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for k, v := range m.Target.Index {
|
||||
for k, v := range m.target.Index {
|
||||
m.Echo("%s", k)
|
||||
m.Echo(": %s %s\n", v.Name, v.Help)
|
||||
}
|
||||
@ -1177,9 +1232,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
"server": &Command{Name: "server [start|exit|switch][args]", Help: "服务启动停止切换", Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
switch len(arg) { // {{{
|
||||
case 0:
|
||||
m.Travel(m.Target.root, func(m *Message) bool {
|
||||
if x, ok := m.Target.Caches["status"]; ok {
|
||||
m.Echo("%s(%s): %s\n", m.Target.Name, x.Value, m.Target.Help)
|
||||
m.Travel(m.target.root, func(m *Message) bool {
|
||||
if x, ok := m.target.Caches["status"]; ok {
|
||||
m.Echo("%s(%s): %s\n", m.target.Name, x.Value, m.target.Help)
|
||||
}
|
||||
return true
|
||||
})
|
||||
@ -1188,9 +1243,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
switch arg[0] {
|
||||
case "start":
|
||||
m.Meta = nil
|
||||
m.Set("detail", arg[1:]...).Target.Start(m)
|
||||
m.Set("detail", arg[1:]...).target.Start(m)
|
||||
case "stop":
|
||||
m.Set("detail", arg[1:]...).Target.Close(m)
|
||||
m.Set("detail", arg[1:]...).target.Close(m)
|
||||
case "switch":
|
||||
}
|
||||
}
|
||||
@ -1200,7 +1255,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
switch len(arg) { // {{{
|
||||
case 0:
|
||||
m.Echo("\033[31mrequests:\033[0m\n")
|
||||
for i, v := range m.Target.Requests {
|
||||
for i, v := range m.target.Requests {
|
||||
m.Echo("%d %s\n", i, v.Format())
|
||||
for i, v := range v.messages {
|
||||
m.Echo(" %d %s\n", i, v.Format())
|
||||
@ -1208,12 +1263,12 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
|
||||
m.Echo("\033[32msessions:\033[0m\n")
|
||||
for k, v := range m.Target.Sessions {
|
||||
for k, v := range m.target.Sessions {
|
||||
m.Echo("%s %s\n", k, v.Format())
|
||||
}
|
||||
|
||||
m.Echo("\033[33mhistorys:\033[0m\n")
|
||||
for i, v := range m.Target.Historys {
|
||||
for i, v := range m.target.Historys {
|
||||
m.Echo("%d %s\n", i, v.Format())
|
||||
for i, v := range v.messages {
|
||||
m.Echo(" %d %s\n", i, v.Format())
|
||||
@ -1293,44 +1348,44 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
v.Data = m.Data
|
||||
switch {
|
||||
case m.Has("switch"):
|
||||
m.Target = v.Target
|
||||
m.target = v.target
|
||||
case m.Has("spawn"):
|
||||
v.Set("detail", arg[2:]...).Target.Spawn(v, arg[0], arg[1]).Begin(v)
|
||||
m.Target = v.Target
|
||||
v.Set("detail", arg[2:]...).target.Spawn(v, arg[0], arg[1]).Begin(v)
|
||||
m.target = v.target
|
||||
case m.Has("start"):
|
||||
v.Set("detail", arg...).Target.Start(v)
|
||||
m.Target = v.Target
|
||||
v.Set("detail", arg...).target.Start(v)
|
||||
m.target = v.target
|
||||
case m.Has("close"):
|
||||
v.Target.Close(v)
|
||||
v.target.Close(v)
|
||||
case m.Has("show"):
|
||||
m.Echo("%s(%s): %s\n", v.Target.Name, v.Target.Owner.Name, v.Target.Help)
|
||||
if len(v.Target.Requests) > 0 {
|
||||
m.Echo("%s(%s): %s\n", v.target.Name, v.target.Owner.Name, v.target.Help)
|
||||
if len(v.target.Requests) > 0 {
|
||||
m.Echo("模块资源:\n")
|
||||
for i, v := range v.Target.Requests {
|
||||
m.Echo(" %d: <- %s %s\n", i, v.Source.Name, v.Meta["detail"])
|
||||
for i, v := range v.target.Requests {
|
||||
m.Echo(" %d: <- %s %s\n", i, v.source.Name, v.Meta["detail"])
|
||||
// for i, v := range v.Messages {
|
||||
// m.Echo(" %d: -> %s %s\n", i, v.Source.Name, v.Meta["detail"])
|
||||
// m.Echo(" %d: -> %s %s\n", i, v.source.Name, v.Meta["detail"])
|
||||
// }
|
||||
}
|
||||
}
|
||||
if len(v.Target.Sessions) > 0 {
|
||||
if len(v.target.Sessions) > 0 {
|
||||
m.Echo("模块引用:\n")
|
||||
for k, v := range v.Target.Sessions {
|
||||
m.Echo(" %s: -> %s %v\n", k, v.Target.Name, v.Meta["detail"])
|
||||
for k, v := range v.target.Sessions {
|
||||
m.Echo(" %s: -> %s %v\n", k, v.target.Name, v.Meta["detail"])
|
||||
}
|
||||
}
|
||||
case m.Has("info"):
|
||||
switch m.Get("info") {
|
||||
case "name":
|
||||
m.Echo("%s", v.Target.Name)
|
||||
m.Echo("%s", v.target.Name)
|
||||
case "owner":
|
||||
m.Echo("%s", v.Target.Owner.Name)
|
||||
m.Echo("%s", v.target.Owner.Name)
|
||||
default:
|
||||
m.Echo("%s(%s): %s\n", v.Target.Name, v.Target.Owner.Name, v.Target.Help)
|
||||
m.Echo("%s(%s): %s\n", v.target.Name, v.target.Owner.Name, v.target.Help)
|
||||
}
|
||||
case m.Has("list") || len(m.Meta["detail"]) == 1:
|
||||
m.Travel(v.Target, func(msg *Message) bool {
|
||||
target := msg.Target
|
||||
m.Travel(v.target, func(msg *Message) bool {
|
||||
target := msg.target
|
||||
m.Echo("%s(", target.Name)
|
||||
|
||||
if target.context != nil {
|
||||
@ -1338,8 +1393,8 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
m.Echo(":")
|
||||
|
||||
if target.Master != nil {
|
||||
m.Echo("%s", target.Master.Name)
|
||||
if target.master != nil {
|
||||
m.Echo("%s", target.master.Name)
|
||||
}
|
||||
m.Echo(":")
|
||||
|
||||
@ -1348,14 +1403,14 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
m.Echo(":")
|
||||
|
||||
msg.Target = msg.Target.Owner
|
||||
if msg.Target != nil && msg.Check(msg.Target, "caches", "username") && msg.Check(msg.Target, "caches", "group") {
|
||||
msg.target = msg.target.Owner
|
||||
if msg.target != nil && msg.Check(msg.target, "caches", "username") && msg.Check(msg.target, "caches", "group") {
|
||||
m.Echo("%s:%s", msg.Cap("username"), msg.Cap("group"))
|
||||
}
|
||||
m.Echo("): ")
|
||||
msg.Target = target
|
||||
msg.target = target
|
||||
|
||||
if msg.Check(msg.Target, "caches", "status") && msg.Check(msg.Target, "caches", "stream") {
|
||||
if msg.Check(msg.target, "caches", "status") && msg.Check(msg.target, "caches", "stream") {
|
||||
m.Echo("%s(%s) ", msg.Cap("status"), msg.Cap("stream"))
|
||||
}
|
||||
m.Echo("%s\n", target.Help)
|
||||
@ -1364,9 +1419,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
case len(arg) > 0 && v != m:
|
||||
v.Cmd(arg...)
|
||||
// m.Meta = v.Meta
|
||||
// m.Target = target
|
||||
// m.target = target
|
||||
default:
|
||||
m.Target = v.Target
|
||||
m.target = v.target
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
@ -1380,10 +1435,10 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
case 0:
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if all {
|
||||
m.Echo("%s comands:\n", m.Target.Name)
|
||||
m.Echo("%s comands:\n", m.target.Name)
|
||||
}
|
||||
for k, x := range m.Target.Commands {
|
||||
if m.Check(m.Target, "commands", k) {
|
||||
for k, x := range m.target.Commands {
|
||||
if m.Check(m.target, "commands", k) {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
}
|
||||
@ -1395,14 +1450,14 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
case 1:
|
||||
switch {
|
||||
case m.Has("delete"):
|
||||
if _, ok := m.Target.Commands[arg[0]]; ok {
|
||||
if m.Target.Owner == nil || m.Master.Owner == m.Target.Owner {
|
||||
delete(m.Target.Commands, arg[0])
|
||||
if _, ok := m.target.Commands[arg[0]]; ok {
|
||||
if m.target.Owner == nil || m.master.Owner == m.target.Owner {
|
||||
delete(m.target.Commands, arg[0])
|
||||
}
|
||||
}
|
||||
case m.Has("void"):
|
||||
if x, ok := m.Target.Commands[arg[0]]; ok {
|
||||
if m.Target.Owner == nil || m.Master.Owner == m.Target.Owner {
|
||||
if x, ok := m.target.Commands[arg[0]]; ok {
|
||||
if m.target.Owner == nil || m.master.Owner == m.target.Owner {
|
||||
x.Hand = nil
|
||||
}
|
||||
}
|
||||
@ -1410,13 +1465,13 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if all {
|
||||
m.Echo("%s commands:\n", m.Target.Name)
|
||||
m.Echo("%s commands:\n", m.target.Name)
|
||||
}
|
||||
if x, ok := m.Target.Commands[arg[0]]; ok {
|
||||
if x, ok := m.target.Commands[arg[0]]; ok {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
}
|
||||
if m.Check(m.Target, "commands", arg[0]) {
|
||||
if m.Check(m.target, "commands", arg[0]) {
|
||||
m.Echo("%s\n %s\n", x.Name, x.Help)
|
||||
}
|
||||
}
|
||||
@ -1425,26 +1480,26 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
case 3:
|
||||
cmd := &Command{}
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if x, ok := m.Target.Commands[arg[0]]; ok && x.Hand != nil {
|
||||
if x, ok := m.target.Commands[arg[0]]; ok && x.Hand != nil {
|
||||
*cmd = *x
|
||||
}
|
||||
return all
|
||||
})
|
||||
|
||||
if m.Check(m.Target, "commands", arg[0]) {
|
||||
if x, ok := m.Target.Commands[arg[0]]; ok {
|
||||
if m.Target.Owner == nil || m.Master.Owner == m.Target.Owner {
|
||||
if m.Check(m.target, "commands", arg[0]) {
|
||||
if x, ok := m.target.Commands[arg[0]]; ok {
|
||||
if m.target.Owner == nil || m.master.Owner == m.target.Owner {
|
||||
x.Name = arg[1]
|
||||
x.Help = arg[2]
|
||||
m.Echo("%s\n %s\n", x.Name, x.Help)
|
||||
}
|
||||
} else {
|
||||
if m.Target.Commands == nil {
|
||||
m.Target.Commands = map[string]*Command{}
|
||||
if m.target.Commands == nil {
|
||||
m.target.Commands = map[string]*Command{}
|
||||
}
|
||||
cmd.Name = arg[1]
|
||||
cmd.Help = arg[2]
|
||||
m.Target.Commands[arg[0]] = cmd
|
||||
m.target.Commands[arg[0]] = cmd
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1459,10 +1514,10 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
case 0:
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if all {
|
||||
m.Echo("%s configs:\n", m.Target.Name)
|
||||
m.Echo("%s configs:\n", m.target.Name)
|
||||
}
|
||||
for k, x := range m.Target.Configs {
|
||||
if m.Check(m.Target, "configs", k) {
|
||||
for k, x := range m.target.Configs {
|
||||
if m.Check(m.target, "configs", k) {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
}
|
||||
@ -1474,9 +1529,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
case 1:
|
||||
switch {
|
||||
case m.Has("delete"):
|
||||
if _, ok := m.Target.Configs[arg[0]]; ok {
|
||||
if m.Target.Owner == nil || m.Master.Owner == m.Target.Owner {
|
||||
delete(m.Target.Configs, arg[0])
|
||||
if _, ok := m.target.Configs[arg[0]]; ok {
|
||||
if m.target.Owner == nil || m.master.Owner == m.target.Owner {
|
||||
delete(m.target.Configs, arg[0])
|
||||
}
|
||||
}
|
||||
case m.Has("void"):
|
||||
@ -1485,10 +1540,10 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
// if all {
|
||||
// m.Echo("%s config:\n", m.Target.Name)
|
||||
// m.Echo("%s config:\n", m.target.Name)
|
||||
// }
|
||||
if x, ok := m.Target.Configs[arg[0]]; ok {
|
||||
if m.Check(m.Target, "configs", arg[0]) {
|
||||
if x, ok := m.target.Configs[arg[0]]; ok {
|
||||
if m.Check(m.target, "configs", arg[0]) {
|
||||
// if all {
|
||||
// m.Echo(" ")
|
||||
// }
|
||||
@ -1517,10 +1572,10 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
case 0:
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if all {
|
||||
m.Echo("%s configs:\n", m.Target.Name)
|
||||
m.Echo("%s configs:\n", m.target.Name)
|
||||
}
|
||||
for k, x := range m.Target.Caches {
|
||||
if m.Check(m.Target, "caches", k) {
|
||||
for k, x := range m.target.Caches {
|
||||
if m.Check(m.target, "caches", k) {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
}
|
||||
@ -1533,9 +1588,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
case 1:
|
||||
switch {
|
||||
case m.Has("delete"):
|
||||
if _, ok := m.Target.Caches[arg[0]]; ok {
|
||||
if m.Target.Owner == nil || m.Master.Owner == m.Target.Owner {
|
||||
delete(m.Target.Caches, arg[0])
|
||||
if _, ok := m.target.Caches[arg[0]]; ok {
|
||||
if m.target.Owner == nil || m.master.Owner == m.target.Owner {
|
||||
delete(m.target.Caches, arg[0])
|
||||
}
|
||||
}
|
||||
case m.Has("void"):
|
||||
@ -1544,10 +1599,10 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
// if all {
|
||||
// m.Echo("%s config:\n", m.Target.Name)
|
||||
// m.Echo("%s config:\n", m.target.Name)
|
||||
// }
|
||||
if x, ok := m.Target.Caches[arg[0]]; ok {
|
||||
if m.Check(m.Target, "caches", arg[0]) {
|
||||
if x, ok := m.target.Caches[arg[0]]; ok {
|
||||
if m.Check(m.target, "caches", arg[0]) {
|
||||
// if all {
|
||||
// m.Echo(" ")
|
||||
// }
|
||||
@ -1606,16 +1661,16 @@ func Start(args ...string) {
|
||||
log.Println("\n\n")
|
||||
Index.Group = "root"
|
||||
Index.Owner = Index.contexts["aaa"]
|
||||
Index.Master = Index.contexts["cli"]
|
||||
Index.master = Index.contexts["cli"]
|
||||
for _, m := range Pulse.Search("") {
|
||||
m.Target.root = Index
|
||||
m.Target.Begin(m)
|
||||
m.target.root = Index
|
||||
m.target.Begin(m)
|
||||
}
|
||||
Index.Requests = append(Index.Requests, Pulse)
|
||||
log.Println()
|
||||
|
||||
for _, m := range Pulse.Search(Pulse.Conf("start")) {
|
||||
m.Put("option", "io", os.Stdout).Target.Start(m)
|
||||
m.Set("detail", "stdout").target.Start(m)
|
||||
}
|
||||
|
||||
for <-Pulse.Wait; Pulse.Capi("nserver") > 0; <-Pulse.Wait {
|
||||
|
@ -49,7 +49,7 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
||||
ssh.send = make(map[string]*ctx.Message)
|
||||
m.Log("info", nil, "%s connect %v <-> %v", Pulse.Cap("nhost"), ssh.Conn.LocalAddr(), ssh.Conn.RemoteAddr())
|
||||
|
||||
target, msg := m.Target, m.Spawn(m.Target)
|
||||
target, msg := m.Target(), m.Spawn(m.Target())
|
||||
|
||||
for {
|
||||
line, e := ssh.Reader.ReadString('\n')
|
||||
@ -60,7 +60,7 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
||||
msg.Log("info", nil, "%d exec: %v", m.Capi("nrecv", 1), msg.Meta["detail"])
|
||||
|
||||
msg.Cmd(msg.Meta["detail"]...)
|
||||
target = msg.Target
|
||||
target = msg.Target()
|
||||
m.Cap("target", target.Name)
|
||||
|
||||
for _, v := range msg.Meta["result"] {
|
||||
@ -99,8 +99,8 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
||||
|
||||
func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool {
|
||||
switch ssh.Context {
|
||||
case m.Target:
|
||||
case m.Source:
|
||||
case m.Target():
|
||||
case m.Source():
|
||||
}
|
||||
return true
|
||||
}
|
||||
@ -122,7 +122,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
||||
m.Start(fmt.Sprintf("host%d", Pulse.Capi("nhost", 1)), "主机连接")
|
||||
}},
|
||||
"remote": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
ssh, ok := m.Target.Server.(*SSH)
|
||||
ssh, ok := m.Target().Server.(*SSH)
|
||||
m.Assert(ok)
|
||||
|
||||
m.Capi("nsend", 1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user