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

mac mod ctx.context&web.travel

This commit is contained in:
shaoying 2018-05-26 07:46:44 +08:00
parent f0bd6d0f0e
commit ec0b608aa4
7 changed files with 209 additions and 148 deletions

View File

@ -1852,7 +1852,13 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
// }}}
}},
"context": &Command{Name: "context back|[[home] [find|search] name] [info|lists|show|switch|[args]", Help: "查找并操作模块,\n查找起点root:根模块、back:父模块、home:本模块,\n查找方法find:路径匹配、search:模糊匹配,\n查找对象name:支持点分和正则,\n操作类型show:显示信息、switch:切换为当前、start:启动模块、spawn:分裂子模块args:启动参数",
Formats: map[string]int{"back": 0, "home": 0, "find": 1, "search": 1, "info": 1, "lists": 0, "show": 0, "switch": 0},
Formats: map[string]int{
"back": 0, "home": 0,
"find": 1, "search": 1,
"info": 1, "lists": 0, "show": 0, "switch": 0,
"cache": 0, "config": 0, "command": 0,
"module": 0, "domain": 0,
},
Hand: func(m *Message, c *Context, key string, arg ...string) {
if m.Has("back") { // {{{
m.target = m.source
@ -1885,6 +1891,56 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
// v.Meta = m.Meta
// v.Data = m.Data
switch {
case m.Has("cache"):
if len(arg) == 0 {
for k, v := range v.target.Caches {
m.Add("append", "key", k)
m.Add("append", "name", v.Name)
m.Add("append", "value", v.Value)
m.Add("append", "help", v.Help)
}
} else {
m.Echo(v.Cap(arg[0], arg[1:]...))
}
case m.Has("config"):
if len(arg) == 0 {
for k, v := range v.target.Configs {
m.Add("append", "key", k)
m.Add("append", "name", v.Name)
m.Add("append", "value", v.Value)
m.Add("append", "help", v.Help)
}
} else {
m.Echo(v.Conf(arg[0], arg[1:]...))
}
case m.Has("command"):
if len(arg) == 0 {
for k, v := range v.target.Commands {
m.Add("append", "key", k)
m.Add("append", "name", v.Name)
m.Add("append", "help", v.Help)
}
} else {
v.Cmd(arg)
m.Copy(v, "result").Copy(v, "append")
}
case m.Has("module"):
m.Travel(v.target, func(m *Message) bool {
m.Add("append", "name", m.target.Name)
m.Add("append", "help", m.target.Help)
m.Add("append", "module", m.Cap("module"))
m.Add("append", "status", m.Cap("status"))
m.Add("append", "stream", m.Cap("stream"))
return true
})
case m.Has("domain"):
msg := m.Find("ssh", true)
msg.Travel(msg.Target(), func(msg *Message) bool {
m.Add("append", "name", msg.Target().Name)
m.Add("append", "help", msg.Target().Help)
m.Add("append", "domain", msg.Cap("domain")+"."+msg.Conf("domains"))
return true
})
case m.Has("switch"), m.Has("back"):
m.target = v.target
case m.Has("show"):

View File

@ -450,6 +450,8 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
nfs.Caches["target"] = &ctx.Cache{Name: "target", Value: "", Help: "文件名"}
nsend := ""
for {
line, e := nfs.Reader.ReadString('\n')
if msg == nil {
@ -473,6 +475,8 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
msg.Add("detail", ls[1])
case "result":
msg.Add("result", ls[1])
case "nsend":
nsend = ls[1]
default:
msg.Add("option", ls[0], ls[1])
}
@ -484,20 +488,21 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
msg.Log("info", nil, "detail: %v", msg.Meta["detail"])
msg.Log("info", nil, "option: %v", msg.Meta["option"])
msg.Options("stdio", false)
msg.Option("nsend", nsend)
func() {
cmd := msg
nsend := cmd.Option("nsend")
nsends := nsend
cmd.Call(func(sub *ctx.Message) *ctx.Message {
for _, v := range sub.Meta["result"] {
_, e := fmt.Fprintf(nfs.Writer, "result: %s\n", url.QueryEscape(v))
sub.Assert(e)
}
sub.Append("nsend", nsend)
sub.Append("nsend", nsends)
for _, k := range sub.Meta["append"] {
for _, v := range sub.Meta[k] {
_, e := fmt.Fprintf(nfs.Writer, "%s: %s\n", k, v)
_, e := fmt.Fprintf(nfs.Writer, "%s: %s\n", k, url.QueryEscape(v))
sub.Assert(e)
}
}
@ -526,11 +531,13 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
msg.Meta["append"] = msg.Meta["option"]
delete(msg.Meta, "option")
msg.Log("info", nil, "%s send", msg.Meta["nsend"])
msg.Log("info", nil, "%s send", nsend)
msg.Log("info", nil, "result: %v", msg.Meta["result"])
msg.Log("info", nil, "append: %v", msg.Meta["append"])
send := nfs.send[msg.Appendi("nsend")]
n, e := strconv.Atoi(nsend)
m.Assert(e)
send := nfs.send[n]
send.Copy(msg, "result")
send.Copy(msg, "append")
@ -544,6 +551,7 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
send.Back(send)
}
nsend = ""
msg = nil
}
return true

View File

@ -167,7 +167,8 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
if m.Options("nsend") { //接收命令
msg := m.Spawn(m.Target())
msg.Cmd(arg)
m.Back(msg)
m.Copy(msg, "result").Copy(msg, "append")
m.Back(m)
} else { //发送命令
ssh.Message.Sesss("nfs").CallBack(m.Options("stdio"), func(host *ctx.Message) *ctx.Message {
m.Back(m.Copy(host, "result").Copy(host, "append"))

View File

@ -370,8 +370,8 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
"query": &ctx.Config{Name: "query", Value: "", Help: "主机参数"},
"output": &ctx.Config{Name: "output", Value: "stdout", Help: "响应输出"},
"editor": &ctx.Config{Name: "editor", Value: "vim", Help: "响应编辑器"},
"upload_tpl": &ctx.Config{Name: "upload_tpl", Value: "usr/upload.html", Help: "上传文件路径"},
"travel_tpl": &ctx.Config{Name: "travel_tpl", Value: "usr/travel.html", Help: "上传文件路径"},
"upload_tpl": &ctx.Config{Name: "upload_tpl", Value: "usr/template/upload.html", Help: "上传文件路径"},
"travel_tpl": &ctx.Config{Name: "travel_tpl", Value: "usr/template/travel.html", Help: "上传文件路径"},
},
Commands: map[string]*ctx.Command{
"serve": &ctx.Command{Name: "serve [directory [address [protocol]]]", Help: "开启应用服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
@ -715,20 +715,33 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
"/travel": &ctx.Command{Name: "/travel", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
w := m.Data["response"].(http.ResponseWriter) // {{{
module := "ctx"
if m.Options("module") {
module = m.Option("module")
}
if m.Options("domain") {
module = "ssh"
}
msg := m.Find(module, true)
if msg == nil {
return
}
if m.Option("method") == "POST" {
if m.Options("domain") {
msg := m.Find("ssh", true)
msg.Detail(0, "send", "domain", m.Option("domain"), "context", "find", m.Option("module"), m.Option("ccc"))
if m.Options("name") {
msg.Add("detail", m.Option("name"))
}
if m.Options("value") {
msg.Add("detail", m.Option("value"))
}
msg.CallBack(true, func(sub *ctx.Message) *ctx.Message {
m.Copy(sub, "result").Copy(sub, "append")
return nil
})
return
}
module := "ctx"
if m.Options("module") {
module = m.Option("module")
}
msg := m.Find(module, true)
if msg == nil {
return
}
switch m.Option("ccc") {
case "cache":
m.Echo(msg.Cap(m.Option("name")))
@ -740,96 +753,46 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
}
case "command":
msg = msg.Spawn(msg.Target())
if m.Options("domain") {
module := "ssh"
if m.Options("module") {
module = m.Option("module")
}
if m.Options("value") {
msg.Detail(0, "send", "context", module, m.Option("name"), m.Option("value"))
} else {
msg.Detail(0, "send", "context", module, m.Option("name"))
}
msg.Log("fuck", nil, "why %d ", len(msg.Meta["detail"]))
msg.Option("domain", m.Option("domain"))
msg.CallBack(true, func(sub *ctx.Message) *ctx.Message {
m.Copy(sub, "result").Copy(sub, "append")
return nil
})
} else {
if m.Options("value") {
msg.Detail(0, m.Option("name"), m.Option("value"))
} else {
msg.Detail(0, m.Option("name"))
}
msg.Cmd()
m.Copy(msg, "result").Copy(msg, "append")
msg.Detail(0, m.Option("name"))
if m.Options("value") {
msg.Add("detail", m.Option("value"))
}
msg.Cmd()
m.Copy(msg, "result").Copy(msg, "append")
}
return
}
module := "ctx"
if m.Options("module") {
module = m.Option("module")
}
w.Header().Add("Content-Type", "text/html")
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "head", m.Meta))
for k, v := range msg.Target().Caches {
msg.Add("append", "key", k)
msg.Add("append", "name", v.Name)
msg.Add("append", "help", v.Help)
msg.Add("append", "value", v.Value)
}
if len(msg.Meta["append"]) > 0 {
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "cache", msg.Meta))
}
if msg := m.Find(module, true); msg != nil {
for _, v := range []string{"cache", "config", "command", "module", "domain"} {
if m.Options("domain") {
msg = m.Find("ssh", true)
msg.Detail(0, "send", "domain", m.Option("domain"), "context", "find", module, v)
msg.CallBack(true, func(sub *ctx.Message) *ctx.Message {
msg.Copy(sub, "result").Copy(sub, "append")
return nil
})
} else {
msg = msg.Spawn(msg.Target())
msg.Cmd("context", "find", module, v)
}
msg = msg.Spawn(msg.Target())
for k, v := range msg.Target().Configs {
msg.Add("append", "key", k)
msg.Add("append", "name", v.Name)
msg.Add("append", "help", v.Help)
msg.Add("append", "value", v.Value)
msg.Add("append", "input", "")
}
if len(msg.Meta["append"]) > 0 {
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "config", msg.Meta))
}
msg = msg.Spawn(msg.Target())
for k, v := range msg.Target().Commands {
msg.Add("append", "key", k)
msg.Add("append", "name", v.Name)
msg.Add("append", "help", v.Help)
msg.Add("append", "input", "")
}
if len(msg.Meta["append"]) > 0 {
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "command", msg.Meta))
}
msg = msg.Spawn(msg.Target())
msg.Travel(msg.Target(), func(m *ctx.Message) bool {
m.Add("append", "name", m.Target().Name)
m.Add("append", "help", m.Target().Help)
m.Add("append", "module", m.Cap("module"))
m.Add("append", "status", m.Cap("status"))
m.Add("append", "stream", m.Cap("stream"))
return true
})
if len(msg.Meta["append"]) > 0 {
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "context", msg.Meta))
}
msg = msg.Find("ssh", true)
msg.Option("domains", m.Option("domain"))
msg.Travel(msg.Target(), func(m *ctx.Message) bool {
m.Add("append", "name", m.Target().Name)
m.Add("append", "help", m.Target().Help)
m.Add("append", "domain", m.Conf("domains"))
return true
})
if len(msg.Meta["append"]) > 0 {
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "domain", msg.Meta))
if len(msg.Meta["append"]) > 0 {
msg.Option("current_module", module)
msg.Option("current_domain", m.Option("domain"))
m.Log("fuck", nil, "fuck %v", msg.Meta)
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, v, msg.Meta))
}
}
}
m.Assert(template.Must(template.ParseGlob(m.Conf("travel_tpl"))).ExecuteTemplate(w, "tail", m.Meta))

View File

@ -1,7 +1,7 @@
ctx = {
Cookie: function(name, value) {
Cookie: function(key, value) {//{{{
if (value == undefined) {
var pattern = new RegExp(name+"=([^;]*);?");
var pattern = new RegExp(key+"=([^;]*);?");
var result = pattern.exec(document.cookie);
if (result && result.length > 0) {
return result[1];
@ -9,10 +9,10 @@ ctx = {
return "";
}
document.cookie = name+"="+value;
return this.Cookie(name);
},
Search: function(name, value) {
document.cookie = key+"="+value;
return this.Cookie(key);
},//}}}
Search: function(key, value) {//{{{
var args = {};
var search = location.search.split("?");
if (search.length > 1) {
@ -23,16 +23,16 @@ ctx = {
}
}
if (typeof name == "object") {
for (var k in name) {
if (name[k] != undefined) {
args[k] = name[k];
if (typeof key == "object") {
for (var k in key) {
if (key[k] != undefined) {
args[k] = key[k];
}
}
} else if (value == undefined) {
return args[name];
return args[key];
} else {
args[name] = value;
args[key] = value;
}
var arg = [];
@ -40,8 +40,8 @@ ctx = {
arg.push(k+"="+encodeURIComponent(args[k]));
}
location.search = arg.join("&");
},
POST: function(url, form, cb) {
},//}}}
POST: function(url, form, cb) {//{{{
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
switch (xhr.readyState) {
@ -50,6 +50,7 @@ ctx = {
case 200:
var msg = JSON.parse(xhr.responseText||'{"result":[]}');
msg && console.log(msg)
msg.result && console.log(msg.result.join(""));
typeof cb == "function" && cb(msg)
}
break;
@ -59,59 +60,85 @@ ctx = {
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
if (!("dir" in form)) {
form = form || {}
form["dir"] = this.Search("dir")
}
if (!("module" in form)) {
form = form || {}
form["module"] = this.Search("module")
}
form = form || {}
form["dir"] = form["dir"] || this.Search("dir") || undefined
form["module"] = form["module"] || this.Search("module") || undefined
form["domain"] = form["domain"] || this.Search("domain") || undefined
var args = [];
for (k in form) {
args.push(k+"="+encodeURIComponent(form[k]));
if (form[k] != undefined) {
args.push(k+"="+encodeURIComponent(form[k]));
}
}
var arg = args.join("&");
console.log("POST: "+url+"?"+arg);
xhr.send(arg);
},
Cap: function(cap, cb) {
this.POST("", {ccc:"cache", name:cap}, function(msg) {
typeof cb == "function" && cb(msg.result.join(""));
},//}}}
Cap: function(cap, cb) {//{{{
if (typeof cap == "function") {
cb = cap;
cap = undefined;
}
var args = {ccc:"cache"};
if (cap != undefined) {
args.name = cap;
}
this.POST("", args, function(msg) {
var value = msg.result.join("");
typeof cb == "function" && cb(value);
});
},
Conf: function(conf, value, cb) {
},//}}}
Conf: function(conf, value, cb) {//{{{
if (typeof conf == "function") {
value = conf;
conf = undefined;
}
if (typeof value == "function") {
cb = value;
value = undefined;
}
var args = {ccc:"config", name:conf};
var args = {ccc:"config"};
if (name != undefined) {
args.name = name
}
if (value != undefined) {
args.value = value
}
this.POST("", args, function(msg) {
typeof cb == "function" && cb(msg.result.join(""));
var value = msg.result.join("");
typeof cb == "function" && cb(value);
});
},
Cmd: function(cmd, value, cb) {
},//}}}
Cmd: function(cmd, value, cb) {//{{{
if (typeof cmd == "function") {
value = cmd;
cmd = undefined;
}
if (typeof value == "function") {
cb = value;
value = undefined;
}
var args = {ccc:"command", name:cmd};
var args = {ccc:"command"};
if (cmd != undefined) {
args.name = cmd
}
if (value != undefined) {
args.value = value
}
this.POST("", args, cb);
},
Module: function(module, domain) {
},//}}}
Module: function(module, domain) {//{{{
this.Search({module:module, domain:domain})
},
}
},//}}}
};

View File

@ -125,8 +125,8 @@
</fieldset>
{{end}}
{{define "context"}}
<fieldset><legend><input type="text" id="module" value="{{index .module 0}}" onkeydown="return Change(event)"></legend>
{{define "module"}}
<fieldset><legend><input type="text" id="module" value="{{index .current_module 0}}" onkeydown="return Change(event, 'module')"></legend>
<table>
<colgroup>{{range .append}}<col class="{{.}}">{{end}}</colgroup>
<tr>{{range .append}}<th class="{{.}}" onclick="list('{{.}}')">{{.}}</th>{{end}}</tr>
@ -138,7 +138,7 @@
{{range $key := index $meta "append"}}
{{if eq $key "name"}}
<td class="{{$key}}">
<code><a href="/travel?module={{index $meta "module" $i}}">{{index $meta $key $i}}</a></code>
<code><a href="" onclick="ctx.Search('module', '{{index $meta "module" $i}}'); return false">{{index $meta $key $i}}</a></code>
</td>
{{else}}
<td class="{{$key}}">
@ -154,7 +154,7 @@
{{end}}
{{define "domain"}}
<fieldset><legend><input type="text" id="domain" value="{{index . "domains" 0}}" onkeydown="return Change(event)"></legend>
<fieldset><legend><input type="text" id="domain" value="{{index . "current_domain" 0}}" onkeydown="return Change(event, 'domain')"></legend>
<table>
<colgroup>{{range .append}}<col class="{{.}}">{{end}}</colgroup>
<tr>{{range .append}}<th class="{{.}}" onclick="list('{{.}}')">{{.}}</th>{{end}}</tr>
@ -166,7 +166,7 @@
{{range $key := index $meta "append"}}
{{if eq $key "name"}}
<td class="{{$key}}">
<code><a href="/travel?domain={{index $meta "domain" $i}}">{{index $meta $key $i}}</a></code>
<code><a href="" onclick="ctx.Search('domain', '{{index $meta "domain" $i}}'); return false">{{index $meta $key $i}}</a></code>
</td>
{{else}}
<td class="{{$key}}">
@ -223,9 +223,15 @@
return false;
}
function Change(event) {
function Input(event, cb, key) {
if (event.keyCode == 13) {
ctx.Search("module", event.target.value);
cb(key, event.target.value);
}
}
function Change(event, key) {
if (event.keyCode == 13) {
ctx.Search(key, event.target.value);
}
}