From 1e911d2cc6d41f8e25a0145cfb2f3bdebb4bb3a4 Mon Sep 17 00:00:00 2001 From: shaoying Date: Wed, 5 Dec 2018 22:25:37 +0800 Subject: [PATCH] add some Change-Id: I258fc3409e027aa70a2a95ecebe87978ef1e38df --- etc/init.shy | 2 +- src/contexts/cli/cli.go | 9 ++- src/contexts/ctx/ctx.go | 138 ++++++++++++++++++---------------------- src/contexts/nfs/nfs.go | 2 + src/contexts/web/web.go | 23 +++++-- usr/librarys/context.js | 8 ++- 6 files changed, 93 insertions(+), 89 deletions(-) diff --git a/etc/init.shy b/etc/init.shy index 5efc36cc..847f3c0f 100644 --- a/etc/init.shy +++ b/etc/init.shy @@ -14,4 +14,4 @@ source etc/local.shy -~shy +~lark diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index 1dcf289d..3e2f99e5 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -187,6 +187,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", m.Log("info", "import %s", arg[1]) module := msg.Cap("module") for k, _ := range msg.Target().Commands { + if len(k) > 0 && k[0] == '/' { + continue + } if len(arg) == 2 { cli.alias[k] = []string{module + "." + k} continue @@ -649,13 +652,13 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", "run": &ctx.Command{Name: "run", Help: "脚本参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if len(arg) == 0 { name := path.Join(m.Option("dir_root"), m.Option("download_dir")) - msg := m.Sess("nfs").Add("option", "dir_reg", ".*\\.(sh|shy|py)$").Add("option", "dir_root", "").Cmd("dir", name) + msg := m.Sess("nfs").Add("option", "dir_reg", ".*\\.(sh|shy|py)$").Add("option", "dir_root", "").Cmd("dir", name, "dir_deep") m.Copy(msg, "append").Copy(msg, "result") return } - name := path.Join(m.Option("dir_root"), m.Option("download_dir"), arg[0]) - msg := m.Spawn(c).Cmd("cmd", name, arg[1:]) + // name := path.Join(m.Option("dir_root"), m.Option("download_dir"), arg[0]) + msg := m.Spawn(c).Cmd("cmd", arg[0], arg[1:]) m.Copy(msg, "append").Copy(msg, "result") }}, diff --git a/src/contexts/ctx/ctx.go b/src/contexts/ctx/ctx.go index fbf10562..a0e24f17 100644 --- a/src/contexts/ctx/ctx.go +++ b/src/contexts/ctx/ctx.go @@ -2968,9 +2968,27 @@ var Index = &Context{Name: "ctx", Help: "模块中心", }) } }}, - "config": &Command{Name: "config [all] [export key..] [save|load file key...] [create map|list|string key name help] [delete key]", + "config": &Command{Name: "config [all] [export key..] [save|load file key...] [list|map arg...] [create map|list|string key name help] [delete key]", Help: "配置管理, export: 导出配置, save: 保存配置到文件, load: 从文件加载配置, create: 创建配置, delete: 删除配置", Hand: func(m *Message, c *Context, key string, arg ...string) { + if len(arg) > 2 && arg[2] == "list" { + chain := strings.Split(arg[1], ".") + chain = append(chain, "-2") + + for _, val := range arg[3:] { + m.Confv(arg[0], chain, val) + } + return + } + if len(arg) > 2 && arg[2] == "map" { + chain := strings.Split(arg[1], ".") + + for i := 3; i < len(arg)-1; i += 2 { + m.Confv(arg[0], append(chain, arg[i]), arg[i+1]) + } + return + } + all := false if len(arg) > 0 && arg[0] == "all" { arg, all = arg[1:], true @@ -3053,15 +3071,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心", switch action { case "save": - // f, e := os.Create(which) - // m.Assert(e) - // defer f.Close() - // buf, e := json.MarshalIndent(save, "", " ") m.Assert(e) m.Sess("nfs").Add("option", "data", string(buf)).Cmd("save", which) - - // f.Write(buf) case "export": buf, e := json.MarshalIndent(save, "", " ") m.Assert(e) @@ -3079,68 +3091,10 @@ var Index = &Context{Name: "ctx", Help: "模块中心", value = m.Confv(arg[0]) } - switch val := value.(type) { - case map[string]interface{}: - if true { - for k, v := range val { - m.Add("append", k, v) - } - sort.Strings(m.Meta["append"]) - m.Table() - } else { - for k, v := range val { - m.Add("append", "key", k) - m.Add("append", "value", v) - } - m.Sort("key", "str").Table() - } - case map[string]string: - for k, v := range val { - m.Add("append", k, v) - } - sort.Strings(m.Meta["append"]) - m.Table() - // for k, v := range val { - // m.Add("append", "key", k) - // m.Add("append", "value", v) - // } - // m.Sort("key", "str").Table() - case []interface{}: - for i, v := range val { - switch value := v.(type) { - case map[string]interface{}: - for k, v := range value { - switch val := v.(type) { - case string: - m.Add("append", k, val) - case float64: - m.Add("append", k, int(val)) - default: - b, _ := json.Marshal(val) - m.Add("append", k, string(b)) - } - } - sort.Strings(m.Meta["append"]) - case map[string]string: - for k, v := range value { - m.Add("append", k, v) - } - sort.Strings(m.Meta["append"]) - default: - m.Add("append", "index", i) - m.Add("append", "value", v) - } - } - m.Table() - case []string: - for i, v := range val { - m.Add("append", "index", i) - m.Add("append", "value", v) - } - m.Table() - default: - m.Echo("%v", value) - } + msg := m.Spawn() + msg.Put("option", "_cache", value) + msg.Cmd("trans", "_cache", "") + m.Copy(msg, "append").Copy(msg, "result") }}, "cache": &Command{Name: "cache [all] |key [value]|key = value|key name value help|delete key]", Help: "查看、读写、赋值、新建、删除缓存变量", @@ -3257,26 +3211,58 @@ var Index = &Context{Name: "ctx", Help: "模块中心", // 解析 nrow := len(m.Meta[m.Meta["append"][0]]) + keys := []string{} for i := 0; i < nrow; i++ { for j := 0; j < len(m.Meta["parse"]); j += 2 { var value interface{} - e := json.Unmarshal([]byte(m.Meta[m.Meta["parse"][j]][i]), &value) + json.Unmarshal([]byte(m.Meta[m.Meta["parse"][j]][i]), &value) + if m.Meta["parse"][j+1] != "" { + value = Chain(m, value, m.Meta["parse"][j+1]) + } + + switch val := value.(type) { + case map[string]interface{}: + for k, _ := range val { + keys = append(keys, k) + } + default: + keys = append(keys, m.Meta["parse"][j+1]) + } + } + } + for i := 0; i < nrow; i++ { + for _, k := range keys { + m.Add("append", k, "") + } + } + for i := 0; i < nrow; i++ { + for j := 0; j < len(m.Meta["parse"]); j += 2 { + var value interface{} + json.Unmarshal([]byte(m.Meta[m.Meta["parse"][j]][i]), &value) if m.Meta["parse"][j+1] != "" { value = Chain(m, value, m.Meta["parse"][j+1]) } - m.Log("fuck", "info %v %v %v %v %T", e, m.Meta["parse"][j], m.Meta["parse"][j+1], value, value) switch val := value.(type) { case map[string]interface{}: for k, v := range val { - m.Add("append", k, v) + switch val := v.(type) { + case string: + m.Meta[k][i] = val + case float64: + m.Meta[k][i] = fmt.Sprintf("%d", int(val)) + default: + b, _ := json.Marshal(val) + m.Meta[k][i] = string(b) + } } + case string: + m.Meta[m.Meta["parse"][j+1]][i] = val case float64: - m.Add("append", m.Meta["parse"][j+1], fmt.Sprintf("%d", int(val))) - case nil: - m.Add("append", m.Meta["parse"][j+1], "") + m.Meta[m.Meta["parse"][j+1]][i] = fmt.Sprintf("%d", int(val)) default: - m.Add("append", m.Meta["parse"][j+1], fmt.Sprintf("%v", val)) + b, _ := json.Marshal(val) + m.Meta[m.Meta["parse"][j+1]][i] = string(b) } } } diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index 0b2439e6..4ce2a54a 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -1016,6 +1016,8 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", } if p, f, e := open(m, arg[0], os.O_WRONLY|os.O_CREATE|os.O_TRUNC); m.Assert(e) { defer f.Close() + m.Append("directory", p) + m.Echo(p) for _, v := range arg[1:] { n, e := fmt.Fprint(f, v) diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 8aa67fd5..488c3fc9 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -121,6 +121,8 @@ func (web *WEB) HandleCmd(m *ctx.Message, key string, cmd *ctx.Command) { if msg.Confs("cas_url") { if !cas.IsAuthenticated(r) && !msg.Confs("skip_cas") { r.URL.Path = r.Header.Get("index_path") + w.Header().Add("Access-Control-Allow-Origin", "*") + w.Header().Add("Vary", "*") cas.RedirectToLogin(w, r) return } @@ -395,7 +397,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", }}, "get": &ctx.Command{Name: "get [method GET|POST] url arg...", Help: "访问服务, method: 请求方法, url: 请求地址, arg: 请求参数", - Form: map[string]int{"method": 1, "headers": 2, "content_type": 1, "body": 1, "path_value": 1, "body_response": 1, "parse": 1}, + Form: map[string]int{"method": 1, "headers": 2, "content_type": 1, "body": 1, "path_value": 1, "body_response": 1, "parse": 1, "save": 1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if web, ok := m.Target().Server.(*WEB); m.Assert(ok) { if m.Has("path_value") { @@ -472,12 +474,18 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", }() if m.Has("save") { - p := path.Join(m.Sess("nfs").Cmd("pwd").Result(0), m.Option("save")) + + p := m.Option("save") + if !strings.Contains(m.Option("save"), "/") { + p = path.Join(m.Sess("nfs").Cmd("pwd").Result(0), m.Option("save")) + } + f, e := os.Create(p) m.Assert(e) io.Copy(f, res.Body) defer f.Close() - m.Log("info", "save file %s", p) + m.Log("info", "save file %s %s", p, m.Sess("aaa").Cmd("md5", p).Result(0)) + m.Echo(p) return } @@ -492,7 +500,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", m.Copy(msg, "append").Copy(msg, "result") return } - b, _ := json.Marshal(result) + b, _ := json.MarshalIndent(result, "", " ") result = string(b) case strings.HasPrefix(ct, "text/html"): html, e := goquery.NewDocumentFromReader(res.Body) @@ -848,7 +856,9 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", "/download/": &ctx.Command{Name: "/download/", 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) - http.ServeFile(w, r, m.Sess("nfs").Cmd("path", strings.TrimPrefix(m.Option("path"), "/download/")).Result(0)) + p := m.Sess("nfs").Cmd("path", strings.TrimPrefix(m.Option("path"), "/download/")).Result(0) + m.Log("info", "download %s %s", p, m.Sess("aaa").Cmd("md5", p).Result(0)) + http.ServeFile(w, r, p) }}, "/render": &ctx.Command{Name: "/render template", Help: "渲染模板, template: 模板名称", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if web, ok := m.Target().Server.(*WEB); m.Assert(ok) { @@ -897,7 +907,8 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } if !right { - if lark := m.Find("web.lark"); lark != nil && m.Confs("login_lark") { + lark := m.Find("web.lark") + if lark != nil && m.Confs("login_lark") { right = ctx.Right(lark.Cmd("auth", m.Option("username"), "check", m.Option("cmd")).Result(0)) } } diff --git a/usr/librarys/context.js b/usr/librarys/context.js index 90116105..83293494 100644 --- a/usr/librarys/context.js +++ b/usr/librarys/context.js @@ -93,10 +93,12 @@ context = { } var arg = args.join("&"); - arg && (url += ((url.indexOf("?")>-1)? "&": "?") + arg) + // arg && (url += ((url.indexOf("?")>-1)? "&": "?") + arg) var xhr = new XMLHttpRequest(); - xhr.open("GET", url); + // xhr.open("POST", url); + xhr.open("POST", url); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") xhr.setRequestHeader("Accept", "application/json") xhr.onreadystatechange = function() { @@ -122,7 +124,7 @@ context = { } typeof cb == "function" && cb(msg) } - xhr.send(); + xhr.send(arg); }, }