1
0
mirror of https://shylinux.com/x/ContextOS synced 2025-04-25 16:58:06 +08:00

opt ctx.go

This commit is contained in:
shaoying 2019-01-14 11:46:39 +08:00
parent 160a51a494
commit e9e4b4e593
10 changed files with 3484 additions and 3448 deletions

View File

@ -1,24 +1,22 @@
BENCH=src/examples/bench.go BENCH=src/examples/app/bench.go
install: install:
@go get github.com/go-sql-driver/mysql
@go get github.com/go-cas/cas
@go get github.com/nsf/termbox-go @go get github.com/nsf/termbox-go
@go get github.com/skip2/go-qrcode @go get github.com/skip2/go-qrcode
@go get github.com/go-sql-driver/mysql
@go get github.com/gomarkdown/markdown @go get github.com/gomarkdown/markdown
@go get github.com/PuerkitoBio/goquery @go get github.com/PuerkitoBio/goquery
@go get github.com/go-cas/cas
GOPATH=$(PWD):$(GOPATH) go install $(BENCH) GOPATH=$(PWD):$(GOPATH) go install $(BENCH)
@date @date
# bench web.code.counter nmake 1 # bench web.code.counter nmake 1
install_all: install install_all: install
touch etc/local.shy
touch etc/local_exit.shy
touch etc/init.shy touch etc/init.shy
touch etc/exit.shy touch etc/exit.shy
touch etc/login.txt touch etc/local.shy
touch etc/history.txt touch etc/local_exit.shy
run: run:
etc/bootstrap.sh etc/bootstrap.sh
@ -51,33 +49,30 @@ tar_all: tar linux64 darwin win64
mv bench.win64.exe tar/bin/ mv bench.win64.exe tar/bin/
tar zcvf tar.tgz tar tar zcvf tar.tgz tar
linux64:
GOARCH=amd64 GOOS=linux go build -o bench.linux64 $(BENCH)
linux32:
GOARCH=386 GOOS=linux go build -o bench.linux32 $(BENCH)
linux_arm: linux_arm:
GOARCH=arm GOOS=linux go build -o bench.linux.arm $(BENCH) GOARCH=arm GOOS=linux go build -o bench.linux.arm $(BENCH)
linux32:
GOARCH=386 GOOS=linux go build -o bench.linux32 $(BENCH)
linux64:
GOARCH=amd64 GOOS=linux go build -o bench.linux64 $(BENCH)
darwin: darwin:
GOARCH=amd64 GOOS=darwin go build -o bench.darwin $(BENCH) GOARCH=amd64 GOOS=darwin go build -o bench.darwin $(BENCH)
win64:
GOARCH=amd64 GOOS=windows go build -o bench.win64.exe $(BENCH)
win32: win32:
GOARCH=386 GOOS=windows go build -o bench.win32.exe $(BENCH) GOARCH=386 GOOS=windows go build -o bench.win32.exe $(BENCH)
win64:
GOARCH=amd64 GOOS=windows go build -o bench.win64.exe $(BENCH)
DOTS=etc/dotsfile DOTS=etc/dotsfile
back_dotsfile: back_dotsfile:
cp ~/.zshrc $(DOTS) cp ~/.zshrc $(DOTS)
cp ~/.tmux.conf $(DOTS) cp ~/.tmux.conf $(DOTS)
# cp ~/context/.git/hooks/post-commit $(DOTS)/git_hooks
cp ~/.vimrc $(DOTS) cp ~/.vimrc $(DOTS)
cp ~/.vim/syntax/shy.vim $(DOTS) cp ~/.vim/syntax/shy.vim $(DOTS)
load_dotsfile:\ load_dotsfile:\
~/.zshrc\ ~/.zshrc\
~/.tmux.conf\ ~/.tmux.conf\
~/context/.git/hooks/post-commit\
~/.vimrc\ ~/.vimrc\
~/.vim/syntax/shy.vim ~/.vim/syntax/shy.vim

View File

@ -1,4 +1,8 @@
~ssh ~ssh
remote listen :9090 remote listen :9090
~aaa
user root shy shy
~web
serve
~stdio ~stdio

View File

@ -233,7 +233,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
} }
} else { } else {
wait := make(chan bool, 1) wait := make(chan bool, 1)
go func() { m.GoFunc(m, func(m *ctx.Message) {
out := bytes.NewBuffer(make([]byte, 0, 1024)) out := bytes.NewBuffer(make([]byte, 0, 1024))
err := bytes.NewBuffer(make([]byte, 0, 1024)) err := bytes.NewBuffer(make([]byte, 0, 1024))
cmd.Stdout = out cmd.Stdout = out
@ -265,7 +265,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
} }
} }
wait <- true wait <- true
}() })
timeout := m.Conf("cmd_timeout") timeout := m.Conf("cmd_timeout")
if conf["timeout"] != nil { if conf["timeout"] != nil {
@ -1073,34 +1073,31 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
if cli.Timer == nil { // 创建时间队列 if cli.Timer == nil { // 创建时间队列
cli.Timer = time.NewTimer((time.Duration)((action - now) / int64(m.Confi("time_unit")) * 1000000000)) cli.Timer = time.NewTimer((time.Duration)((action - now) / int64(m.Confi("time_unit")) * 1000000000))
go func() { m.GoLoop(m, func(m *ctx.Message) {
for { select {
select { case <-cli.Timer.C:
case <-cli.Timer.C: m.Log("info", "timer %s", m.Conf("timer_next"))
m.Log("info", "timer %s", m.Conf("timer_next")) if m.Conf("timer_next") == "" {
if m.Conf("timer_next") == "" { break
break
}
if timer := m.Confm("timer", m.Conf("timer_next")); timer != nil && !kit.Right(timer["stop"]) {
m.Log("info", "timer %s %v", m.Conf("timer_next"), timer["cmd"])
msg := m.Sess("cli").Cmd("source", timer["cmd"])
timer["result"] = msg.Meta["result"]
timer["msg"] = msg.Code()
if timer["repeat"].(bool) {
timer["action_time"] = int64(m.Sess("cli").Cmd("time", timer["action_time"], timer["order"], timer["time"]).Appendi("timestamp"))
} else {
timer["done"] = true
}
}
cli.schedule(m)
} }
if timer := m.Confm("timer", m.Conf("timer_next")); timer != nil && !kit.Right(timer["stop"]) {
m.Log("info", "timer %s %v", m.Conf("timer_next"), timer["cmd"])
msg := m.Sess("cli").Cmd("source", timer["cmd"])
timer["result"] = msg.Meta["result"]
timer["msg"] = msg.Code()
if timer["repeat"].(bool) {
timer["action_time"] = int64(m.Sess("cli").Cmd("time", timer["action_time"], timer["order"], timer["time"]).Appendi("timestamp"))
} else {
timer["done"] = true
}
}
cli.schedule(m)
} }
cli.Timer = nil })
}()
} }
// 调度任务 // 调度任务

File diff suppressed because it is too large Load Diff

536
src/contexts/ctx/ctx_cgi.go Normal file
View File

@ -0,0 +1,536 @@
package ctx
import (
"encoding/json"
"fmt"
"html/template"
"strconv"
"strings"
"toolkit"
)
var CGI = template.FuncMap{
"meta": func(arg ...interface{}) string {
//meta meta [key [index]]
if len(arg) == 0 {
return ""
}
up := ""
list := []string{}
switch data := arg[0].(type) {
case map[string][]string:
if len(arg) == 1 {
list = append(list, fmt.Sprintf("detail: %s\n", data["detail"]))
list = append(list, fmt.Sprintf("option: %s\n", data["option"]))
list = append(list, fmt.Sprintf("result: %s\n", data["result"]))
list = append(list, fmt.Sprintf("append: %s\n", data["append"]))
break
}
if key, ok := arg[1].(string); ok {
if list, ok = data[key]; ok {
arg = arg[1:]
} else {
return up
}
} else {
return fmt.Sprintf("%v", data)
}
case []string:
list = data
default:
if data == nil {
return ""
}
return fmt.Sprintf("%v", data)
}
if len(arg) == 1 {
return strings.Join(list, "")
}
index, ok := arg[1].(int)
if !ok {
return strings.Join(list, "")
}
if index >= len(list) {
return ""
}
return list[index]
},
"sess": func(arg ...interface{}) string {
if len(arg) == 0 {
return ""
}
if m, ok := arg[0].(*Message); ok {
if len(arg) == 1 {
return fmt.Sprintf("%v", m)
}
switch which := arg[1].(type) {
case string:
m.Sess(which, arg[2:]...)
return ""
}
}
return ""
},
"ctx": func(arg ...interface{}) string {
if len(arg) == 0 {
return ""
}
if m, ok := arg[0].(*Message); ok {
if len(arg) == 1 {
return fmt.Sprintf("%v", m)
}
switch which := arg[1].(type) {
case string:
switch which {
case "name":
return fmt.Sprintf("%s", m.target.Name)
case "help":
return fmt.Sprintf("%s", m.target.Help)
case "context":
return fmt.Sprintf("%s", m.target.context.Name)
case "contexts":
ctx := []string{}
for _, v := range m.target.contexts {
ctx = append(ctx, fmt.Sprintf("%d", v.Name))
}
return strings.Join(ctx, " ")
case "time":
return m.time.Format("2006-01-02 15:04:05")
case "source":
return m.source.Name
case "target":
return m.target.Name
case "message":
return fmt.Sprintf("%d", m.message.code)
case "messages":
case "sessions":
msg := []string{}
for k, _ := range m.Sessions {
msg = append(msg, fmt.Sprintf("%s", k))
}
return strings.Join(msg, " ")
}
case int:
}
}
return ""
},
"msg": func(arg ...interface{}) interface{} {
if len(arg) == 0 {
return ""
}
if m, ok := arg[0].(*Message); ok {
if len(arg) == 1 {
return fmt.Sprintf("%v", m.Format())
}
switch which := arg[1].(type) {
case string:
switch which {
case "spawn":
return m.Spawn()
case "code":
return m.code
case "time":
return m.time.Format("2006-01-02 15:04:05")
case "source":
return m.source.Name
case "target":
return m.target.Name
case "message":
return m.message
case "messages":
return m.messages
case "sessions":
return m.Sessions
default:
return m.Sess(which)
}
case int:
ms := []*Message{m}
for i := 0; i < len(ms); i++ {
if ms[i].code == which {
return ms[i]
}
ms = append(ms, ms[i].messages...)
}
}
}
return ""
},
"cap": func(arg ...interface{}) string {
if len(arg) == 0 {
return ""
}
if m, ok := arg[0].(*Message); ok {
if len(arg) == 1 {
return fmt.Sprintf("%v", m)
}
switch which := arg[1].(type) {
case string:
if len(arg) == 2 {
return m.Cap(which)
}
switch value := arg[2].(type) {
case string:
return m.Cap(which, value)
case int:
return fmt.Sprintf("%d", m.Capi(which, value))
case bool:
return fmt.Sprintf("%t", m.Caps(which, value))
default:
return m.Cap(which, fmt.Sprintf("%v", arg[2]))
}
}
}
return ""
},
"conf": func(arg ...interface{}) interface{} {
if len(arg) == 0 {
return ""
}
if m, ok := arg[0].(*Message); ok {
if len(arg) == 1 {
list := []string{}
for k, _ := range m.target.Configs {
list = append(list, k)
}
return list
}
switch which := arg[1].(type) {
case string:
if len(arg) == 2 {
return m.Confv(which)
}
return m.Confv(which, arg[2:]...)
}
}
return ""
},
"cmd": func(arg ...interface{}) string {
if len(arg) == 0 {
return ""
}
return strings.Join(Pulse.Sess("cli").Cmd(arg).Meta["result"], "")
},
"detail": func(arg ...interface{}) interface{} {
if len(arg) == 0 {
return ""
}
switch m := arg[0].(type) {
case *Message:
if len(arg) == 1 {
return m.Meta["detail"]
}
index := 0
switch value := arg[1].(type) {
case int:
index = value
case string:
i, e := strconv.Atoi(value)
m.Assert(e)
index = i
}
if len(arg) == 2 {
return m.Detail(index)
}
return m.Detail(index, arg[2])
case map[string][]string:
return strings.Join(m["detail"], "")
case []string:
return strings.Join(m, "")
default:
return m
}
return ""
},
"option": func(arg ...interface{}) interface{} {
if len(arg) == 0 {
return ""
}
switch m := arg[0].(type) {
case *Message:
if len(arg) == 1 {
return m.Meta["option"]
}
switch value := arg[1].(type) {
case int:
if 0 <= value && value < len(m.Meta["option"]) {
return m.Meta["option"][value]
}
case string:
if len(arg) == 2 {
return m.Optionv(value)
}
switch val := arg[2].(type) {
case int:
if 0 <= val && val < len(m.Meta[value]) {
return m.Meta[value][val]
}
}
}
case map[string][]string:
if len(arg) == 1 {
return strings.Join(m["option"], "")
}
switch value := arg[1].(type) {
case string:
return strings.Join(m[value], "")
}
case []string:
return strings.Join(m, "")
default:
return m
}
return ""
},
"result": func(arg ...interface{}) interface{} {
if len(arg) == 0 {
return ""
}
switch m := arg[0].(type) {
case *Message:
if len(arg) == 1 {
return m.Meta["result"]
}
index := 0
switch value := arg[1].(type) {
case int:
index = value
case string:
i, e := strconv.Atoi(value)
m.Assert(e)
index = i
}
if len(arg) == 2 {
return m.Result(index)
}
return m.Result(index, arg[2])
case map[string][]string:
return strings.Join(m["result"], "")
case []string:
return strings.Join(m, "")
default:
return m
}
return ""
},
"append": func(arg ...interface{}) interface{} {
if len(arg) == 0 {
return ""
}
switch m := arg[0].(type) {
case *Message:
if len(arg) == 1 {
return m.Meta["append"]
}
switch value := arg[1].(type) {
case int:
if 0 <= value && value < len(m.Meta["append"]) {
return m.Meta["append"][value]
}
case string:
if len(arg) == 2 {
return m.Meta[value]
}
switch val := arg[2].(type) {
case int:
if 0 <= val && val < len(m.Meta[value]) {
return m.Meta[value][val]
}
}
}
case map[string][]string:
if len(arg) == 1 {
return strings.Join(m["append"], "")
}
switch value := arg[1].(type) {
case string:
return strings.Join(m[value], "")
}
case []string:
return strings.Join(m, "")
default:
return m
}
return ""
},
"table": func(arg ...interface{}) []interface{} {
if len(arg) == 0 {
return []interface{}{}
}
switch m := arg[0].(type) {
case *Message:
if len(m.Meta["append"]) == 0 {
return []interface{}{}
}
if len(arg) == 1 {
data := []interface{}{}
nrow := len(m.Meta[m.Meta["append"][0]])
for i := 0; i < nrow; i++ {
line := map[string]string{}
for _, k := range m.Meta["append"] {
line[k] = m.Meta[k][i]
if len(m.Meta[k]) != i {
continue
}
}
data = append(data, line)
}
return data
}
case map[string][]string:
if len(arg) == 1 {
data := []interface{}{}
nrow := len(m[m["append"][0]])
for i := 0; i < nrow; i++ {
line := map[string]string{}
for _, k := range m["append"] {
line[k] = m[k][i]
}
data = append(data, line)
}
return data
}
}
return []interface{}{}
},
"list": func(arg interface{}) interface{} {
n := 0
switch v := arg.(type) {
case string:
i, e := strconv.Atoi(v)
if e == nil {
n = i
}
case int:
n = v
}
list := make([]int, n)
for i := 1; i <= n; i++ {
list[i-1] = i
}
return list
},
"slice": func(list interface{}, arg ...interface{}) interface{} {
switch l := list.(type) {
case string:
if len(arg) == 0 {
return l
}
if len(arg) == 1 {
return l[arg[0].(int):]
}
if len(arg) == 2 {
return l[arg[0].(int):arg[1].(int)]
}
}
return ""
},
"work": func(m *Message, arg ...interface{}) interface{} {
switch len(arg) {
case 0:
list := map[string]map[string]interface{}{}
m.Confm("auth", []string{m.Option("sessid"), "ship"}, func(key string, ship map[string]interface{}) {
if ship["type"] == "bench" {
if work := m.Confm("auth", key); work != nil {
list[key] = work
}
}
})
return list
}
return nil
},
"unescape": func(str string) interface{} {
return template.HTML(str)
},
"json": func(arg ...interface{}) interface{} {
if len(arg) == 0 {
return ""
}
b, _ := json.MarshalIndent(arg[0], "", " ")
return string(b)
},
"so": func(arg ...interface{}) interface{} {
if len(arg) == 0 {
return ""
}
cli := Pulse.Sess("cli")
cmd := strings.Join(kit.Trans(arg), " ")
cli.Cmd("source", cmd)
result := []string{}
if len(cli.Meta["append"]) > 0 {
result = append(result, "<table>")
result = append(result, "<caption>", cmd, "</caption>")
cli.Table(func(maps map[string]string, list []string, line int) bool {
if line == -1 {
result = append(result, "<tr>")
for _, v := range list {
result = append(result, "<th>", v, "</th>")
}
result = append(result, "</tr>")
return true
}
result = append(result, "<tr>")
for _, v := range list {
result = append(result, "<td>", v, "</td>")
}
result = append(result, "</tr>")
return true
})
result = append(result, "</table>")
} else {
result = append(result, "<pre><code>")
result = append(result, fmt.Sprintf("%s", cli.Find("shy", false).Conf("prompt")), cmd, "\n")
result = append(result, cli.Meta["result"]...)
result = append(result, "</code></pre>")
}
return template.HTML(strings.Join(result, ""))
},
}

1323
src/contexts/ctx/ctx_init.go Normal file

File diff suppressed because it is too large Load Diff

1565
src/contexts/ctx/ctx_type.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -878,29 +878,27 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool {
nfs.echo = make(chan *ctx.Message, 10) nfs.echo = make(chan *ctx.Message, 10)
nfs.hand = map[int]*ctx.Message{} nfs.hand = map[int]*ctx.Message{}
go func() { //发送消息队列 m.GoLoop(m, func(m *ctx.Message) {
for { msg, code, meta, body := m, 0, "detail", "option"
msg, code, meta, body := m, 0, "detail", "option" select {
select { case msg = <-nfs.send:
case msg = <-nfs.send: code = msg.Code()
code = msg.Code() nfs.hand[code] = msg
nfs.hand[code] = msg case msg = <-nfs.echo:
case msg = <-nfs.echo: code, meta, body = msg.Optioni("remote_code"), "result", "append"
code, meta, body = msg.Optioni("remote_code"), "result", "append"
}
nfs.Send("code", code)
for _, v := range msg.Meta[meta] {
nfs.Send(meta, v)
}
for _, k := range msg.Meta[body] {
for _, v := range msg.Meta[k] {
nfs.Send(k, v)
}
}
nfs.Send("")
} }
}()
nfs.Send("code", code)
for _, v := range msg.Meta[meta] {
nfs.Send(meta, v)
}
for _, k := range msg.Meta[body] {
for _, v := range msg.Meta[k] {
nfs.Send(k, v)
}
}
nfs.Send("")
})
//接收消息队列 //接收消息队列
msg, code, head, body := m, "0", "result", "append" msg, code, head, body := m, "0", "result", "append"