From 53abac7812dbfedbdd8ab21360c5393cfa74fdc7 Mon Sep 17 00:00:00 2001 From: shaoying Date: Sun, 8 Jul 2018 21:21:06 +0800 Subject: [PATCH] tce add cli.time.monday --- src/contexts/cli/cli.go | 70 ++++++++++++++++++++++--- src/contexts/ctx.go | 19 +++++-- src/contexts/mdb/mdb.go | 16 +++++- src/contexts/web/web.go | 38 ++++++++------ usr/template/travel.html | 107 ++++++++++++++++++++------------------- 5 files changed, 169 insertions(+), 81 deletions(-) diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index b990dd77..5a9c58fd 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -307,7 +307,8 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", }}, }, Configs: map[string]*ctx.Config{ - "time_format": &ctx.Config{Name: "time_format", Value: "2006-01-02 15:04:05", Help: "时间格式"}, + "time_format": &ctx.Config{Name: "time_format", Value: "2006-01-02 15:04:05", Help: "时间格式"}, + "time_interval": &ctx.Config{Name: "time_interval(open/close)", Value: "open", Help: "时间区间"}, }, Commands: map[string]*ctx.Command{ "alias": &ctx.Command{Name: "alias [short [long]]|[delete short]", Help: "查看、定义或删除命令别名, short: 命令别名, long: 命令原名, delete: 删除别名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { @@ -339,22 +340,75 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", } } // }}} }}, - "time": &ctx.Command{Name: "time [parse when] [time_format format] when", - Form: map[string]int{"parse": 1, "time_format": 1}, + "time": &ctx.Command{Name: "time [time_format format] [parse when] when [begin|end|yestoday|tommorow|monday|sunday|first|last|origin|last]", + Form: map[string]int{"parse": 1, "time_format": 1, "time_interval": 1}, Help: "睡眠, time(ns/us/ms/s/m/h): 时间值(纳秒/微秒/毫秒/秒/分钟/小时)", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { t := time.Now() // {{{ f := m.Confx("time_format") - if len(arg) > 0 { - if i, e := strconv.Atoi(arg[0]); e == nil { - t = time.Unix(int64(i), 0) - } - } + if m.Options("parse") { n, e := time.ParseInLocation(f, m.Option("parse"), time.Local) m.Assert(e) t = n } + if len(arg) > 0 { + if i, e := strconv.Atoi(arg[0]); e == nil { + t = time.Unix(int64(i/1000), 0) + arg = arg[1:] + } + } + + if len(arg) > 0 { + switch arg[0] { + case "begin": + // t.Add(-((time.Second)t.Second() + (time.Minute)t.Minute() + (time.Hour)t.Hour())) + d, e := time.ParseDuration(fmt.Sprintf("%dh%dm%ds", t.Hour(), t.Minute(), t.Second())) + m.Assert(e) + t = t.Add(-d) + case "end": + d, e := time.ParseDuration(fmt.Sprintf("%dh%dm%ds%dns", t.Hour(), t.Minute(), t.Second(), t.Nanosecond())) + m.Assert(e) + t = t.Add(time.Duration(24*time.Hour) - d) + if m.Confx("time_interval") == "close" { + t = t.Add(-time.Second) + } + case "yestoday": + t = t.Add(-time.Duration(24 * time.Hour)) + case "tomorrow": + t = t.Add(time.Duration(24 * time.Hour)) + case "monday": + d, e := time.ParseDuration(fmt.Sprintf("%dh%dm%ds", int((t.Weekday()-time.Monday+7)%7)*24+t.Hour(), t.Minute(), t.Second())) + m.Assert(e) + t = t.Add(-d) + case "sunday": + d, e := time.ParseDuration(fmt.Sprintf("%dh%dm%ds", int((t.Weekday()-time.Monday+7)%7)*24+t.Hour(), t.Minute(), t.Second())) + m.Assert(e) + t = t.Add(time.Duration(7*24*time.Hour) - d) + if m.Confx("time_interval") == "close" { + t = t.Add(-time.Second) + } + case "first": + t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local) + case "last": + month, year := t.Month()+1, t.Year() + if month >= 13 { + month, year = 1, year+1 + } + t = time.Date(year, month, 1, 0, 0, 0, 0, time.Local) + if m.Confx("time_interval") == "close" { + t = t.Add(-time.Second) + } + case "origin": + t = time.Date(t.Year(), 1, 1, 0, 0, 0, 0, time.Local) + case "final": + t = time.Date(t.Year()+1, 1, 1, 0, 0, 0, 0, time.Local) + if m.Confx("time_interval") == "close" { + t = t.Add(-time.Second) + } + } + } + if m.Options("parse") || !m.Options("time_format") { m.Echo("%d000", t.Unix()) } else { diff --git a/src/contexts/ctx.go b/src/contexts/ctx.go index 9d11473d..c90e9302 100644 --- a/src/contexts/ctx.go +++ b/src/contexts/ctx.go @@ -1124,7 +1124,10 @@ func (m *Message) Echo(str string, arg ...interface{}) *Message { // {{{ // }}} func (m *Message) Color(color int, str string, arg ...interface{}) *Message { // {{{ - return m.Add("result", fmt.Sprintf("\033[%dm%s\033[0m", color, fmt.Sprintf(str, arg...))) + if str = fmt.Sprintf(str, arg...); m.Options("terminal_color") { + str = fmt.Sprintf("\033[%dm%s\033[0m", color, str) + } + return m.Add("result", str) } // }}} @@ -2393,8 +2396,16 @@ var Index = &Context{Name: "ctx", Help: "模块中心", return } - sub := msg.Spawn().Cmd(arg) - m.Copy(sub, "result").Copy(sub, "append") + switch arg[0] { + case "message": + for msg := m; msg != nil; msg = msg.message { + m.Echo("%d: %s\n", msg.code, msg.Format()) + } + default: + sub := msg.Spawn().Cmd(arg) + m.Copy(sub, "result").Copy(sub, "append") + } + // }}} }}, "detail": &Command{Name: "detail [index] [value...]", Help: "查看或添加参数", Hand: func(m *Message, c *Context, key string, arg ...string) { @@ -2657,7 +2668,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心", if len(arg) > 1 { which = arg[1] } - m.Log("fuck", nil, "what %s", which) switch which { case "cache": for k, v := range msg.target.Caches { @@ -3306,6 +3316,7 @@ func Start(args ...string) { } Pulse.Options("log", true) + Pulse.Options("terminal_color", true) Pulse.Sesss("log", "log").Conf("bench.log", Pulse.Conf("bench.log")) for _, m := range Pulse.Search(Pulse.Conf("start")) { diff --git a/src/contexts/mdb/mdb.go b/src/contexts/mdb/mdb.go index 5ddf8885..71769e99 100644 --- a/src/contexts/mdb/mdb.go +++ b/src/contexts/mdb/mdb.go @@ -259,11 +259,23 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", where := m.Confx("where", m.Option("where"), "where %s") group := m.Confx("group", m.Option("group"), "group by %s") - order := m.Confx("order", m.Option("order"), "order by %s") + order := m.Confx("order", m.Option("orders"), "order by %s") limit := m.Confx("limit", m.Option("limit"), "limit %s") offset := m.Confx("offset", m.Option("offset"), "offset %s") - msg := m.Spawn().Cmd("query", fmt.Sprintf("select %s from %s %s %s %s %s %s", field, table, where, group, order, limit, offset), m.Meta["other"]) + other := m.Meta["other"] + for i, v := range other { + if len(v) > 1 { + switch v[0] { + case '$': + other[i] = m.Cap(v[1:]) + case '@': + other[i] = m.Conf(v[1:]) + } + } + } + + msg := m.Spawn().Cmd("query", fmt.Sprintf("select %s from %s %s %s %s %s %s", field, table, where, group, order, limit, offset), other) if m.Optioni("query", msg.Code()); !m.Options("save") { m.Color(31, table).Echo(" %s %s %s %s %s %v\n", where, group, order, limit, offset, m.Meta["other"]) } diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 23f48934..809fb079 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -91,6 +91,7 @@ func (web *WEB) Merge(m *ctx.Message, uri string, arg ...string) string { // {{{ func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.Context, string, ...string)) { // {{{ web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) { msg := m.Spawn().Set("detail", key) + msg.Option("terminal_color", false) msg.Option("method", r.Method) msg.Option("referer", r.Header.Get("Referer")) @@ -506,12 +507,18 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", // }}} }}, "/travel": &ctx.Command{Name: "/travel", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - if !m.Options("module") { //{{{ + // r := m.Optionv("request").(*http.Request) // {{{ + // w := m.Optionv("response").(http.ResponseWriter) + + if !m.Options("module") { m.Option("module", "ctx") } - r := m.Data["request"].(*http.Request) - w := m.Data["response"].(http.ResponseWriter) + check := m.Spawn().Cmd("/share", "/travel", "module", m.Option("module")) + if !check.Results(0) { + m.Copy(check, "append") + return + } // 权限检查 if m.Option("method") == "POST" { @@ -560,11 +567,8 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } // 解析模板 - render := m.Spawn(m.Target()).Put("option", "request", r).Put("option", "response", w) - defer render.Cmd(m.Conf("travel_main"), m.Conf("travel_tmpl")) - + m.Set("append", "tmpl", "userinfo", "share") if msg := m.Find(m.Option("module"), true); msg != nil { - m.Option("tmpl", "") for _, v := range []string{"cache", "config", "command", "module", "domain"} { if m.Options("domain") { msg = m.Find("ssh", true) @@ -574,17 +578,19 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", return nil }) } else { - msg = msg.Spawn(msg.Target()) - msg.Cmd("context", "find", m.Option("module"), v) + msg = msg.Spawn() + msg.Cmd("context", "find", msg.Cap("module"), "list", v) } if len(msg.Meta["append"]) > 0 { - render.Option("current_module", m.Option("module")) - render.Option("current_domain", m.Option("domain")) - render.Sesss(v, msg).Add("option", "tmpl", v) + msg.Option("current_module", m.Option("module")) + msg.Option("current_domain", m.Option("domain")) + m.Add("option", "tmpl", v) + m.Sesss(v, msg) } } } + m.Append("template", m.Conf("travel_main"), m.Conf("travel_tmpl")) // }}} }}, "/upload": &ctx.Command{Name: "/upload", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { @@ -595,7 +601,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", m.Option("dir", m.Cap("directory")) } - check := m.Spawn().Cmd("/share") + check := m.Spawn().Cmd("/share", "/upload", "dir", m.Option("dir")) if !check.Results(0) { m.Copy(check, "append") return @@ -753,7 +759,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", // }}} }}, "/share": &ctx.Command{Name: "/share", Help: "资源共享", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - check := m.Spawn().Cmd("/check", "command", "/share", "dir", m.Option("dir")) // {{{ + check := m.Spawn().Cmd("/check", "command", arg[0], arg[1], arg[2]) // {{{ if !check.Results(0) { m.Copy(check, "append") return @@ -761,10 +767,10 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", msg := check.Appendv("aaa").(*ctx.Message).Spawn(m.Target()) if m.Options("shareto") { - msg.Cmd("right", "add", m.Option("shareto"), "command", "/share", "dir", m.Option("dir")) + msg.Cmd("right", "add", m.Option("shareto"), "command", arg[0], arg[1], arg[2]) } if m.Options("notshareto") { - msg.Cmd("right", "del", m.Option("notshareto"), "command", "/share", "dir", m.Option("dir")) + msg.Cmd("right", "del", m.Option("notshareto"), "command", arg[0], arg[1], arg[2]) } m.Echo("ok") // }}} diff --git a/usr/template/travel.html b/usr/template/travel.html index 0c450893..2d769027 100644 --- a/usr/template/travel.html +++ b/usr/template/travel.html @@ -90,6 +90,14 @@ {{end}} + {{end}} {{define "command"}} @@ -119,9 +127,48 @@
-

-
+
+
+ {{end}} {{define "module"}} @@ -183,51 +230,6 @@ {{define "tail"}} {{end}} @@ -251,7 +250,13 @@ {{$meta := .Meta}} {{$sess := $msg.Sessions}} {{range .Meta.tmpl}} - {{if eq . "cache"}} + {{if eq . "login"}} + {{template "login" $meta}} + {{else if eq . "userinfo"}} + {{template "userinfo" $msg}} + {{else if eq . "share"}} + {{template "share" $sess.share}} + {{else if eq . "cache"}} {{template "cache" $sess.cache.Meta}} {{else if eq . "config"}} {{template "config" $sess.config.Meta}}