1
0
forked from x/icebergs

add inner.go

This commit is contained in:
shaoying 2020-05-28 23:00:15 +08:00
parent 7b71098b83
commit b2b608e5f7
7 changed files with 641 additions and 520 deletions

View File

@ -11,63 +11,93 @@ import (
"strings"
)
var Index = &ice.Context{Name: "ctx", Help: "配置模块",
Caches: map[string]*ice.Cache{},
Configs: map[string]*ice.Config{
ice.CTX_CONFIG: {Name: "config", Help: "配置", Value: kit.Data("path", "var/conf")},
"demo": {Name: "demo", Help: "配置", Value: kit.Data("path", "var/conf")},
},
Commands: map[string]*ice.Command{
ice.CTX_CONTEXT: {Name: "context [all]", Help: "模块", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
all := false
func _parse_arg_all(m *ice.Message, arg ...string) (bool, []string) {
if len(arg) > 0 && arg[0] == "all" {
all, arg = true, arg[1:]
return true, arg[1:]
}
return false, arg
}
func _parse_arg_chain(m *ice.Message, arg ...string) (string, []string) {
if len(arg) > 1 {
return kit.Keys(arg[0], arg[1]), arg[2:]
}
return arg[0], arg[1:]
}
if p := m.Spawn(m.Source()); len(arg) == 0 {
if all == true {
func _config_list(m *ice.Message, all bool) {
p := m.Spawn(m.Source())
if all {
p = ice.Pulse
}
// 模块列表
p.Travel(func(p *ice.Context, s *ice.Context) {
if p != nil {
m.Push("ups", kit.Select("shy", p.Cap(ice.CTX_FOLLOW)))
} else {
m.Push("ups", "shy")
}
m.Push("name", s.Name)
m.Push(ice.CTX_STATUS, s.Cap(ice.CTX_STATUS))
m.Push(ice.CTX_STREAM, s.Cap(ice.CTX_STREAM))
m.Push("help", s.Help)
p.Travel(func(p *ice.Context, s *ice.Context, key string, conf *ice.Config) {
m.Push("key", key)
m.Push("name", conf.Name)
m.Push("value", kit.Format(conf.Value))
})
}
func _config_save(m *ice.Message, name string, arg ...string) {
msg := m.Spawn(m.Source())
// 保存配置
if m.Cap(ice.CTX_STATUS) != ice.ICE_START {
return
} else if len(arg) == 1 {
m.Cmdy(ice.CTX_COMMAND, arg[0]+".")
} else {
m.Search(arg[0]+".", func(p *ice.Context, s *ice.Context, key string) {
msg := m.Spawn(s)
switch arg[1] {
case "command":
msg.Cmdy(ice.CTX_COMMAND, arg[0], arg[2:])
case "config":
msg.Cmdy(ice.CTX_CONFIG, arg[2:])
case "cache":
}
m.Copy(msg)
name = path.Join(msg.Conf(ice.CTX_CONFIG, "meta.path"), name)
if f, p, e := kit.Create(name); m.Assert(e) {
data := map[string]interface{}{}
for _, k := range arg {
data[k] = msg.Confv(k)
}
if s, e := json.MarshalIndent(data, "", " "); m.Assert(e) {
if n, e := f.Write(s); m.Assert(e) {
m.Log("info", "save %d %s", n, p)
}
}
m.Echo(p)
}
}
func _config_load(m *ice.Message, name string, arg ...string) {
msg := m.Spawn(m.Source())
// 加载配置
name = path.Join(msg.Conf(ice.CTX_CONFIG, "meta.path"), name)
if f, e := os.Open(name); e == nil {
data := map[string]interface{}{}
json.NewDecoder(f).Decode(&data)
for k, v := range data {
msg.Search(k, func(p *ice.Context, s *ice.Context, key string) {
m.Log("info", "load %s.%s %v", s.Name, key, kit.Format(v))
s.Configs[key].Value = v
})
}
}},
ice.CTX_COMMAND: {Name: "command [all] [context [command run arg...]]", Help: "命令", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
all := false
if len(arg) > 0 && arg[0] == "all" {
all, arg = true, arg[1:]
}
}
func _config_make(m *ice.Message, chain string, arg ...string) {
msg := m.Spawn(m.Source())
if len(arg) > 1 {
if strings.HasPrefix(arg[1], "@") {
msg.Conf(chain, arg[0], msg.Cmdx("nfs.cat", arg[1][1:]))
} else {
msg.Conf(chain, arg[0], kit.Parse(nil, "", arg[1:]...))
}
}
if p := m.Spawn(m.Source()); len(arg) == 0 {
if all == true {
if len(arg) > 0 {
// 读取配置
m.Echo(kit.Formats(msg.Confv(chain, arg[0])))
} else {
// 读取配置
m.Echo(kit.Formats(msg.Confv(chain)))
}
}
func _command_list(m *ice.Message, all bool) {
p := m.Spawn(m.Source())
if all {
p = ice.Pulse
}
// 命令列表
p.Travel(func(p *ice.Context, s *ice.Context) {
list := []string{}
@ -86,26 +116,11 @@ var Index = &ice.Context{Name: "ctx", Help: "配置模块",
m.Push("index", k)
m.Push("name", kit.Format(v.Name))
m.Push("help", kit.Simple(v.Help)[0])
m.Push("list", kit.Format(v.List))
// m.Push("list", kit.Format(v.List))
}
})
return
}
chain := arg[0]
if len(arg) > 1 {
chain = kit.Keys(arg[0], arg[1])
arg = arg[1:]
}
arg = arg[1:]
m.Search(chain, func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) {
if len(arg) == 0 {
// 命令列表
m.Push("key", key)
m.Push("name", cmd.Name)
m.Push("help", kit.Simple(cmd.Help)[0])
m.Push("meta", kit.Format(cmd.Meta))
if len(cmd.List) == 0 {
func _command_make(m *ice.Message, cmd *ice.Command) {
var list []string
switch name := cmd.Name.(type) {
case []string, []interface{}:
@ -149,11 +164,76 @@ var Index = &ice.Context{Name: "ctx", Help: "配置模块",
cmd.List = append(cmd.List, kit.List(kit.MDB_INPUT, "button", "name", "返回", "value", "Last")...)
}
}
func _context_list(m *ice.Message, all bool) {
p := m.Spawn(m.Source())
if all {
p = ice.Pulse
}
p.Travel(func(p *ice.Context, s *ice.Context) {
if p != nil {
m.Push("ups", kit.Select("shy", p.Cap(ice.CTX_FOLLOW)))
} else {
m.Push("ups", "shy")
}
m.Push("name", s.Name)
m.Push(ice.CTX_STATUS, s.Cap(ice.CTX_STATUS))
m.Push(ice.CTX_STREAM, s.Cap(ice.CTX_STREAM))
m.Push("help", s.Help)
})
}
var Index = &ice.Context{Name: "ctx", Help: "配置模块",
Caches: map[string]*ice.Cache{},
Configs: map[string]*ice.Config{
ice.CTX_CONFIG: {Name: "config", Help: "配置", Value: kit.Data("path", "var/conf")},
},
Commands: map[string]*ice.Command{
ice.CTX_CONTEXT: {Name: "context [all]", Help: "模块", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if all, arg := _parse_arg_all(m, arg...); len(arg) == 0 {
_context_list(m, all)
return
}
if len(arg) == 1 {
m.Cmdy(ice.CTX_COMMAND, arg[0]+".")
} else {
m.Search(arg[0]+".", func(p *ice.Context, s *ice.Context, key string) {
msg := m.Spawn(s)
switch arg[1] {
case "command":
msg.Cmdy(ice.CTX_COMMAND, arg[0], arg[2:])
case "config":
msg.Cmdy(ice.CTX_CONFIG, arg[2:])
}
m.Copy(msg)
})
}
}},
ice.CTX_COMMAND: {Name: "command [all] [context [command run arg...]]", Help: "命令", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if all, arg := _parse_arg_all(m, arg...); len(arg) == 0 {
_command_list(m, all)
return
}
chain, arg := _parse_arg_chain(m, arg...)
m.Search(chain, func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) {
if len(arg) == 0 {
// 命令列表
m.Push("key", key)
m.Push("name", cmd.Name)
m.Push("help", kit.Simple(cmd.Help)[0])
m.Push("meta", kit.Format(cmd.Meta))
if len(cmd.List) == 0 {
_command_make(m, cmd)
}
m.Push("list", kit.Format(cmd.List))
} else {
if you := m.Option(kit.Format(kit.Value(cmd.Meta, "remote"))); you != "" {
// 远程命令
m.Copy(m.Spawns(s).Cmd(ice.WEB_SPACE, you, "ctx.command", chain, "run", arg[1:]))
m.Copy(m.Spawns(s).Cmd(ice.WEB_SPACE, you, ice.CTX_COMMAND, chain, "run", arg[1:]))
} else {
// 本地命令
m.Copy(s.Run(m.Spawns(s), cmd, key, arg[1:]...))
@ -161,75 +241,19 @@ var Index = &ice.Context{Name: "ctx", Help: "配置模块",
}
})
}},
ice.CTX_CONFIG: {Name: "config [all] [save|load] chain key arg...", Help: "配置", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
all := false
if len(arg) > 0 && arg[0] == "all" {
all, arg = true, arg[1:]
}
msg := m.Spawn(m.Source())
if len(arg) == 0 {
if all == true {
msg = ice.Pulse
}
// 配置列表
msg.Travel(func(p *ice.Context, s *ice.Context, key string, conf *ice.Config) {
m.Push("key", key)
m.Push("name", conf.Name)
m.Push("value", kit.Format(conf.Value))
})
ice.CTX_CONFIG: {Name: "config [all] [chain [key [arg...]]]", Help: "配置", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if all, arg := _parse_arg_all(m, arg...); len(arg) == 0 {
_config_list(m, all)
return
}
switch arg[0] {
case "save":
// 保存配置
if m.Cap(ice.CTX_STATUS) != ice.ICE_START {
break
}
arg[1] = path.Join(msg.Conf(ice.CTX_CONFIG, "meta.path"), arg[1])
if f, p, e := kit.Create(arg[1]); m.Assert(e) {
data := map[string]interface{}{}
for _, k := range arg[2:] {
data[k] = msg.Confv(k)
}
if s, e := json.MarshalIndent(data, "", " "); m.Assert(e) {
if n, e := f.Write(s); m.Assert(e) {
m.Log("info", "save %d %s", n, p)
}
}
m.Echo(p)
}
_config_save(m, arg[1], arg[2:]...)
case "load":
// 加载配置
arg[1] = path.Join(msg.Conf(ice.CTX_CONFIG, "meta.path"), arg[1])
if f, e := os.Open(arg[1]); e == nil {
data := map[string]interface{}{}
json.NewDecoder(f).Decode(&data)
for k, v := range data {
msg.Search(k, func(p *ice.Context, s *ice.Context, key string) {
m.Log("info", "load %s.%s %v", s.Name, key, kit.Format(v))
s.Configs[key].Value = v
})
}
}
_config_load(m, arg[1], arg[2:]...)
default:
if len(arg) > 2 {
if strings.HasPrefix(arg[2], "@") {
msg.Conf(arg[0], arg[1], msg.Cmdx("nfs.cat", arg[2][1:]))
} else {
msg.Conf(arg[0], arg[1], kit.Parse(nil, "", arg[2:]...))
}
}
if len(arg) > 1 {
// 读取配置
m.Echo(kit.Formats(msg.Confv(arg[0], arg[1])))
} else {
// 读取配置
m.Echo(kit.Formats(msg.Confv(arg[0])))
}
_config_make(m, arg[0], arg[1:]...)
}
}},
},

View File

@ -6,33 +6,11 @@ import (
"os"
"path"
"strings"
"time"
)
func init() {
Index.Merge(&ice.Context{
Configs: map[string]*ice.Config{
ice.WEB_STORY: {Name: "story", Help: "故事会", Value: kit.Dict(
kit.MDB_META, kit.Dict(kit.MDB_SHORT, "data"),
"head", kit.Data(kit.MDB_SHORT, "story"),
"mime", kit.Dict("md", "txt"),
)},
},
Commands: map[string]*ice.Command{
ice.WEB_STORY: {Name: "story story=auto key=auto auto", Help: "故事会", Meta: kit.Dict(
"exports", []string{"top", "story"}, "detail", []string{"共享", "更新", "推送"},
), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) > 1 && arg[0] == "action" {
story, list := m.Option("story"), m.Option("list")
switch arg[2] {
case "story":
story = arg[3]
case "list":
list = arg[3]
}
switch arg[1] {
case "share", "共享":
func _story_share(m *ice.Message, story string, list string, arg ...string) {
if m.Echo("share: "); list == "" {
msg := m.Cmd(ice.WEB_STORY, ice.STORY_INDEX, story)
m.Cmdy(ice.WEB_SHARE, "add", "story", story, msg.Append("list"))
@ -41,20 +19,30 @@ func init() {
m.Cmdy(ice.WEB_SHARE, "add", msg.Append("scene"), msg.Append("story"), msg.Append("text"))
}
}
return
}
if len(arg) == 0 {
func _story_list(m *ice.Message, arg ...string) {
// 故事列表
m.Richs(ice.WEB_STORY, "head", "*", func(key string, value map[string]interface{}) {
m.Push(key, value, []string{"time", "story", "count"})
})
m.Sort("time", "time_r")
}
func _story_show(m *ice.Message, arg ...string) {
if len(arg) == 1 {
// 故事记录
m.Cmdy(ice.WEB_STORY, "history", arg)
return
}
// 故事详情
m.Cmd(ice.WEB_STORY, ice.STORY_INDEX, arg[1]).Table(func(index int, value map[string]string, head []string) {
for k, v := range value {
m.Push("key", k)
m.Push("value", v)
}
m.Sort("key")
})
}
switch arg[0] {
case ice.STORY_PULL: // story [spide [story]]
func _story_pull(m *ice.Message, arg ...string) {
// 起止节点
prev, begin, end := "", arg[3], ""
repos := kit.Keys("remote", arg[2], arg[3])
@ -125,7 +113,8 @@ func init() {
}
}
case ice.STORY_PUSH:
}
func _story_push(m *ice.Message, arg ...string) {
// 更新分支
m.Cmdx(ice.WEB_STORY, "pull", arg[1:])
@ -196,7 +185,8 @@ func init() {
// 更新分支
m.Cmd(ice.WEB_STORY, "pull", arg[1:])
case "commit":
}
func _story_commit(m *ice.Message, arg ...string) {
// 查询索引
head, prev, value, count := "", "", map[string]interface{}{}, 0
m.Richs(ice.WEB_STORY, "head", arg[1], func(key string, val map[string]interface{}) {
@ -231,12 +221,14 @@ func init() {
}
m.Echo(list)
case ice.STORY_TRASH:
}
func _story_trash(m *ice.Message, arg ...string) {
bak := kit.Select(kit.Keys(arg[1], "bak"), arg, 2)
os.Remove(bak)
os.Rename(arg[1], bak)
case ice.STORY_WATCH:
}
func _story_watch(m *ice.Message, arg ...string) {
// 备份文件
name := kit.Select(arg[1], arg, 2)
m.Cmd(ice.WEB_STORY, ice.STORY_TRASH, name)
@ -257,20 +249,21 @@ func init() {
}
}
m.Echo(name)
case ice.STORY_CATCH:
}
func _story_catch(m *ice.Message, arg ...string) {
if last := m.Richs(ice.WEB_STORY, "head", arg[2], nil); last != nil {
if t, e := time.ParseInLocation(ice.ICE_TIME, kit.Format(last["time"]), time.Local); e == nil {
// 文件对比
if s, e := os.Stat(arg[2]); e == nil && s.ModTime().Before(t) {
m.Info("%s last: %s", arg[2], kit.Format(t))
m.Echo("%s", last["list"])
break
return
}
}
}
fallthrough
case "add", ice.STORY_UPLOAD, ice.STORY_DOWNLOAD:
_story_add(m, arg...)
}
func _story_add(m *ice.Message, arg ...string) {
if m.Richs(ice.WEB_CACHE, nil, kit.Select("", arg, 3), func(key string, value map[string]interface{}) {
// 复用缓存
arg[3] = key
@ -317,7 +310,9 @@ func init() {
m.Cmd(ice.WEB_PROXY, p, ice.WEB_STORY, ice.STORY_PULL, arg[2], "dev", arg[2])
}
case ice.STORY_INDEX:
}
func _story_index(m *ice.Message, arg ...string) {
m.Richs(ice.WEB_STORY, "head", arg[1], func(key string, value map[string]interface{}) {
// 查询索引
arg[1] = kit.Format(value["list"])
@ -336,8 +331,8 @@ func init() {
m.Push(key, value, []string{"text", "time", "size", "type", "name", "file"})
m.Echo("%s", value["text"])
})
case ice.STORY_HISTORY:
}
func _story_history(m *ice.Message, arg ...string) {
// 历史记录
list := m.Cmd(ice.WEB_STORY, ice.STORY_INDEX, arg[1]).Append("list")
for i := 0; i < kit.Int(kit.Select("30", m.Option("cache.limit"))) && list != ""; i++ {
@ -365,29 +360,77 @@ func init() {
list = kit.Format(value["prev"])
})
}
}
func StoryWatch(m *ice.Message, file string, name string) { _story_watch(m, file, name) }
func StoryCatch(m *ice.Message, mime string, file string) {
_story_catch(m, "catch", kit.Select(mime, strings.TrimPrefix(path.Ext(file), ".")), file)
}
func init() {
Index.Merge(&ice.Context{
Configs: map[string]*ice.Config{
ice.WEB_STORY: {Name: "story", Help: "故事会", Value: kit.Dict(
kit.MDB_META, kit.Dict(kit.MDB_SHORT, "data"),
"head", kit.Data(kit.MDB_SHORT, "story"),
"mime", kit.Dict("md", "txt"),
)},
},
Commands: map[string]*ice.Command{
ice.WEB_STORY: {Name: "story story=auto key=auto auto", Help: "故事会", Meta: kit.Dict(
"exports", []string{"top", "story"}, "detail", []string{"共享", "更新", "推送"},
), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) > 1 && arg[0] == "action" {
story, list := m.Option("story"), m.Option("list")
switch arg[2] {
case "story":
story = arg[3]
case "list":
list = arg[3]
}
switch arg[1] {
case "share", "共享":
_story_share(m, story, list, arg...)
}
return
}
if len(arg) == 0 {
_story_list(m, arg...)
return
}
switch arg[0] {
case ice.STORY_PULL: // story [spide [story]]
_story_pull(m, arg...)
case ice.STORY_PUSH:
_story_push(m, arg...)
case "commit":
_story_commit(m, arg...)
case ice.STORY_TRASH:
_story_trash(m, arg...)
case ice.STORY_WATCH:
_story_watch(m, arg...)
case ice.STORY_CATCH:
_story_catch(m, arg...)
case "add", ice.STORY_UPLOAD, ice.STORY_DOWNLOAD:
_story_add(m, arg...)
case ice.STORY_INDEX:
_story_index(m, arg...)
case ice.STORY_HISTORY:
_story_history(m, arg...)
default:
if len(arg) == 1 {
// 故事记录
m.Cmdy(ice.WEB_STORY, "history", arg)
break
}
// 故事详情
m.Cmd(ice.WEB_STORY, ice.STORY_INDEX, arg[1]).Table(func(index int, value map[string]string, head []string) {
for k, v := range value {
m.Push("key", k)
m.Push("value", v)
}
m.Sort("key")
})
_story_show(m, arg...)
}
}},
"/story/": {Name: "/story/", Help: "故事会", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
case ice.STORY_PULL:
// 下载节点
list := m.Cmd(ice.WEB_STORY, ice.STORY_INDEX, m.Option("begin")).Append("list")
for i := 0; i < 10 && list != "" && list != m.Option("end"); i++ {
if m.Richs(ice.WEB_STORY, nil, list, func(key string, value map[string]interface{}) {
@ -404,8 +447,6 @@ func init() {
m.Log(ice.LOG_EXPORT, "%s %s", m.Option("begin"), m.Format("append"))
case ice.STORY_PUSH:
// 上传节点
if m.Richs(ice.WEB_CACHE, nil, m.Option("data"), nil) == nil {
// 导入缓存
m.Log(ice.LOG_IMPORT, "%v: %v", m.Option("data"), m.Option("save"))

View File

@ -46,7 +46,9 @@ func Format(key string, arg ...interface{}) string {
return ""
}
func Render(msg *ice.Message, cmd string, args ...interface{}) {
if cmd != "" {
msg.Log(ice.LOG_EXPORT, "%s: %v", cmd, args)
}
switch arg := kit.Simple(args...); cmd {
case ice.RENDER_OUTPUT:
case "redirect":

View File

@ -5,7 +5,7 @@ import (
"github.com/shylinux/toolkits"
)
func _action_share_create(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
func _action_share_create(m *ice.Message, arg ...string) {
if m.Option("index") != "" {
arg = append(arg, "tool.0.pod", m.Option("node"))
arg = append(arg, "tool.0.ctx", m.Option("group"))
@ -96,7 +96,7 @@ func init() {
"/action": {Name: "/action", Help: "工作台", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
case "share":
_action_share_create(m, c, cmd, arg...)
_action_share_create(m, arg...)
return
}
if len(arg) == 0 || arg[0] == "" {

44
core/wiki/inner.go Normal file
View File

@ -0,0 +1,44 @@
package wiki
import (
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/web"
kit "github.com/shylinux/toolkits"
"os"
)
func _inner_save(m *ice.Message, name, text string) {
if f, e := os.Create(name); m.Assert(e) {
defer f.Close()
if n, e := f.WriteString(text); m.Assert(e) {
m.Logs(ice.LOG_EXPORT, "file", name, "size", n)
}
}
}
func init() {
const (
INNER = "inner"
SAVE = "save"
COMMIT = "commit"
)
Index.Merge(&ice.Context{
Configs: map[string]*ice.Config{
INNER: {Name: "inner", Help: "编辑器", Value: kit.Data(kit.MDB_SHORT, INNER)},
},
Commands: map[string]*ice.Command{
INNER: {Name: "inner path=auto auto", Help: "编辑器", Action: map[string]*ice.Action{
SAVE: {Name: "save name content", Help: "保存", Hand: func(m *ice.Message, arg ...string) {
_inner_save(m, arg[0], kit.Select(m.Option("content"), arg, 1))
}},
COMMIT: {Name: "commit name", Help: "提交", Hand: func(m *ice.Message, arg ...string) {
web.StoryCatch(m, "", arg[0])
}},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Cmdy("nfs.dir", arg)
}},
},
}, nil)
}

10
info.go
View File

@ -14,7 +14,7 @@ func (m *Message) Logs(level string, arg ...interface{}) *Message {
for i := 0; i < len(arg)-1; i += 2 {
list = append(list, fmt.Sprintf("%v: %v", arg[i], arg[i+1]))
}
m.Log(level, strings.Join(list, " "))
m.log(level, strings.Join(list, " "))
return m
}
func (m *Message) Log(level string, str string, arg ...interface{}) *Message {
@ -43,18 +43,22 @@ func (m *Message) log(level string, str string, arg ...interface{}) *Message {
prefix, suffix = "\033[31m", "\033[0m"
}
switch level {
case LOG_CMDS, LOG_INFO, LOG_WARN, LOG_COST:
default:
_, file, line, _ := runtime.Caller(2)
ls := strings.Split(file, "/")
if len(ls) > 2 {
ls = ls[len(ls)-2:]
}
suffix += fmt.Sprintf(" %s:%d", strings.Join(ls, "/"), line)
}
if os.Getenv("ctx_mod") != "" && m != nil {
// 输出日志
fmt.Fprintf(os.Stderr, "%s %02d %9s %s%s %s%s %s\n",
fmt.Fprintf(os.Stderr, "%s %02d %9s %s%s %s%s\n",
m.time.Format(ICE_TIME), m.code, fmt.Sprintf("%4s->%-4s", m.source.Name, m.target.Name),
prefix, level, str, suffix,
fmt.Sprintf("%s:%d", strings.Join(ls, "/"), line),
)
}
return m

30
type.go
View File

@ -29,12 +29,18 @@ type Config struct {
Help string
Value interface{}
}
type Action struct {
Name string
Help string
Hand func(m *Message, arg ...string)
}
type Command struct {
Name interface{} // string []string
Help interface{} // string []string
List []interface{}
Meta map[string]interface{}
Hand func(m *Message, c *Context, key string, arg ...string)
Action map[string]*Action
}
type Context struct {
Name string
@ -73,8 +79,19 @@ func (c *Context) Cap(key string, arg ...interface{}) string {
return c.Caches[key].Value
}
func (c *Context) Run(m *Message, cmd *Command, key string, arg ...string) *Message {
m.Hand = true
m.Log(LOG_CMDS, "%s.%s %d %v", c.Name, key, len(arg), arg)
if m.Hand = true; len(arg) > 1 && arg[0] == "action" && cmd.Action != nil {
if h, ok := cmd.Action[arg[1]]; ok {
h.Hand(m, arg[2:]...)
return m
}
for _, h := range cmd.Action {
if h.Name == arg[1] || h.Help == arg[1] {
h.Hand(m, arg[2:]...)
return m
}
}
}
cmd.Hand(m, c, key, arg...)
return m
}
@ -654,17 +671,6 @@ func (m *Message) Cmd(arg ...interface{}) *Message {
m.Hand, msg.Hand = true, true
msg.meta[MSG_DETAIL] = list
// _key := kit.Format(kit.Value(cmd.Meta, "remote"))
// if you := m.Option(_key); you != "" {
// // 远程命令
// msg.Option(_key, "")
// msg.Option("_option", m.Optionv("option"))
// msg.Copy(msg.Spawns(c).Cmd(WEB_LABEL, you, list[0], list[1:]))
// } else {
// // 本地命令
// p.Run(msg, cmd, key, list[1:]...)
// }
p.Run(msg, cmd, key, list[1:]...)
m.Hand, msg.Hand, m = true, true, msg
})