diff --git a/src/contexts/cli/cli_darwin.go b/src/contexts/cli/cli_darwin.go deleted file mode 100644 index 58d55aea..00000000 --- a/src/contexts/cli/cli_darwin.go +++ /dev/null @@ -1,21 +0,0 @@ -package cli - -import ( - "contexts/ctx" - "fmt" - "runtime" - "syscall" - "toolkit" -) - -func sysinfo(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - m.Append("NumCPU", runtime.NumCPU()) - - fs := &syscall.Statfs_t{} - syscall.Statfs("./", fs) - m.Append("blocks", kit.FmtSize(fs.Blocks*uint64(fs.Bsize))) - m.Append("bavail", kit.FmtSize(fs.Bavail*uint64(fs.Bsize))) - m.Append("bper", fmt.Sprintf("%d%%", fs.Bavail*100/fs.Blocks)) - m.Table() - return -} diff --git a/src/contexts/cli/cli_linux.go b/src/contexts/cli/cli_linux.go deleted file mode 100644 index 824c1565..00000000 --- a/src/contexts/cli/cli_linux.go +++ /dev/null @@ -1,38 +0,0 @@ -package cli - -import ( - "contexts/ctx" - "fmt" - "runtime" - "syscall" - "time" - "toolkit" -) - -func sysinfo(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - sys := &syscall.Sysinfo_t{} - syscall.Sysinfo(sys) - - d, e := time.ParseDuration(fmt.Sprintf("%ds", sys.Uptime)) - m.Assert(e) - m.Append("NumCPU", runtime.NumCPU()) - m.Append("uptime", d) - m.Append("procs", sys.Procs) - - m.Append("total", kit.FmtSize(uint64(sys.Totalram))) - m.Append("free", kit.FmtSize(uint64(sys.Freeram))) - m.Append("mper", fmt.Sprintf("%d%%", sys.Freeram*100/sys.Totalram)) - - fs := &syscall.Statfs_t{} - syscall.Statfs("./", fs) - m.Append("blocks", kit.FmtSize(fs.Blocks*uint64(fs.Bsize))) - m.Append("bavail", kit.FmtSize(fs.Bavail*uint64(fs.Bsize))) - m.Append("bper", fmt.Sprintf("%d%%", fs.Bavail*100/fs.Blocks)) - - m.Append("files", fs.Files) - m.Append("ffree", fs.Ffree) - m.Append("fper", fmt.Sprintf("%d%%", fs.Ffree*100/fs.Files)) - - m.Table() - return -} diff --git a/src/contexts/cli/cli_windows.go b/src/contexts/cli/cli_windows.go deleted file mode 100644 index 5a9aff1f..00000000 --- a/src/contexts/cli/cli_windows.go +++ /dev/null @@ -1,9 +0,0 @@ -package cli - -import ( - "contexts/ctx" -) - -func sysinfo(m *ctx.Message, c *ctx.Context, key string, arg ...string) error{ - return nil -} diff --git a/src/contexts/cli/version.go b/src/contexts/cli/version.go index 30e126fd..748b13a8 100644 --- a/src/contexts/cli/version.go +++ b/src/contexts/cli/version.go @@ -4,5 +4,5 @@ var version = struct { host string self int }{ - "2019-07-27 15:21:07", "ZYB-20190522USI", 292, + "2019-07-27 20:28:57", "ZYB-20190522USI", 295, } diff --git a/src/contexts/ctx/core.go b/src/contexts/ctx/core.go index 795a9f19..3501a4e5 100644 --- a/src/contexts/ctx/core.go +++ b/src/contexts/ctx/core.go @@ -2,54 +2,12 @@ package ctx import ( "errors" - "fmt" "io" - "os" "strings" "time" "toolkit" ) -func (m *Message) Log(action string, str string, arg ...interface{}) *Message { - if action == "error" { - kit.Log("error", fmt.Sprintf("chain: %s", m.Format("chain"))) - kit.Log("error", fmt.Sprintf("%s %s %s", m.Format(), action, fmt.Sprintf(str, arg...))) - kit.Log("error", fmt.Sprintf("stack: %s", m.Format("stack"))) - } - - if m.Options("log.disable") { - return m - } - - if l := m.Sess("log", false); l != nil { - if log, ok := l.target.Server.(LOGGER); ok { - if action == "error" { - log.Log(m, "error", "chain: %s", m.Format("chain")) - } - if log.Log(m, action, str, arg...); action == "error" { - log.Log(m, "error", "stack: %s", m.Format("stack")) - } - return m - } - } else { - fmt.Fprintf(os.Stderr, str, arg...) - } - - return m -} -func (m *Message) Gdb(arg ...interface{}) interface{} { - // if !m.Options("log.enable") { - // return "" - // } - - if g := m.Sess("gdb", false); g != nil { - if gdb, ok := g.target.Server.(DEBUG); ok { - return gdb.Wait(m, arg...) - } - } - return "" -} - func (c *Context) Register(s *Context, x Server, args ...interface{}) *Context { name, force := s.Name, false if len(args) > 0 { @@ -137,15 +95,6 @@ func (c *Context) Begin(m *Message, arg ...string) *Context { m.source.sessions = append(m.source.sessions, m) c.exit = make(chan bool, 3) - /* - m.Log("begin", "%d context %v %v", m.Capi("ncontext", 1), m.Meta["detail"], m.Meta["option"]) - for k, x := range c.Configs { - if x.Hand != nil { - m.Log("begin", "%s config %v", k, m.Conf(k, x.Value)) - } - } - */ - if c.Server != nil { c.Server.Begin(m, m.Meta["detail"]...) } @@ -228,6 +177,29 @@ func (c *Context) Close(m *Message, arg ...string) bool { return true } +func (m *Message) TryCatch(msg *Message, safe bool, hand ...func(msg *Message)) *Message { + defer func() { + switch e := recover(); e { + case io.EOF: + case nil: + default: + m.Log("bench", "chain: %s", msg.Format("chain")) + m.Log("bench", "catch: %s", e) + m.Log("bench", "stack: %s", msg.Format("stack")) + + if m.Log("error", "catch: %s", e); len(hand) > 1 { + m.TryCatch(msg, safe, hand[1:]...) + } else if !safe { + m.Assert(e) + } + } + }() + + if len(hand) > 0 { + hand[0](msg) + } + return m +} func (m *Message) Assert(e interface{}, msg ...string) bool { switch v := e.(type) { case nil: @@ -252,27 +224,12 @@ func (m *Message) Assert(e interface{}, msg ...string) bool { kit.Log("error", "%v", e) panic(e) } -func (m *Message) TryCatch(msg *Message, safe bool, hand ...func(msg *Message)) *Message { - defer func() { - switch e := recover(); e { - case io.EOF: - case nil: - default: - m.Log("bench", "chain: %s", msg.Format("chain")) - m.Log("bench", "catch: %s", e) - m.Log("bench", "stack: %s", msg.Format("stack")) - - if m.Log("error", "catch: %s", e); len(hand) > 1 { - m.TryCatch(msg, safe, hand[1:]...) - } else if !safe { - m.Assert(e) - } +func (m *Message) GoLoop(msg *Message, hand ...func(msg *Message)) *Message { + m.Gos(msg, func(msg *Message) { + for { + hand[0](msg) } - }() - - if len(hand) > 0 { - hand[0](msg) - } + }) return m } func (m *Message) Gos(msg *Message, hand ...func(msg *Message)) *Message { @@ -371,6 +328,36 @@ func (m *Message) Sess(key string, arg ...interface{}) *Message { return nil } +func (m *Message) Form(x *Command, arg []string) []string { + for _, form := range []map[string]int{m.Optionv("ctx.form").(map[string]int), x.Form} { + + if args := []string{}; form != nil { + for i := 0; i < len(arg); i++ { + if n, ok := form[arg[i]]; ok { + if n < 0 { + n += len(arg) - i + } + for j := i + 1; j <= i+n && j < len(arg); j++ { + if _, ok := form[arg[j]]; ok { + n = j - i - 1 + } + } + if i+1+n > len(arg) { + m.Add("option", arg[i], arg[i+1:]) + } else { + m.Add("option", arg[i], arg[i+1:i+1+n]) + } + i += n + } else { + args = append(args, arg[i]) + } + } + arg = args + } + } + + return arg +} func (m *Message) Call(cb func(msg *Message) (sub *Message), arg ...interface{}) *Message { if m == nil { return m @@ -415,13 +402,11 @@ func (m *Message) CallBack(sync bool, cb func(msg *Message) (sub *Message), arg } wait := make(chan *Message, 10) - // m.Gos(m, func(m *Message) { m.Call(func(sub *Message) *Message { msg := cb(sub) wait <- m return msg }, arg...) - // }) select { case <-time.After(kit.Duration(m.Confx("call_timeout"))): @@ -447,6 +432,83 @@ func (m *Message) Free(cbs ...func(msg *Message) (done bool)) *Message { return m } +func (m *Message) Match(key string, spawn bool, hand func(m *Message, s *Context, c *Context, key string) bool) *Message { + if m == nil { + return m + } + + context := []*Context{m.target} + for _, v := range kit.Trans(m.Optionv("ctx.chain")) { + if msg := m.Sess(v, false); msg != nil && msg.target != nil { + context = append(context, msg.target) + } + } + context = append(context, m.source) + + for _, s := range context { + for c := s; c != nil; c = c.context { + if hand(m, s, c, key) { + return m + } + } + } + return m +} +func (m *Message) Magic(begin string, chain interface{}, args ...interface{}) interface{} { + auth := []string{"bench", "session", "user", "role", "componet", "command"} + key := []string{"bench", "sessid", "username", "role", "componet", "command"} + aaa := m.Sess("aaa", false) + for i, v := range auth { + if v == begin { + h := m.Option(key[i]) + if v == "user" { + h, _ = kit.Hash("username", m.Option("username")) + } + + data := aaa.Confv("auth", []string{h, "data"}) + + if kit.Format(chain) == "" { + return data + } + + if len(args) > 0 { + value := kit.Chain(data, chain, args[0]) + aaa.Conf("auth", []string{m.Option(key[i]), "data"}, value) + return value + } + + value := kit.Chain(data, chain) + if value != nil { + return value + } + + if i < len(auth)-1 { + begin = auth[i+1] + } + } + } + return nil +} +func (m *Message) Parse(arg interface{}) string { + switch str := arg.(type) { + case string: + if len(str) > 1 && str[0] == '$' { + return m.Cap(str[1:]) + } + if len(str) > 1 && str[0] == '@' { + if v := m.Option(str[1:]); v != "" { + return v + } + if v := kit.Format(m.Magic("bench", str[1:])); v != "" { + return v + } + v := m.Conf(str[1:]) + return v + } + return str + } + return "" +} func (m *Message) Goshy(input []string, index int, stack *kit.Stack, cb func(*Message)) bool { if stack == nil { stack = &kit.Stack{} diff --git a/src/contexts/ctx/ctx.go b/src/contexts/ctx/ctx.go index e5da66a3..ff65719a 100644 --- a/src/contexts/ctx/ctx.go +++ b/src/contexts/ctx/ctx.go @@ -731,8 +731,14 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{}, if len(arg) > 0 { if code, e := strconv.Atoi(arg[0]); e == nil { - if msg = m.root.Tree(code); msg != nil { - arg = arg[1:] + ms := []*Message{m} + for i := 0; i < len(ms); i++ { + if ms[i].Code() == code { + msg = ms[i] + arg = arg[1:] + break + } + ms = append(ms, ms[i].messages...) } } } @@ -849,20 +855,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{}, m.Sort("key", "str").Table() return }}, - "magic": &Command{Name: "magic", Help: "随机组员", Hand: func(m *Message, c *Context, key string, arg ...string) (e error) { - switch len(arg) { - case 0: - m.Optionv("magic", m.Magic("bench", "")) - case 1: - m.Optionv("magic", m.Magic(arg[0], "")) - case 2: - m.Optionv("magic", m.Magic(arg[0], arg[1])) - case 3: - m.Optionv("magic", m.Magic(arg[0], arg[1], arg[2])) - } - m.Cmdy("ctx.trans", "magic") - return - }}, "result": &Command{Name: "result [index] [value...]", Help: "查看或添加返回值", Hand: func(m *Message, c *Context, key string, arg ...string) (e error) { msg := m.message if len(arg) == 0 { diff --git a/src/contexts/ctx/misc.go b/src/contexts/ctx/misc.go index 5d054b71..daec24e8 100644 --- a/src/contexts/ctx/misc.go +++ b/src/contexts/ctx/misc.go @@ -2,6 +2,7 @@ package ctx import ( "fmt" + "os" "regexp" "runtime" "strings" @@ -10,72 +11,57 @@ import ( "toolkit" ) -func (c *Context) Has(key ...string) bool { - switch len(key) { - case 2: - if _, ok := c.Commands[key[0]]; ok && key[1] == "command" { - return true - } - if _, ok := c.Configs[key[0]]; ok && key[1] == "config" { - return true - } - if _, ok := c.Caches[key[0]]; ok && key[1] == "cache" { - return true - } - case 1: - if _, ok := c.Commands[key[0]]; ok { - return true - } - if _, ok := c.Configs[key[0]]; ok { - return true - } - if _, ok := c.Caches[key[0]]; ok { - return true - } - } - return false -} -func (c *Context) Sub(key string) *Context { - return c.contexts[key] -} -func (c *Context) Travel(m *Message, hand func(m *Message, n int) (stop bool)) *Context { - if c == nil { - return nil - } - target := m.target - - cs := []*Context{c} - for i := 0; i < len(cs); i++ { - if m.target = cs[i]; hand(m, i) { - return cs[i] - } - - keys := []string{} - for k, _ := range cs[i].contexts { - keys = append(keys, k) - } - sort.Strings(keys) - for _, k := range keys { - cs = append(cs, cs[i].contexts[k]) - } +func (m *Message) Log(action string, str string, arg ...interface{}) *Message { + if action == "error" { + kit.Log("error", fmt.Sprintf("chain: %s", m.Format("chain"))) + kit.Log("error", fmt.Sprintf("%s %s %s", m.Format(), action, fmt.Sprintf(str, arg...))) + kit.Log("error", fmt.Sprintf("stack: %s", m.Format("stack"))) } - m.target = target - return target -} -func (c *Context) BackTrace(m *Message, hand func(m *Message) (stop bool)) *Context { - target := m.target - - for s := m.target; s != nil; s = s.context { - if m.target = s; hand(m) { - return s - } + if m.Options("log.disable") { + return m } - m.target = target - return target -} + if l := m.Sess("log", false); l != nil { + if log, ok := l.target.Server.(LOGGER); ok { + if action == "error" { + log.Log(m, "error", "chain: %s", m.Format("chain")) + } + if log.Log(m, action, str, arg...); action == "error" { + log.Log(m, "error", "stack: %s", m.Format("stack")) + } + return m + } + } else { + fmt.Fprintf(os.Stderr, str, arg...) + } + return m +} +func (m *Message) Gdb(arg ...interface{}) interface{} { + // if !m.Options("log.enable") { + // return "" + // } + + if g := m.Sess("gdb", false); g != nil { + if gdb, ok := g.target.Server.(DEBUG); ok { + return gdb.Wait(m, arg...) + } + } + return "" +} +func (m *Message) Show(str string, args ...interface{}) *Message { + res := fmt.Sprintf(str, args...) + + if m.Option("bio.modal") == "action" { + fmt.Printf(res) + } else if kit.STDIO != nil { + kit.STDIO.Show(res) + } else { + m.Log("info", "show: %v", res) + } + return m +} func (m *Message) Format(arg ...interface{}) string { if len(arg) == 0 { arg = append(arg, "time", "ship") @@ -205,20 +191,115 @@ func (m *Message) Format(arg ...interface{}) string { } default: - meta = append(meta, kit.FileName(kit.Format(v), "time")) + meta = append(meta, kit.Format(v)) } } return strings.Join(meta, " ") } -func (m *Message) Tree(code int) *Message { - ms := []*Message{m} - for i := 0; i < len(ms); i++ { - if ms[i].Code() == code { - return ms[i] - } - ms = append(ms, ms[i].messages...) + +func (m *Message) Start(name string, help string, arg ...string) bool { + return m.Set("detail", arg).target.Spawn(m, name, help).Begin(m).Start(m) +} +func (m *Message) Wait() bool { + if m.target.exit != nil { + return <-m.target.exit } - return nil + return true +} +func (m *Message) Find(name string, root ...bool) *Message { + if name == "" { + return m.Spawn() + } + target := m.target.root + if len(root) > 0 && !root[0] { + target = m.target + } + + cs := target.contexts + for _, v := range strings.Split(name, ".") { + if x, ok := cs[v]; ok { + target, cs = x, x.contexts + } else if target.Name == v { + continue + } else { + m.Log("error", "context not find %s", name) + return nil + } + } + + if len(root) > 1 && root[1] { + m.target = target + return m + } + + return m.Spawn(target) +} +func (m *Message) Search(key string, root ...bool) []*Message { + reg, e := regexp.Compile(key) + m.Assert(e) + + target := m.target + if target == nil { + return []*Message{nil} + } + if len(root) > 0 && root[0] { + target = m.target.root + } + + cs := make([]*Context, 0, 3) + target.Travel(m, func(m *Message, i int) bool { + if reg.MatchString(m.target.Name) || reg.FindString(m.target.Help) != "" { + cs = append(cs, m.target) + } + return false + }) + + ms := make([]*Message, len(cs)) + for i := 0; i < len(cs); i++ { + ms[i] = m.Spawn(cs[i]) + } + if len(ms) == 0 { + ms = append(ms, nil) + } + + return ms +} +func (c *Context) Travel(m *Message, hand func(m *Message, n int) (stop bool)) *Context { + if c == nil { + return nil + } + target := m.target + + cs := []*Context{c} + for i := 0; i < len(cs); i++ { + if m.target = cs[i]; hand(m, i) { + return cs[i] + } + + keys := []string{} + for k, _ := range cs[i].contexts { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + cs = append(cs, cs[i].contexts[k]) + } + } + + m.target = target + return target +} +func (c *Context) BackTrace(m *Message, hand func(m *Message) (stop bool)) *Context { + target := m.target + + for s := m.target; s != nil; s = s.context { + if m.target = s; hand(m) { + return s + } + } + + m.target = target + return target } func (m *Message) Add(meta string, key string, value ...interface{}) *Message { @@ -374,96 +455,6 @@ func (m *Message) CopyFuck(msg *Message, arg ...string) *Message { return m } -func (m *Message) Auto(arg ...string) *Message { - for i := 0; i < len(arg); i += 3 { - m.Add("append", "value", arg[i]) - m.Add("append", "name", arg[i+1]) - m.Add("append", "help", arg[i+2]) - } - return m -} - -func (m *Message) Insert(meta string, index int, arg ...interface{}) string { - if m.Meta == nil { - m.Meta = make(map[string][]string) - } - m.Meta[meta] = kit.Array(m.Meta[meta], index, arg) - - if -1 < index && index < len(m.Meta[meta]) { - return m.Meta[meta][index] - } - return "" -} -func (m *Message) Magic(begin string, chain interface{}, args ...interface{}) interface{} { - auth := []string{"bench", "session", "user", "role", "componet", "command"} - key := []string{"bench", "sessid", "username", "role", "componet", "command"} - aaa := m.Sess("aaa", false) - for i, v := range auth { - if v == begin { - h := m.Option(key[i]) - if v == "user" { - h, _ = kit.Hash("username", m.Option("username")) - } - - data := aaa.Confv("auth", []string{h, "data"}) - - if kit.Format(chain) == "" { - return data - } - - if len(args) > 0 { - value := kit.Chain(data, chain, args[0]) - aaa.Conf("auth", []string{m.Option(key[i]), "data"}, value) - return value - } - - value := kit.Chain(data, chain) - if value != nil { - return value - } - - if i < len(auth)-1 { - begin = auth[i+1] - } - } - } - return nil -} -func (m *Message) Current(text string) string { - cs := []string{} - if pod := kit.Format(m.Magic("session", "current.pod")); pod != "" { - cs = append(cs, "context", "ssh", "remote", "'"+pod+"'") - } - if ctx := kit.Format(m.Magic("session", "current.ctx")); ctx != "" { - cs = append(cs, "context", ctx) - } - if cmd := kit.Format(m.Magic("session", "current.cmd")); cmd != "" { - cs = append(cs, cmd) - } - m.Log("info", "%s %s current %v", m.Option("username"), m.Option("sessid"), cs) - cs = append(cs, text) - return strings.Join(cs, " ") -} -func (m *Message) Parse(arg interface{}) string { - switch str := arg.(type) { - case string: - if len(str) > 1 && str[0] == '$' { - return m.Cap(str[1:]) - } - if len(str) > 1 && str[0] == '@' { - if v := m.Option(str[1:]); v != "" { - return v - } - if v := kit.Format(m.Magic("bench", str[1:])); v != "" { - return v - } - v := m.Conf(str[1:]) - return v - } - return str - } - return "" -} func (m *Message) ToHTML(style string) string { cmd := strings.Join(m.Meta["detail"], " ") result := []string{} @@ -494,134 +485,3 @@ func (m *Message) ToHTML(style string) string { } return strings.Join(result, "") } - -func (m *Message) Show(str string, args ...interface{}) *Message { - res := fmt.Sprintf(str, args...) - - if m.Option("bio.modal") == "action" { - fmt.Printf(res) - } else if kit.STDIO != nil { - kit.STDIO.Show(res) - } else { - m.Log("info", "show: %v", res) - } - return m -} -func (m *Message) GoLoop(msg *Message, hand ...func(msg *Message)) *Message { - m.Gos(msg, func(msg *Message) { - for { - hand[0](msg) - } - }) - return m -} - -func (m *Message) Start(name string, help string, arg ...string) bool { - return m.Set("detail", arg).target.Spawn(m, name, help).Begin(m).Start(m) -} -func (m *Message) Close(arg ...string) bool { - return m.Target().Close(m, arg...) -} -func (m *Message) Wait() bool { - if m.target.exit != nil { - return <-m.target.exit - } - return true -} - -func (m *Message) Find(name string, root ...bool) *Message { - if name == "" { - return m.Spawn() - } - target := m.target.root - if len(root) > 0 && !root[0] { - target = m.target - } - - cs := target.contexts - for _, v := range strings.Split(name, ".") { - if x, ok := cs[v]; ok { - target, cs = x, x.contexts - } else if target.Name == v { - continue - } else { - m.Log("error", "context not find %s", name) - return nil - } - } - - if len(root) > 1 && root[1] { - m.target = target - return m - } - - return m.Spawn(target) -} -func (m *Message) Search(key string, root ...bool) []*Message { - reg, e := regexp.Compile(key) - m.Assert(e) - - target := m.target - if target == nil { - return []*Message{nil} - } - if len(root) > 0 && root[0] { - target = m.target.root - } - - cs := make([]*Context, 0, 3) - target.Travel(m, func(m *Message, i int) bool { - if reg.MatchString(m.target.Name) || reg.FindString(m.target.Help) != "" { - m.Log("search", "%d %s match [%s]", len(cs), m.target.Name, key) - cs = append(cs, m.target) - } - return false - }) - - ms := make([]*Message, len(cs)) - for i := 0; i < len(cs); i++ { - ms[i] = m.Spawn(cs[i]) - } - if len(ms) == 0 { - ms = append(ms, nil) - } - - return ms -} -func (m *Message) Match(key string, spawn bool, hand func(m *Message, s *Context, c *Context, key string) bool) *Message { - if m == nil { - return m - } - - context := []*Context{m.target} - for _, v := range kit.Trans(m.Optionv("ctx.chain")) { - if msg := m.Sess(v, false); msg != nil && msg.target != nil { - context = append(context, msg.target) - } - } - // if m.target.root != nil && m.target.root.Configs != nil && m.target.root.Configs["search"] != nil && m.target.root.Configs["search"].Value != nil { - // target := m.target - // for _, v := range kit.Trans(kit.Chain(m.target.root.Configs["search"].Value, "context")) { - // if t := m.Find(v, true, true); t != nil { - // kit.Log("error", "%v", t) - // // // context = append(context, t.target) - // } - // } - // m.target = target - // } - - context = append(context, m.source) - - for _, s := range context { - for c := s; c != nil; c = c.context { - if hand(m, s, c, key) { - return m - } - } - } - return m -} -func (m *Message) Backs(msg *Message) *Message { - m.Back(msg) - return msg -} diff --git a/src/contexts/ctx/type.go b/src/contexts/ctx/type.go index 7ee91d6a..df5134d8 100644 --- a/src/contexts/ctx/type.go +++ b/src/contexts/ctx/type.go @@ -2,8 +2,6 @@ package ctx import ( "fmt" - "math/rand" - "sort" "strconv" "strings" "time" @@ -93,9 +91,7 @@ func (m *Message) Time(arg ...interface{}) string { t := m.time if len(arg) > 0 { if d, e := time.ParseDuration(arg[0].(string)); e == nil { - arg = arg[1:] - t = t.Add(d) - } else { + arg, t = arg[1:], t.Add(d) } } @@ -120,10 +116,18 @@ func (m *Message) Source() *Context { func (m *Message) Target() *Context { return m.target } -func (m *Message) Message() *Message { - return m.message -} +func (m *Message) Insert(meta string, index int, arg ...interface{}) string { + if m.Meta == nil { + m.Meta = make(map[string][]string) + } + m.Meta[meta] = kit.Array(m.Meta[meta], index, arg) + + if -1 < index && index < len(m.Meta[meta]) { + return m.Meta[meta][index] + } + return "" +} func (m *Message) Detail(arg ...interface{}) string { noset, index := true, 0 if len(arg) > 0 { @@ -138,12 +142,6 @@ func (m *Message) Detail(arg ...interface{}) string { return m.Insert("detail", index, arg...) } -func (m *Message) Detaili(arg ...interface{}) int { - return kit.Int(m.Detail(arg...)) -} -func (m *Message) Details(arg ...interface{}) bool { - return kit.Right(m.Detail(arg...)) -} func (m *Message) Option(key string, arg ...interface{}) string { if len(arg) > 0 { m.Insert(key, 0, arg...) @@ -175,10 +173,6 @@ func (m *Message) Optionv(key string, arg ...interface{}) interface{} { if len(arg) > 0 { switch arg[0].(type) { case nil: - // case []string: - // m.Option(key, v...) - // case string: - // m.Option(key, v) default: m.Put("option", key, arg[0]) } @@ -199,17 +193,6 @@ func (m *Message) Optionv(key string, arg ...interface{}) interface{} { } return nil } -func (m *Message) Optionx(key string, arg ...string) interface{} { - value := m.Conf(key) - if value == "" { - value = m.Option(key) - } - - if len(arg) > 0 { - value = fmt.Sprintf(arg[0], value) - } - return value -} func (m *Message) Append(key string, arg ...interface{}) string { if len(arg) > 0 { m.Insert(key, 0, arg...) @@ -239,28 +222,6 @@ func (m *Message) Appendi(key string, arg ...interface{}) int64 { func (m *Message) Appends(key string, arg ...interface{}) bool { return kit.Right(m.Append(key, arg...)) } -func (m *Message) Appendv(key string, arg ...interface{}) interface{} { - if len(arg) > 0 { - m.Put("append", key, arg[0]) - } - - ms := []*Message{m} - for i := 0; i < len(ms); i++ { - ms = append(ms, ms[i].messages...) - if !ms[i].Has(key) { - continue - } - for _, k := range ms[i].Meta["append"] { - if k == key { - if v, ok := ms[i].Data[key]; ok { - return v - } - return ms[i].Meta[key] - } - } - } - return nil -} func (m *Message) Result(arg ...interface{}) string { noset, index := true, 0 if len(arg) > 0 { @@ -275,43 +236,7 @@ func (m *Message) Result(arg ...interface{}) string { return m.Insert("result", index, arg...) } -func (m *Message) Resulti(arg ...interface{}) int { - return kit.Int(m.Result(arg...)) -} -func (m *Message) Results(arg ...interface{}) bool { - return kit.Right(m.Result(arg...)) -} -func (m *Message) Form(x *Command, arg []string) []string { - for _, form := range []map[string]int{m.Optionv("ctx.form").(map[string]int), x.Form} { - - if args := []string{}; form != nil { - for i := 0; i < len(arg); i++ { - if n, ok := form[arg[i]]; ok { - if n < 0 { - n += len(arg) - i - } - for j := i + 1; j <= i+n && j < len(arg); j++ { - if _, ok := form[arg[j]]; ok { - n = j - i - 1 - } - } - if i+1+n > len(arg) { - m.Add("option", arg[i], arg[i+1:]) - } else { - m.Add("option", arg[i], arg[i+1:i+1+n]) - } - i += n - } else { - args = append(args, arg[i]) - } - } - arg = args - } - } - - return arg -} func (m *Message) Push(str string, arg ...interface{}) *Message { return m.Add("append", str, arg...) } @@ -417,26 +342,21 @@ func (m *Message) Table(cbs ...interface{}) *Message { } // 回调函数 - var cb func(maps map[string]string, list []string, line int) (goon bool) - if len(cbs) > 0 { - cb = cbs[0].(func(maps map[string]string, list []string, line int) (goon bool)) - } else { - row := kit.Select(m.Conf("table", "row_sep"), m.Option("table.row_sep")) - col := kit.Select(m.Conf("table", "col_sep"), m.Option("table.col_sep")) - compact := kit.Right(kit.Select(m.Conf("table", "compact"), m.Option("table.compact"))) - cb = func(maps map[string]string, lists []string, line int) bool { - for i, v := range lists { - if k := m.Meta["append"][i]; compact { - v = maps[k] - } - - if m.Echo(v); i < len(lists)-1 { - m.Echo(col) - } + rows := kit.Select(m.Conf("table", "row_sep"), m.Option("table.row_sep")) + cols := kit.Select(m.Conf("table", "col_sep"), m.Option("table.col_sep")) + compact := kit.Right(kit.Select(m.Conf("table", "compact"), m.Option("table.compact"))) + cb := func(maps map[string]string, lists []string, line int) bool { + for i, v := range lists { + if k := m.Meta["append"][i]; compact { + v = maps[k] + } + + if m.Echo(v); i < len(lists)-1 { + m.Echo(cols) } - m.Echo(row) - return true } + m.Echo(rows) + return true } // 输出表头 @@ -468,12 +388,6 @@ func (m *Message) Table(cbs ...interface{}) *Message { return m } -func (m *Message) Echo(str string, arg ...interface{}) *Message { - if len(arg) > 0 { - return m.Add("result", fmt.Sprintf(str, arg...)) - } - return m.Add("result", str) -} func (m *Message) Copy(msg *Message, arg ...string) *Message { if msg == nil || m == msg { return m @@ -527,6 +441,12 @@ func (m *Message) Copy(msg *Message, arg ...string) *Message { return m } +func (m *Message) Echo(str string, arg ...interface{}) *Message { + if len(arg) > 0 { + return m.Add("result", fmt.Sprintf(str, arg...)) + } + return m.Add("result", str) +} func (m *Message) Cmdp(t time.Duration, head []string, prefix []string, suffix [][]string) *Message { if head != nil && len(head) > 0 { @@ -542,21 +462,6 @@ func (m *Message) Cmdp(t time.Duration, head []string, prefix []string, suffix [ m.Table() return m } -func (m *Message) Cmdm(args ...interface{}) *Message { - m.Log("info", "current: %v", m.Magic("session", "current")) - - arg := []string{} - if pod := kit.Format(m.Magic("session", "current.pod")); pod != "" { - arg = append(arg, "context", "ssh", "remote", pod) - } - if ctx := kit.Format(m.Magic("session", "current.ctx")); ctx != "" { - arg = append(arg, "context", ctx) - } - arg = append(arg, kit.Trans(args...)...) - - m.Spawn().Cmd(arg).CopyTo(m) - return m -} func (m *Message) Cmdy(args ...interface{}) *Message { m.Cmd(args...).CopyTo(m) return m @@ -569,7 +474,7 @@ func (m *Message) Cmdx(args ...interface{}) string { return msg.Result(0) } func (m *Message) Cmds(args ...interface{}) bool { - return m.Cmd(args...).Results(0) + return kit.Right(m.Cmdx(args...)) } func (m *Message) Cmd(args ...interface{}) *Message { if m == nil { @@ -604,27 +509,6 @@ func (m *Message) Cmd(args ...interface{}) *Message { msg.Log("cmd", "%s %s %v %v", c.Name, key, arg, msg.Meta["option"]) msg.Hand = true x.Hand(msg, c, key, msg.Form(x, arg)...) - // msg.Log("cmd", "%s %s %v %v", c.Name, key, len(msg.Meta["result"]), msg.Meta["append"]) - return - - target := msg.target - msg.target = s - - switch v := msg.Gdb("command", key, arg).(type) { - case string: - msg.Echo(v) - case nil: - if msg.Options("bio.cmd") { - if x.Auto != nil { - x.Auto(msg, c, key, arg...) - } - } else { - x.Hand(msg, c, key, arg...) - } - } - if msg.target == s { - msg.target = target - } }) } return msg.Hand @@ -638,6 +522,7 @@ func (m *Message) Cmd(args ...interface{}) *Message { func (m *Message) Confm(key string, args ...interface{}) map[string]interface{} { random := "" + var chain interface{} if len(args) > 0 { switch arg := args[0].(type) { @@ -661,93 +546,7 @@ func (m *Message) Confm(key string, args ...interface{}) map[string]interface{} } else { v = m.Confv(key, chain) } - - table, _ := v.([]interface{}) - value, _ := v.(map[string]interface{}) - if len(args) == 0 { - return value - } - - switch fun := args[0].(type) { - case func(int, string): - for i, v := range table { - fun(i, kit.Format(v)) - } - case func(int, string) bool: - for i, v := range table { - if fun(i, kit.Format(v)) { - break - } - } - case func(string, string): - for k, v := range value { - fun(k, kit.Format(v)) - } - case func(string, string) bool: - for k, v := range value { - if fun(k, kit.Format(v)) { - break - } - } - case func(map[string]interface{}): - if len(value) == 0 { - return nil - } - fun(value) - case func(string, map[string]interface{}): - switch random { - case "%": - n, i := rand.Intn(len(value)), 0 - for k, v := range value { - if val, ok := v.(map[string]interface{}); i == n && ok { - fun(k, val) - break - } - i++ - } - case "*": - fallthrough - default: - for k, v := range value { - if val, ok := v.(map[string]interface{}); ok { - fun(k, val) - } - } - } - case func(string, int, map[string]interface{}): - keys := make([]string, 0, len(value)) - for k, _ := range value { - keys = append(keys, k) - } - sort.Strings(keys) - - for _, k := range keys { - v := value[k] - if val, ok := v.([]interface{}); ok { - for i, v := range val { - if val, ok := v.(map[string]interface{}); ok { - fun(k, i, val) - } - } - } - } - - case func(string, map[string]interface{}) bool: - for k, v := range value { - if val, ok := v.(map[string]interface{}); ok { - if fun(k, val) { - break - } - } - } - case func(int, map[string]interface{}): - for i := m.Optioni("page.begin"); i < len(table); i++ { - if val, ok := table[i].(map[string]interface{}); ok { - fun(i, val) - } - } - } - return value + return kit.Map(v, random, args...) } func (m *Message) Confx(key string, args ...interface{}) string { value := kit.Select(m.Conf(key), m.Option(key)) @@ -783,12 +582,6 @@ func (m *Message) Confx(key string, args ...interface{}) string { return kit.Format(arg...) } -func (m *Message) Confs(key string, arg ...interface{}) bool { - return kit.Right(m.Confv(key, arg...)) -} -func (m *Message) Confi(key string, arg ...interface{}) int { - return kit.Int(m.Confv(key, arg...)) -} func (m *Message) Confv(key string, args ...interface{}) interface{} { if strings.Contains(key, ".") { target := m.target @@ -842,6 +635,12 @@ func (m *Message) Confv(key string, args ...interface{}) interface{} { return config.Value } +func (m *Message) Confs(key string, arg ...interface{}) bool { + return kit.Right(m.Confv(key, arg...)) +} +func (m *Message) Confi(key string, arg ...interface{}) int { + return kit.Int(m.Confv(key, arg...)) +} func (m *Message) Conf(key string, args ...interface{}) string { return kit.Format(m.Confv(key, args...)) } diff --git a/src/contexts/mdb/mdb.go b/src/contexts/mdb/mdb.go index 6e6913f7..643193e4 100644 --- a/src/contexts/mdb/mdb.go +++ b/src/contexts/mdb/mdb.go @@ -476,7 +476,7 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", } } - arg, h := kit.Slice(arg) + h, arg := arg[0], arg[1:] if h != "" { if temp := m.Confm("temp", h); temp != nil { m.Echo(h) diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index f4051810..763ac9fb 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -443,7 +443,8 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { }) } - m.Sess("tcp", false).Close() + msg = m.Sess("tcp", false) + msg.Target().Close(msg) return true } func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool { diff --git a/src/contexts/nfs/term.go b/src/contexts/nfs/term.go index 9b6ecad2..c0ba233a 100644 --- a/src/contexts/nfs/term.go +++ b/src/contexts/nfs/term.go @@ -272,7 +272,7 @@ func (nfs *NFS) Auto(what []rune, trigger string, index int) (change bool, frame case " ": switch m.Conf("term", "help_state") { case "context": - auto_target = auto_target.Sub(m.Option("auto_key")) + // auto_target = auto_target.Sub(m.Option("auto_key")) m.Optionv("bio.ctx", auto_target) trigger = ":" case "command": diff --git a/src/contexts/ssh/ssh.go b/src/contexts/ssh/ssh.go index 55a3659e..d27ceb45 100644 --- a/src/contexts/ssh/ssh.go +++ b/src/contexts/ssh/ssh.go @@ -927,19 +927,18 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", case "file": switch arg[1] { case "import": - if msg := m.Cmd("nfs.hash", arg[2]); msg.Results(0) { - h := msg.Result(0) - m.Conf("file", kit.Hashs(h, msg.Append("name")), map[string]interface{}{ - "create_time": m.Time(), - "create_user": m.Option("username"), - "name": msg.Append("name"), - "type": msg.Append("type"), - "size": msg.Append("size"), - "hash": h, - }) + msg := m.Cmd("nfs.hash", arg[2]) + h := msg.Result(0) + m.Conf("file", kit.Hashs(h, msg.Append("name")), map[string]interface{}{ + "create_time": m.Time(), + "create_user": m.Option("username"), + "name": msg.Append("name"), + "type": msg.Append("type"), + "size": msg.Append("size"), + "hash": h, + }) - m.Cmdy("nfs.copy", path.Join("var/file/hash", h[:2], h), arg[2]) - } + m.Cmdy("nfs.copy", path.Join("var/file/hash", h[:2], h), arg[2]) case "fetch": if m.Confs("file", arg[2]) { diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 9d3d1073..66a58fcf 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -178,7 +178,6 @@ func (web *WEB) HandleCmd(m *ctx.Message, key string, cmd *ctx.Command) { r.ParseMultipartForm(int64(msg.Confi("serve", "form_size"))) if r.ParseForm(); len(r.PostForm) > 0 { for k, v := range r.PostForm { - m.Log("info", "waht %v ", m.Gdb("web")) msg.Log("info", "%s: %v", k, v) } msg.Log("info", "") diff --git a/src/contexts/yac/yac.go b/src/contexts/yac/yac.go index 084ca352..6d8ab664 100644 --- a/src/contexts/yac/yac.go +++ b/src/contexts/yac/yac.go @@ -110,8 +110,8 @@ func (yac *YAC) train(m *ctx.Message, page, hash int, word []string, level int) default: x, ok := yac.page[word[i]] if !ok { - if x = yac.lex.Spawn().Cmd("parse", word[i], yac.name(s)).Resulti(0); x == 0 { - x = yac.lex.Spawn().Cmd("train", word[i], len(yac.mat[s]), yac.name(s)).Resulti(0) + if x = kit.Int(yac.lex.Spawn().Cmdx("parse", word[i], yac.name(s))); x == 0 { + x = kit.Int(yac.lex.Spawn().Cmdx("train", word[i], len(yac.mat[s]), yac.name(s))) } } @@ -213,7 +213,7 @@ func (yac *YAC) parse(m *ctx.Message, msg *ctx.Message, stack *kit.Stack, page i //全局语法检查 if state != nil { - if key := yac.lex.Spawn().Cmd("parse", line, "key"); key.Resulti(0) == 0 || len(key.Result(2)) <= len(result[2]) { + if key := yac.lex.Spawn().Cmd("parse", line, "key"); key.Result(0) == "0" || len(key.Result(2)) <= len(result[2]) { line, word = result[1], append(word, result[2]) } else { state = nil @@ -611,10 +611,6 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心", v1, e1 := strconv.Atoi(arg[0]) v2, e2 := strconv.Atoi(arg[2]) switch arg[1] { - case ":=": - if !m.Target().Has(arg[0]) { - result = m.Cap(arg[0], arg[0], arg[2], "临时变量") - } case "=": result = m.Cap(arg[0], arg[2]) case "+=": diff --git a/src/examples/chat/chat.go b/src/examples/chat/chat.go index ba2ee9d4..e74522ee 100644 --- a/src/examples/chat/chat.go +++ b/src/examples/chat/chat.go @@ -309,7 +309,7 @@ var Index = &ctx.Context{Name: "chat", Help: "会议中心", if len(arg) == 1 { m.Confm("flow", []string{arg[0], "tool"}, func(key string, value map[string]interface{}) { m.Add("append", "key", key) - m.Add("append", "count", kit.Len(value["list"])) + m.Add("append", "count", len(value["list"].([]interface{}))) }) m.Sort("key").Table() return diff --git a/src/examples/wiki/wiki.go b/src/examples/wiki/wiki.go index b7c89f5b..cab540f1 100644 --- a/src/examples/wiki/wiki.go +++ b/src/examples/wiki/wiki.go @@ -357,7 +357,7 @@ var Index = &ctx.Context{Name: "wiki", Help: "文档中心", return }}, "/wx/": &ctx.Command{Name: "/wx/", Help: "微信", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - if !m.Sess("aaa").Cmd("wx").Results(0) { + if !m.Sess("aaa").Cmds("wx") { return } if m.Has("echostr") { diff --git a/src/toolkit/core.go b/src/toolkit/core.go index 36ff9ad3..b7b7704a 100644 --- a/src/toolkit/core.go +++ b/src/toolkit/core.go @@ -3,42 +3,13 @@ package kit import ( "crypto/md5" "encoding/hex" - "fmt" "math/rand" - "os" - "path" + "sort" "strconv" "strings" "time" ) -var DisableLog = false - -func Log(action string, str string, args ...interface{}) { - if DisableLog { - return - } - - if len(args) > 0 { - str = fmt.Sprintf(str, args...) - } - fmt.Fprintf(os.Stderr, "%s: %s\n", action, str) -} - -func Pwd() string { - wd, _ := os.Getwd() - return wd -} -func Env(key string) { - os.Getenv(key) -} -func Errorf(str string, args ...interface{}) { - Log("error", str, args...) -} -func Debugf(str string, args ...interface{}) { - Log("debug", str, args...) -} - func Time(arg ...string) int { if len(arg) == 0 { return Int(time.Now()) @@ -90,6 +61,52 @@ func Hashs(arg ...interface{}) string { h, _ := Hash(arg...) return h } + +func Select(value string, args ...interface{}) string { + if len(args) == 0 { + return value + } + + switch arg := args[0].(type) { + case string: + if len(args) > 1 { + switch b := args[1].(type) { + case bool: + if b && arg != "" { + return arg + } + return value + } + } + if arg != "" { + return arg + } + case []interface{}: + index := 0 + if len(args) > 1 { + index = Int(args[1]) + } + if index < len(arg) && Format(arg[index]) != "" { + return Format(arg[index]) + } + case []string: + index := 0 + if len(args) > 1 { + index = Int(args[1]) + } + if index < len(arg) && arg[index] != "" { + return arg[index] + } + default: + if v := Format(args...); v != "" { + return v + } + } + return value +} +func Chains(root interface{}, args ...interface{}) string { + return Format(Chain(root, args...)) +} func Chain(root interface{}, args ...interface{}) interface{} { for i := 0; i < len(args); i += 2 { if arg, ok := args[i].(map[string]interface{}); ok { @@ -114,8 +131,6 @@ func Chain(root interface{}, args ...interface{}) interface{} { for j, key := range keys { index, e := strconv.Atoi(key) - // Log("error", "chain [%v %v] [%v %v] [%v/%v %v/%v] %v", parent_key, parent_index, key, index, i, len(args), j, len(keys), data) - var next interface{} switch value := data.(type) { case nil: @@ -208,73 +223,41 @@ func Chain(root interface{}, args ...interface{}) interface{} { return root } -func Chains(root interface{}, args ...interface{}) string { - return Format(Chain(root, args...)) -} - -func Select(value string, args ...interface{}) string { - if len(args) == 0 { - return value - } - - switch arg := args[0].(type) { - case string: - if len(args) > 1 { - switch b := args[1].(type) { - case bool: - if b && arg != "" { - return arg - } - return value - } - } - if arg != "" { - return arg - } - case []interface{}: - index := 0 - if len(args) > 1 { - index = Int(args[1]) - } - if index < len(arg) && Format(arg[index]) != "" { - return Format(arg[index]) - } - case []string: - index := 0 - if len(args) > 1 { - index = Int(args[1]) - } - if index < len(arg) && arg[index] != "" { - return arg[index] - } - default: - if v := Format(args...); v != "" { - return v - } - } - return value -} -func Slice(arg []string, args ...interface{}) ([]string, string) { +func Array(list []string, index int, arg ...interface{}) []string { if len(arg) == 0 { - return arg, "" - } - if len(args) == 0 { - return arg[1:], arg[0] + if -1 < index && index < len(list) { + return []string{list[index]} + } + return []string{""} } - result := "" - switch v := args[0].(type) { - case int: - case string: - if arg[0] == v && len(arg) > 1 { - return arg[2:], arg[1] + str := Trans(arg...) + + index = (index+2)%(len(list)+2) - 2 + if index == -1 { + list = append(str, list...) + } else if index == -2 { + list = append(list, str...) + } else { + if index < -2 { + index += len(list) + 2 } - if len(args) > 1 { - return arg, Format(args[1]) + if index < 0 { + index = 0 + } + + for i := len(list); i < index+len(str); i++ { + list = append(list, "") + } + for i := 0; i < len(str); i++ { + list[index+i] = str[i] } } - return arg, result + return list +} +func Width(str string, mul int) int { + return len([]rune(str)) + (len(str)-len([]rune(str)))/2/mul } func View(args []string, conf map[string]interface{}) []string { if len(args) == 0 { @@ -291,13 +274,91 @@ func View(args []string, conf map[string]interface{}) []string { } return keys } +func Map(v interface{}, random string, args ...interface{}) map[string]interface{} { + table, _ := v.([]interface{}) + value, _ := v.(map[string]interface{}) + if len(args) == 0 { + return value + } -func Create(p string) (*os.File, string, error) { - if dir, _ := path.Split(p); dir != "" { - if e := os.MkdirAll(dir, 0777); e != nil { - return nil, p, e + switch fun := args[0].(type) { + case func(map[string]interface{}): + if len(value) == 0 { + return nil + } + fun(value) + case func(int, string): + for i, v := range table { + fun(i, Format(v)) + } + case func(int, string) bool: + for i, v := range table { + if fun(i, Format(v)) { + break + } + } + case func(string, string): + for k, v := range value { + fun(k, Format(v)) + } + case func(string, string) bool: + for k, v := range value { + if fun(k, Format(v)) { + break + } + } + + case func(int, map[string]interface{}): + for i := 0; i < len(table); i++ { + if val, ok := table[i].(map[string]interface{}); ok { + fun(i, val) + } + } + case func(string, map[string]interface{}): + switch random { + case "%": + n, i := rand.Intn(len(value)), 0 + for k, v := range value { + if val, ok := v.(map[string]interface{}); i == n && ok { + fun(k, val) + break + } + i++ + } + case "*": + fallthrough + default: + for k, v := range value { + if val, ok := v.(map[string]interface{}); ok { + fun(k, val) + } + } + } + case func(string, map[string]interface{}) bool: + for k, v := range value { + if val, ok := v.(map[string]interface{}); ok { + if fun(k, val) { + break + } + } + } + case func(string, int, map[string]interface{}): + keys := make([]string, 0, len(value)) + for k, _ := range value { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, k := range keys { + v := value[k] + if val, ok := v.([]interface{}); ok { + for i, v := range val { + if val, ok := v.(map[string]interface{}); ok { + fun(k, i, val) + } + } + } } } - f, e := os.Create(p) - return f, p, e + return value } diff --git a/src/toolkit/misc.go b/src/toolkit/misc.go index d6a684a6..8fefdfbf 100644 --- a/src/toolkit/misc.go +++ b/src/toolkit/misc.go @@ -2,14 +2,8 @@ package kit import ( "fmt" - "strconv" - "strings" - - "io/ioutil" - "math/rand" "os" "path" - "time" ) type TERM interface { @@ -17,6 +11,34 @@ type TERM interface { } var STDIO TERM +var DisableLog = false + +func Log(action string, str string, args ...interface{}) { + if DisableLog { + return + } + + if len(args) > 0 { + str = fmt.Sprintf(str, args...) + } + fmt.Fprintf(os.Stderr, "%s: %s\n", action, str) +} +func Env(key string) { + os.Getenv(key) +} +func Pwd() string { + wd, _ := os.Getwd() + return wd +} +func Create(p string) (*os.File, string, error) { + if dir, _ := path.Split(p); dir != "" { + if e := os.MkdirAll(dir, 0777); e != nil { + return nil, p, e + } + } + f, e := os.Create(p) + return f, p, e +} func Split(str string, n int) []string { res := []string{} @@ -36,119 +58,6 @@ func Split(str string, n int) []string { } return res } - -func Width(str string, mul int) int { - return len([]rune(str)) + (len(str)-len([]rune(str)))/2/mul -} -func Len(arg interface{}) int { - switch arg := arg.(type) { - case []interface{}: - return len(arg) - case map[string]interface{}: - return len(arg) - } - return 0 -} -func Simple(str string) string { - return strings.Replace(strings.TrimSpace(str), "\n", "\\n", -1) -} -func Array(list []string, index int, arg ...interface{}) []string { - if len(arg) == 0 { - if -1 < index && index < len(list) { - return []string{list[index]} - } - return []string{""} - } - - str := Trans(arg...) - - index = (index+2)%(len(list)+2) - 2 - if index == -1 { - list = append(str, list...) - } else if index == -2 { - list = append(list, str...) - } else { - if index < -2 { - index += len(list) + 2 - } - if index < 0 { - index = 0 - } - - for i := len(list); i < index+len(str); i++ { - list = append(list, "") - } - for i := 0; i < len(str); i++ { - list[index+i] = str[i] - } - } - - return list -} -func Elect(last interface{}, args ...interface{}) string { - if len(args) > 0 { - switch arg := args[0].(type) { - case []string: - index := 0 - if len(args) > 1 { - switch a := args[1].(type) { - case string: - i, e := strconv.Atoi(a) - if e == nil { - index = i - } - case int: - index = a - } - } - - if 0 <= index && index < len(arg) && arg[index] != "" { - return arg[index] - } - case string: - if arg != "" { - return arg - } - } - } - - switch l := last.(type) { - case string: - return l - } - return "" -} - -func Link(name string, url string) string { - return fmt.Sprintf("%s", url, name) -} -func FileName(name string, meta ...string) string { - result, app := strings.Split(name, "."), "" - if len(result) > 1 { - app, result = result[len(result)-1], result[:len(result)-1] - } - - for _, v := range meta { - switch v { - case "year": - result = append(result, "_", time.Now().Format("2006")) - case "date": - result = append(result, "_", time.Now().Format("0102")) - case "time": - result = append(result, "_", time.Now().Format("2006_0102_1504")) - case "rand": - result = append(result, "_", Format(rand.Int())) - case "uniq": - result = append(result, "_", Format(Time())) - result = append(result, "_", Format(rand.Int())) - } - } - - if app != "" { - result = append(result, ".", app) - } - return strings.Join(result, "") -} func FmtSize(size uint64) string { if size > 1<<30 { return fmt.Sprintf("%d.%dG", size>>30, (size>>20)%1024*100/1024) @@ -164,44 +73,3 @@ func FmtSize(size uint64) string { return fmt.Sprintf("%dB", size) } -func FmtNano(nano int64) string { - if nano > 1000000000 { - return fmt.Sprintf("%d.%ds", nano/1000000000, nano/100000000%100) - } - - if nano > 1000000 { - return fmt.Sprintf("%d.%dms", nano/100000, nano/100000%100) - } - - if nano > 1000 { - return fmt.Sprintf("%d.%dus", nano/1000, nano/100%100) - } - - return fmt.Sprintf("%dns", nano) -} - -func Block(root interface{}, args ...interface{}) interface{} { - - return root -} - -func Check(e error) bool { - if e != nil { - panic(e) - } - return true -} -func DirWalk(file string, hand func(file string)) { - s, e := os.Stat(file) - Check(e) - hand(file) - - if s.IsDir() { - fs, e := ioutil.ReadDir(file) - Check(e) - - for _, v := range fs { - DirWalk(path.Join(file, v.Name()), hand) - } - } -} diff --git a/src/toolkit/type.go b/src/toolkit/type.go index b5f3933f..d66c82e4 100644 --- a/src/toolkit/type.go +++ b/src/toolkit/type.go @@ -50,12 +50,8 @@ func Int64(arg ...interface{}) int64 { result += int64(val) case int16: result += int64(val) - // case int32: - // result += int64(val) case int64: result += int64(val) - // case uint8: - // result += int64(val) case uint16: result += int64(val) case uint32: @@ -124,8 +120,6 @@ func Format(arg ...interface{}) string { } else { result = append(result, fmt.Sprintf("%T", v)) } - // case error: - // result = append(result, fmt.Sprintf("%v", val)) default: if b, e := json.Marshal(val); e == nil { result = append(result, string(b)) @@ -167,12 +161,6 @@ func Trans(arg ...interface{}) []string { ls := []string{} for _, v := range arg { switch val := v.(type) { - // case *Message: - // if val.Hand { - // ls = append(ls, val.Meta["result"]...) - // } else { - // ls = append(ls, val.Meta["detail"]...) - // } case nil: case []float64: for _, v := range val {