From 74359c00486ccb787224e9961de1c9db7d22e9f4 Mon Sep 17 00:00:00 2001 From: shylinux Date: Thu, 29 Jul 2021 08:40:12 +0800 Subject: [PATCH] opt icebergs --- base/cli/qrcode.go | 5 + base/web/render.go | 18 +- base/web/share.go | 22 ++ core/wiki/spark.go | 21 ++ data.go | 28 ++ exec.go | 33 ++ meta.go | 11 + misc.go | 793 ++++++++++++++++++--------------------------- type.go | 310 +----------------- 9 files changed, 447 insertions(+), 794 deletions(-) diff --git a/base/cli/qrcode.go b/base/cli/qrcode.go index a9b124b9..f005e76f 100644 --- a/base/cli/qrcode.go +++ b/base/cli/qrcode.go @@ -132,6 +132,11 @@ func init() { QRCODE: {Name: QRCODE, Help: "二维码", Value: kit.Data()}, }, Commands: map[string]*ice.Command{ + ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + ice.AddRender(ice.RENDER_QRCODE, func(m *ice.Message, cmd string, args ...interface{}) string { + return m.Cmd(QRCODE, kit.Simple(args...)).Result() + }) + }}, QRCODE: {Name: "qrcode text fg bg size auto", Help: "二维码", Action: map[string]*ice.Action{}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Option(SIZE, kit.Select("240", arg, 3)) m.Option(BG, kit.Select(WHITE, arg, 2)) diff --git a/base/web/render.go b/base/web/render.go index bc10d654..d6720318 100644 --- a/base/web/render.go +++ b/base/web/render.go @@ -12,9 +12,8 @@ import ( ) const ( - REFRESH = "refresh" - STATUS = "status" - COOKIE = "cookie" + STATUS = "status" + COOKIE = "cookie" ) func Render(msg *ice.Message, cmd string, args ...interface{}) { @@ -23,21 +22,15 @@ func Render(msg *ice.Message, cmd string, args ...interface{}) { } switch arg := kit.Simple(args...); cmd { - case ice.RENDER_REDIRECT: // url [arg...] - http.Redirect(msg.W, msg.R, kit.MergeURL(arg[0], arg[1:]), 307) - - case REFRESH: // [delay [text]] - arg = []string{"200", fmt.Sprintf(`%s`, - kit.Int(kit.Select("3", arg, 0)), kit.Select("请稍后,系统初始化中...", arg, 1), - )} - fallthrough - case STATUS: // [code [text]] RenderStatus(msg, kit.Int(kit.Select("200", arg, 0)), kit.Select("", arg, 1)) case COOKIE: // value [name [path [expire]]] RenderCookie(msg, arg[0], arg[1:]...) + case ice.RENDER_REDIRECT: // url [arg...] + http.Redirect(msg.W, msg.R, kit.MergeURL(arg[0], arg[1:]), 307) + case ice.RENDER_DOWNLOAD: // file [type [name]] msg.W.Header().Set("Content-Disposition", fmt.Sprintf("filename=%s", kit.Select(path.Base(kit.Select(arg[0], msg.Option("filename"))), arg, 2))) if RenderType(msg.W, arg[0], kit.Select("", arg, 1)); !ice.Dump(msg.W, arg[0], nil) { @@ -71,7 +64,6 @@ func Render(msg *ice.Message, cmd string, args ...interface{}) { fmt.Fprint(msg.W, msg.Formats(kit.MDB_META)) } } - func RenderStatus(msg *ice.Message, code int, text string) { msg.W.WriteHeader(code) msg.W.Write([]byte(text)) diff --git a/base/web/share.go b/base/web/share.go index db7a55ab..6417f3e9 100644 --- a/base/web/share.go +++ b/base/web/share.go @@ -1,6 +1,7 @@ package web import ( + "fmt" "net/http" "os" "path" @@ -114,6 +115,27 @@ func init() { SHARE: {Name: SHARE, Help: "共享链", Value: kit.Data(kit.MDB_EXPIRE, "72h")}, }, Commands: map[string]*ice.Command{ + ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + ice.AddRender(ice.RENDER_DOWNLOAD, func(m *ice.Message, cmd string, args ...interface{}) string { + arg := kit.Simple(args...) + if arg[0] == "" { + return "" + } + list := []string{} + if m.Option(ice.MSG_USERPOD) != "" { + list = append(list, "pod", m.Option(ice.MSG_USERPOD)) + } + if len(arg) == 1 { + arg[0] = kit.MergeURL2(m.Option(ice.MSG_USERWEB), path.Join(kit.Select("", "/share/local", + !strings.HasPrefix(arg[0], "/")), arg[0]), list) + } else { + arg[1] = kit.MergeURL2(m.Option(ice.MSG_USERWEB), path.Join(kit.Select("", "/share/local", + !strings.HasPrefix(arg[1], "/")), arg[1]), list, "filename", arg[0]) + } + arg[0] = m.ReplaceLocalhost(arg[0]) + return fmt.Sprintf(`%s`, m.ReplaceLocalhost(kit.Select(arg[0], arg, 1)), path.Base(arg[0]), arg[0]) + }) + }}, SHARE: {Name: "share hash auto prunes", Help: "共享链", Action: map[string]*ice.Action{ mdb.CREATE: {Name: "create type name text", Help: "创建", Hand: func(m *ice.Message, arg ...string) { m.Cmdy(mdb.INSERT, SHARE, "", mdb.HASH, kit.MDB_TIME, m.Time(m.Conf(SHARE, kit.Keym(kit.MDB_EXPIRE))), diff --git a/core/wiki/spark.go b/core/wiki/spark.go index 843a8d63..80ac1905 100644 --- a/core/wiki/spark.go +++ b/core/wiki/spark.go @@ -34,6 +34,27 @@ const SPARK = "spark" func init() { Index.Merge(&ice.Context{ Commands: map[string]*ice.Command{ + ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + ice.AddRender(ice.RENDER_SCRIPT, func(m *ice.Message, cmd string, args ...interface{}) string { + arg := kit.Simple(args...) + if len(arg) == 1 && arg[0] != kit.SSH_BREAK { + arg = []string{kit.SSH_SHELL, arg[0]} + } + list := []string{kit.Format(`
`, arg[0])} + for _, l := range strings.Split(strings.Join(arg[1:], "\n"), "\n") { + switch list = append(list, "
"); arg[0] { + case kit.SSH_SHELL: + list = append(list, "") + default: + list = append(list, "") + } + list = append(list, "", l, "") + list = append(list, "
") + } + list = append(list, "
") + return strings.Join(list, "") + }) + }}, SPARK: {Name: "spark [name] text", Help: "段落", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if len(arg) == 0 { m.Echo(`
`) diff --git a/data.go b/data.go index 2fcd0037..5f383609 100644 --- a/data.go +++ b/data.go @@ -8,6 +8,34 @@ import ( "github.com/shylinux/toolkits/miss" ) +func (m *Message) Prefix(arg ...string) string { + return kit.Keys(m.Cap(CTX_FOLLOW), arg) +} +func (m *Message) PrefixKey(arg ...string) string { + return kit.Keys(m.Cap(CTX_FOLLOW), m._key, arg) +} +func (m *Message) Save(arg ...string) *Message { + if len(arg) == 0 { + for k := range m.target.Configs { + arg = append(arg, k) + } + } + list := []string{} + for _, k := range arg { + list = append(list, m.Prefix(k)) + } + m.Cmd("ctx.config", "save", m.Prefix("json"), list) + return m +} +func (m *Message) Load(arg ...string) *Message { + list := []string{} + for _, k := range arg { + list = append(list, m.Prefix(k)) + } + m.Cmd("ctx.config", "load", m.Prefix("json"), list) + return m +} + func (m *Message) Richs(prefix string, chain interface{}, raw interface{}, cb interface{}) (res map[string]interface{}) { cache := m.Confm(prefix, chain) if cache == nil { diff --git a/exec.go b/exec.go index 529fee03..47b874c5 100644 --- a/exec.go +++ b/exec.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io" + "strings" "time" kit "github.com/shylinux/toolkits" @@ -76,6 +77,7 @@ func (m *Message) Done(b bool) bool { ctx.wg.Done() return true } + func (m *Message) Call(sync bool, cb func(*Message) *Message) *Message { wait := make(chan bool, 2) p := kit.Select("10s", m.Option("timeout")) @@ -128,3 +130,34 @@ func (m *Message) Go(cb interface{}, args ...interface{}) *Message { } return m.Gos(m, cb, args...) } + +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, " ")) + return m +} +func (m *Message) Event(key string, arg ...string) *Message { + m.Cmd("gdb.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", + m.Option(MSG_USERROLE), strings.ReplaceAll(kit.Keys(arg...), "/", ".")) != "ok", + ErrNotRight, m.Option(MSG_USERROLE), " of ", strings.Join(kit.Simple(arg), "."), " 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") { + return nil + } + return []string{"web.space", kit.Format(arg)} +} +func (m *Message) PodCmd(arg ...interface{}) bool { + if pod := m.Option("pod"); pod != "" { + m.Option("pod", "") + m.Cmd(append([]interface{}{"space", pod}, arg...)) + return true + } + return false +} diff --git a/meta.go b/meta.go index d6a311ad..992b7a24 100644 --- a/meta.go +++ b/meta.go @@ -444,3 +444,14 @@ func (m *Message) Result(arg ...interface{}) string { } return strings.Join(m.Resultv(arg...), "") } + +func (m *Message) SortInt(key string) { m.Sort(key, "int") } +func (m *Message) SortIntR(key string) { m.Sort(key, "int_r") } +func (m *Message) SortStr(key string) { m.Sort(key, "str") } +func (m *Message) SortStrR(key string) { m.Sort(key, "str_r") } +func (m *Message) SortTime(key string) { m.Sort(key, "time") } +func (m *Message) SortTimeR(key string) { m.Sort(key, "time_r") } + +func (m *Message) FormatMeta() string { return m.Format("meta") } +func (m *Message) FormatSize() string { return m.Format("size") } +func (m *Message) FormatCost() string { return m.Format("cost") } diff --git a/misc.go b/misc.go index 114448bc..88bd257c 100644 --- a/misc.go +++ b/misc.go @@ -5,440 +5,37 @@ import ( "encoding/csv" "encoding/json" "fmt" - "net/url" - "os" "path" + "reflect" + "runtime" "strings" "time" kit "github.com/shylinux/toolkits" ) -func (m *Message) Prefix(arg ...string) string { - return kit.Keys(m.Cap(CTX_FOLLOW), arg) -} -func (m *Message) PrefixKey(arg ...string) string { - return kit.Keys(m.Cap(CTX_FOLLOW), m._key, arg) -} -func (m *Message) Save(arg ...string) *Message { - if len(arg) == 0 { - for k := range m.target.Configs { - arg = append(arg, k) - } - } - list := []string{} - for _, k := range arg { - list = append(list, m.Prefix(k)) - } - m.Cmd("ctx.config", "save", m.Prefix("json"), list) - return m -} -func (m *Message) Load(arg ...string) *Message { - list := []string{} - for _, k := range arg { - list = append(list, m.Prefix(k)) - } - m.Cmd("ctx.config", "load", m.Prefix("json"), list) - return m -} - -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, " ")) - return m -} -func (m *Message) Event(key string, arg ...string) *Message { - m.Cmd("gdb.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", - m.Option(MSG_USERROLE), strings.ReplaceAll(kit.Keys(arg...), "/", ".")) != "ok", - ErrNotRight, m.Option(MSG_USERROLE), " of ", strings.Join(kit.Simple(arg), "."), " 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") { - return nil - } - return []string{"web.space", kit.Format(arg)} -} - -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) PushPodCmd(cmd string, arg ...string) { - m.Table(func(index int, value map[string]string, head []string) { - m.Push("pod", m.Option(MSG_USERPOD)) - }) - - m.Cmd("web.space").Table(func(index int, value map[string]string, head []string) { - switch value[kit.MDB_TYPE] { - case "worker", "server": - 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) { - val["pod"] = kit.Keys(value[kit.MDB_NAME], val["pod"]) - m.Push("", val, head) - }) - } - }) -} -func (m *Message) PushSearch(args ...interface{}) { - data := kit.Dict(args...) - for _, k := range kit.Split(m.Option(MSG_FIELDS)) { - switch k { - case "pod": - // m.Push(k, kit.Select(m.Option(MSG_USERPOD), data[kit.SSH_POD])) - case "ctx": - m.Push(k, m.Prefix()) - case "cmd": - m.Push(k, kit.Format(data["cmd"])) - case kit.MDB_TIME: - m.Push(k, kit.Select(m.Time(), data[k])) - default: - m.Push(k, kit.Format(kit.Select("", data[k]))) - } - } -} -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 Render(m *Message, cmd string, args ...interface{}) string { - if m.IsCliUA() { - switch arg := kit.Simple(args...); cmd { - case RENDER_QRCODE: // text [size] - msg := m.Cmd("cli.qrcode", arg[0]) - return msg.Result() - } - return "" - } - - switch arg := kit.Simple(args...); cmd { - case RENDER_DOWNLOAD: // [name] file - if arg[0] == "" { - return "" - } - list := []string{} - if m.Option(MSG_USERPOD) != "" { - list = append(list, "pod", m.Option(MSG_USERPOD)) - } - if len(arg) == 1 { - arg[0] = kit.MergeURL2(m.Option(MSG_USERWEB), path.Join(kit.Select("", "/share/local", - !strings.HasPrefix(arg[0], "/")), arg[0]), list) - } else { - arg[1] = kit.MergeURL2(m.Option(MSG_USERWEB), path.Join(kit.Select("", "/share/local", - !strings.HasPrefix(arg[1], "/")), arg[1]), list, "filename", arg[0]) - } - arg[0] = m.ReplaceLocalhost(arg[0]) - return fmt.Sprintf(`%s`, m.ReplaceLocalhost(kit.Select(arg[0], arg, 1)), path.Base(arg[0]), arg[0]) - - case RENDER_ANCHOR: // [name] link - return fmt.Sprintf(`%s`, kit.Select(arg[0], arg, 1), arg[0]) - - case RENDER_BUTTON: // name... - if m._cmd == nil || m._cmd.Meta == nil { - return "" - } - list := []string{} - for _, k := range kit.Split(strings.Join(arg, ",")) { - list = append(list, fmt.Sprintf(``, - k, kit.Select(k, kit.Value(m._cmd.Meta, kit.Keys("_trans", k))))) - } - return strings.Join(list, "") - - case RENDER_IMAGES: // src [size] - return fmt.Sprintf(``, arg[0], kit.Select("120", arg, 1)) - - case RENDER_VIDEOS: // src [size] - return fmt.Sprintf(`