From da6859c5e0e2205a5d9245cc668cb73ff0e911c2 Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 14 Aug 2018 09:27:01 +0800 Subject: [PATCH] mac add web.index --- src/contexts/cli/cli.go | 8 ++++ src/contexts/ctx.go | 87 +++++++++++++++++++++++++++++++++++++++- src/contexts/log/log.go | 24 ++++++----- src/contexts/nfs/nfs.go | 1 + src/contexts/web/web.go | 48 ++++++++++++++++++++++ usr/template/upload.html | 53 +++++++++++++++--------- 6 files changed, 189 insertions(+), 32 deletions(-) diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index 0852c197..316a2866 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -208,6 +208,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", "time_format": &ctx.Config{Name: "time_format", Value: "2006-01-02 15:04:05", Help: "时间格式"}, "time_unit": &ctx.Config{Name: "time_unit", Value: "1000", Help: "时间倍数"}, "time_interval": &ctx.Config{Name: "time_interval(open/close)", Value: "open", Help: "时间区间"}, + + "tmux_default": &ctx.Config{Name: "tmux_default", Value: map[string]interface{}{ + "session": []interface{}{"list-sessions"}, + "buffer": []interface{}{"show-buffer"}, + }, Help: "时间区间"}, }, Commands: map[string]*ctx.Command{ "source": &ctx.Command{ @@ -809,6 +814,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", "clear": &ctx.Command{Name: "clear", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { m.Log("fuck", strings.Repeat("\n", 20)) }}, + "tmux": &ctx.Command{Name: "tmux", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Copy(m.Spawn().Cmd("system", "tmux", arg), "result") + }}, }, Index: map[string]*ctx.Context{ "void": &ctx.Context{Caches: map[string]*ctx.Cache{"nshell": &ctx.Cache{}}}, diff --git a/src/contexts/ctx.go b/src/contexts/ctx.go index 6e747c3b..fee70e76 100644 --- a/src/contexts/ctx.go +++ b/src/contexts/ctx.go @@ -43,6 +43,17 @@ func Trans(arg ...interface{}) []string { // {{{ ls = append(ls, fmt.Sprintf("%t", val)) case int, int8, int16, int32, int64: ls = append(ls, fmt.Sprintf("%d", val)) + case []interface{}: + for _, v := range val { + switch val := v.(type) { + case string: + ls = append(ls, val) + case bool: + ls = append(ls, fmt.Sprintf("%t", val)) + case int, int8, int16, int32, int64: + ls = append(ls, fmt.Sprintf("%d", val)) + } + } case []string: ls = append(ls, val...) case []bool: @@ -1572,6 +1583,73 @@ func (m *Message) Confi(key string, arg ...interface{}) int { // {{{ return n } +// }}} +func (m *Message) Confv(key string, args ...interface{}) interface{} { // {{{ + var hand func(m *Message, x *Config, arg ...string) string + arg := Trans(args...) + + for _, c := range []*Context{m.target, m.source} { + for s := c; s != nil; s = s.context { + if x, ok := s.Configs[key]; ok { + if len(args) == 0 { + return x.Value + } + if len(arg) == 3 { + hand = x.Hand + } + + switch value := x.Value.(type) { + case string: + x.Value = fmt.Sprintf("%v", value) + case bool: + x.Value = Right(fmt.Sprintf("%v", value)) + case int: + i, e := strconv.Atoi(fmt.Sprintf("%v", value)) + m.Assert(e) + x.Value = i + case nil: + x.Value = args[0] + default: + for i := 0; i < len(args); i += 2 { + if i < len(args)-1 { + x.Value = Chain(x.Value, args[i], args[i+1]) + } + if i == len(args)-2 { + return Chain(x.Value, args[len(args)-2]) + } + if i == len(args)-1 { + return Chain(x.Value, args[len(args)-1]) + } + } + } + } + } + } + + if len(args) == 0 { + return nil + } + + m.Log("conf", "%s %v", key, args) + if m.target.Configs == nil { + m.target.Configs = make(map[string]*Config) + } + if len(arg) == 3 { + m.target.Configs[key] = &Config{Name: arg[0], Value: arg[1], Help: arg[2], Hand: hand} + return m.Conf(key, arg[1]) + } + if !m.Confs("auto_make") { + return nil + } + + if len(arg) == 1 { + m.target.Configs[key] = &Config{Name: key, Value: arg[0], Help: "auto make", Hand: hand} + return m.Conf(key, arg[0]) + } + m.target.Configs[key] = &Config{Name: key, Value: Chain(nil, args), Help: "auto make", Hand: hand} + return Chain(key, args[len(args)-2+(len(args)%2)]) +} + // }}} func (m *Message) Conf(key string, args ...interface{}) string { // {{{ var hand func(m *Message, x *Config, arg ...string) string @@ -1610,6 +1688,8 @@ func (m *Message) Conf(key string, args ...interface{}) string { // {{{ hand = x.Hand } } + case bool: + case int: default: values := "" for i := 0; i < len(args); i += 2 { @@ -2037,6 +2117,9 @@ var CGI = template.FuncMap{ } return "" }, // }}} + "unscaped": func(str string) interface{} { // {{{ + return template.HTML(str) + }, // }}} } var Pulse = &Message{code: 0, time: time.Now(), source: Index, target: Index, Meta: map[string][]string{}} @@ -2868,8 +2951,8 @@ var Index = &Context{Name: "ctx", Help: "模块中心", save[k] = v.Value } case "load": - if len(have) == 0 || have[k] { - v.Value = save[k] + if x, ok := save[k]; ok && (len(have) == 0 || have[k]) { + v.Value = x } case "pop": switch val := v.Value.(type) { diff --git a/src/contexts/log/log.go b/src/contexts/log/log.go index 2bd79f93..1a64b4ec 100644 --- a/src/contexts/log/log.go +++ b/src/contexts/log/log.go @@ -54,23 +54,27 @@ func (log *LOG) Close(m *ctx.Message, arg ...string) bool { // {{{ var Pulse *ctx.Message var Index = &ctx.Context{Name: "log", Help: "日志中心", - Caches: map[string]*ctx.Cache{}, + Caches: map[string]*ctx.Cache{ + "nlog": &ctx.Cache{Name: "nlog", Value: "0", Help: "日志屏蔽类型"}, + }, Configs: map[string]*ctx.Config{ + "silent": &ctx.Config{Name: "silent", Value: map[string]interface{}{}, Help: "日志屏蔽类型"}, "module": &ctx.Config{Name: "module", Value: map[string]interface{}{ - "shy": map[string]interface{}{"true": true, "false": false, "one": 1, "zero": 0, "yes": "yes", "no": "no"}, "log": map[string]interface{}{"cmd": true}, "lex": map[string]interface{}{"cmd": true, "debug": true}, "yac": map[string]interface{}{"cmd": true, "debug": true}, - }, Help: "模块日志输出的文件"}, - "silent": &ctx.Config{Name: "silent", Value: map[string]string{}, Help: "模块日志输出的文件"}, - "color": &ctx.Config{Name: "color", Value: map[string]string{ - "debug": "0", "error": "31", "check": "31", - "cmd": "32", "conf": "33", - "search": "35", "find": "35", "cb": "35", "lock": "35", - "begin": "36", "start": "36", "close": "36", - }, Help: "模块日志输出颜色"}, + }, Help: "日志屏蔽模块"}, + "color": &ctx.Config{Name: "color", Value: map[string]interface{}{ + "debug": 0, "error": 31, "check": 31, + "cmd": 32, "conf": 33, + "search": 35, "find": 35, "cb": 35, "lock": 35, + "begin": 36, "start": 36, "close": 36, + }, Help: "日志输出颜色"}, + "log_name": &ctx.Config{Name: "log_name", Value: "dump", Help: "日志屏蔽类型"}, }, Commands: map[string]*ctx.Command{ + "open": &ctx.Command{Name: "open file", Help: "打开日志文件", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + }}, "log": &ctx.Command{Name: "log level string...", Help: "输出日志, level: 日志类型, string: 日志内容", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if log, ok := m.Target().Server.(*LOG); m.Assert(ok) && log.out != nil { // {{{ if m.Confs("silent", arg[0]) { diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index bc2f783d..42f5b291 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -1269,6 +1269,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", m.Log("info", "cmd: %s %v", "git", ctx.Trans("-C", p, c, args)) msg := m.Sess("cli").Cmd("system", "git", "-C", p, c, args) m.Copy(msg, "result").Copy(msg, "append") + m.Echo("\n") } } // }}} }}, diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 97a55e03..53d6397a 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -279,6 +279,25 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", }, Configs: map[string]*ctx.Config{ "cmd": &ctx.Config{Name: "cmd", Value: "tmux", Help: "路由数量"}, + "index": &ctx.Config{Name: "index", Value: map[string]interface{}{ + "shy": []interface{}{ + map[string]interface{}{ + "module": "cli", "command": "system", + "argument": []interface{}{"tmux", "list-clients"}, + "template": "result", "title": "client", + }, + map[string]interface{}{ + "module": "cli", "command": "system", + "argument": []interface{}{"tmux", "list-sessions"}, + "template": "result", "title": "session", + }, + map[string]interface{}{ + "module": "cli", "command": "system", + "argument": []interface{}{"tmux", "show-buffer"}, + "template": "result", "title": "buffer", + }, + }, + }, Help: "资源列表"}, }, Commands: map[string]*ctx.Command{ "client": &ctx.Command{ @@ -544,6 +563,35 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", m.Copy(msg, "result") // }}} }}, + "/index": &ctx.Command{Name: "/index", Help: "网页门户", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + w := m.Optionv("response").(http.ResponseWriter) + w.Header().Add("Content-Type", "text/html") + + tpl := template.New("render").Funcs(ctx.CGI) + tpl = template.Must(tpl.ParseGlob(path.Join(m.Conf("template_dir"), m.Conf("common_tmpl")))) + tpl = template.Must(tpl.ParseGlob(path.Join(m.Conf("template_dir"), m.Conf("upload_tmpl")))) + + replace := [][]byte{ + []byte{27, 91, 51, 50, 109}, []byte(""), + []byte{27, 91, 51, 49, 109}, []byte(""), + []byte{27, 91, 109}, []byte(""), + } + + list := m.Confv("index", "shy") + for _, v := range list.([]interface{}) { + val := v.(map[string]interface{}) + msg := m.Find(val["module"].(string)).Cmd(val["command"], val["argument"]) + for i, v := range msg.Meta["result"] { + b := []byte(v) + for i := 0; i < len(replace)-1; i += 2 { + b = bytes.Replace(b, replace[i], replace[i+1], -1) + } + msg.Meta["result"][i] = string(b) + } + msg.Option("title", val["title"]) + m.Assert(tpl.ExecuteTemplate(w, val["template"].(string), msg.Meta)) + } + }}, "/travel": &ctx.Command{Name: "/travel", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { // r := m.Optionv("request").(*http.Request) // {{{ // w := m.Optionv("response").(http.ResponseWriter) diff --git a/usr/template/upload.html b/usr/template/upload.html index f12c1d50..63a5b734 100644 --- a/usr/template/upload.html +++ b/usr/template/upload.html @@ -78,34 +78,44 @@ {{define "git"}}
branch -
{{index . "branch"}}
+
{{index . "result"}}
status -
{{index . "status"}}
+
{{index . "result"}}
{{end}} {{define "tmux"}} -
sessions -
{{meta . "sessions"}}
+{{$meta := .}} +{{range $index, $value := index $meta "append"}} +
{{$value}} + {{if eq $value "buffer"}} + + + + + {{else}} +
{{meta $meta $value}}
+ {{end}}
-
buffer - - - +{{end}} +{{end}} + +{{define "result"}} +
{{meta . "title"}} +
{{meta . "result"|unscaped}}
- {{end}} {{define "upload"}} @@ -132,6 +142,8 @@ {{$meta := .Meta}} {{$sess := .Sessions}} {{range .Meta.tmpl}} + {{$tmpl := index $sess .}} + {{if eq . "login"}} {{template "login" $meta}} {{else if eq . "userinfo"}} @@ -150,6 +162,7 @@ {{template "upload" $msg}} {{else if eq . "create"}} {{template "create" $msg}} + {{else}} {{end}} {{end}} {{end}}