1
0
mirror of https://shylinux.com/x/ContextOS synced 2025-04-25 16:58:06 +08:00

tce add cli.time.monday

This commit is contained in:
shaoying 2018-07-08 21:21:06 +08:00
parent 9f95a85249
commit 53abac7812
5 changed files with 169 additions and 81 deletions

View File

@ -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 {

View File

@ -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")) {

View File

@ -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"])
}

View File

@ -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")
// }}}

View File

@ -90,6 +90,14 @@
{{end}}
</table>
</fieldset>
<script>
function Config(event, name) {
if (event.keyCode == 13) {
ctx.Conf(name, event.currentTarget.value, function(msg) {ctx.Refresh()});
}
}
</script>
{{end}}
{{define "command"}}
@ -119,9 +127,48 @@
</table>
</fieldset>
<fieldset><legend><input id="direct" type="text" onkeydown="return Command(event)"></legend>
<pre class="result"><code id="result">
</code></pre>
<pre class="result"><code id="result"></code></pre>
<table id="append"></table>
</fieldset>
<script>
function Result(msg) {
var result = document.getElementById("result");
result.innerHTML = msg.result.join("");
var append = document.getElementById("append");
var tr = document.createElement("tr");
append.appendChild(tr)
for (var k in msg.append) {
var th = document.createElement("th");
th.innerHTML = k;
}
}
function Command(event, name) {
if (event.keyCode == 13) {
var direct = document.getElementById("direct");
if (name) {
direct.value = name;
ctx.Cmd(name, event.currentTarget.value, Result);
} else {
ctx.Cmd(event.currentTarget.value, "", Result);
}
}
}
function Direct(event, name, value) {
console.log(event)
if (!name) {
name = event.currentTarget.value;
}
var direct = document.getElementById("direct");
direct.value = name;
ctx.Cmd(name, value, Result);
return false;
}
</script>
{{end}}
{{define "module"}}
@ -183,51 +230,6 @@
{{define "tail"}}
<script src="library/context.js"></script>
<script>
function Refresh() {
location.assign(location.href);
}
function Config(event, name) {
if (event.keyCode == 13) {
ctx.Conf(name, event.currentTarget.value, function(msg) {Refresh()});
}
}
function Result(msg) {
var result = document.getElementById("result");
result.innerHTML = msg.result.join("");
}
function Command(event, name) {
if (event.keyCode == 13) {
var direct = document.getElementById("direct");
if (name) {
direct.value = name;
ctx.Cmd(name, event.currentTarget.value, Result);
} else {
ctx.Cmd(event.currentTarget.value, "", Result);
}
}
}
function Direct(event, name, value) {
console.log(event)
if (!name) {
name = event.currentTarget.value;
}
var direct = document.getElementById("direct");
direct.value = name;
ctx.Cmd(name, value, Result);
return false;
}
function Input(event, cb, key) {
if (event.keyCode == 13) {
cb(key, event.target.value);
}
}
function Change(event, key) {
if (event.keyCode == 13) {
ctx.Search(key, event.target.value);
@ -239,9 +241,6 @@
ctx.Cookie("order", ctx.Cookie("order")*1 + 1);
location.assign(location.href);
}
function show(name) {
ctx.Cap(name, function(msg) {console.log(msg)});
}
</script>
</body>
{{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}}