mirror of
https://shylinux.com/x/ContextOS
synced 2025-04-25 16:58:06 +08:00
tce add ctx.trans
This commit is contained in:
parent
2a654f675f
commit
5bdfc65206
@ -6,7 +6,7 @@ syn match shNumber "-\=\<\d\+\>#\="
|
||||
syn match shOperator "=\|+\|-\|*\|/"
|
||||
syn match shOperator "<\|<=\|>\|>=\|!=\|=="
|
||||
syn match shOperator "\\"
|
||||
syn match shOperator "\~[-_a-zA-Z0-9]\+\>"
|
||||
syn match shOperator "\~[-_a-zA-Z0-9\.]\+\>"
|
||||
syn match shShellVariables "\$[-_a-zA-Z0-9]\+\>"
|
||||
syn match shShellVariables "@[-_a-zA-Z0-9]\+\>"
|
||||
|
||||
|
10
etc/init.shy
10
etc/init.shy
@ -1,9 +1,3 @@
|
||||
source etc/local.shy
|
||||
|
||||
~nfs
|
||||
config paths -2 /Users/shaoying
|
||||
|
||||
~shell1
|
||||
target web
|
||||
|
||||
|
||||
~stdio
|
||||
config load var/history.json history
|
||||
|
@ -239,6 +239,28 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
msg = m.Spawn(cli.target)
|
||||
}
|
||||
}
|
||||
msg.Copy(m, "append")
|
||||
msg.Copy(m, "option")
|
||||
|
||||
args := []string{}
|
||||
rest := []string{}
|
||||
exports := []map[string]string{}
|
||||
for i := 0; i < len(detail); i++ {
|
||||
switch detail[i] {
|
||||
case "<":
|
||||
pipe := m.Spawn().Cmd("import", detail[i+1])
|
||||
msg.Copy(pipe, "append")
|
||||
i++
|
||||
case ">":
|
||||
exports = append(exports, map[string]string{"file": detail[i+1]})
|
||||
i++
|
||||
case "|":
|
||||
detail, rest = detail[:i], detail[i+1:]
|
||||
default:
|
||||
args = append(args, detail[i])
|
||||
}
|
||||
}
|
||||
detail = args
|
||||
|
||||
if msg.Cmd(detail); msg.Hand {
|
||||
m.Cap("ps_target", msg.Cap("module"))
|
||||
@ -246,10 +268,20 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
msg.Copy(m, "target").Detail(-1, "system")
|
||||
msg.Cmd()
|
||||
}
|
||||
|
||||
for _, v := range exports {
|
||||
m.Spawn().Copy(msg, "option").Copy(msg, "append").Cmd("export", v["file"])
|
||||
}
|
||||
|
||||
if len(rest) > 0 {
|
||||
pipe := m.Spawn().Copy(msg, "option").Copy(msg, "append").Cmd("cmd", rest)
|
||||
msg.Set("result").Set("append").Copy(pipe, "result").Copy(pipe, "append")
|
||||
}
|
||||
|
||||
m.Target().Message().Set("result").Set("append").Copy(msg, "result").Copy(msg, "append")
|
||||
m.Copy(msg, "result").Copy(msg, "append")
|
||||
// m.Capi("last_msg", 0, msg.Code())
|
||||
// m.Capi("ps_count", 1)
|
||||
m.Capi("last_msg", 0, msg.Code())
|
||||
m.Capi("ps_count", 1)
|
||||
}
|
||||
}},
|
||||
"str": &ctx.Command{Name: "str word", Help: "解析字符串", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
@ -519,7 +551,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
yac.Cmd("train", "stm", "echo", "echo", "rep{", "exp", "}")
|
||||
yac.Cmd("train", "stm", "return", "return", "rep{", "exp", "}")
|
||||
|
||||
yac.Cmd("train", "word", "word", "mul{", "~", "!", "=", "\\|", "exe", "str", "[a-zA-Z0-9_/\\-.:]+", "}")
|
||||
yac.Cmd("train", "word", "word", "mul{", "~", "!", "=", "\\|", "\\<", "\\>", "exe", "str", "[a-zA-Z0-9_/\\-.:]+", "}")
|
||||
yac.Cmd("train", "cmd", "cmd", "rep{", "word", "}")
|
||||
yac.Cmd("train", "exe", "exe", "$", "(", "cmd", ")")
|
||||
|
||||
|
@ -406,7 +406,9 @@ func (c *Context) Close(m *Message, arg ...string) bool {
|
||||
|
||||
if c.context != nil {
|
||||
m.Log("close", "%d context %v", m.root.Capi("ncontext", -1)+1, arg)
|
||||
delete(c.context.contexts, c.Name)
|
||||
if c.Name != "stdio" {
|
||||
delete(c.context.contexts, c.Name)
|
||||
}
|
||||
c.exit <- true
|
||||
}
|
||||
return true
|
||||
@ -520,7 +522,7 @@ func (m *Message) Copy(msg *Message, meta string, arg ...string) *Message {
|
||||
m.Put(meta, k, v)
|
||||
}
|
||||
if v, ok := msg.Meta[k]; ok {
|
||||
m.Set(meta, k).Add(meta, k, v)
|
||||
m.Add(meta, k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1388,13 +1390,6 @@ func (m *Message) Cmd(args ...interface{}) *Message {
|
||||
if x, ok := s.Commands[key]; ok && x.Hand != nil {
|
||||
m.TryCatch(m, true, func(m *Message) {
|
||||
m.Log("cmd", "%s:%s %v %v", s.Name, c.Name, m.Meta["detail"], m.Meta["option"])
|
||||
rest := []string{}
|
||||
for i := 0; i < len(arg); i++ {
|
||||
if arg[i] == "|" {
|
||||
arg, rest = arg[:i], arg[i+1:]
|
||||
}
|
||||
}
|
||||
|
||||
if args := []string{}; x.Form != nil {
|
||||
for i := 0; i < len(arg); i++ {
|
||||
n, ok := x.Form[arg[i]]
|
||||
@ -1416,10 +1411,6 @@ func (m *Message) Cmd(args ...interface{}) *Message {
|
||||
m.Hand = true
|
||||
x.Hand(m, s, key, arg...)
|
||||
|
||||
if m.Hand = true; len(rest) > 0 {
|
||||
msg := m.Spawn().Copy(m, "option").Copy(m, "append").Cmd(rest)
|
||||
m.Set("result").Set("append").Copy(msg, "result").Copy(msg, "append")
|
||||
}
|
||||
})
|
||||
return m
|
||||
}
|
||||
@ -3058,23 +3049,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
|
||||
}},
|
||||
"config": &Command{
|
||||
Name: "config [all] [save|load file key...] [delete] [pop index] key [value...]|key name value help",
|
||||
Help: "查看、读写、添加配置变量",
|
||||
Form: map[string]int{
|
||||
"chains": 1, "fields": 1,
|
||||
"where_field": 1, "where_value": 1,
|
||||
"sort_field": 1, "sort_order": 1,
|
||||
"page_limit": 1, "page_offset": 1,
|
||||
"format_field": 2,
|
||||
"select": 3,
|
||||
},
|
||||
"config": &Command{Name: "config [all] [export key..] [save|load file key...] [create map|list|string key name help] [delete key]",
|
||||
Help: "配置管理, export: 导出配置, save: 保存配置到文件, load: 从文件加载配置, create: 创建配置, delete: 删除配置",
|
||||
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
offset, e := strconv.Atoi(m.Confx("page_offset"))
|
||||
m.Assert(e)
|
||||
limit, e := strconv.Atoi(m.Confx("page_limit"))
|
||||
m.Assert(e)
|
||||
|
||||
all := false
|
||||
if len(arg) > 0 && arg[0] == "all" {
|
||||
arg, all = arg[1:], true
|
||||
@ -3084,213 +3061,128 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
have := map[string]bool{}
|
||||
if len(arg) > 0 {
|
||||
switch arg[0] {
|
||||
case "pop", "delete":
|
||||
action, which, arg = arg[0], arg[1], arg[2:]
|
||||
case "save", "load":
|
||||
action, which, arg = arg[0], arg[1], arg[2:]
|
||||
for _, v := range arg {
|
||||
have[v] = true
|
||||
}
|
||||
case "export":
|
||||
action, arg = arg[0], arg[1:]
|
||||
for _, v := range arg {
|
||||
have[v] = true
|
||||
}
|
||||
case "save", "load":
|
||||
action, which, arg = arg[0], arg[1], arg[2:]
|
||||
for _, v := range arg {
|
||||
have[v] = true
|
||||
}
|
||||
case "create", "delete":
|
||||
action, arg = arg[0], arg[1:]
|
||||
}
|
||||
}
|
||||
|
||||
save := map[string]interface{}{}
|
||||
if action == "load" {
|
||||
f, e := os.Open(which)
|
||||
if e != nil {
|
||||
return
|
||||
if len(arg) == 0 || action != "" {
|
||||
save := map[string]interface{}{}
|
||||
if action == "load" {
|
||||
f, e := os.Open(which)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
de := json.NewDecoder(f)
|
||||
de.Decode(&save)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
de := json.NewDecoder(f)
|
||||
de.Decode(&save)
|
||||
}
|
||||
|
||||
format_field := m.Option("format_field")
|
||||
format_str := "%s"
|
||||
if format_field != "" {
|
||||
format_str = m.Meta["format_field"][1]
|
||||
}
|
||||
|
||||
// sort_field := m.Option("sort_field")
|
||||
// sort_order := m.Option("sort_order")
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
for k, v := range m.target.Configs {
|
||||
switch action {
|
||||
case "save", "export":
|
||||
if len(have) == 0 || have[k] {
|
||||
save[k] = v.Value
|
||||
}
|
||||
case "load":
|
||||
if x, ok := save[k]; ok && (len(have) == 0 || have[k]) {
|
||||
v.Value = x
|
||||
}
|
||||
case "pop":
|
||||
switch val := v.Value.(type) {
|
||||
case map[string]string:
|
||||
delete(val, which)
|
||||
case map[string]interface{}:
|
||||
delete(val, which)
|
||||
case []string:
|
||||
if i, e := strconv.Atoi(which); e == nil {
|
||||
i = (i+2+len(val)+2)%(len(val)+2) - 2
|
||||
if i > -1 {
|
||||
m.Echo(val[i])
|
||||
for i := i; i < len(val)-1; i++ {
|
||||
val[i] = val[i+1]
|
||||
}
|
||||
val = val[:len(val)-1]
|
||||
}
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
for k, v := range m.target.Configs {
|
||||
switch action {
|
||||
case "export", "save":
|
||||
if len(have) == 0 || have[k] {
|
||||
save[k] = v.Value
|
||||
}
|
||||
v.Value = val
|
||||
case []interface{}:
|
||||
if i, e := strconv.Atoi(which); e == nil {
|
||||
i = (i+2+len(val)+2)%(len(val)+2) - 2
|
||||
if i > -1 {
|
||||
for i := i; i < len(val)-1; i++ {
|
||||
val[i] = val[i+1]
|
||||
}
|
||||
val = val[:len(val)-1]
|
||||
}
|
||||
case "load":
|
||||
if x, ok := save[k]; ok && (len(have) == 0 || have[k]) {
|
||||
v.Value = x
|
||||
}
|
||||
v.Value = val
|
||||
}
|
||||
case "delete":
|
||||
if which == k {
|
||||
delete(m.target.Configs, which)
|
||||
}
|
||||
default:
|
||||
switch len(arg) {
|
||||
case 0:
|
||||
m.Add("append", "key", k)
|
||||
m.Add("append", "value", m.Conf(k))
|
||||
m.Add("append", "name", v.Name)
|
||||
case 1:
|
||||
if k != arg[0] {
|
||||
continue
|
||||
case "create":
|
||||
m.Assert(k != arg[1], "%s exists", arg[1])
|
||||
case "delete":
|
||||
if k == arg[0] {
|
||||
delete(m.target.Configs, k)
|
||||
}
|
||||
|
||||
value := m.Confv(arg[0])
|
||||
if m.Options("chains") {
|
||||
value = m.Confv(arg[0], strings.Split(m.Option("chains"), "."))
|
||||
}
|
||||
|
||||
fields := map[string]bool{}
|
||||
for _, k := range strings.Split(m.Option("fields"), " ") {
|
||||
if k == "" {
|
||||
continue
|
||||
}
|
||||
if fields[k] = true; len(fields) == 1 {
|
||||
m.Meta["append"] = append(m.Meta["append"], "index")
|
||||
}
|
||||
m.Meta["append"] = append(m.Meta["append"], k)
|
||||
}
|
||||
|
||||
switch val := value.(type) {
|
||||
case map[string]interface{}:
|
||||
m.Add("append", "index", "0")
|
||||
for k, v := range val {
|
||||
if len(fields) == 0 || fields[k] {
|
||||
switch value := v.(type) {
|
||||
case float64:
|
||||
m.Add("append", k, fmt.Sprintf("%d", int(value)))
|
||||
default:
|
||||
m.Add("append", k, fmt.Sprintf("%v", value))
|
||||
}
|
||||
}
|
||||
}
|
||||
case map[string]string:
|
||||
m.Add("append", "index", "0")
|
||||
for k, v := range val {
|
||||
if len(fields) == 0 || fields[k] {
|
||||
if k == format_field {
|
||||
m.Add("append", k, fmt.Sprintf(format_str, v))
|
||||
} else {
|
||||
m.Add("append", k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
case []interface{}:
|
||||
n := 0
|
||||
for i, value := range val {
|
||||
switch val := value.(type) {
|
||||
case map[string]interface{}:
|
||||
if m.Options("where_field") && m.Options("where_value") {
|
||||
if !strings.Contains(fmt.Sprintf("%v", val[m.Option("where_field")]), m.Option("where_value")) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
n++
|
||||
if n <= offset || n > offset+limit {
|
||||
continue
|
||||
}
|
||||
|
||||
m.Add("append", "index", i)
|
||||
for k, v := range val {
|
||||
if len(fields) == 0 || fields[k] {
|
||||
if k == format_field {
|
||||
m.Add("append", k, fmt.Sprintf(format_str, v, v))
|
||||
} else {
|
||||
m.Add("append", k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case []string:
|
||||
for i, v := range val {
|
||||
m.Add("append", "index", i)
|
||||
if k == format_field {
|
||||
m.Add("append", k, fmt.Sprintf(format_str, v, v))
|
||||
} else {
|
||||
m.Add("append", k, v)
|
||||
}
|
||||
}
|
||||
default:
|
||||
m.Echo("%v", Trans(val)[0])
|
||||
}
|
||||
case 3:
|
||||
m.Echo("%v", m.Confv(arg[0], strings.Split(arg[1], "."), arg[2]))
|
||||
|
||||
fallthrough
|
||||
default:
|
||||
m.Echo("%v", m.Confv(arg[0], arg[1:]))
|
||||
return false
|
||||
m.Add("append", "key", k)
|
||||
m.Add("append", "value", strings.Replace(strings.Replace(m.Conf(k), "\n", "\\n", -1), "\t", "\\t", -1))
|
||||
m.Add("append", "name", v.Name)
|
||||
}
|
||||
}
|
||||
switch action {
|
||||
case "create":
|
||||
var value interface{}
|
||||
switch arg[0] {
|
||||
case "map":
|
||||
value = map[string]interface{}{}
|
||||
case "list":
|
||||
value = []interface{}{}
|
||||
default:
|
||||
value = ""
|
||||
}
|
||||
m.target.Configs[arg[1]] = &Config{Name: arg[2], Value: value, Help: arg[3]}
|
||||
}
|
||||
return all
|
||||
})
|
||||
m.Sort("key", "str").Table()
|
||||
|
||||
switch action {
|
||||
case "save":
|
||||
f, e := os.Create(which)
|
||||
m.Assert(e)
|
||||
defer f.Close()
|
||||
|
||||
buf, e := json.MarshalIndent(save, "", " ")
|
||||
m.Assert(e)
|
||||
f.Write(buf)
|
||||
case "export":
|
||||
buf, e := json.MarshalIndent(save, "", " ")
|
||||
m.Assert(e)
|
||||
m.Echo("%s", string(buf))
|
||||
}
|
||||
return all
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
m.Table()
|
||||
// if m.Sort(sort_field, sort_order); m.Has("index") {
|
||||
// for i := 0; i < len(m.Meta["index"]); i++ {
|
||||
// m.Meta["index"][i] = fmt.Sprintf("%d", i+offset)
|
||||
// }
|
||||
// }
|
||||
// if m.Has("select") {
|
||||
// m.Echo(m.Cap(m.Meta["select"][2], m.Meta[m.Meta["select"][1]][m.Optioni("select")]))
|
||||
// } else {
|
||||
// m.Table()
|
||||
// }
|
||||
var value interface{}
|
||||
if len(arg) > 2 {
|
||||
value = m.Confv(arg[0], arg[1], arg[2])
|
||||
} else if len(arg) > 1 {
|
||||
value = m.Confv(arg[0], arg[1])
|
||||
} else {
|
||||
value = m.Confv(arg[0])
|
||||
}
|
||||
|
||||
switch action {
|
||||
case "save":
|
||||
f, e := os.Create(which)
|
||||
m.Assert(e)
|
||||
defer f.Close()
|
||||
|
||||
buf, e := json.MarshalIndent(save, "", " ")
|
||||
m.Assert(e)
|
||||
f.Write(buf)
|
||||
case "export":
|
||||
buf, e := json.MarshalIndent(save, "", " ")
|
||||
m.Assert(e)
|
||||
m.Echo("%s", string(buf))
|
||||
switch val := value.(type) {
|
||||
case map[string]interface{}:
|
||||
for k, v := range val {
|
||||
m.Add("append", "key", k)
|
||||
m.Add("append", "value", v)
|
||||
}
|
||||
m.Sort("key", "str").Table()
|
||||
case map[string]string:
|
||||
for k, v := range val {
|
||||
m.Add("append", "key", k)
|
||||
m.Add("append", "value", v)
|
||||
}
|
||||
m.Sort("key", "str").Table()
|
||||
case []interface{}:
|
||||
for i, v := range val {
|
||||
m.Add("append", "index", i)
|
||||
m.Add("append", "value", v)
|
||||
}
|
||||
m.Table()
|
||||
case []string:
|
||||
for i, v := range val {
|
||||
m.Add("append", "index", i)
|
||||
m.Add("append", "value", v)
|
||||
}
|
||||
m.Table()
|
||||
default:
|
||||
m.Echo("%v", value)
|
||||
}
|
||||
}},
|
||||
"cache": &Command{
|
||||
@ -3572,13 +3464,81 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
m.Echo(arg[0], values...)
|
||||
m.Append("format", m.Result(0))
|
||||
}},
|
||||
"select": &Command{Name: "select key value", Form: map[string]int{"order": 2, "limit": 1, "offset": 1, "vertical": 1}, Help: "选取数据", Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
"trans": &Command{Name: "trans key index", Help: "数据转换", Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
value := m.Data[(arg[0])]
|
||||
if arg[1] != "" {
|
||||
v := Chain(m, value, arg[1])
|
||||
value = v
|
||||
}
|
||||
m.Log("fuck", "info %T", value)
|
||||
m.Log("fuck", "info %v", value)
|
||||
|
||||
switch val := value.(type) {
|
||||
case map[string]interface{}:
|
||||
for k, v := range val {
|
||||
m.Log("fuck", "info %v %v", k, v)
|
||||
m.Add("append", "key", k)
|
||||
m.Add("append", "value", v)
|
||||
}
|
||||
m.Sort("key", "str").Table()
|
||||
case map[string]string:
|
||||
for k, v := range val {
|
||||
m.Add("append", "key", k)
|
||||
m.Add("append", "value", v)
|
||||
}
|
||||
m.Sort("key", "str").Table()
|
||||
case []interface{}:
|
||||
for i, v := range val {
|
||||
switch value := v.(type) {
|
||||
case map[string]interface{}:
|
||||
for k, v := range value {
|
||||
m.Add("append", k, v)
|
||||
}
|
||||
default:
|
||||
m.Add("append", "index", i)
|
||||
m.Add("append", "value", v)
|
||||
}
|
||||
}
|
||||
m.Table()
|
||||
case []string:
|
||||
for i, v := range val {
|
||||
m.Add("append", "index", i)
|
||||
m.Add("append", "value", v)
|
||||
}
|
||||
m.Table()
|
||||
case float64:
|
||||
m.Echo("%d", int(val))
|
||||
case nil:
|
||||
m.Echo("")
|
||||
default:
|
||||
m.Echo("%v", val)
|
||||
}
|
||||
}},
|
||||
"select": &Command{Name: "select key value", Form: map[string]int{"parse": 2, "order": 2, "limit": 1, "offset": 1, "vertical": 0}, Help: "选取数据", Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
msg := m.Spawn()
|
||||
|
||||
nrow := len(m.Meta[m.Meta["append"][0]])
|
||||
for i := 0; i < nrow; i++ {
|
||||
if len(arg) == 0 || strings.Contains(m.Meta[arg[0]][i], arg[1]) {
|
||||
for _, k := range m.Meta["append"] {
|
||||
msg.Add("append", k, m.Meta[k][i])
|
||||
if m.Has("parse") && m.Option("parse") == k {
|
||||
var value interface{}
|
||||
m.Assert(json.Unmarshal([]byte(m.Meta[k][i]), &value))
|
||||
if m.Meta["parse"][1] != "" {
|
||||
value = Chain(m, value, m.Meta["parse"][1])
|
||||
}
|
||||
|
||||
switch val := value.(type) {
|
||||
case map[string]interface{}:
|
||||
for k, v := range val {
|
||||
msg.Add("append", k, v)
|
||||
}
|
||||
case []interface{}:
|
||||
default:
|
||||
}
|
||||
} else {
|
||||
msg.Add("append", k, m.Meta[k][i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3610,6 +3570,8 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
msg := m.Spawn()
|
||||
nrow := len(m.Meta[m.Meta["append"][0]])
|
||||
sort.Strings(m.Meta["append"])
|
||||
msg.Add("append", "field", "")
|
||||
msg.Add("append", "value", "")
|
||||
for i := 0; i < nrow; i++ {
|
||||
for _, k := range m.Meta["append"] {
|
||||
msg.Add("append", "field", k)
|
||||
@ -3728,16 +3690,12 @@ func Start(args ...string) {
|
||||
Pulse.Sess("log", "log")
|
||||
|
||||
if len(args) > 0 {
|
||||
if Pulse.Sess("nfs").Cmd("exist", args[0]).Results(0) {
|
||||
Pulse.Sess("cli", false).Conf("init.shy", args[0])
|
||||
args = args[1:]
|
||||
}
|
||||
Pulse.Sess("cli", false).Conf("init.shy", args[0])
|
||||
args = args[1:]
|
||||
}
|
||||
if len(args) > 0 {
|
||||
if Pulse.Sess("nfs").Cmd("exist", args[0]).Results(0) {
|
||||
Pulse.Sess("log", false).Conf("bench.log", args[0])
|
||||
args = args[1:]
|
||||
}
|
||||
Pulse.Sess("log", false).Conf("bench.log", args[0])
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
Pulse.Options("log", true)
|
||||
|
@ -21,15 +21,13 @@ import (
|
||||
)
|
||||
|
||||
type NFS struct {
|
||||
in *os.File
|
||||
out *os.File
|
||||
history []string
|
||||
pages []string
|
||||
width int
|
||||
height int
|
||||
in *os.File
|
||||
out *os.File
|
||||
pages []string
|
||||
width int
|
||||
height int
|
||||
|
||||
paths []string
|
||||
io io.ReadWriter
|
||||
io io.ReadWriter
|
||||
|
||||
send chan *ctx.Message
|
||||
recv chan *ctx.Message
|
||||
@ -49,9 +47,10 @@ func (nfs *NFS) open(name string) (*os.File, error) {
|
||||
nfs.Message.Log("info", "open %s", name)
|
||||
return os.Open(name)
|
||||
}
|
||||
for i := len(nfs.paths) - 1; i >= 0; i-- {
|
||||
if f, e := os.Open(path.Join(nfs.paths[i], name)); e == nil {
|
||||
nfs.Log("info", "open %s", path.Join(nfs.paths[i], name))
|
||||
paths := nfs.Context.Message().Confv("paths").([]interface{})
|
||||
for i := len(paths) - 1; i >= 0; i-- {
|
||||
if f, e := os.Open(path.Join(paths[i].(string), name)); e == nil {
|
||||
nfs.Log("info", "open %s", path.Join(paths[i].(string), name))
|
||||
return f, e
|
||||
}
|
||||
}
|
||||
@ -355,7 +354,8 @@ func (nfs *NFS) Read(p []byte) (n int, err error) {
|
||||
|
||||
back := buf
|
||||
|
||||
his := len(nfs.history)
|
||||
history := nfs.Context.Message().Confv("history").([]interface{})
|
||||
his := len(history)
|
||||
|
||||
tab := []string{}
|
||||
tabi := 0
|
||||
@ -394,21 +394,21 @@ func (nfs *NFS) Read(p []byte) (n int, err error) {
|
||||
return
|
||||
|
||||
case termbox.KeyCtrlP:
|
||||
for i := 0; i < len(nfs.history); i++ {
|
||||
his = (his + len(nfs.history) - 1) % len(nfs.history)
|
||||
if strings.HasPrefix(nfs.history[his], string(buf)) {
|
||||
for i := 0; i < len(history); i++ {
|
||||
his = (his + len(history) - 1) % len(history)
|
||||
if strings.HasPrefix(history[his].(string), string(buf)) {
|
||||
rest = rest[:0]
|
||||
rest = append(rest, []rune(nfs.history[his][len(buf):])...)
|
||||
rest = append(rest, []rune(history[his].(string)[len(buf):])...)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
case termbox.KeyCtrlN:
|
||||
for i := 0; i < len(nfs.history); i++ {
|
||||
his = (his + len(nfs.history) + 1) % len(nfs.history)
|
||||
if strings.HasPrefix(nfs.history[his], string(buf)) {
|
||||
for i := 0; i < len(history); i++ {
|
||||
his = (his + len(history) + 1) % len(history)
|
||||
if strings.HasPrefix(history[his].(string), string(buf)) {
|
||||
rest = rest[:0]
|
||||
rest = append(rest, []rune(nfs.history[his][len(buf):])...)
|
||||
rest = append(rest, []rune(history[his].(string)[len(buf):])...)
|
||||
break
|
||||
}
|
||||
}
|
||||
@ -546,7 +546,9 @@ func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
||||
"nwrite": &ctx.Cache{Name: "nwrite", Value: "0", Help: "nwrite"},
|
||||
"nline": &ctx.Cache{Name: "缓存命令行数", Value: "0", Help: "缓存命令行数"},
|
||||
}
|
||||
c.Configs = map[string]*ctx.Config{}
|
||||
c.Configs = map[string]*ctx.Config{
|
||||
"history": &ctx.Config{Name: "history", Value: []interface{}{}, Help: "读取记录"},
|
||||
}
|
||||
} else {
|
||||
c.Caches = map[string]*ctx.Cache{
|
||||
"nsend": &ctx.Cache{Name: "消息发送数量", Value: "0", Help: "消息发送数量"},
|
||||
@ -630,11 +632,12 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool {
|
||||
line = line[:len(line)-1]
|
||||
continue
|
||||
}
|
||||
nfs.history = append(nfs.history, line)
|
||||
m.Capi("nline", 1)
|
||||
m.Confv("history", -2, line)
|
||||
history := m.Confv("history").([]interface{})
|
||||
|
||||
for i := len(nfs.history) - 1; i < len(nfs.history); i++ {
|
||||
line = nfs.history[i]
|
||||
for i := len(history) - 1; i < len(history); i++ {
|
||||
line = history[i].(string)
|
||||
|
||||
msg := m.Spawn(m.Source()).Set("detail", line)
|
||||
msg.Option("file_pos", i)
|
||||
@ -839,39 +842,6 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
||||
}
|
||||
}
|
||||
}},
|
||||
"paths": &ctx.Command{
|
||||
Name: "paths [add path]|[del index]|[set index path]|[index]",
|
||||
Help: "设置文件搜索路径, add: 添加目录, del: 删除目录, set: 修改目录,index: 目录序号, path: 目录名",
|
||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) {
|
||||
if len(arg) == 0 {
|
||||
for i, v := range nfs.paths {
|
||||
m.Echo("%d: %s\n", i, v)
|
||||
}
|
||||
return
|
||||
}
|
||||
switch arg[0] {
|
||||
case "add":
|
||||
nfs.paths = append(nfs.paths, arg[1])
|
||||
case "del":
|
||||
if i, e := strconv.Atoi(arg[1]); e == nil && i < len(nfs.paths) {
|
||||
for ; i < len(nfs.paths)-1; i++ {
|
||||
nfs.paths[i] = nfs.paths[i+1]
|
||||
}
|
||||
nfs.paths = nfs.paths[:len(nfs.paths)-1]
|
||||
}
|
||||
case "set":
|
||||
if i, e := strconv.Atoi(arg[1]); e == nil && i < len(nfs.paths) {
|
||||
nfs.paths[i] = arg[2]
|
||||
m.Echo("%d: %s\n", i, nfs.paths[i])
|
||||
}
|
||||
default:
|
||||
if i, e := strconv.Atoi(arg[0]); e == nil && i < len(nfs.paths) {
|
||||
m.Echo("%d: %s\n", i, nfs.paths[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}},
|
||||
"scan": &ctx.Command{
|
||||
Name: "scan filename [nfs_name [nfs_help]]",
|
||||
Help: "扫描文件, filename: 文件名, nfs_name: 模块名, nfs_help: 模块帮助",
|
||||
@ -890,97 +860,10 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
||||
}
|
||||
}
|
||||
|
||||
m.Start(m.Confx("nfs_name", arg, 1), m.Confx("nfs_help", arg, 2), key, arg[0])
|
||||
}
|
||||
}},
|
||||
"history": &ctx.Command{
|
||||
Name: "history [save|load filename [lines [pos]]] [find|search key]",
|
||||
Help: "扫描记录, save: 保存记录, load: 加载记录, filename: 文件名, lines: 加载或保存记录数量, pos: 加载或保存的起始位置, find: 查找记录, search: 搜索记录, key: 查找或搜索的参数",
|
||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) {
|
||||
if len(arg) == 0 {
|
||||
for i, v := range nfs.history {
|
||||
m.Echo("%d: %s\n", i, v)
|
||||
}
|
||||
return
|
||||
}
|
||||
switch arg[0] {
|
||||
case "load":
|
||||
f, e := nfs.open(arg[1])
|
||||
m.Assert(e)
|
||||
defer f.Close()
|
||||
|
||||
pos, lines := 0, -1
|
||||
if len(arg) > 3 {
|
||||
i, e := strconv.Atoi(arg[3])
|
||||
m.Assert(e)
|
||||
pos = i
|
||||
|
||||
}
|
||||
if len(arg) > 2 {
|
||||
i, e := strconv.Atoi(arg[2])
|
||||
m.Assert(e)
|
||||
lines = i
|
||||
}
|
||||
|
||||
bio := bufio.NewScanner(f)
|
||||
for i := 0; bio.Scan(); i++ {
|
||||
if i < pos {
|
||||
continue
|
||||
}
|
||||
if lines != -1 && (i-pos) >= lines {
|
||||
break
|
||||
}
|
||||
line := bio.Text()
|
||||
for _, v := range nfs.history {
|
||||
if line == v {
|
||||
line = ""
|
||||
}
|
||||
}
|
||||
if line != "" {
|
||||
nfs.history = append(nfs.history, line)
|
||||
}
|
||||
}
|
||||
m.Capi("nline", 0, len(nfs.history))
|
||||
case "save":
|
||||
f, e := os.Create(arg[1])
|
||||
// f, e := os.OpenFile(arg[1], os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
||||
m.Assert(e)
|
||||
defer f.Close()
|
||||
|
||||
pos, lines := 0, -1
|
||||
if len(arg) > 3 {
|
||||
i, e := strconv.Atoi(arg[3])
|
||||
m.Assert(e)
|
||||
pos = i
|
||||
|
||||
}
|
||||
if len(arg) > 2 {
|
||||
i, e := strconv.Atoi(arg[2])
|
||||
m.Assert(e)
|
||||
lines = i
|
||||
}
|
||||
|
||||
for i, v := range nfs.history {
|
||||
if i < pos {
|
||||
continue
|
||||
}
|
||||
if lines != -1 && (i-pos) >= lines {
|
||||
break
|
||||
}
|
||||
fmt.Fprintln(f, v)
|
||||
}
|
||||
case "find":
|
||||
for i, v := range nfs.history {
|
||||
if strings.HasPrefix(v, arg[1]) {
|
||||
m.Echo("%d: %s\n", i, v)
|
||||
}
|
||||
}
|
||||
case "search":
|
||||
default:
|
||||
if i, e := strconv.Atoi(arg[0]); e == nil && i < len(nfs.history) {
|
||||
m.Echo(nfs.history[i])
|
||||
}
|
||||
if arg[0] == "stdio" {
|
||||
m.Start("stdio", "stdio", key, arg[0])
|
||||
} else {
|
||||
m.Start(m.Confx("nfs_name", arg, 1), m.Confx("nfs_help", arg, 2), key, arg[0])
|
||||
}
|
||||
}
|
||||
}},
|
||||
|
@ -405,7 +405,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||
}},
|
||||
"get": &ctx.Command{Name: "get [method GET|POST] url arg...",
|
||||
Help: "访问服务, method: 请求方法, url: 请求地址, arg: 请求参数",
|
||||
Form: map[string]int{"method": 1, "headers": 2, "content_type": 1, "body": 1, "path_value": 1, "body_response": 1},
|
||||
Form: map[string]int{"method": 1, "headers": 2, "content_type": 1, "body": 1, "path_value": 1, "body_response": 1, "trans": 1},
|
||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if web, ok := m.Target().Server.(*WEB); m.Assert(ok) {
|
||||
if m.Has("path_value") {
|
||||
@ -471,6 +471,15 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||
switch {
|
||||
case strings.HasPrefix(ct, "application/json"):
|
||||
json.Unmarshal(buf, &result)
|
||||
if m.Has("trans") {
|
||||
msg := m.Spawn()
|
||||
msg.Put("option", "response", result)
|
||||
msg.Cmd("trans", "response", m.Option("trans"))
|
||||
m.Copy(msg, "append")
|
||||
m.Copy(msg, "result")
|
||||
return
|
||||
}
|
||||
|
||||
default:
|
||||
result = string(buf)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user