1
0
forked from x/ContextOS

tce add command test

This commit is contained in:
shaoying 2018-06-22 22:11:20 +08:00
parent d37ccbf5cb
commit 74bd31bbd7
3 changed files with 72 additions and 5 deletions

View File

@ -327,7 +327,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
t := time.Now() // {{{ t := time.Now() // {{{
if m.Options("parse") { if m.Options("parse") {
f := "2006-01-02 15:04:05" f := "2006-01-02 15:04:05"
n, e := time.Parse(f, m.Option("parse")) n, e := time.ParseInLocation(f, m.Option("parse"), time.Local)
m.Assert(e) m.Assert(e)
t = n t = n
} }

View File

@ -2363,7 +2363,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
// }}} // }}}
}}, }},
"command": &Command{Name: "command [all] add [key [name help]]", Help: "查看或修改命令", "command": &Command{Name: "command [all] add [key [name help]]", Help: "查看或修改命令",
Formats: map[string]int{"all": 0, "delete": 0, "void": 0}, Formats: map[string]int{"all": 0, "delete": 0, "void": 0, "condition": -1},
Hand: func(m *Message, c *Context, key string, arg ...string) { Hand: func(m *Message, c *Context, key string, arg ...string) {
all := m.Has("all") // {{{ all := m.Has("all") // {{{
if len(arg) == 0 { if len(arg) == 0 {
@ -2417,12 +2417,61 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
m.Capi("part", 1) m.Capi("part", 1)
return return
case "list": case "list":
for i := 0; i < m.Capi("part"); i++ { begin, end := 0, m.Capi("part")
if len(arg) > 1 {
n, e := strconv.Atoi(arg[1])
m.Assert(e)
begin = n
}
if len(arg) > 2 {
n, e := strconv.Atoi(arg[2])
m.Assert(e)
end = n
}
for i := begin; i < end; i++ {
if c, ok := m.target.Commands[fmt.Sprintf("%d", i)]; ok { if c, ok := m.target.Commands[fmt.Sprintf("%d", i)]; ok {
m.Echo("%d: %s\n", i, c.Name) m.Echo("%d: %s\n", i, c.Name)
} }
} }
return return
case "test":
begin, end := 0, m.Capi("part")
if len(arg) > 1 {
n, e := strconv.Atoi(arg[1])
m.Assert(e)
begin = n
}
if len(arg) > 2 {
n, e := strconv.Atoi(arg[2])
m.Assert(e)
end = n
}
success, failure := 0, 0
for i := begin; i < end; i++ {
key := fmt.Sprintf("%d", i)
if c, ok := m.target.Commands[key]; ok {
msg := m.Spawn(m.Target())
msg.Cmd(key)
if m.Options("condition") {
condition := m.Meta["condition"]
done := true
for j := 0; j < len(condition)-1; j += 2 {
if !msg.Has(condition[j]) || msg.Append(condition[j]) != condition[j+1] {
m.Echo("\033[31m%s %s %s\033[0m\n", key, " fail", c.Name)
failure++
done = false
}
}
if done {
// m.Echo("%s %s\n", key, " done")
m.Echo("%s %s %s\n", key, " done", c.Name)
success++
}
}
}
}
m.Echo("\033[32msuccess: %d\033[0m, \033[31mfailure: %d\033[0m, \033[33mtotal: %d\033[0m", success, failure, success+failure)
return
} }
switch len(arg) { switch len(arg) {

View File

@ -494,7 +494,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
io.Copy(part, file) io.Copy(part, file)
for i := 0; i < len(arg)-1; i += 2 { for i := 0; i < len(arg)-1; i += 2 {
writer.WriteField(arg[0], arg[1]) writer.WriteField(arg[i], arg[i])
} }
contenttype = writer.FormDataContentType() contenttype = writer.FormDataContentType()
@ -553,7 +553,25 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
buf, e := ioutil.ReadAll(res.Body) buf, e := ioutil.ReadAll(res.Body)
m.Assert(e) m.Assert(e)
m.Echo(string(buf))
if res.Header.Get("Content-Type") == "application/json" {
result := map[string]interface{}{}
json.Unmarshal(buf, &result)
for k, v := range result {
switch value := v.(type) {
case string:
m.Append(k, value)
case float64:
m.Append(k, fmt.Sprintf("%d", int(value)))
default:
m.Put("append", k, value)
}
}
}
result := string(buf)
m.Echo(result)
m.Append("response", result)
// }}} // }}}
}}, }},
"list": &ctx.Command{Name: "list [set|add|del [url]]", Help: "查看、访问、添加url", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "list": &ctx.Command{Name: "list [set|add|del [url]]", Help: "查看、访问、添加url", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {