1
0
forked from x/ContextOS

Merge branch '0.2.0'

Conflicts:
	etc/init.sh
	src/context/cli/cli.go
This commit is contained in:
shaoying 2017-12-26 08:34:34 +08:00
commit 0679f8e231
4 changed files with 71 additions and 49 deletions

View File

@ -1,25 +1,27 @@
@lex lex
~cli ~cli
@lex lex @lex lex
~aaa login root root ~aaa login root root
~cli var a = 10
if $username == root
echo welcome root user: $username
end
~web serve ~web serve
return
function nice ~ssh dial chat.shylinux.com:9090 true
echo who
end
return hello hello
sleep 1 sleep 1
~host1 ~host1
remote context mpa register terminal shhylinux term term term 1 ~nfs load usr/sess.txt
$sessid $result var a = $result
remote cache sessid $sessid return
~nfs save usr/sess.txt "terminal: " $sessid $a = $result
~nfs genqr usr/sess.png "terminal: " $sessid if $a != ""
remote context mpa register $a
end
if $a == ""
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 return
# ~ssh dial chat.shylinux.com:9090 true # ~ssh dial chat.shylinux.com:9090 true
# ~cli # ~cli

View File

@ -58,6 +58,11 @@ func (cli *CLI) parse(m *ctx.Message) bool {
cli.which++ cli.which++
} else { } else {
l, e := cli.bio.ReadString('\n') l, e := cli.bio.ReadString('\n')
if e == io.EOF {
cli.exit = true
m.Spawn(cli.target).Set("detail", "end").Post(cli.Context)
return false
}
m.Assert(e) m.Assert(e)
line = l line = l
} }
@ -84,11 +89,13 @@ func (cli *CLI) parse(m *ctx.Message) bool {
msg := m.Spawn(cli.target) msg := m.Spawn(cli.target)
for i := 0; i < len(ls); i++ { for i := 0; i < len(ls); i++ {
if ls[i] = strings.TrimSpace(ls[i]); ls[i] == "" { if cli.lex == nil {
continue if ls[i] = strings.TrimSpace(ls[i]); ls[i] == "" {
} continue
if ls[i][0] == '#' { }
break if ls[i][0] == '#' {
break
}
} }
if r := rune(ls[i][0]); r == '$' || r == '_' || (!unicode.IsNumber(r) && !unicode.IsLetter(r)) { if r := rune(ls[i][0]); r == '$' || r == '_' || (!unicode.IsNumber(r) && !unicode.IsLetter(r)) {
@ -115,14 +122,14 @@ func (cli *CLI) parse(m *ctx.Message) bool {
if msg.Target().Context() != nil || msg.Target() == ctx.Index { if msg.Target().Context() != nil || msg.Target() == ctx.Index {
cli.target = msg.Target() cli.target = msg.Target()
} }
m.Cap("target", cli.target.Name)
result := strings.TrimRight(strings.Join(msg.Meta["result"], ""), "\n") result := strings.TrimRight(strings.Join(msg.Meta["result"], ""), "\n")
m.Cap("result", result) if m.Cap("result", result); len(result) > 0 {
if len(result) > 0 {
cli.echo(result + "\n") cli.echo(result + "\n")
} }
m.Cap("target", cli.target.Name)
m.Cap("back", line) if m.Cap("back", line); cli.bio != nil {
if cli.bio != nil {
cli.lines = append(cli.lines, line) cli.lines = append(cli.lines, line)
} }
return !cli.exit return !cli.exit
@ -187,7 +194,7 @@ 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 { cli.Configs["lex"] = &ctx.Config{Name: "词法解析器", Value: "", Help: "命令行词法解析器", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
if len(arg) > 0 { // {{{ if len(arg) > 0 && len(arg[0]) > 0 { // {{{
cli, ok := m.Target().Server.(*CLI) cli, ok := m.Target().Server.(*CLI)
m.Assert(ok, "模块类型错误") m.Assert(ok, "模块类型错误")
@ -202,6 +209,7 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server {
m.Spawn(lex.Target()).Cmd("train", "#[^\n]*\n", "void", "void") m.Spawn(lex.Target()).Cmd("train", "#[^\n]*\n", "void", "void")
} }
cli.lex = lex cli.lex = lex
return arg[0]
} }
return x.Value return x.Value
// }}} // }}}
@ -218,11 +226,11 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server {
cli.target = cli.Context cli.target = cli.Context
cli.alias = map[string]string{ cli.alias = map[string]string{
"~": "context", "~": "context",
"!": "command", "!": "message",
"@": "config", "@": "config",
"$": "cache", "$": "cache",
"&": "server", "&": "server",
"*": "message", ":": "command",
} }
return cli return cli
@ -363,28 +371,31 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
Configs: map[string]*ctx.Config{}, Configs: map[string]*ctx.Config{},
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"sleep": &ctx.Command{Name: "sleep time", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "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]) t, e := strconv.Atoi(arg[0]) // {{{
m.Assert(e) m.Assert(e)
m.Log("info", nil, "sleep %ds", t) m.Log("info", nil, "sleep %ds", t)
time.Sleep(time.Second * time.Duration(t)) time.Sleep(time.Second * time.Duration(t))
m.Log("info", nil, "sleep %ds done", t) m.Log("info", nil, "sleep %ds done", t)
// }}}
}}, }},
"source": &ctx.Command{Name: "source file", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "source": &ctx.Command{Name: "source file", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
f, e := os.Open(arg[0]) f, e := os.Open(arg[0]) // {{{
m.Assert(e) m.Assert(e)
m.Put("option", "file", f).Start(key, "脚本文件") m.Put("option", "file", f).Start(key, "脚本文件")
<-m.Target().Exit <-m.Target().Exit
// }}}
}}, }},
"return": &ctx.Command{Name: "return result...", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "return": &ctx.Command{Name: "return result...", Help: "结束脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
cli, ok := m.Source().Server.(*CLI) cli, ok := m.Source().Server.(*CLI) // {{{
m.Assert(ok) m.Assert(ok)
cli.exit = true cli.exit = true
for _, v := range arg { for _, v := range arg {
cli.Pulse.Echo(v) cli.Pulse.Echo(v)
} }
// }}}
}}, }},
"if": &ctx.Command{Name: "if a [ == | != ] b", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "if": &ctx.Command{Name: "if a [ == | != ] b", Help: "条件语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
cli, ok := m.Source().Server.(*CLI) cli, ok := m.Source().Server.(*CLI) // {{{
m.Assert(ok) m.Assert(ok)
if arg[1] == "==" && arg[0] != arg[2] { if arg[1] == "==" && arg[0] != arg[2] {
@ -395,9 +406,10 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
} }
m.Put("option", "file", cli.bio).Start(key, "条件语句") m.Put("option", "file", cli.bio).Start(key, "条件语句")
<-m.Target().Exit <-m.Target().Exit
// }}}
}}, }},
"for": &ctx.Command{Name: "for var in list", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "for": &ctx.Command{Name: "for var in list", Help: "循环语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
cli, ok := m.Source().Server.(*CLI) cli, ok := m.Source().Server.(*CLI) // {{{
m.Assert(ok) m.Assert(ok)
if arg[1] == "==" && arg[0] != arg[2] { if arg[1] == "==" && arg[0] != arg[2] {
@ -408,33 +420,31 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
} }
m.Put("option", "file", cli.bio).Start(key, "条件语句") m.Put("option", "file", cli.bio).Start(key, "条件语句")
<-m.Target().Exit <-m.Target().Exit
// }}}
}}, }},
"end": &ctx.Command{Name: "end", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "end": &ctx.Command{Name: "end", Help: "结束语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
cli, ok := m.Source().Server.(*CLI) cli, ok := m.Source().Server.(*CLI) // {{{
m.Assert(ok) m.Assert(ok)
cli.exit = true cli.exit = true
m.Log("fuck", nil, "%v", cli.lines) // }}}
m.Echo(strings.Join(arg, ""))
}}, }},
"var": &ctx.Command{Name: "var a [= b]", Help: "定义变量", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "var": &ctx.Command{Name: "var a [= b]", Help: "定义变量", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
cli, ok := m.Source().Server.(*CLI)
m.Assert(ok)
val := "" val := ""
if len(arg) > 2 { if len(arg) > 2 {
val = arg[2] val = arg[2]
} }
cli.Pulse.Cap(arg[0], arg[0], val, "临时变量") m.Cap(arg[0], arg[0], val, "临时变量")
}}, }},
"function": &ctx.Command{Name: "function name", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "function": &ctx.Command{Name: "function name", Help: "定义函数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
cli, ok := m.Source().Server.(*CLI) cli, ok := m.Source().Server.(*CLI) // {{{
m.Target(m.Source().Context()) m.Target(m.Source().Context())
m.Assert(ok) m.Assert(ok)
m.Add("option", "test") m.Add("option", "test")
m.Add("option", "save") m.Add("option", "save")
m.Put("option", "file", cli.bio).Start(arg[0], "定义函数") m.Put("option", "file", cli.bio).Start(arg[0], "定义函数")
m.Log("fuck", nil, "end function") // }}}
}}, }},
"alias": &ctx.Command{Name: "alias [short [long]]", Help: "查看日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "alias": &ctx.Command{Name: "alias [short [long]]", Help: "定义别名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
cli := c.Server.(*CLI) // {{{ cli := c.Server.(*CLI) // {{{
switch len(arg) { switch len(arg) {
case 0: case 0:

View File

@ -239,6 +239,17 @@ func (c *Context) Master(s ...*Context) *Context { // {{{
return c.master return c.master
} }
// }}}
func (c *Context) Has(key string) bool { // {{{
if _, ok := c.Caches[key]; ok {
return true
}
if _, ok := c.Configs[key]; ok {
return true
}
return false
}
// }}} // }}}
func (c *Context) Add(group string, arg ...string) { // {{{ func (c *Context) Add(group string, arg ...string) { // {{{

View File

@ -152,7 +152,6 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
size = s size = s
} }
} }
m.Log("fuck", nil, "%v %v", arg[0], arg[1:])
qrcode.WriteFile(strings.Join(arg[1:], ""), qrcode.Medium, size, arg[0]) qrcode.WriteFile(strings.Join(arg[1:], ""), qrcode.Medium, size, arg[0])
}}, }},
}, },