From ff63b234cb53b2cb5f1d08ed2385778785bf1270 Mon Sep 17 00:00:00 2001 From: shaoying Date: Thu, 19 Sep 2019 19:15:05 +0800 Subject: [PATCH] add cli.date --- bin/boot.sh | 2 +- etc/init.shy | 3 + src/contexts/cli/cli.go | 120 ++++++++++++++++++++++++++--------- src/contexts/cli/version.go | 2 +- src/contexts/ctx/misc.go | 24 +++---- src/plugin/context/index.shy | 2 +- src/plugin/love/index.js | 15 ++++- src/plugin/love/index.shy | 8 ++- src/toolkit/core.go | 3 + usr/librarys/example.js | 2 +- usr/librarys/toolkit.js | 1 + 11 files changed, 131 insertions(+), 51 deletions(-) diff --git a/bin/boot.sh b/bin/boot.sh index f553bc78..843bdb2d 100755 --- a/bin/boot.sh +++ b/bin/boot.sh @@ -58,7 +58,7 @@ main() { trap HUP hup log "\nstarting..." while true; do - date && ${ctx_bin} "$@" && break + date && ${ctx_bin} "$@" 2>error.log && break log "\n\nrestarting..." && sleep 1 done } diff --git a/etc/init.shy b/etc/init.shy index f8aec5e9..960d8654 100644 --- a/etc/init.shy +++ b/etc/init.shy @@ -13,3 +13,6 @@ ~nfs source local.shy +~cli + upgrade plugin context + upgrade plugin love diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index 0aac4ee7..653727c6 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -57,13 +57,10 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", Caches: map[string]*ctx.Cache{}, Configs: map[string]*ctx.Config{ "runtime": &ctx.Config{Name: "runtime", Value: map[string]interface{}{ - "init": []interface{}{ - "ctx_log", "ctx_app", "ctx_bin", - "ctx_ups", "ctx_box", "ctx_dev", - "ctx_cas", - "ctx_root", "ctx_home", - "ctx_type", - "web_port", "ssh_port", + "init": []interface{}{"ctx_log", + "ctx_cas", "ctx_ups", "ctx_box", "ctx_dev", + "ctx_app", "ctx_bin", "ctx_root", "ctx_home", + "ctx_type", "ssh_port", "web_port", }, "boot": map[string]interface{}{ "web_port": ":9095", @@ -139,7 +136,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", }, }, Help: "版本发布"}, "upgrade": &ctx.Config{Name: "upgrade", Value: map[string]interface{}{ - "install": []interface{}{"context", "tmux", "mind", "love"}, + "install": []interface{}{"context", "love"}, "system": []interface{}{"boot.sh", "zone.sh", "user.sh", "node.sh", "init.shy", "common.shy", "exit.shy"}, "portal": []interface{}{"template.tar.gz", "librarys.tar.gz"}, "script": []interface{}{"test.php"}, @@ -709,6 +706,66 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", } return }}, + "date": &ctx.Command{Name: "date", Help: "日历", Form: map[string]int{"space": 1, "format": 2, "count": 1, "nature": 1, "cmd": -1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + if m.Has("cmd") { + m.Log("fino", "what %v", m.Meta["cmd"]) + m.Cmdy(m.Meta["cmd"]) + return + } + show := map[int]string{0: "周日", 1: "周一", 2: "周二", 3: "周三", 4: "周四", 5: "周五", 6: "周六"} + + format, format_time := "", "" + if m.Has("format") { + format, format_time = kit.Select("%s", m.Meta["format"], 0), kit.Select("20060102", m.Meta["format"], 1) + } + space := m.Options("space") + now := kit.Times(m.Cmd("cli.time", arg).Append("datetime")) + n := kit.Int(kit.Select("1", m.Option("count"))) + if m.Has("nature") { + n = 0 + nature := kit.Times(m.Option("nature")) + for cur := now; cur.Before(nature); cur = cur.AddDate(0, 1, 0) { + n++ + } + } + + cur := now + for i := 0; i < n; i, now = i+1, now.AddDate(0, 1, 0) { + begin := time.Unix(now.Unix()-int64(now.Day()-1)*24*3600, 0) + last := time.Unix(begin.Unix()-int64(begin.Weekday())*24*3600, 0) + cur = last + + if last.Month() != now.Month() { + for month := cur.Month(); cur.Month() == month; cur = cur.AddDate(0, 0, 1) { + if space || i == 0 { + m.Push(show[int(cur.Weekday())], "") + } + } + } + for month := cur.Month(); cur.Month() == month; cur = cur.AddDate(0, 0, 1) { + data := fmt.Sprintf("%d", cur.Day()) + if cur.Day() == 1 { + if cur.Month() == 1 { + data = fmt.Sprintf("%d年", cur.Year()) + } else { + data = fmt.Sprintf("%d月", cur.Month()) + } + } + if format != "" { + data = fmt.Sprintf(format, cur.Format(format_time), data) + } + m.Push(show[int(cur.Weekday())], data) + } + if space || i == n-1 { + for ; cur.Weekday() > 0; cur = cur.AddDate(0, 0, 1) { + m.Push(show[int(cur.Weekday())], "") + } + } + } + + m.Table() + return + }}, "proc": &ctx.Command{Name: "proc", Help: "进程管理", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { m.Cmdy("cli.system", "ps", kit.Select("ax", arg, 0), "cmd_parse", "cut") if len(arg) > 1 { @@ -940,7 +997,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", m.Sort("file").Table() return }}, - "upgrade": &ctx.Command{Name: "upgrade project|bench|system|portal|plugin|restart|script", Help: "服务升级", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + "upgrade": &ctx.Command{Name: "upgrade install|bench|system|portal|script|plugin|restart|package|project", Help: "服务升级", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { if len(arg) == 0 { m.Cmdy("ctx.config", "upgrade") return @@ -954,11 +1011,6 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", m.Cmd("cli.upgrade", "package", value) }) - case "project": - m.Cmd("cli.project", "init") - m.Cmd("cli.compile", "all") - m.Cmd("cli.publish") - case "script": // 脚本列表 if len(arg) == 1 { @@ -977,18 +1029,6 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", "missyou", miss, "save", path.Join(m.Conf("project", "script.path"), v)) } - case "restart": - m.Cmdy("cli.quit", "1") - - case "package": - name := arg[1] + ".tar.gz" - p := path.Join(m.Conf("publish", "path"), name) - - m.Cmd("web.get", "dev", fmt.Sprintf("publish/%s", name), "save", p, - "GOARCH", m.Conf("runtime", "host.GOARCH"), "GOOS", m.Conf("runtime", "host.GOOS")) - - m.Cmd("cli.system", "tar", "-xvf", p, "-C", path.Dir(p)) - case "plugin": // 模块列表 if arg = arg[1:]; len(arg) == 0 { @@ -1013,6 +1053,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", // 查找脚本 p := m.Cmdx("nfs.path", path.Join(msg.Conf("project", "plugin.path"), arg[0], "index.shy")) + if p == "" { + p = m.Cmdx("nfs.path", path.Join(msg.Conf("publish", "path"), arg[0], "index.shy")) + } if p == "" { p = m.Cmdx("nfs.hash", m.Cmdx("web.get", "dev", fmt.Sprintf("publish/%s", arg[0]), "GOARCH", m.Conf("runtime", "host.GOARCH"), @@ -1031,13 +1074,30 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", // 组件列表 m.Confm("ssh.componet", arg[0], func(index int, value map[string]interface{}) { m.Push("index", index) - m.Push("name", value["componet_name"]) - m.Push("help", value["componet_help"]) - m.Push("ctx", value["componet_ctx"]) - m.Push("cmd", value["componet_cmd"]) + m.Push("name", value["name"]) + m.Push("help", value["help"]) + m.Push("ctx", value["ctx"]) + m.Push("cmd", value["cmd"]) }) m.Table() + case "restart": + m.Cmdy("cli.quit", "1") + + case "package": + name := arg[1] + ".tar.gz" + p := path.Join(m.Conf("publish", "path"), name) + + m.Cmd("web.get", "dev", fmt.Sprintf("publish/%s", name), "save", p, + "GOARCH", m.Conf("runtime", "host.GOARCH"), "GOOS", m.Conf("runtime", "host.GOOS")) + + m.Cmd("cli.system", "tar", "-xvf", p, "-C", path.Dir(p)) + + case "project": + m.Cmd("cli.project", "init") + m.Cmd("cli.compile", "all") + m.Cmd("cli.publish") + default: restart := false for _, link := range kit.View([]string{arg[0]}, m.Confm("upgrade")) { diff --git a/src/contexts/cli/version.go b/src/contexts/cli/version.go index 2fa55746..95c75c90 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-09-19 08:57:38", "com.mac", 501, + "2019-09-19 19:02:39", "centos", 562, } diff --git a/src/contexts/ctx/misc.go b/src/contexts/ctx/misc.go index 7f889826..5a59f830 100644 --- a/src/contexts/ctx/misc.go +++ b/src/contexts/ctx/misc.go @@ -153,37 +153,37 @@ func (m *Message) Format(arg ...interface{}) string { for i := len(ms) - 1; i >= 0; i-- { msg := ms[i] - meta = append(meta, fmt.Sprintf("%s\n", msg.Format("time", "ship"))) + meta = append(meta, fmt.Sprintf("%s", msg.Format("time", "ship"))) if len(msg.Meta["detail"]) > 0 { - meta = append(meta, fmt.Sprintf(" detail: %d %v\n", len(msg.Meta["detail"]), msg.Meta["detail"])) + meta = append(meta, fmt.Sprintf("detail:%d %v", len(msg.Meta["detail"]), msg.Meta["detail"])) } + if len(msg.Meta["option"]) > 0 { - meta = append(meta, fmt.Sprintf(" option: %d %v\n", len(msg.Meta["option"]), msg.Meta["option"])) + meta = append(meta, fmt.Sprintf("option:%d %v\n", len(msg.Meta["option"]), msg.Meta["option"])) for _, k := range msg.Meta["option"] { - if _, ok := msg.Data[k]; ok { - // meta = append(meta, fmt.Sprintf(" %s: %v\n", k, kit.Format(v))) - } else if v, ok := msg.Meta[k]; ok { + if v, ok := msg.Meta[k]; ok { meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v)) } } + } else { + meta = append(meta, "\n") } + if len(msg.Meta["append"]) > 0 { - meta = append(meta, fmt.Sprintf(" append: %d %v\n", len(msg.Meta["append"]), msg.Meta["append"])) + meta = append(meta, fmt.Sprintf(" append:%d %v\n", len(msg.Meta["append"]), msg.Meta["append"])) for _, k := range msg.Meta["append"] { - if _, ok := msg.Data[k]; ok { - // meta = append(meta, fmt.Sprintf(" %s: %v\n", k, kit.Format(v))) - } else if v, ok := msg.Meta[k]; ok { + if v, ok := msg.Meta[k]; ok { meta = append(meta, fmt.Sprintf(" %s: %d %v\n", k, len(v), v)) } } } if len(msg.Meta["result"]) > 0 { - meta = append(meta, fmt.Sprintf(" result: %d %v\n", len(msg.Meta["result"]), msg.Meta["result"])) + meta = append(meta, fmt.Sprintf(" result:%d %v\n", len(msg.Meta["result"]), msg.Meta["result"])) } } case "stack": pc := make([]uintptr, 100) - pc = pc[:runtime.Callers(0, pc)] + pc = pc[:runtime.Callers(5, pc)] frames := runtime.CallersFrames(pc) for { diff --git a/src/plugin/context/index.shy b/src/plugin/context/index.shy index 1bc82c91..bbaf7704 100644 --- a/src/plugin/context/index.shy +++ b/src/plugin/context/index.shy @@ -17,7 +17,7 @@ kit publish "发布" private \ kit upgrade "升级" private "ssh._route" _ "cli.upgrade" \ text "" name pod imports plugin_pod \ - select "" values script values restart values plugin values bench values portal values system action auto \ + select "" values script values plugin values restart values package values bench values portal values system action auto \ text "" name see imports plugin_see \ button "升级" diff --git a/src/plugin/love/index.js b/src/plugin/love/index.js index 28ad8994..69ec965b 100644 --- a/src/plugin/love/index.js +++ b/src/plugin/love/index.js @@ -1,4 +1,15 @@ {init: function(run, field, option, output) { - kit.Log("hello world") - return {} + return { + show: function(event) { + run(event, ["", "", "cmd", "ssh.data", "show", "love"], function(msg) { + ctx.Table(msg, function(value) { + var ts = ".s"+value.when.split(" ")[0].split("-").join("") + kit.Selector(output, ts, function(item) { + item.parentNode.style.backgroundColor = "red" + item.parentNode.title = value.where + }) + }) + }) + }, + } }} diff --git a/src/plugin/love/index.shy b/src/plugin/love/index.shy index aab5afb1..890dba7f 100644 --- a/src/plugin/love/index.shy +++ b/src/plugin/love/index.shy @@ -16,7 +16,9 @@ fun meets "第一眼" private \ copy ssh.data insert _ when _ where __ end -fun meet "第一眼" protected "ssh.data" show \ - text "love" name table imports plugin_table \ - button "查看" action auto +kit date "日历" "index.js" protected "cli.date" format '%s' "s20060102" space true _ nature _ \ + text "2019-07-08" name "起始时间" \ + text "date" name "截止时间" \ + button "查看" action auto \ + button "显示" click show diff --git a/src/toolkit/core.go b/src/toolkit/core.go index b7b7704a..46af428a 100644 --- a/src/toolkit/core.go +++ b/src/toolkit/core.go @@ -34,6 +34,9 @@ func Time(arg ...string) int { } return 0 } +func Times(arg ...string) time.Time { + return time.Unix(int64(Time(arg...)), 0) +} func Duration(arg ...string) time.Duration { d, _ := time.ParseDuration(arg[0]) return d diff --git a/usr/librarys/example.js b/usr/librarys/example.js index f107e5c7..0df45816 100644 --- a/usr/librarys/example.js +++ b/usr/librarys/example.js @@ -564,7 +564,7 @@ function Pane(page, field) { list.push(ui.last), field.scrollBy(0, field.scrollHeight+100) key && key.length > 0 && (member[line[which]] = member[line[key[0]]] = {index:index, key:line[which]}); - line.name && (type == "plugin" || type == "field") && pane.Plugin(page, pane, ui.field, function(event, cmds, cbs) { + (type == "plugin" && line.name || type == "field") && pane.Plugin(page, pane, ui.field, function(event, cmds, cbs) { typeof cb == "function" && cb(line, index, event, cmds, cbs) }) return ui diff --git a/usr/librarys/toolkit.js b/usr/librarys/toolkit.js index a5de6b16..57dddb65 100644 --- a/usr/librarys/toolkit.js +++ b/usr/librarys/toolkit.js @@ -477,6 +477,7 @@ kit = toolkit = { // HTML显示文本 Color: function(s) { + if (!s) {return s} s = s.replace(/\033\[1m/g, "") s = s.replace(/\033\[36m/g, "") s = s.replace(/\033\[33m/g, "")