From f8e96a5d76bb603aa8e8e498aeff4b0a59eb5e57 Mon Sep 17 00:00:00 2001 From: harveyshao Date: Sun, 24 Oct 2021 00:11:49 +0800 Subject: [PATCH] opt icebergs --- base/cli/qrcode.go | 2 +- base/gdb/gdb.go | 4 +- base/gdb/routine.go | 9 +-- base/gdb/timer.go | 3 +- base/log/log.go | 21 +++---- base/mdb/engine.go | 1 + base/mdb/hash.go | 15 ++--- base/mdb/list.go | 18 +++--- base/mdb/mdb.go | 23 ++------ base/mdb/plugin.go | 1 + base/mdb/search.go | 2 +- base/nfs/cat.go | 6 +- base/nfs/dir.go | 12 ++-- base/nfs/save.go | 4 +- base/nfs/tail.go | 6 +- base/nfs/trash.go | 2 +- base/ssh/scripts.go | 33 ++++------- base/tcp/host.go | 2 +- base/web/cache.go | 6 +- base/web/route.go | 3 +- base/web/serve.go | 5 +- base/web/space.go | 8 +-- base/web/story.go | 2 +- base/web/web.go | 1 + base/yac/matrix.go | 2 +- conf.go | 28 ++++++---- core/code/csdn.go | 35 ------------ core/code/github.go | 36 ------------ core/team/plan.go | 2 +- core/wiki/field.go | 23 +++++++- core/wiki/word.go | 1 - data.go | 7 +-- exec.go | 21 ++++--- init.go | 36 +++++------- logs.go | 106 +++++++++++++++++------------------ meta.go | 133 +++++++++++++------------------------------- misc.go | 78 ++++++++++---------------- misc/input/input.go | 4 +- misc/lark/event.go | 4 +- misc/lark/msg.go | 6 +- option.go | 16 +++--- render.go | 85 ++++++++++++---------------- type.go | 100 ++++++++++++--------------------- 43 files changed, 350 insertions(+), 562 deletions(-) delete mode 100644 core/code/csdn.go delete mode 100644 core/code/github.go diff --git a/base/cli/qrcode.go b/base/cli/qrcode.go index 0980f284..d6606858 100644 --- a/base/cli/qrcode.go +++ b/base/cli/qrcode.go @@ -139,7 +139,7 @@ func init() { m.Option(FG, kit.Select(BLUE, arg, 1)) if m.IsCliUA() { - _qrcode_cli(m, kit.Select(m.Conf("web.share", kit.Keym(kit.MDB_DOMAIN)), arg, 0)) + _qrcode_cli(m, kit.Select(m.Conf("share", kit.Keym(kit.MDB_DOMAIN)), arg, 0)) } else { _qrcode_web(m, kit.Select(m.Option(ice.MSG_USERWEB), arg, 0)) } diff --git a/base/gdb/gdb.go b/base/gdb/gdb.go index f288a4c5..7fd7c9f3 100644 --- a/base/gdb/gdb.go +++ b/base/gdb/gdb.go @@ -39,6 +39,7 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool { return true } func (f *Frame) Close(m *ice.Message, arg ...string) bool { + f.e <- true return true } @@ -49,9 +50,6 @@ var Index = &ice.Context{Name: GDB, Help: "事件模块", Commands: map[string]* m.Load(TIMER) }}, ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - if f, ok := m.Target().Server().(*Frame); ok { - f.e <- true - } m.Save(TIMER) }}, }} diff --git a/base/gdb/routine.go b/base/gdb/routine.go index 501dfaf3..50de0631 100644 --- a/base/gdb/routine.go +++ b/base/gdb/routine.go @@ -9,9 +9,6 @@ import ( kit "shylinux.com/x/toolkits" ) -const ( - INNER = "inner" -) const ROUTINE = "routine" func init() { @@ -27,13 +24,13 @@ func init() { m.Cmdy(mdb.PRUNES, ROUTINE, "", mdb.HASH, kit.MDB_STATUS, cli.STOP) m.Cmdy(mdb.PRUNES, ROUTINE, "", mdb.HASH, kit.MDB_STATUS, cli.ERROR) }}, - INNER: {Name: "inner", Help: "源码", Hand: func(m *ice.Message, arg ...string) { + "inner": {Name: "inner", Help: "源码", Hand: func(m *ice.Message, arg ...string) { ls := kit.Split(m.Option("fileline"), ":") - m.ProcessCommand(INNER, []string{path.Dir(ls[0]), path.Base(ls[0]), ls[1]}, arg...) + m.ProcessCommand("inner", []string{path.Dir(ls[0]), path.Base(ls[0]), ls[1]}, arg...) }}, }, mdb.HashAction()), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { mdb.HashSelect(m, arg...) - m.PushAction(INNER, mdb.REMOVE) + m.PushAction("inner", mdb.REMOVE) }}, }}) } diff --git a/base/gdb/timer.go b/base/gdb/timer.go index 5efe459e..6fb8f260 100644 --- a/base/gdb/timer.go +++ b/base/gdb/timer.go @@ -43,8 +43,7 @@ const TIMER = "timer" func init() { Index.Merge(&ice.Context{Configs: map[string]*ice.Config{ TIMER: {Name: TIMER, Help: "定时器", Value: kit.Data( - kit.MDB_FIELD, "time,hash,delay,interval,order,next,cmd", - TICK, "1s", + kit.MDB_FIELD, "time,hash,delay,interval,order,next,cmd", TICK, "1s", )}, }, Commands: map[string]*ice.Command{ TIMER: {Name: "timer hash id auto create prunes", Help: "定时器", Action: ice.MergeAction(map[string]*ice.Action{ diff --git a/base/log/log.go b/base/log/log.go index 11bcfa17..4afe0653 100644 --- a/base/log/log.go +++ b/base/log/log.go @@ -61,6 +61,8 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool { return true } func (f *Frame) Close(m *ice.Message, arg ...string) bool { + ice.Info.Log = nil + close(f.p) return true } @@ -90,30 +92,32 @@ var Index = &ice.Context{Name: "log", Help: "日志模块", Configs: map[string] WATCH, kit.Dict(kit.MDB_PATH, path.Join(ice.VAR_LOG, "watch.log"), kit.MDB_LIST, []string{ ice.LOG_CREATE, ice.LOG_REMOVE, ice.LOG_INSERT, ice.LOG_DELETE, - ice.LOG_SELECT, ice.LOG_MODIFY, + ice.LOG_MODIFY, ice.LOG_SELECT, ice.LOG_EXPORT, ice.LOG_IMPORT, }), BENCH, kit.Dict(kit.MDB_PATH, path.Join(ice.VAR_LOG, "bench.log"), kit.MDB_LIST, []string{}), ERROR, kit.Dict(kit.MDB_PATH, path.Join(ice.VAR_LOG, "error.log"), kit.MDB_LIST, []string{ - ice.LOG_WARN, ice.LOG_ERROR, + ice.LOG_WARN, ice.LOG_ERROR, ice.LOG_DEBUG, }), TRACE, kit.Dict(kit.MDB_PATH, path.Join(ice.VAR_LOG, "trace.log"), kit.MDB_LIST, []string{}), )}, VIEW: {Name: VIEW, Help: "日志格式", Value: kit.Dict( GREEN, kit.Dict(PREFIX, "\033[32m", SUFFIX, "\033[0m", kit.MDB_LIST, []string{ - ice.LOG_START, ice.LOG_SERVE, - ice.LOG_CMDS, + ice.LOG_START, ice.LOG_SERVE, ice.LOG_CMDS, }), YELLOW, kit.Dict(PREFIX, "\033[33m", SUFFIX, "\033[0m", kit.MDB_LIST, []string{ ice.LOG_AUTH, ice.LOG_COST, }), RED, kit.Dict(PREFIX, "\033[31m", SUFFIX, "\033[0m", kit.MDB_LIST, []string{ - ice.LOG_WARN, ice.LOG_CLOSE, + ice.LOG_CLOSE, ice.LOG_WARN, }), )}, SHOW: {Name: SHOW, Help: "日志分流", Value: kit.Dict()}, }, Commands: map[string]*ice.Command{ ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if log.LogDisable { + return // 禁用日志 + } m.Confm(VIEW, nil, func(key string, value map[string]interface{}) { kit.Fetch(value[kit.MDB_LIST], func(index int, k string) { m.Conf(SHOW, kit.Keys(k, VIEW), key) @@ -123,9 +127,6 @@ var Index = &ice.Context{Name: "log", Help: "日志模块", Configs: map[string] kit.Fetch(value[kit.MDB_LIST], func(index int, k string) { m.Conf(SHOW, kit.Keys(k, FILE), key) }) - if log.LogDisable { - return // 禁用日志 - } // 日志文件 if f, p, e := kit.Create(kit.Format(value[kit.MDB_PATH])); m.Assert(e) { m.Cap(ice.CTX_STREAM, path.Base(p)) @@ -135,10 +136,6 @@ var Index = &ice.Context{Name: "log", Help: "日志模块", Configs: map[string] }) }}, ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - if f, ok := m.Target().Server().(*Frame); ok { // 关闭日志 - ice.Info.Log = nil - close(f.p) - } }}, }} diff --git a/base/mdb/engine.go b/base/mdb/engine.go index ec84784c..5bcef0cb 100644 --- a/base/mdb/engine.go +++ b/base/mdb/engine.go @@ -13,6 +13,7 @@ func init() { }, Commands: map[string]*ice.Command{ ENGINE: {Name: "engine type name text auto", Help: "引擎", Action: map[string]*ice.Action{ CREATE: {Name: "create type cmd ctx", Help: "创建", Hand: func(m *ice.Message, arg ...string) { + m.Log_CREATE(ENGINE, arg[0], kit.MDB_NAME, kit.Select(arg[0], arg, 1)) m.Rich(ENGINE, nil, kit.Dict(kit.MDB_TYPE, arg[0], kit.MDB_NAME, kit.Select(arg[0], arg, 1), kit.MDB_TEXT, kit.Select("", arg, 2))) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { diff --git a/base/mdb/hash.go b/base/mdb/hash.go index ad7dfc05..c1b87e62 100644 --- a/base/mdb/hash.go +++ b/base/mdb/hash.go @@ -10,7 +10,7 @@ import ( ) func _hash_fields(m *ice.Message) []string { - return kit.Split(kit.Select("time,hash,type,name,text", kit.Join(kit.Simple(m.Optionv(FIELDS))))) + return kit.Split(kit.Select("time,hash,type,name,text", m.OptionFields())) } func _hash_inputs(m *ice.Message, prefix, chain string, field, value string) { list := map[string]int{} @@ -57,9 +57,8 @@ func _hash_select(m *ice.Message, prefix, chain, field, value string) { value = kit.MDB_RANDOMS } fields := _hash_fields(m) - cb := m.Optionv(kit.Keycb(SELECT)) m.Richs(prefix, chain, value, func(key string, val map[string]interface{}) { - switch val = kit.GetMeta(val); cb := cb.(type) { + switch val = kit.GetMeta(val); cb := m.Optionv(kit.Keycb(SELECT)).(type) { case func(fields []string, value map[string]interface{}): cb(fields, val) default: @@ -81,7 +80,7 @@ func _hash_export(m *ice.Message, prefix, chain, file string) { en := json.NewEncoder(f) en.SetIndent("", " ") - e = en.Encode(m.Confv(prefix, kit.Keys(chain, HASH))) + m.Assert(en.Encode(m.Confv(prefix, kit.Keys(chain, HASH)))) m.Log_EXPORT(kit.MDB_KEY, path.Join(prefix, chain), kit.MDB_FILE, p) m.Conf(prefix, kit.Keys(chain, kit.MDB_HASH), "") @@ -93,8 +92,7 @@ func _hash_import(m *ice.Message, prefix, chain, file string) { defer f.Close() list := map[string]interface{}{} - de := json.NewDecoder(f) - de.Decode(&list) + m.Assert(json.NewDecoder(f).Decode(&list)) count := 0 if m.Conf(prefix, kit.Keys(chain, kit.MDB_META, kit.MDB_SHORT)) == "" { @@ -115,10 +113,7 @@ func _hash_import(m *ice.Message, prefix, chain, file string) { func _hash_prunes(m *ice.Message, prefix, chain string, arg ...string) { fields := _hash_fields(m) m.Richs(prefix, chain, kit.MDB_FOREACH, func(key string, val map[string]interface{}) { - if val[kit.MDB_META] != nil { - val = val[kit.MDB_META].(map[string]interface{}) - } - switch cb := m.Optionv(kit.Keycb(PRUNES)).(type) { + switch val = kit.GetMeta(val); cb := m.Optionv(kit.Keycb(PRUNES)).(type) { case func(string, map[string]interface{}) bool: if !cb(key, val) { return diff --git a/base/mdb/list.go b/base/mdb/list.go index 4cae0bce..607eb45c 100644 --- a/base/mdb/list.go +++ b/base/mdb/list.go @@ -4,14 +4,13 @@ import ( "encoding/csv" "os" "path" - "sort" ice "shylinux.com/x/icebergs" kit "shylinux.com/x/toolkits" ) func _list_fields(m *ice.Message) []string { - return kit.Split(kit.Select("time,id,type,name,text", kit.Join(kit.Simple(m.Optionv(FIELDS))))) + return kit.Split(kit.Select("time,id,type,name,text", m.OptionFields())) } func _list_inputs(m *ice.Message, prefix, chain string, field, value string) { list := map[string]int{} @@ -51,9 +50,8 @@ func _list_select(m *ice.Message, prefix, chain, field, value string) { field = "" } fields := _list_fields(m) - cb := m.Optionv(kit.Keycb(SELECT)) - m.Grows(prefix, chain, kit.Select(m.Option(CACHE_FIELD), field), kit.Select(m.Option(CACHE_VALUE), value), func(index int, val map[string]interface{}) { - switch val = kit.GetMeta(val); cb := cb.(type) { + m.Grows(prefix, chain, kit.Select(m.Option(ice.CACHE_FIELD), field), kit.Select(m.Option(ice.CACHE_VALUE), value), func(index int, val map[string]interface{}) { + switch val = kit.GetMeta(val); cb := m.Optionv(kit.Keycb(SELECT)).(type) { case func(fields []string, value map[string]interface{}): cb(fields, val) default: @@ -74,14 +72,14 @@ func _list_export(m *ice.Message, prefix, chain, file string) { defer w.Flush() count := 0 - head := kit.Split(m.Option(FIELDS)) + head := kit.Split(m.OptionFields()) m.Grows(prefix, chain, "", "", func(index int, val map[string]interface{}) { if val = kit.GetMeta(val); index == 0 { - if len(head) == 0 { // 默认表头 + if len(head) == 0 || head[0] == "detail" { // 默认表头 for k := range val { head = append(head, k) } - sort.Strings(head) + kit.Sort(head) } w.Write(head) // 输出表头 } @@ -95,6 +93,8 @@ func _list_export(m *ice.Message, prefix, chain, file string) { }) m.Log_EXPORT(kit.MDB_KEY, path.Join(prefix, chain), kit.MDB_FILE, p, kit.MDB_COUNT, count) + m.Conf(prefix, kit.Keys(chain, kit.Keym(kit.MDB_COUNT)), 0) + m.Conf(prefix, kit.Keys(chain, kit.MDB_LIST), "") m.Echo(p) } func _list_import(m *ice.Message, prefix, chain, file string) { @@ -154,7 +154,7 @@ func ListAction(fields ...string) map[string]*ice.Action { m.Config(kit.MDB_COUNT, 0) }}, IMPORT: {Name: "import", Help: "导入", Hand: func(m *ice.Message, arg ...string) { - m.Cmdy(IMPORT, m.PrefixKey(), "", LIST) + m.Cmdy(IMPORT, m.PrefixKey(), "", LIST, arg) }}, PRUNES: {Name: "prunes", Help: "清理", Hand: func(m *ice.Message, arg ...string) { m.Cmdy(PRUNES, m.PrefixKey(), "", LIST, arg) diff --git a/base/mdb/mdb.go b/base/mdb/mdb.go index f1eb3edd..374b48fe 100644 --- a/base/mdb/mdb.go +++ b/base/mdb/mdb.go @@ -46,12 +46,6 @@ const ( PREV = "prev" ) const ( - CACHE_LIMIT = "cache.limit" - CACHE_FIELD = "cache.field" - CACHE_VALUE = "cache.value" - CACHE_OFFEND = "cache.offend" - CACHE_FILTER = "cache.filter" - CACHE_CLEAR_ON_EXIT = "cache.clear.on.exit" ) @@ -92,19 +86,12 @@ func NextPageLimit(m *ice.Message, total string, arg ...string) { m.ProcessHold() } } -func SetPage(m *ice.Message, arg ...string) { - m.Option(CACHE_LIMIT, kit.Select("10", arg, 0)) - m.Option(CACHE_OFFEND, kit.Select("0", arg, 1)) - m.Option(CACHE_FILTER, kit.Select("", arg, 2)) -} const MDB = "mdb" var Index = &ice.Context{Name: MDB, Help: "数据模块", Commands: map[string]*ice.Command{ - ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - }}, - ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - }}, + ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}}, + ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}}, INSERT: {Name: "insert key sub type arg...", Help: "添加", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { switch arg[2] { case ZONE: @@ -168,11 +155,11 @@ var Index = &ice.Context{Name: MDB, Help: "数据模块", Commands: map[string]* INPUTS: {Name: "inputs key sub type field value", Help: "补全", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { switch arg[2] { case ZONE: - _list_inputs(m, arg[0], _domain_chain(m, kit.Keys(arg[1], kit.KeyHash(arg[3]))), kit.Select("name", arg, 4), kit.Select("", arg, 5)) + _list_inputs(m, arg[0], _domain_chain(m, kit.Keys(arg[1], kit.KeyHash(arg[3]))), kit.Select(kit.MDB_NAME, arg, 4), kit.Select("", arg, 5)) case HASH: - _hash_inputs(m, arg[0], _domain_chain(m, arg[1]), kit.Select("name", arg, 3), kit.Select("", arg, 4)) + _hash_inputs(m, arg[0], _domain_chain(m, arg[1]), kit.Select(kit.MDB_NAME, arg, 3), kit.Select("", arg, 4)) case LIST: - _list_inputs(m, arg[0], _domain_chain(m, arg[1]), kit.Select("name", arg, 3), kit.Select("", arg, 4)) + _list_inputs(m, arg[0], _domain_chain(m, arg[1]), kit.Select(kit.MDB_NAME, arg, 3), kit.Select("", arg, 4)) } }}, PRUNES: {Name: "prunes key sub type [field value]...", Help: "清理", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { diff --git a/base/mdb/plugin.go b/base/mdb/plugin.go index 4b7b88f7..e0bbf37c 100644 --- a/base/mdb/plugin.go +++ b/base/mdb/plugin.go @@ -13,6 +13,7 @@ func init() { }, Commands: map[string]*ice.Command{ PLUGIN: {Name: "plugin type name text auto", Help: "插件", Action: map[string]*ice.Action{ CREATE: {Name: "create type cmd ctx", Help: "创建", Hand: func(m *ice.Message, arg ...string) { + m.Log_CREATE(PLUGIN, arg[0], kit.MDB_NAME, kit.Select(arg[0], arg, 1)) m.Rich(PLUGIN, nil, kit.Dict(kit.MDB_TYPE, arg[0], kit.MDB_NAME, kit.Select(arg[0], arg, 1), kit.MDB_TEXT, kit.Select("", arg, 2))) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { diff --git a/base/mdb/search.go b/base/mdb/search.go index 1dc649ad..9e108a7d 100644 --- a/base/mdb/search.go +++ b/base/mdb/search.go @@ -26,7 +26,7 @@ func init() { return } - m.Option(ice.MSG_FIELDS, kit.Select("ctx,cmd,time,size,type,name,text", kit.Select(m.Option(ice.MSG_FIELDS), arg, 2))) + m.OptionFields(kit.Select("ctx,cmd,time,size,type,name,text", kit.Select(m.OptionFields(), arg, 2))) for _, k := range strings.Split(arg[0], ",") { for _, kk := range strings.Split(arg[1], ",") { m.Richs(SEARCH, nil, k, func(key string, value map[string]interface{}) { diff --git a/base/nfs/cat.go b/base/nfs/cat.go index aed33b8b..d972a686 100644 --- a/base/nfs/cat.go +++ b/base/nfs/cat.go @@ -61,7 +61,7 @@ func _cat_find(m *ice.Message, name string) io.ReadCloser { return NewReadCloser(bytes.NewBuffer(b)) } - msg := m.Cmd("web.spide", ice.DEV, "raw", "GET", path.Join("/share/local/", name)) + msg := m.Cmd("spide", ice.DEV, "raw", "GET", path.Join("/share/local/", name)) if msg.Result(0) == ice.ErrWarn { return NewReadCloser(bytes.NewBufferString("")) } @@ -74,7 +74,7 @@ func _cat_list(m *ice.Message, name string) { f := _cat_find(m, name) if f == nil { - return + return // 没有文件 } defer f.Close() @@ -141,7 +141,7 @@ func init() { _cat_list(m, path.Join(arg[2], arg[1])) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - if len(arg) == 0 || strings.HasSuffix(arg[0], "/") { + if len(arg) == 0 || strings.HasSuffix(arg[0], ice.PS) { m.Cmdy(DIR, arg) return } diff --git a/base/nfs/dir.go b/base/nfs/dir.go index 1797f6a8..77745007 100644 --- a/base/nfs/dir.go +++ b/base/nfs/dir.go @@ -16,7 +16,7 @@ import ( ) func _dir_list(m *ice.Message, root string, name string, level int, deep bool, dir_type string, dir_reg *regexp.Regexp, fields []string) *ice.Message { - if !_cat_right(m, name) { + if !_cat_right(m, path.Join(root, name)) { return m // 没有权限 } @@ -32,10 +32,10 @@ func _dir_list(m *ice.Message, root string, name string, level int, deep bool, d } for _, f := range fs { - if f.Name() == "." || f.Name() == ".." { + if f.Name() == ice.PT || f.Name() == ".." { continue } - if strings.HasPrefix(f.Name(), ".") && dir_type != TYPE_ALL { + if strings.HasPrefix(f.Name(), ice.PT) && dir_type != TYPE_ALL { continue } @@ -61,11 +61,11 @@ func _dir_list(m *ice.Message, root string, name string, level int, deep bool, d m.Push(field, strings.Repeat("| ", level-1)+"|-"+f.Name()) } case "full": - m.Push(field, path.Join(root, name, f.Name())+kit.Select("", "/", f.IsDir())) + m.Push(field, path.Join(root, name, f.Name())+kit.Select("", ice.PS, f.IsDir())) case kit.MDB_PATH: - m.Push(field, path.Join(name, f.Name())+kit.Select("", "/", f.IsDir())) + m.Push(field, path.Join(name, f.Name())+kit.Select("", ice.PS, f.IsDir())) case kit.MDB_FILE: - m.Push(field, f.Name()+kit.Select("", "/", f.IsDir())) + m.Push(field, f.Name()+kit.Select("", ice.PS, f.IsDir())) case kit.MDB_NAME: m.Push(field, f.Name()) diff --git a/base/nfs/save.go b/base/nfs/save.go index 5bbb5cf4..b4b909e0 100644 --- a/base/nfs/save.go +++ b/base/nfs/save.go @@ -29,7 +29,7 @@ func _defs_file(m *ice.Message, name string, text ...string) { } func _push_file(m *ice.Message, name string, text ...string) { p := path.Join(m.Option(DIR_ROOT), name) - if strings.Contains(p, "/") { + if strings.Contains(p, ice.PS) { os.MkdirAll(path.Dir(p), ice.MOD_DIR) } @@ -67,7 +67,7 @@ func _link_file(m *ice.Message, name string, from string) { } os.Remove(name) os.MkdirAll(path.Dir(name), ice.MOD_DIR) - os.Link(from, name) + m.Warn(os.Link(from, name) != nil, "link err of", from) m.Echo(name) } diff --git a/base/nfs/tail.go b/base/nfs/tail.go index cc2dcd77..fd536fea 100644 --- a/base/nfs/tail.go +++ b/base/nfs/tail.go @@ -56,7 +56,7 @@ func init() { m.Cmdy(DIR, kit.Select("./", arg, 1), PATH).RenameAppend(PATH, FILE) m.ProcessAgain() case kit.MDB_NAME: - m.Push(arg[0], kit.Split(m.Option(FILE), "/")) + m.Push(arg[0], kit.Split(m.Option(FILE), ice.PS)) case kit.MDB_LIMIT: m.Push(arg[0], kit.List("10", "20", "30", "50")) } @@ -66,10 +66,10 @@ func init() { }}, }, mdb.ZoneAction()), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Fields(len(kit.Slice(arg, 0, 2)), "time,name,count,file", m.Config(kit.MDB_FIELD)) - mdb.SetPage(m, kit.Slice(arg, 2)...) + m.OptionPage(kit.Slice(arg, 2)...) mdb.ZoneSelect(m.Spawn(c), arg...).Table(func(index int, value map[string]string, head []string) { - if strings.Contains(value[kit.MDB_TEXT], m.Option(mdb.CACHE_FILTER)) { + if strings.Contains(value[kit.MDB_TEXT], m.Option(ice.CACHE_FILTER)) { m.Push("", value, head) } }) diff --git a/base/nfs/trash.go b/base/nfs/trash.go index 9b85916d..c0c85cc7 100644 --- a/base/nfs/trash.go +++ b/base/nfs/trash.go @@ -22,7 +22,7 @@ func _trash_create(m *ice.Message, name string) { defer f.Close() h := kit.Hashs(f) - p := path.Join(m.Conf(TRASH, kit.META_PATH), h[:2], h) + p := path.Join(m.Config(kit.MDB_PATH), h[:2], h) os.MkdirAll(path.Dir(p), ice.MOD_DIR) os.Rename(name, p) m.Cmdy(mdb.INSERT, TRASH, "", mdb.HASH, kit.MDB_FILE, p, kit.MDB_FROM, name) diff --git a/base/ssh/scripts.go b/base/ssh/scripts.go index 7e56e5f9..aae30a62 100644 --- a/base/ssh/scripts.go +++ b/base/ssh/scripts.go @@ -95,8 +95,8 @@ func (f *Frame) change(m *ice.Message, ls []string) []string { if target == "~" { target = "" } - m.Spawn(f.target).Search(target+".", func(p *ice.Context, s *ice.Context, key string) { - m.Info("choice: %s", s.Name) + m.Spawn(f.target).Search(target+ice.PT, func(p *ice.Context, s *ice.Context, key string) { + m.Log_SELECT(ctx.CONTEXT, s.Name) f.target = s }) } @@ -113,20 +113,14 @@ func (f *Frame) alias(m *ice.Message, ls []string) []string { return ls } func (f *Frame) parse(m *ice.Message, line string) string { - if strings.HasPrefix(line, "<") { - fmt.Fprintf(m.O, line) - return "" - } - for _, one := range kit.Split(line, ";", ";", ";") { - one = strings.TrimSpace(one) msg := m.Spawn(f.target) - msg.Option("_cmd", one) - ls := f.change(msg, f.alias(msg, kit.Split(one))) + ls := f.change(msg, f.alias(msg, kit.Split(strings.TrimSpace(one)))) if len(ls) == 0 { continue } + msg.Cmdy(ls[0], ls[1:]) _args, _ := msg.Optionv(ice.MSG_ARGS).([]interface{}) @@ -176,12 +170,6 @@ func (f *Frame) scan(m *ice.Message, h, line string) *Frame { } return f } -func (f *Frame) close() { - if stdin, ok := f.stdin.(io.Closer); ok { - stdin.Close() - } - f.stdin = nil -} func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server { return f @@ -211,7 +199,7 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool { f.scan(m, STDIO, "") default: // 脚本文件 - if strings.Contains(m.Option(ice.MSG_SCRIPT), "/") { + if strings.Contains(m.Option(ice.MSG_SCRIPT), ice.PS) { f.source = path.Join(path.Dir(m.Option(ice.MSG_SCRIPT)), f.source) } m.Option(ice.MSG_SCRIPT, f.source) @@ -232,6 +220,10 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool { return true } func (f *Frame) Close(m *ice.Message, arg ...string) bool { + if stdin, ok := f.stdin.(io.Closer); ok { + stdin.Close() + } + f.stdin = nil return true } @@ -259,11 +251,6 @@ func init() { PS2, []interface{}{kit.MDB_COUNT, " ", TARGET, "> "}, )}, }, Commands: map[string]*ice.Command{ - ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - if f, ok := m.Target().Server().(*Frame); ok { - f.close() - } - }}, SOURCE: {Name: "source file", Help: "脚本解析", Action: ice.MergeAction(map[string]*ice.Action{ mdb.REPEAT: {Name: "repeat", Help: "执行", Hand: func(m *ice.Message, arg ...string) { m.Cmdy(SCREEN, m.Option(kit.MDB_TEXT)) @@ -300,7 +287,7 @@ func init() { }}, RETURN: {Name: "return", Help: "结束脚本", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { f := m.Optionv(FRAME).(*Frame) - f.close() + f.Close(m, arg...) }}, }}) } diff --git a/base/tcp/host.go b/base/tcp/host.go index 5d562c04..f076dc4a 100644 --- a/base/tcp/host.go +++ b/base/tcp/host.go @@ -21,7 +21,7 @@ func _host_list(m *ice.Message, name string) { if ips, e := v.Addrs(); m.Assert(e) { for _, x := range ips { - ip := strings.Split(x.String(), "/") + ip := strings.Split(x.String(), ice.PS) if strings.Contains(ip[0], ":") || len(ip) == 0 { continue } diff --git a/base/web/cache.go b/base/web/cache.go index 212c01ee..b974a09f 100644 --- a/base/web/cache.go +++ b/base/web/cache.go @@ -145,10 +145,10 @@ func init() { }, Commands: map[string]*ice.Command{ "/cache/": {Name: "/cache/", Help: "缓存池", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Richs(CACHE, nil, arg[0], func(key string, value map[string]interface{}) { - if kit.Format(value[kit.MDB_FILE]) != "" { - m.RenderDownload(value[kit.MDB_FILE]) - } else { + if kit.Format(value[kit.MDB_FILE]) == "" { m.RenderResult(value[kit.MDB_TEXT]) + } else { + m.RenderDownload(value[kit.MDB_FILE]) } }) }}, diff --git a/base/web/route.go b/base/web/route.go index bf3109cc..e5270dd5 100644 --- a/base/web/route.go +++ b/base/web/route.go @@ -122,7 +122,8 @@ func init() { } } else if len(arg) > 2 { // 加载插件 - m.ShowPlugin(arg[0], arg[1], arg[2], ctx.ACTION, ctx.COMMAND) + m.Cmdy(SPACE, arg[0], ctx.CONTEXT, arg[1], ctx.COMMAND, arg[2]) + m.ProcessField(ctx.ACTION, ctx.COMMAND) } else if len(arg) > 1 { // 命令列表 m.Cmd(SPACE, arg[0], ctx.CONTEXT, arg[1], ctx.COMMAND).Table(func(index int, value map[string]string, head []string) { diff --git a/base/web/serve.go b/base/web/serve.go index 5234d145..e446c0f1 100644 --- a/base/web/serve.go +++ b/base/web/serve.go @@ -99,7 +99,7 @@ func _serve_params(msg *ice.Message, path string) { } func _serve_handle(key string, cmd *ice.Command, msg *ice.Message, w http.ResponseWriter, r *http.Request) { // 环境变量 - msg.Option(mdb.CACHE_LIMIT, "10") + msg.Option(ice.CACHE_LIMIT, "10") msg.Option(ice.MSG_OUTPUT, "") msg.Option(ice.MSG_SESSID, "") for _, v := range r.Cookies() { @@ -173,7 +173,7 @@ func _serve_handle(key string, cmd *ice.Command, msg *ice.Message, w http.Respon if cmds, ok := _serve_login(msg, kit.Simple(msg.Optionv(ice.MSG_CMDS)), w, r); ok { msg.Option(ice.MSG_OPTS, msg.Optionv(ice.MSG_OPTION)) msg.Target().Cmd(msg, key, cmds...) - msg.Cost(kit.Format("%s %v %v", r.URL.Path, cmds, msg.Format(ice.MSG_APPEND))) + msg.Cost(kit.Format("%s %v %v", r.URL.Path, cmds, msg.FormatSize())) } // 输出响应 @@ -255,7 +255,6 @@ func init() { )}, }, Commands: map[string]*ice.Command{ ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Done(true) m.Cmd(SERVE).Table(func(index int, value map[string]string, head []string) { m.Done(value[kit.MDB_STATUS] == tcp.START) }) diff --git a/base/web/space.go b/base/web/space.go index 8fa64634..f9cf4127 100644 --- a/base/web/space.go +++ b/base/web/space.go @@ -83,7 +83,7 @@ func _space_handle(m *ice.Message, safe bool, send map[string]*ice.Message, c *w socket, msg := c, m.Spawn(b) target := kit.Simple(msg.Optionv(ice.MSG_TARGET)) source := kit.Simple(msg.Optionv(ice.MSG_SOURCE), name) - msg.Log("recv", "%v->%v %s %v", source, target, msg.Detailv(), msg.Format(kit.MDB_META)) + msg.Log("recv", "%v->%v %s %v", source, target, msg.Detailv(), msg.FormatMeta()) if len(target) == 0 { // 本地执行 msg.Log_AUTH(aaa.USERROLE, msg.Option(ice.MSG_USERROLE), aaa.USERNAME, msg.Option(ice.MSG_USERNAME)) @@ -134,15 +134,15 @@ func _space_exec(msg *ice.Message, source, target []string, c *websocket.Conn, n msg.Set(ice.MSG_OPTS) _space_echo(msg, []string{}, kit.Revert(source)[1:], c, name) - msg.Cost(kit.Format("%v->%v %v %v", source, target, msg.Detailv(), msg.Format(ice.MSG_APPEND))) + msg.Cost(kit.Format("%v->%v %v %v", source, target, msg.Detailv(), msg.FormatSize())) } func _space_echo(msg *ice.Message, source, target []string, c *websocket.Conn, name string) { msg.Optionv(ice.MSG_SOURCE, source) msg.Optionv(ice.MSG_TARGET, target) - msg.Assert(c.WriteMessage(1, []byte(msg.Format(kit.MDB_META)))) + msg.Assert(c.WriteMessage(1, []byte(msg.FormatMeta()))) target = append([]string{name}, target...) - msg.Log("send", "%v->%v %v %v", source, target, msg.Detailv(), msg.Format(kit.MDB_META)) + msg.Log("send", "%v->%v %v %v", source, target, msg.Detailv(), msg.FormatMeta()) } func _space_send(m *ice.Message, space string, arg ...string) { if space == "" || space == MYSELF || space == ice.Info.NodeName { diff --git a/base/web/story.go b/base/web/story.go index 037940ef..0621cb3b 100644 --- a/base/web/story.go +++ b/base/web/story.go @@ -58,7 +58,7 @@ func _story_index(m *ice.Message, name string, withdata bool) { func _story_history(m *ice.Message, name string) { // 历史记录 list := m.Cmd(STORY, INDEX, name).Append(LIST) - for i := 0; i < kit.Int(kit.Select("30", m.Option(mdb.CACHE_LIMIT))) && list != ""; i++ { + for i := 0; i < kit.Int(kit.Select("30", m.Option(ice.CACHE_LIMIT))) && list != ""; i++ { m.Richs(STORY, nil, list, func(key string, value map[string]interface{}) { // 直连节点 m.Push(key, value, []string{kit.MDB_TIME, kit.MDB_KEY, kit.MDB_COUNT, SCENE, STORY}) diff --git a/base/web/web.go b/base/web/web.go index 7b50c559..e8a6ae48 100644 --- a/base/web/web.go +++ b/base/web/web.go @@ -88,6 +88,7 @@ func (web *Frame) Start(m *ice.Message, arg ...string) bool { return true } func (web *Frame) Close(m *ice.Message, arg ...string) bool { + m.Done(true) return true } diff --git a/base/yac/matrix.go b/base/yac/matrix.go index 7beabbbd..c6bf0d42 100644 --- a/base/yac/matrix.go +++ b/base/yac/matrix.go @@ -418,7 +418,7 @@ func init() { m.ProcessInner() }}, }, Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) { - m.Option(mdb.CACHE_LIMIT, -1) + m.Option(ice.CACHE_LIMIT, -1) if m.Action(mdb.CREATE); len(arg) == 0 { // 矩阵列表 m.Fields(len(arg), "time,name,npage,nhash") m.Cmdy(mdb.SELECT, m.Prefix(MATRIX), "", mdb.HASH) diff --git a/conf.go b/conf.go index dc9d3358..23958550 100644 --- a/conf.go +++ b/conf.go @@ -3,6 +3,7 @@ package ice const ( TB = "\t" SP = " " + PS = "/" PT = "." NL = "\n" OF = " of " @@ -106,11 +107,11 @@ const ( // DIR ETC_PATH = "etc/path" SRC_HELP = "src/help" - SRC_MAIN_SHY = "src/main.shy" SRC_MAIN_GO = "src/main.go" + SRC_MAIN_SHY = "src/main.shy" SRC_VERSION_GO = "src/version.go" SRC_BINPACK_GO = "src/binpack.go" - MAKEFILE = "makefile" + MAKEFILE = "Makefile" GO_MOD = "go.mod" GO_SUM = "go.sum" ) @@ -125,7 +126,6 @@ const ( // MSG MSG_SOURCE = "_source" MSG_TARGET = "_target" MSG_HANDLE = "_handle" - MSG_RENDER = "_render" MSG_OUTPUT = "_output" MSG_ARGS = "_args" @@ -149,9 +149,9 @@ const ( // MSG MSG_USERPOD = "user.pod" MSG_USERADDR = "user.addr" MSG_USERDATA = "user.data" - MSG_USERNICK = "user.nick" - MSG_USERNAME = "user.name" MSG_USERROLE = "user.role" + MSG_USERNAME = "user.name" + MSG_USERNICK = "user.nick" MSG_USERZONE = "user.zone" MSG_LANGUAGE = "user.lang" @@ -159,13 +159,16 @@ const ( // MSG MSG_TOPIC = "sess.topic" MSG_RIVER = "sess.river" MSG_STORM = "sess.storm" - MSG_LOCAL = "sess.local" MSG_TOAST = "sess.toast" + MSG_LOCAL = "sess.local" CACHE_LIMIT = "cache.limit" CACHE_BEGIN = "cache.begin" CACHE_COUNT = "cache.count" CACHE_OFFEND = "cache.offend" + CACHE_FILTER = "cache.filter" + CACHE_VALUE = "cache.value" + CACHE_FIELD = "cache.field" ) const ( // RENDER RENDER_RAW = "_raw" @@ -198,12 +201,13 @@ const ( // PROCESS FIELD_PREFIX = "_prefix" ) const ( // Err - ErrWarn = "warn: " - ErrExists = "exists: " - ErrExpire = "expire: " - ErrNotLogin = "not login: " - ErrNotFound = "not found: " - ErrNotRight = "not right: " + ErrWarn = "warn: " + ErrExists = "exists: " + ErrExpire = "expire: " + ErrNotLogin = "not login: " + ErrNotFound = "not found: " + ErrNotRight = "not right: " + ErrNotImplement = "not implement: " ) const ( // LOG // 通用 diff --git a/core/code/csdn.go b/core/code/csdn.go deleted file mode 100644 index c6c30741..00000000 --- a/core/code/csdn.go +++ /dev/null @@ -1,35 +0,0 @@ -package code - -import ( - ice "shylinux.com/x/icebergs" - "shylinux.com/x/icebergs/base/mdb" - kit "shylinux.com/x/toolkits" -) - -func _csdn_show(m *ice.Message, name, text string, arg ...string) { -} -func _csdn_search(m *ice.Message, kind, name, text string) { - if kit.Contains(kind, kit.MDB_FOREACH) || kit.Contains(kind, CSDN) { - m.PushSearchWeb(CSDN, name) - } -} - -const CSDN = "csdn" - -func init() { - Index.Merge(&ice.Context{ - Configs: map[string]*ice.Config{ - CSDN: {Name: "csdn", Help: "博客", Value: kit.Data(kit.MDB_SHORT, kit.MDB_TEXT)}, - }, Commands: map[string]*ice.Command{ - CSDN: {Name: "csdn [name] word", Help: "博客", Action: map[string]*ice.Action{ - mdb.CREATE: {Name: "create type name text", Help: "创建", Hand: func(m *ice.Message, arg ...string) { - m.Cmd(mdb.INSERT, m.Prefix(CSDN), "", mdb.HASH, arg) - }}, - mdb.SEARCH: {Name: "search type name text", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { - _csdn_search(m, arg[0], arg[1], arg[2]) - }}, - }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - _csdn_show(m, arg[0], kit.Select(arg[0], arg[1]), arg[2:]...) - }}, - }}) -} diff --git a/core/code/github.go b/core/code/github.go deleted file mode 100644 index 5ebd5dc2..00000000 --- a/core/code/github.go +++ /dev/null @@ -1,36 +0,0 @@ -package code - -import ( - ice "shylinux.com/x/icebergs" - "shylinux.com/x/icebergs/base/mdb" - kit "shylinux.com/x/toolkits" -) - -func _github_show(m *ice.Message, name, text string, arg ...string) { -} -func _github_search(m *ice.Message, kind, name, text string) { - if kit.Contains(kind, kit.MDB_FOREACH) || kit.Contains(kind, GITHUB) { - m.PushSearchWeb(GITHUB, name) - } -} - -const GITHUB = "github" - -func init() { - Index.Merge(&ice.Context{ - Configs: map[string]*ice.Config{ - GITHUB: {Name: "github", Help: "仓库", Value: kit.Data(kit.MDB_SHORT, kit.MDB_TEXT)}, - }, - Commands: map[string]*ice.Command{ - GITHUB: {Name: "github [name] word", Help: "百度", Action: map[string]*ice.Action{ - mdb.CREATE: {Name: "create type name text", Help: "创建", Hand: func(m *ice.Message, arg ...string) { - m.Cmd(mdb.INSERT, m.Prefix(GITHUB), "", mdb.HASH, arg) - }}, - mdb.SEARCH: {Name: "search type name text", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { - _github_search(m, arg[0], arg[1], arg[2]) - }}, - }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - _github_show(m, arg[0], kit.Select(arg[0], arg[1]), arg[2:]...) - }}, - }}) -} diff --git a/core/team/plan.go b/core/team/plan.go index 803c23b8..851726cd 100644 --- a/core/team/plan.go +++ b/core/team/plan.go @@ -10,7 +10,7 @@ import ( ) func _plan_list(m *ice.Message, begin_time, end_time time.Time) *ice.Message { - m.Option(mdb.CACHE_LIMIT, "100") + m.Option(ice.CACHE_LIMIT, "100") m.Fields(0, "begin_time,close_time,zone,id,level,status,score,type,name,text,extra") m.Option(kit.Keycb(mdb.SELECT), func(key string, fields []string, value, val map[string]interface{}) { begin, _ := time.ParseInLocation(ice.MOD_TIME, kit.Format(value[BEGIN_TIME]), time.Local) diff --git a/core/wiki/field.go b/core/wiki/field.go index 9df004b0..3a457c88 100644 --- a/core/wiki/field.go +++ b/core/wiki/field.go @@ -7,6 +7,27 @@ import ( kit "shylinux.com/x/toolkits" ) +func Parse(m *ice.Message, meta string, key string, arg ...string) *ice.Message { + list := []string{} + for _, line := range kit.Split(strings.Join(arg, ice.SP), ice.NL) { + ls := kit.Split(line) + for i := 0; i < len(ls); i++ { + if strings.HasPrefix(ls[i], "#") { + ls = ls[:i] + break + } + } + list = append(list, ls...) + } + + switch data := kit.Parse(nil, "", list...); meta { + case ice.MSG_OPTION: + m.Option(key, data) + case ice.MSG_APPEND: + m.Append(key, data) + } + return m +} func _field_show(m *ice.Message, name, text string, arg ...string) { // 命令参数 meta, cmds := kit.Dict(), kit.Split(text) @@ -32,7 +53,7 @@ func _field_show(m *ice.Message, name, text string, arg ...string) { m.Option(arg[i], kit.Split(strings.TrimSuffix(strings.TrimPrefix(arg[i+1], "["), "]"))) kit.Value(meta, arg[i], m.Optionv(arg[i])) } else { - m.Parse(ice.MSG_OPTION, arg[i], arg[i+1]) + Parse(m, ice.MSG_OPTION, arg[i], arg[i+1]) kit.Value(meta, arg[i], m.Optionv(arg[i])) } diff --git a/core/wiki/word.go b/core/wiki/word.go index 2ab5b5a3..a67c4de5 100644 --- a/core/wiki/word.go +++ b/core/wiki/word.go @@ -37,7 +37,6 @@ func _word_show(m *ice.Message, name string, arg ...string) { m.Option(ice.MSG_ALIAS, m.Confv(WORD, kit.Keym(kit.MDB_ALIAS))) m.Option(nfs.DIR_ROOT, _wiki_path(m, WORD)) - m.Option(ice.MSG_RENDER, ice.RENDER_RAW) m.Cmdy(ssh.SOURCE, name) } diff --git a/data.go b/data.go index a35c325e..c75bcc7e 100644 --- a/data.go +++ b/data.go @@ -24,7 +24,7 @@ func (m *Message) Config(key string, arg ...interface{}) string { return m.Conf(m.PrefixKey(), kit.Keym(key)) } func (m *Message) ConfigSimple(key string) []string { - return []string{key, m.Conf(m.PrefixKey(), kit.Keym(key))} + return []string{key, m.Config(key)} } func (m *Message) Save(arg ...string) *Message { if len(arg) == 0 { @@ -36,7 +36,7 @@ func (m *Message) Save(arg ...string) *Message { for _, k := range arg { list = append(list, m.Prefix(k)) } - m.Cmd("ctx.config", SAVE, m.Prefix("json"), list) + m.Cmd("config", SAVE, m.Prefix("json"), list) return m } func (m *Message) Load(arg ...string) *Message { @@ -44,7 +44,7 @@ func (m *Message) Load(arg ...string) *Message { for _, k := range arg { list = append(list, m.Prefix(k)) } - m.Cmd("ctx.config", LOAD, m.Prefix("json"), list) + m.Cmd("config", LOAD, m.Prefix("json"), list) return m } @@ -57,7 +57,6 @@ func (m *Message) Richs(prefix string, chain interface{}, raw interface{}, cb in switch cb := cb.(type) { case func(*sync.Mutex, string, map[string]interface{}): mu := &sync.Mutex{} - wg := &sync.WaitGroup{} defer wg.Wait() diff --git a/exec.go b/exec.go index e3097766..1c82d81f 100644 --- a/exec.go +++ b/exec.go @@ -82,8 +82,8 @@ func (m *Message) Call(sync bool, cb func(*Message) *Message) *Message { p := kit.Select("10s", m.Option(kit.MDB_TIMEOUT)) t := time.AfterFunc(kit.Duration(p), func() { m.Warn(true, "%s timeout %v", p, m.Detailv()) - wait <- false m.Back(nil) + wait <- false }) m.cb = func(sub *Message) *Message { @@ -109,8 +109,8 @@ func (m *Message) Back(res *Message) *Message { } return m } -func (m *Message) Go(cb interface{}, args ...interface{}) *Message { - task.Put(kit.FileLine(3, 3), func(task *task.Task) error { +func (m *Message) Go(cb interface{}) *Message { + task.Put(kit.FileLine(cb, 3), func(task *task.Task) error { m.TryCatch(m, true, func(m *Message) { switch cb := cb.(type) { case func(*Message): @@ -128,28 +128,27 @@ func (m *Message) Watch(key string, arg ...string) *Message { if len(arg) == 0 { arg = append(arg, m.Prefix(AUTO)) } - m.Cmd("gdb.event", "action", "listen", "event", key, CMD, strings.Join(arg, SP)) + m.Cmd("event", "action", "listen", "event", key, CMD, strings.Join(arg, SP)) return m } func (m *Message) Event(key string, arg ...string) *Message { - m.Cmd("gdb.event", "action", "action", "event", key, arg) + m.Cmd("event", "action", "action", "event", key, arg) return m } func (m *Message) Right(arg ...interface{}) bool { - return m.Option(MSG_USERROLE) == "root" || !m.Warn(m.Cmdx("aaa.role", "right", + return m.Option(MSG_USERROLE) == "root" || !m.Warn(m.Cmdx("role", "right", m.Option(MSG_USERROLE), strings.ReplaceAll(kit.Keys(arg...), "/", PT)) != OK, - ErrNotRight, m.Option(MSG_USERROLE), " of ", strings.Join(kit.Simple(arg), PT), " at ", kit.FileLine(2, 3)) + ErrNotRight, m.Option(MSG_USERROLE), OF, strings.Join(kit.Simple(arg), PT), " at ", kit.FileLine(2, 3)) } func (m *Message) Space(arg interface{}) []string { - if arg == nil || arg == "" || kit.Format(arg) == m.Conf("cli.runtime", "node.name") { + if arg == nil || arg == "" || kit.Format(arg) == m.Conf("runtime", "node.name") { return nil } - return []string{"web.space", kit.Format(arg)} + return []string{"space", kit.Format(arg)} } func (m *Message) PodCmd(arg ...interface{}) bool { if pod := m.Option(POD); pod != "" { - m.Option(POD, "") - if m.Option(MSG_UPLOAD) != "" { + if m.Option(POD, ""); m.Option(MSG_UPLOAD) != "" { msg := m.Cmd("cache", "upload") m.Option(MSG_UPLOAD, msg.Append(kit.MDB_HASH), msg.Append(kit.MDB_NAME), msg.Append(kit.MDB_SIZE)) } diff --git a/init.go b/init.go index 02e7e832..fb332968 100644 --- a/init.go +++ b/init.go @@ -10,16 +10,14 @@ import ( log "shylinux.com/x/toolkits/logs" ) -type Frame struct { - wait chan int -} +type Frame struct{} func (f *Frame) Spawn(m *Message, c *Context, arg ...string) Server { return &Frame{} } func (f *Frame) Begin(m *Message, arg ...string) Server { m.Log(LOG_BEGIN, ICE) - defer m.Cost("begin ice") + defer m.Cost(LOG_BEGIN, ICE) list := map[*Context]*Message{m.target: m} m.Travel(func(p *Context, s *Context) { @@ -33,24 +31,25 @@ func (f *Frame) Begin(m *Message, arg ...string) Server { } func (f *Frame) Start(m *Message, arg ...string) bool { m.Log(LOG_START, ICE) - defer m.Cost("start ice") + defer m.Cost(LOG_START, ICE) m.Cap(CTX_STATUS, CTX_START) - m.Cap(CTX_STREAM, strings.Split(m.Time(), " ")[1]) + m.Cap(CTX_STREAM, strings.Split(m.Time(), SP)[1]) m.Cmdy(INIT, arg) + m.target.root.wg = &sync.WaitGroup{} - for _, k := range kit.Split(kit.Select("ssh,gdb,log")) { + for _, k := range kit.Split("log,gdb,ssh") { m.Start(k) } + defer m.TryCatch(m, true, func(msg *Message) { m.target.root.wg.Wait() }) + m.Cmdy(arg) return true } func (f *Frame) Close(m *Message, arg ...string) bool { - m.TryCatch(m, true, func(m *Message) { m.target.wg.Wait() }) - m.Log(LOG_CLOSE, ICE) - defer m.Cost("close ice") + defer m.Cost(LOG_CLOSE, ICE) list := map[*Context]*Message{m.target: m} m.Travel(func(p *Context, s *Context) { @@ -63,7 +62,7 @@ func (f *Frame) Close(m *Message, arg ...string) bool { } var Index = &Context{Name: "ice", Help: "冰山模块", Caches: map[string]*Cache{ - CTX_FOLLOW: {Value: ICE}, CTX_STREAM: {Value: SHY}, CTX_STATUS: {Value: CTX_BEGIN}, + CTX_FOLLOW: {Value: ICE}, CTX_STATUS: {Value: CTX_BEGIN}, CTX_STREAM: {Value: SHY}, }, Configs: map[string]*Config{ HELP: {Value: kit.Data("index", Info.Help)}, }, Commands: map[string]*Command{ @@ -77,14 +76,15 @@ var Index = &Context{Name: "ice", Help: "冰山模块", Caches: map[string]*Cach }}, INIT: {Name: "init", Help: "启动", Hand: func(m *Message, c *Context, cmd string, arg ...string) { m.root.Cmd(CTX_INIT) - m.Cmd("ssh.source", ETC_INIT_SHY) + m.Cmd("source", ETC_INIT_SHY) }}, HELP: {Name: "help", Help: "帮助", Hand: func(m *Message, c *Context, cmd string, arg ...string) { m.Echo(m.Config("index")) }}, EXIT: {Name: "exit", Help: "结束", Hand: func(m *Message, c *Context, cmd string, arg ...string) { + defer c.server.(*Frame).Close(m.root.Spawn(), arg...) m.root.Option(EXIT, kit.Select("0", arg, 0)) - m.Cmd("ssh.source", ETC_EXIT_SHY) + m.Cmd("source", ETC_EXIT_SHY) m.root.Cmd(CTX_EXIT) }}, CTX_EXIT: {Hand: func(m *Message, c *Context, cmd string, arg ...string) { @@ -96,7 +96,6 @@ var Index = &Context{Name: "ice", Help: "冰山模块", Caches: map[string]*Cach }) } }) - c.server.(*Frame).wait <- kit.Int(m.root.Option(EXIT)) }}, }} var Pulse = &Message{ @@ -115,7 +114,7 @@ func Run(arg ...string) string { arg = append(arg, HELP) } - frame := &Frame{wait: make(chan int, 1)} + frame := &Frame{} Index.Merge(Index) Index.server = frame Index.root = Index @@ -124,18 +123,13 @@ func Run(arg ...string) string { switch kit.Select("", arg, 0) { case "space", "serve": if log.LogDisable = false; frame.Begin(Pulse.Spawn(), arg...).Start(Pulse, arg...) { - frame.Close(Pulse.Spawn(), arg...) + os.Exit(kit.Int(Pulse.Option(EXIT))) } - os.Exit(<-frame.wait) - default: if Pulse.Cmdy(arg); Pulse.Result() == "" { Pulse.Table() } - if strings.TrimSpace(Pulse.Result()) == "" { - Pulse.Set(MSG_RESULT).Cmdy("cli.system", arg) - } Pulse.Sleep("10ms") } diff --git a/logs.go b/logs.go index d7543d20..f0d55a47 100644 --- a/logs.go +++ b/logs.go @@ -15,7 +15,7 @@ func (m *Message) log(level string, str string, arg ...interface{}) *Message { return m // 禁用日志 } if str = strings.TrimSpace(kit.Format(str, arg...)); Info.Log != nil { - Info.Log(m, m.Format(kit.MDB_PREFIX), level, str) // 日志分流 + Info.Log(m, m.FormatPrefix(), level, str) // 日志分流 } // 日志颜色 @@ -41,7 +41,7 @@ func (m *Message) log(level string, str string, arg ...interface{}) *Message { // 长度截断 switch level { - case LOG_INFO, "send", "recv": + case LOG_INFO, LOG_SEND, LOG_RECV: if len(str) > 1024 { str = str[:1024] } @@ -61,7 +61,7 @@ func (m *Message) join(arg ...interface{}) string { list = append(list, kit.Format(arg[i])+":", kit.Format(arg[i+1])) } } - return strings.Join(list, " ") + return kit.Join(list, SP) } func (m *Message) Log(level string, str string, arg ...interface{}) *Message { @@ -72,7 +72,7 @@ func (m *Message) Info(str string, arg ...interface{}) *Message { } func (m *Message) Cost(arg ...interface{}) *Message { list := []string{m.FormatCost(), m.join(arg...)} - return m.log(LOG_COST, strings.Join(list, " ")) + return m.log(LOG_COST, kit.Join(list, SP)) } func (m *Message) Warn(err bool, arg ...interface{}) bool { if !err || len(m.meta[MSG_RESULT]) > 0 && m.meta[MSG_RESULT][0] == ErrWarn { @@ -85,7 +85,7 @@ func (m *Message) Warn(err bool, arg ...interface{}) bool { } func (m *Message) Error(err bool, str string, arg ...interface{}) bool { if err { - m.Echo("error: ").Echo(str, arg...) + m.Echo(ErrWarn).Echo(str, arg...) m.log(LOG_ERROR, m.FormatStack()) m.log(LOG_ERROR, str, arg...) m.log(LOG_ERROR, m.FormatChain()) @@ -131,8 +131,29 @@ func (m *Message) Log_IMPORT(arg ...interface{}) *Message { return m.log(LOG_IMPORT, m.join(arg...)) } +func (m *Message) FormatPrefix() string { + return kit.Format("%s %d %s->%s", m.Time(), m.code, m.source.Name, m.target.Name) +} +func (m *Message) FormatTime() string { + return m.Time() +} +func (m *Message) FormatShip() string { + return kit.Format("%s->%s", m.source.Name, m.target.Name) +} +func (m *Message) FormatCost() string { + return kit.FmtTime(kit.Int64(time.Since(m.time))) +} +func (m *Message) FormatSize() string { + if len(m.meta[MSG_APPEND]) == 0 { + return kit.Format("%dx%d %s", 0, 0, "[]") + } else { + return kit.Format("%dx%d %v", len(m.meta[m.meta[MSG_APPEND][0]]), len(m.meta[MSG_APPEND]), kit.Format(m.meta[MSG_APPEND])) + } +} +func (m *Message) FormatMeta() string { + return kit.Format(m.meta) +} func (m *Message) FormatStack() string { - // 调用栈 pc := make([]uintptr, 100) pc = pc[:runtime.Callers(5, pc)] frames := runtime.CallersFrames(pc) @@ -140,14 +161,14 @@ func (m *Message) FormatStack() string { meta := []string{} for { frame, more := frames.Next() - file := strings.Split(frame.File, "/") - name := strings.Split(frame.Function, "/") - meta = append(meta, kit.Format("\n%s:%d\t%s", file[len(file)-1], frame.Line, name[len(name)-1])) + file := kit.Split(frame.File, "/") + name := kit.Split(frame.Function, "/") + meta = append(meta, kit.Format("%s:%d\t%s", file[len(file)-1], frame.Line, name[len(name)-1])) if !more { break } } - return strings.Join(meta, "") + return kit.Join(meta, NL) } func (m *Message) FormatChain() string { ms := []*Message{} @@ -155,51 +176,40 @@ func (m *Message) FormatChain() string { ms = append(ms, msg) } - meta := append([]string{}, "\n\n") + meta := append([]string{}, NL) for i := len(ms) - 1; i >= 0; i-- { msg := ms[i] - meta = append(meta, kit.Format("%s ", msg.Format("prefix"))) if len(msg.meta[MSG_DETAIL]) > 0 { - meta = append(meta, kit.Format("%s:%d %v", MSG_DETAIL, len(msg.meta[MSG_DETAIL]), msg.meta[MSG_DETAIL])) + meta = append(meta, kit.Format("%s %s:%d %v", msg.FormatPrefix(), MSG_DETAIL, len(msg.meta[MSG_DETAIL]), msg.meta[MSG_DETAIL])) + } else { + meta = append(meta, kit.Format("%s ", msg.FormatPrefix())) } if len(msg.meta[MSG_OPTION]) > 0 { - meta = append(meta, kit.Format("%s:%d %v\n", MSG_OPTION, len(msg.meta[MSG_OPTION]), msg.meta[MSG_OPTION])) + meta = append(meta, kit.Format("%s:%d %v", MSG_OPTION, len(msg.meta[MSG_OPTION]), msg.meta[MSG_OPTION])) for _, k := range msg.meta[MSG_OPTION] { if v, ok := msg.meta[k]; ok { - meta = append(meta, kit.Format(" %s: %d %v\n", k, len(v), v)) + meta = append(meta, kit.Format("\t%s: %d %v", k, len(v), v)) } } } else { - meta = append(meta, "\n") + meta = append(meta, NL) } if len(msg.meta[MSG_APPEND]) > 0 { - meta = append(meta, kit.Format(" %s:%d %v\n", MSG_APPEND, len(msg.meta[MSG_APPEND]), msg.meta[MSG_APPEND])) + meta = append(meta, kit.Format("%s:%d %v", MSG_APPEND, len(msg.meta[MSG_APPEND]), msg.meta[MSG_APPEND])) for _, k := range msg.meta[MSG_APPEND] { if v, ok := msg.meta[k]; ok { - meta = append(meta, kit.Format(" %s: %d %v\n", k, len(v), v)) + meta = append(meta, kit.Format("\t%s: %d %v", k, len(v), v)) } } } if len(msg.meta[MSG_RESULT]) > 0 { - meta = append(meta, kit.Format(" %s:%d %v\n", MSG_RESULT, len(msg.meta[MSG_RESULT]), msg.meta[MSG_RESULT])) + meta = append(meta, kit.Format("%s:%d %v", MSG_RESULT, len(msg.meta[MSG_RESULT]), msg.meta[MSG_RESULT])) } } - return strings.Join(meta, "") -} -func (m *Message) FormatTime() string { - return m.Format(kit.MDB_TIME) -} -func (m *Message) FormatMeta() string { - return m.Format(kit.MDB_META) -} -func (m *Message) FormatSize() string { - return m.Format(kit.MDB_SIZE) -} -func (m *Message) FormatCost() string { - return m.Format(kit.MDB_COST) + return kit.Join(meta, NL) } func (m *Message) Format(key interface{}) string { switch key := key.(type) { @@ -207,35 +217,23 @@ func (m *Message) Format(key interface{}) string { json.Unmarshal(key, &m.meta) case string: switch key { - case kit.MDB_COST: - return kit.FmtTime(kit.Int64(time.Since(m.time))) - case kit.MDB_SIZE: - if len(m.meta[MSG_APPEND]) == 0 { - return kit.Format("%dx%d", 0, 0) - } else { - return kit.Format("%dx%d", len(m.meta[m.meta[MSG_APPEND][0]]), len(m.meta[MSG_APPEND])) - } - case kit.MDB_META: - return kit.Format(m.meta) - case kit.MDB_SHIP: - return kit.Format("%s->%s", m.source.Name, m.target.Name) case kit.MDB_PREFIX: - return kit.Format("%s %d %s->%s", m.Time(), m.code, m.source.Name, m.target.Name) - - case MSG_APPEND: - if len(m.meta[MSG_APPEND]) == 0 { - return kit.Format("%dx%d %s", 0, 0, "[]") - } else { - return kit.Format("%dx%d %s", len(m.meta[m.meta[MSG_APPEND][0]]), len(m.meta[MSG_APPEND]), kit.Format(m.meta[MSG_APPEND])) - } - + return m.FormatPrefix() + case kit.MDB_SHIP: + return m.FormatShip() + case kit.MDB_COST: + return m.FormatCost() + case kit.MDB_SIZE: + return m.FormatSize() + case kit.MDB_META: + return m.FormatMeta() case kit.MDB_CHAIN: return m.FormatChain() case kit.MDB_STACK: return m.FormatStack() } } - return m.time.Format(MOD_TIME) + return m.FormatTime() } func (m *Message) Formats(key string) string { switch key { diff --git a/meta.go b/meta.go index fe2571ca..f54f1558 100644 --- a/meta.go +++ b/meta.go @@ -1,7 +1,6 @@ package ice import ( - "fmt" "sort" "strconv" "strings" @@ -26,9 +25,13 @@ func (m *Message) Set(key string, arg ...string) *Message { return m } default: - if len(m.meta[MSG_APPEND]) == 2 && m.meta[MSG_APPEND][0] == kit.MDB_KEY && m.meta[MSG_APPEND][1] == kit.MDB_VALUE { + if m.FieldsIsDetail() { for i := 0; i < len(m.meta[kit.MDB_KEY]); i++ { if m.meta[kit.MDB_KEY][i] == key { + if len(arg) > 0 { + m.meta[kit.MDB_VALUE][i] = arg[0] + return m + } for ; i < len(m.meta[kit.MDB_KEY])-1; i++ { m.meta[kit.MDB_KEY][i] = m.meta[kit.MDB_KEY][i+1] m.meta[kit.MDB_VALUE][i] = m.meta[kit.MDB_VALUE][i+1] @@ -38,12 +41,9 @@ func (m *Message) Set(key string, arg ...string) *Message { break } } - break + return m } delete(m.meta, key) - for _, k := range arg { - delete(m.meta, k) - } } return m.Add(key, arg...) } @@ -68,26 +68,11 @@ func (m *Message) Cut(fields ...string) *Message { } func (m *Message) Push(key string, value interface{}, arg ...interface{}) *Message { switch value := value.(type) { - case map[string]string: - head := kit.Simple(arg) - if len(head) == 0 || (len(head) == 1 && head[0] == "detail") { - head = head[:0] - for k := range value { - head = append(head, k) - } - sort.Strings(head) - } - - for _, k := range head { - m.Push(k, value[k]) - } - case map[string]interface{}: - // 键值排序 head := kit.Simple() if len(arg) > 0 { head = kit.Simple(arg[0]) - } else { + } else { // 键值排序 for k := range kit.KeyValue(map[string]interface{}{}, "", value) { head = append(head, k) } @@ -104,7 +89,7 @@ func (m *Message) Push(key string, value interface{}, arg ...interface{}) *Messa var v interface{} switch k { case kit.MDB_KEY, kit.MDB_HASH: - if key != "" { + if key != "" && key != "detail" { v = key break } @@ -129,8 +114,23 @@ func (m *Message) Push(key string, value interface{}, arg ...interface{}) *Messa } } + case map[string]string: + head := kit.Simple() + if len(arg) > 0 { + head = kit.Simple(arg[0]) + } else { // 键值排序 + for k := range value { + head = append(head, k) + } + sort.Strings(head) + } + + for _, k := range head { + m.Push(k, value[k]) + } + default: - if m.Option(MSG_FIELDS) == "detail" || (len(m.meta[MSG_APPEND]) == 2 && m.meta[MSG_APPEND][0] == kit.MDB_KEY && m.meta[MSG_APPEND][1] == kit.MDB_VALUE) { + if m.FieldsIsDetail() { if key != kit.MDB_KEY || key != kit.MDB_VALUE { m.Add(MSG_APPEND, kit.MDB_KEY, key) m.Add(MSG_APPEND, kit.MDB_VALUE, kit.Format(value)) @@ -145,59 +145,31 @@ func (m *Message) Push(key string, value interface{}, arg ...interface{}) *Messa return m } func (m *Message) Echo(str string, arg ...interface{}) *Message { - if len(arg) > 0 { - str = fmt.Sprintf(str, arg...) - } - m.meta[MSG_RESULT] = append(m.meta[MSG_RESULT], str) + m.meta[MSG_RESULT] = append(m.meta[MSG_RESULT], kit.Format(str, arg...)) return m } func (m *Message) Copy(msg *Message, arg ...string) *Message { - if m == msg { - return m - } - if m == nil { + if m == nil || m == msg { return m } if len(arg) > 0 { // 精确复制 for _, k := range arg[1:] { - if kit.IndexOf(m.meta[arg[0]], k) == -1 { - m.meta[arg[0]] = append(m.meta[arg[0]], k) - } - m.meta[k] = append(m.meta[k], msg.meta[k]...) + m.Add(arg[0], kit.Simple(k, msg.meta[k])...) } return m } - for _, k := range msg.meta[MSG_OPTION] { // 复制选项 - if kit.IndexOf(m.meta[MSG_OPTION], k) == -1 { - m.meta[MSG_OPTION] = append(m.meta[MSG_OPTION], k) - } - if _, ok := msg.meta[k]; ok { - m.meta[k] = msg.meta[k] - } else { - m.data[k] = msg.data[k] - } + for _, k := range msg.meta[MSG_OPTION] { + m.Add(MSG_OPTION, kit.Simple(k, msg.meta[k])...) } - - for _, k := range msg.meta[MSG_APPEND] { // 复制数据 - if i := kit.IndexOf(m.meta[MSG_OPTION], k); i > -1 && len(m.meta[k]) > 0 { - m.meta[k] = m.meta[k][:0] - } - if i := kit.IndexOf(m.meta[MSG_OPTS], k); i > -1 && len(m.meta[k]) > 0 { - m.meta[k] = m.meta[k][:0] - } - if kit.IndexOf(m.meta[MSG_APPEND], k) == -1 { - m.meta[MSG_APPEND] = append(m.meta[MSG_APPEND], k) - } - m.meta[k] = append(m.meta[k], msg.meta[k]...) + for _, k := range msg.meta[MSG_APPEND] { + m.Add(MSG_APPEND, kit.Simple(k, msg.meta[k])...) } - - // 复制文本 m.meta[MSG_RESULT] = append(m.meta[MSG_RESULT], msg.meta[MSG_RESULT]...) return m } func (m *Message) Sort(key string, arg ...string) *Message { - if m.Option(MSG_FIELDS) == "detail" { + if m.FieldsIsDetail() && key != kit.MDB_KEY { return m } // 排序方法 @@ -217,8 +189,7 @@ func (m *Message) Sort(key string, arg ...string) *Message { number := map[int]int64{} table := []map[string]string{} m.Table(func(index int, line map[string]string, head []string) { - table = append(table, line) - switch cmp { + switch table = append(table, line); cmp { case "int": number[index] = kit.Int64(line[key]) case "int_r": @@ -269,7 +240,7 @@ func (m *Message) Sort(key string, arg ...string) *Message { } func (m *Message) Table(cbs ...func(index int, value map[string]string, head []string)) *Message { if len(cbs) > 0 && cbs[0] != nil { - if len(m.meta[MSG_APPEND]) == 2 && m.meta[MSG_APPEND][0] == kit.MDB_KEY { + if m.FieldsIsDetail() { line := map[string]string{} for i, k := range m.meta[kit.MDB_KEY] { line[k] = kit.Select("", m.meta[kit.MDB_VALUE], i) @@ -290,7 +261,6 @@ func (m *Message) Table(cbs ...func(index int, value map[string]string, head []s for _, k := range m.meta[MSG_APPEND] { line[k] = kit.Select("", m.meta[k], i) } - // 依次回调 cbs[0](i, line, m.meta[MSG_APPEND]) } return m @@ -330,8 +300,7 @@ func (m *Message) Table(cbs ...func(index int, value map[string]string, head []s } // 输出表头 - row := map[string]string{} - wor := []string{} + row, wor := map[string]string{}, []string{} for _, k := range m.meta[MSG_APPEND] { row[k], wor = k, append(wor, k+strings.Repeat(space, width[k]-kit.Width(k, len(space)))) } @@ -341,8 +310,7 @@ func (m *Message) Table(cbs ...func(index int, value map[string]string, head []s // 输出数据 for i := 0; i < depth; i++ { - row := map[string]string{} - wor := []string{} + row, wor := map[string]string{}, []string{} for _, k := range m.meta[MSG_APPEND] { data := "" if i < len(m.meta[k]) { @@ -351,7 +319,6 @@ func (m *Message) Table(cbs ...func(index int, value map[string]string, head []s row[k], wor = data, append(wor, data+strings.Repeat(space, width[k]-kit.Width(data, len(space)))) } - // 依次回调 if !cb(row, wor, i) { break } @@ -375,18 +342,13 @@ func (m *Message) Optionv(key string, arg ...interface{}) interface{} { case nil: delete(m.meta, key) case string: - m.meta[key] = kit.Simple(arg) + m.meta[key] = kit.Simple(arg...) case []string: - m.meta[key] = str delete(m.data, key) + m.meta[key] = str default: m.data[key] = str } - if key == MSG_FIELDS { - for _, k := range kit.Split(kit.Join(m.meta[key])) { - delete(m.meta, k) - } - } } for msg := m; msg != nil; msg = msg.message { @@ -399,9 +361,6 @@ func (m *Message) Optionv(key string, arg ...interface{}) interface{} { } return nil } -func (m *Message) Options(key string, arg ...interface{}) bool { - return kit.Select("", kit.Simple(m.Optionv(key, arg...)), 0) != "" -} func (m *Message) Option(key string, arg ...interface{}) string { return kit.Select("", kit.Simple(m.Optionv(key, arg...)), 0) } @@ -416,21 +375,7 @@ func (m *Message) Appendv(key string, arg ...interface{}) []string { return m.meta[key] } - if key == "_index" { - max := 0 - for _, k := range m.meta[MSG_APPEND] { - if len(m.meta[k]) > max { - max = len(m.meta[k]) - } - } - index := []string{} - for i := 0; i < max; i++ { - index = append(index, kit.Format(i)) - } - return index - } - - if len(m.meta[MSG_APPEND]) == 2 && m.meta[MSG_APPEND][0] == kit.MDB_KEY { + if m.FieldsIsDetail() { for i, k := range m.meta[kit.MDB_KEY] { if k == key { return []string{kit.Select("", m.meta[kit.MDB_VALUE], i)} diff --git a/misc.go b/misc.go index 76ca4836..852c49b5 100644 --- a/misc.go +++ b/misc.go @@ -29,41 +29,17 @@ func (m *Message) CSV(text string, head ...string) *Message { } return m } -func (m *Message) Parse(meta string, key string, arg ...string) *Message { - list := []string{} - for _, line := range kit.Split(strings.Join(arg, SP), NL) { - ls := kit.Split(line) - for i := 0; i < len(ls); i++ { - if strings.HasPrefix(ls[i], "#") { - ls = ls[:i] - break - } - } - list = append(list, ls...) - } - - switch data := kit.Parse(nil, "", list...); meta { - case MSG_OPTION: - m.Option(key, data) - case MSG_APPEND: - m.Append(key, data) - } - return m -} -func (m *Message) Split(str string, field string, space string, enter string) *Message { - indexs := []int{} - fields := kit.Split(field, space, space, space) - for i, l := range kit.Split(str, enter, enter, enter) { +func (m *Message) Split(str string, field string, sp string, nl string) *Message { + fields, indexs := kit.Split(field, sp, sp, sp), []int{} + for i, l := range kit.Split(str, nl, nl, nl) { if strings.HasPrefix(l, "Binary") { continue } if strings.TrimSpace(l) == "" { continue } - if i == 0 && (field == "" || field == "index") { - // 表头行 - fields = kit.Split(l, space, space) - if field == "index" { + if i == 0 && (field == "" || field == "index") { // 表头行 + if fields = kit.Split(l, sp, sp); field == "index" { for _, v := range fields { indexs = append(indexs, strings.Index(l, v)) } @@ -71,41 +47,41 @@ func (m *Message) Split(str string, field string, space string, enter string) *M continue } - if len(indexs) > 0 { - // 数据行 + if len(indexs) > 0 { // 按位切分 for i, v := range indexs { if i == len(indexs)-1 { - m.Push(kit.Select("some", fields, i), l[v:]) + m.Push(kit.Select(SP, fields, i), l[v:]) } else { - m.Push(kit.Select("some", fields, i), l[v:indexs[i+1]]) + m.Push(kit.Select(SP, fields, i), l[v:indexs[i+1]]) } } continue } - ls := kit.Split(l, space, space) + ls := kit.Split(l, sp, sp) for i, v := range ls { if i == len(fields)-1 { - m.Push(kit.Select("some", fields, i), strings.Join(ls[i:], space)) + m.Push(kit.Select(SP, fields, i), strings.Join(ls[i:], sp)) break } - m.Push(kit.Select("some", fields, i), v) + m.Push(kit.Select(SP, fields, i), v) } } return m } -func (m *Message) ShowPlugin(pod, ctx, cmd string, arg ...string) { - m.Cmdy("web.space", pod, "context", ctx, "command", cmd) - m.Option(MSG_PROCESS, PROCESS_FIELD) - m.Option(FIELD_PREFIX, arg) +func (m *Message) FieldsIsDetail() bool { + if m.OptionFields() == "detail" { + return true + } + if len(m.meta[MSG_APPEND]) == 2 && m.meta[MSG_APPEND][0] == kit.MDB_KEY && m.meta[MSG_APPEND][1] == kit.MDB_VALUE { + return true + } + return false } func (m *Message) OptionUserWeb() *url.URL { return kit.ParseURL(m.Option(MSG_USERWEB)) } -func (m *Message) SetAppend(key ...string) { - m.Set(MSG_APPEND, key...) -} func (m *Message) RenameAppend(from, to string) { for i, v := range m.meta[MSG_APPEND] { if v == from { @@ -157,6 +133,8 @@ func (m *Message) cmd(arg ...interface{}) *Message { case Option: opts[val.Name] = val.Value + case string: + args = append(args, v) default: if reflect.Func == reflect.TypeOf(val).Kind() { cbs = val @@ -185,9 +163,7 @@ func (m *Message) cmd(arg ...interface{}) *Message { } // 执行命令 - m.TryCatch(msg, true, func(msg *Message) { - m = ctx.cmd(msg, cmd, key, arg...) - }) + m.TryCatch(msg, true, func(msg *Message) { m = ctx.cmd(msg, cmd, key, arg...) }) } // 查找命令 @@ -203,7 +179,7 @@ func (m *Message) cmd(arg ...interface{}) *Message { // 系统命令 if m.Warn(!ok, ErrNotFound, list) { - return m.Set(MSG_RESULT).Cmdy("cli.system", list) + return m.Set(MSG_RESULT).Cmdy("system", list) } return m } @@ -252,10 +228,12 @@ func (c *Context) _cmd(m *Message, cmd *Command, key string, k string, h *Action order = true } if order { - value = kit.Select(value, arg, i) + m.Option(name, kit.Select(value, arg, i)) + } else { + if m.Option(name) == "" { + m.Option(name, value) + } } - - m.Option(name, kit.Select(m.Option(name), value, !strings.HasPrefix(value, "@"))) } if !order { for i := 0; i < len(arg)-1; i += 2 { diff --git a/misc/input/input.go b/misc/input/input.go index 6ffdd110..0ceae52d 100644 --- a/misc/input/input.go +++ b/misc/input/input.go @@ -84,7 +84,7 @@ func _input_save(m *ice.Message, file string, lib ...string) { if f, p, e := kit.Create(file); m.Assert(e) { defer f.Close() n := 0 - m.Option(mdb.CACHE_LIMIT, -2) + m.Option(ice.CACHE_LIMIT, -2) for _, lib := range lib { m.Richs(INPUT, "", lib, func(key string, value map[string]interface{}) { m.Grows(INPUT, kit.Keys(kit.MDB_HASH, key), "", "", func(index int, value map[string]interface{}) { @@ -174,7 +174,7 @@ var Index = &ice.Context{Name: INPUT, Help: "输入法", _input_load(m, kit.Select("usr/wubi-dict/person", m.Option(FILE)), m.Option(ZONE)) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - _input_find(m, arg[0], arg[1], m.Option(mdb.CACHE_LIMIT)) + _input_find(m, arg[0], arg[1], m.Option(ice.CACHE_LIMIT)) }}, }, } diff --git a/misc/lark/event.go b/misc/lark/event.go index d548e95e..2fa06af5 100644 --- a/misc/lark/event.go +++ b/misc/lark/event.go @@ -18,7 +18,7 @@ func init() { Index.Merge(&ice.Context{Commands: map[string]*ice.Command{ EVENT: {Name: "event", Help: "事件", Action: map[string]*ice.Action{ P2P_CHAT_CREATE: {Name: "", Help: "", Hand: func(m *ice.Message, arg ...string) { - if m.Options(OPEN_CHAT_ID) { + if m.Option(OPEN_CHAT_ID) != "" { m.Cmdy(SEND, m.Option(APP_ID), m.Option(OPEN_CHAT_ID), m.Conf(APP, kit.Keym(kit.MDB_TEMPLATE, m.Option(kit.MDB_TYPE)))) } }}, @@ -27,7 +27,7 @@ func init() { CHAT_DISBAND: {Name: "", Help: "", Hand: func(m *ice.Message, arg ...string) { }}, ADD_BOT: {Name: "", Help: "", Hand: func(m *ice.Message, arg ...string) { - if m.Options(OPEN_CHAT_ID) { + if m.Option(OPEN_CHAT_ID) != "" { m.Cmdy(SEND, m.Option(APP_ID), m.Option(OPEN_CHAT_ID), m.Conf(APP, kit.Keym(kit.MDB_TEMPLATE, m.Option(kit.MDB_TYPE)))) } }}, diff --git a/misc/lark/msg.go b/misc/lark/msg.go index 2d6bdb81..77cfe16c 100644 --- a/misc/lark/msg.go +++ b/misc/lark/msg.go @@ -93,10 +93,10 @@ func init() { m.Option(wiki.TITLE)+" "+m.Option(ice.CMD), m.Result()) }}, }, Hand: func(m *ice.Message, c *ice.Context, key string, arg ...string) { - if m.Options(OPEN_CHAT_ID) { - m.Cmdy(TALK, strings.TrimSpace(m.Option("text_without_at_bot"))) - } else { + if m.Option(OPEN_CHAT_ID) == "" { m.Cmdy(DUTY, m.Option(kit.MDB_TYPE), kit.Formats(m.Optionv(ice.MSG_USERDATA))) + } else { + m.Cmdy(TALK, strings.TrimSpace(m.Option("text_without_at_bot"))) } }}, }}) diff --git a/option.go b/option.go index 394b0413..77cb5d10 100644 --- a/option.go +++ b/option.go @@ -22,11 +22,12 @@ func (m *Message) OptionFields(arg ...string) string { if len(arg) > 0 { m.Option(MSG_FIELDS, kit.Join(arg)) } - return m.Option(MSG_FIELDS) + return kit.Join(kit.Simple(m.Optionv(MSG_FIELDS))) } func (m *Message) OptionPage(arg ...string) { m.Option(CACHE_LIMIT, kit.Select("10", arg, 0)) m.Option(CACHE_OFFEND, kit.Select("0", arg, 1)) + m.Option(CACHE_FILTER, kit.Select("", arg, 2)) } func (m *Message) OptionLoad(file string) *Message { if f, e := os.Open(file); e == nil { @@ -80,12 +81,9 @@ func (m *Message) Fields(length int, fields ...string) string { func (m *Message) Upload(dir string) { up := kit.Simple(m.Optionv(MSG_UPLOAD)) if p := path.Join(dir, up[1]); m.Option(MSG_USERPOD) == "" { - // 本机文件 - m.Cmdy("web.cache", "watch", up[0], p) - } else { - // 下拉文件 - m.Cmdy("web.spide", DEV, SAVE, p, "GET", - kit.MergeURL2(m.Option(MSG_USERWEB), path.Join("/share/cache", up[0]))) + m.Cmdy("cache", "watch", up[0], p) // 本机文件 + } else { // 下发文件 + m.Cmdy("spide", DEV, SAVE, p, "GET", kit.MergeURL2(m.Option(MSG_USERWEB), path.Join("/share/cache", up[0]))) } } func (m *Message) Action(arg ...string) { @@ -109,7 +107,7 @@ func (m *Message) StatusTimeCountTotal(arg ...interface{}) { m.Status(kit.MDB_TIME, m.Time(), kit.MDB_COUNT, m.FormatSize(), kit.MDB_TOTAL, arg, kit.MDB_COST, m.FormatCost()) } -func (m *Message) Toast(text string, arg ...interface{}) { // [title [duration]] +func (m *Message) Toast(text string, arg ...interface{}) { // [title [duration [progress]]] if len(arg) > 1 { switch val := arg[1].(type) { case string: @@ -120,7 +118,7 @@ func (m *Message) Toast(text string, arg ...interface{}) { // [title [duration]] } if m.Option(MSG_USERPOD) == "" { - m.Cmd("web.space", m.Option(MSG_DAEMON), "toast", "", text, arg) + m.Cmd("space", m.Option(MSG_DAEMON), "toast", "", text, arg) } else { m.Option(MSG_TOAST, kit.Simple(text, arg)) } diff --git a/render.go b/render.go index dd621f79..7fe47a58 100644 --- a/render.go +++ b/render.go @@ -1,7 +1,6 @@ package ice import ( - "net/url" "path" "strings" @@ -44,6 +43,37 @@ func Render(m *Message, cmd string, args ...interface{}) string { return "" } +func (m *Message) Render(cmd string, args ...interface{}) *Message { + m.Optionv(MSG_OUTPUT, cmd) + m.Optionv(MSG_ARGS, args) + + switch cmd { + case RENDER_TEMPLATE: // text [data [type]] + if len(args) == 1 { + args = append(args, m) + } + if res, err := kit.Render(args[0].(string), args[1]); m.Assert(err) { + m.Echo(string(res)) + } + } + return m +} +func (m *Message) RenderResult(args ...interface{}) *Message { + return m.Render(RENDER_RESULT, args...) +} +func (m *Message) RenderTemplate(args ...interface{}) *Message { + return m.Render(RENDER_TEMPLATE, args...) +} +func (m *Message) RenderRedirect(args ...interface{}) *Message { + return m.Render(RENDER_REDIRECT, args...) +} +func (m *Message) RenderDownload(args ...interface{}) *Message { + return m.Render(RENDER_DOWNLOAD, args...) +} +func (m *Message) RenderIndex(serve, repos string, file ...string) *Message { + return m.RenderDownload(path.Join(m.Conf(serve, kit.Keym(repos, kit.MDB_PATH)), kit.Select(m.Conf(serve, kit.Keym(repos, kit.MDB_INDEX)), path.Join(file...)))) +} + func (m *Message) IsCliUA() bool { if m.Option(MSG_USERUA) == "" || !strings.HasPrefix(m.Option(MSG_USERUA), "Mozilla/5.0") { return true @@ -98,13 +128,13 @@ func (m *Message) PushPodCmd(cmd string, arg ...string) { }) } - m.Cmd("web.space").Table(func(index int, value map[string]string, head []string) { + m.Cmd("space").Table(func(index int, value map[string]string, head []string) { switch value[kit.MDB_TYPE] { - case "worker", "server": + case "server", "worker": if value[kit.MDB_NAME] == Info.HostName { break } - m.Cmd("web.space", value[kit.MDB_NAME], m.Prefix(cmd), arg).Table(func(index int, val map[string]string, head []string) { + m.Cmd("space", value[kit.MDB_NAME], m.Prefix(cmd), arg).Table(func(index int, val map[string]string, head []string) { val[POD] = kit.Keys(value[kit.MDB_NAME], val[POD]) m.Push("", val, head) }) @@ -117,11 +147,10 @@ func (m *Message) PushSearch(args ...interface{}) { switch k { case POD: m.Push(k, kit.Select("", data[k])) - // m.Push(k, kit.Select(m.Option(MSG_USERPOD), data[POD])) case CTX: - m.Push(k, kit.Select(m.Prefix(), data[CTX])) + m.Push(k, kit.Select(m.Prefix(), data[k])) case CMD: - m.Push(k, kit.Select(m.CommandKey(), data[CMD])) + m.Push(k, kit.Select(m.CommandKey(), data[k])) case kit.MDB_TIME: m.Push(k, kit.Select(m.Time(), data[k])) default: @@ -129,17 +158,6 @@ func (m *Message) PushSearch(args ...interface{}) { } } } -func (m *Message) PushSearchWeb(cmd string, name string) { - msg := m.Spawn() - msg.Option(MSG_FIELDS, "type,name,text") - msg.Cmd("mdb.select", m.Prefix(cmd), "", kit.MDB_HASH).Table(func(index int, value map[string]string, head []string) { - text := kit.MergeURL(value[kit.MDB_TEXT], value[kit.MDB_NAME], name) - if value[kit.MDB_NAME] == "" { - text = kit.MergeURL(value[kit.MDB_TEXT] + url.QueryEscape(name)) - } - m.PushSearch(CMD, cmd, kit.MDB_TYPE, kit.Select("", value[kit.MDB_TYPE]), kit.MDB_NAME, name, kit.MDB_TEXT, text) - }) -} func (m *Message) EchoDownload(arg ...interface{}) *Message { // [name] file return m.Echo(Render(m, RENDER_DOWNLOAD, arg...)) @@ -162,34 +180,3 @@ func (m *Message) EchoImages(src string, arg ...string) *Message { // src [size] func (m *Message) EchoVideos(src string, arg ...string) *Message { // src [size] return m.Echo(Render(m, RENDER_VIDEOS, src, arg)) } - -func (m *Message) Render(cmd string, args ...interface{}) *Message { - m.Optionv(MSG_OUTPUT, cmd) - m.Optionv(MSG_ARGS, args) - - switch cmd { - case RENDER_TEMPLATE: // text [data [type]] - if len(args) == 1 { - args = append(args, m) - } - if res, err := kit.Render(args[0].(string), args[1]); m.Assert(err) { - m.Echo(string(res)) - } - } - return m -} -func (m *Message) RenderResult(args ...interface{}) *Message { - return m.Render(RENDER_RESULT, args...) -} -func (m *Message) RenderTemplate(args ...interface{}) *Message { - return m.Render(RENDER_TEMPLATE, args...) -} -func (m *Message) RenderRedirect(args ...interface{}) *Message { - return m.Render(RENDER_REDIRECT, args...) -} -func (m *Message) RenderDownload(args ...interface{}) *Message { - return m.Render(RENDER_DOWNLOAD, args...) -} -func (m *Message) RenderIndex(serve, repos string, file ...string) *Message { - return m.RenderDownload(path.Join(m.Conf(serve, kit.Keym(repos, kit.MDB_PATH)), kit.Select(m.Conf(serve, kit.Keym(repos, kit.MDB_INDEX)), path.Join(file...)))) -} diff --git a/type.go b/type.go index 239e7fb8..bb5b6255 100644 --- a/type.go +++ b/type.go @@ -65,9 +65,6 @@ type Context struct { } func (c *Context) ID() int32 { - if c == nil { - return 1 - } return atomic.AddInt32(&c.id, 1) } func (c *Context) Cap(key string, arg ...interface{}) string { @@ -84,10 +81,6 @@ func (c *Context) Server() Server { } func (c *Context) Register(s *Context, x Server, name ...string) *Context { - if s.Merge(s); s.Name == "" { - s.Name = kit.Split(kit.Split(kit.FileLine(2, 3), ":")[0], "/")[1] - } - for _, n := range name { Name(n, s) } @@ -99,6 +92,7 @@ func (c *Context) Register(s *Context, x Server, name ...string) *Context { s.root = c.root s.context = c s.server = x + s.Merge(s) return s } func (c *Context) Merge(s *Context) *Context { @@ -112,29 +106,24 @@ func (c *Context) Merge(s *Context) *Context { c.Commands[CTX_EXIT] = &Command{Hand: func(m *Message, c *Context, cmd string, arg ...string) { m.Save() }} } for k, v := range s.Commands { - if o, ok := c.Commands[k]; ok && s != c { - func() { - switch last, next := o.Hand, v.Hand; k { - case CTX_INIT: - v.Hand = func(m *Message, c *Context, key string, arg ...string) { - last(m, c, key, arg...) - next(m, c, key, arg...) - } - case CTX_EXIT: - v.Hand = func(m *Message, c *Context, key string, arg ...string) { - next(m, c, key, arg...) - last(m, c, key, arg...) - } + if p, ok := c.Commands[k]; ok && s != c { + switch last, next := p.Hand, v.Hand; k { + case CTX_INIT: + v.Hand = func(m *Message, c *Context, key string, arg ...string) { + last(m, c, key, arg...) + next(m, c, key, arg...) } - }() + case CTX_EXIT: + v.Hand = func(m *Message, c *Context, key string, arg ...string) { + next(m, c, key, arg...) + last(m, c, key, arg...) + } + } } if v.Meta == nil { v.Meta = kit.Dict() } - if p := kit.Format(v.Meta[kit.MDB_STYLE]); p == "" { - v.Meta[kit.MDB_STYLE] = k - } if c.Commands[k] = v; v.List == nil { v.List = c.split(v.Name) } @@ -144,13 +133,9 @@ func (c *Context) Merge(s *Context) *Context { if len(help) == 1 || help[1] == "" { help = strings.SplitN(help[0], ":", 2) } - kit.Value(v.Meta, kit.Keys("_trans", k), help[0]) - if len(help) > 1 { + if kit.Value(v.Meta, kit.Keys("_trans", k), help[0]); len(help) > 1 { kit.Value(v.Meta, kit.Keys(kit.MDB_TITLE, k), help[1]) } - if a.Hand == nil { - continue - } if a.List == nil { a.List = c.split(a.Name) } @@ -198,28 +183,22 @@ func (c *Context) Begin(m *Message, arg ...string) *Context { return c } func (c *Context) Start(m *Message, arg ...string) bool { - wait := make(chan bool) - m.Hold(1) m.Go(func() { - m.Log(LOG_START, c.Cap(CTX_FOLLOW)) + defer m.Done(true) + c.Cap(CTX_STATUS, CTX_START) - wait <- true + m.Log(LOG_START, c.Cap(CTX_FOLLOW)) if c.start = m; c.server != nil { c.server.Start(m, arg...) } - if m.Done(true); m.wait != nil { - m.wait <- true - } }) - - <-wait return true } func (c *Context) Close(m *Message, arg ...string) bool { - m.Log(LOG_CLOSE, c.Cap(CTX_FOLLOW)) c.Cap(CTX_STATUS, CTX_CLOSE) + m.Log(LOG_CLOSE, c.Cap(CTX_FOLLOW)) if c.server != nil { return c.server.Close(m, arg...) @@ -231,7 +210,6 @@ type Message struct { time time.Time code int Hand bool - wait chan bool meta map[string][]string data map[string]interface{} @@ -281,11 +259,10 @@ func (m *Message) Source() *Context { func (m *Message) Spawn(arg ...interface{}) *Message { msg := &Message{ time: time.Now(), code: int(m.target.root.ID()), - meta: map[string][]string{}, - data: map[string]interface{}{}, + meta: map[string][]string{}, data: map[string]interface{}{}, message: m, root: m.root, - source: m.target, target: m.target, _key: m._key, + source: m.target, target: m.target, _cmd: m._cmd, _key: m._key, W: m.W, R: m.R, O: m.O, I: m.I, } @@ -300,24 +277,16 @@ func (m *Message) Spawn(arg ...interface{}) *Message { return msg } func (m *Message) Start(key string, arg ...string) *Message { - m.Search(key+PT, func(p *Context, s *Context) { - s.Start(m.Spawn(s), arg...) - }) - return m -} -func (m *Message) Starts(name string, help string, arg ...string) *Message { - m.wait = make(chan bool) - m.target.Spawn(m, name, help, arg...).Begin(m, arg...).Start(m, arg...) - <-m.wait + m.Search(key+PT, func(p *Context, s *Context) { s.Start(m.Spawn(s), arg...) }) return m } func (m *Message) Travel(cb interface{}) *Message { list := []*Context{m.root.target} for i := 0; i < len(list); i++ { switch cb := cb.(type) { - case func(*Context, *Context): - // 遍历模块 + case func(*Context, *Context): // 遍历模块 cb(list[i].context, list[i]) + case func(*Context, *Context, string, *Command): ls := []string{} for k := range list[i].Commands { @@ -331,6 +300,7 @@ func (m *Message) Travel(cb interface{}) *Message { cb(list[i].context, list[i], k, list[i].Commands[k]) } m.target = target + case func(*Context, *Context, string, *Config): ls := []string{} for k := range list[i].Configs { @@ -338,9 +308,12 @@ func (m *Message) Travel(cb interface{}) *Message { } sort.Strings(ls) + target := m.target for _, k := range ls { // 配置列表 + m.target = list[i] cb(list[i].context, list[i], k, list[i].Configs[k]) } + m.target = target } ls := []string{} @@ -369,9 +342,10 @@ func (m *Message) Search(key string, cb interface{}) *Message { } else if key == PT { p, key = m.target, "" } else if key == ".." { - if m.target.context != nil { - p, key = m.target.context, "" + if m.target.context == nil { + return m } + p, key = m.target.context, "" } else if strings.Contains(key, PT) { list := strings.Split(key, PT) for _, p = range []*Context{m.target.root, m.target, m.source} { @@ -390,6 +364,7 @@ func (m *Message) Search(key string, cb interface{}) *Message { break } } + if m.Warn(p == nil, ErrNotFound, key) { return m } @@ -398,9 +373,8 @@ func (m *Message) Search(key string, cb interface{}) *Message { p = m.target } - // 遍历命令 switch cb := cb.(type) { - case func(key string, cmd *Command): + case func(key string, cmd *Command): // 遍历命令 if key == "" { for k, v := range p.Commands { cb(k, v) @@ -417,21 +391,19 @@ func (m *Message) Search(key string, cb interface{}) *Message { break } - // 查找命令 for _, p := range []*Context{p, m.target, m.source} { for s := p; s != nil; s = s.context { if cmd, ok := s.Commands[key]; ok { - cb(s.context, s, key, cmd) + cb(s.context, s, key, cmd) // 查找命令 return m } } } case func(p *Context, s *Context, key string, conf *Config): - // 查找配置 - for _, p := range []*Context{m.target, p, m.source} { + for _, p := range []*Context{p, m.target, m.source} { for s := p; s != nil; s = s.context { if cmd, ok := s.Configs[key]; ok { - cb(s.context, s, key, cmd) + cb(s.context, s, key, cmd) // 查找配置 return m } } @@ -440,6 +412,8 @@ func (m *Message) Search(key string, cb interface{}) *Message { cb(p.context, p, key) case func(p *Context, s *Context): cb(p.context, p) + default: + m.Error(true, ErrNotImplement) } return m }