From dd200672cb4a8b4f0ca93c8b1da6898607d799cb Mon Sep 17 00:00:00 2001 From: shaoying Date: Sun, 29 Sep 2019 17:43:34 +0800 Subject: [PATCH] add nfs.template --- .gitignore | 2 + src/contexts/cli/cli.go | 32 ++++++--------- src/contexts/cli/version.go | 5 ++- src/contexts/ctx/cgi.go | 8 ++++ src/contexts/nfs/nfs.go | 32 +++++++++++++++ src/toolkit/misc.go | 12 ++++++ usr/template/plugin/index.css | 3 ++ .../template/plugin/index.go | 39 +++++-------------- usr/template/plugin/index.js | 5 +++ usr/template/plugin/index.shy | 6 +++ usr/template/version/version.go | 11 ++++++ 11 files changed, 103 insertions(+), 52 deletions(-) create mode 100644 usr/template/plugin/index.css rename src/contexts/cli/template.go => usr/template/plugin/index.go (50%) create mode 100644 usr/template/plugin/index.js create mode 100644 usr/template/plugin/index.shy create mode 100644 usr/template/version/version.go diff --git a/.gitignore b/.gitignore index 9c1bcae5..c1789bff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +error.log +usr/publish var/ pkg/ *.swp diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index d24dbc04..47b215d7 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -9,7 +9,6 @@ import ( "encoding/csv" "encoding/json" "fmt" - "io/ioutil" "os" "os/exec" "os/user" @@ -163,8 +162,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", "project": &ctx.Config{Name: "project", Value: map[string]interface{}{ "github": "https://github.com/shylinux/context", "goproxy": "https://goproxy.cn", + "template": map[string]interface{}{ + "path": "usr/template", + }, "plugin": map[string]interface{}{ - "path": "src/plugin", "list": Template, + "path": "src/plugin", "template": "usr/template/plugin", }, "script": map[string]interface{}{ "path": "usr/script", }, "trash": map[string]interface{}{ @@ -887,15 +889,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", } fallthrough default: - // 创建插件 - p := path.Join(m.Conf("project", "plugin.path"), arg[0]) - if _, e := os.Stat(p); os.IsNotExist(e) && m.Assert(os.MkdirAll(p, 0777)) { - m.Confm("project", "plugin.list", func(index int, value map[string]interface{}) { - ioutil.WriteFile(path.Join(p, kit.Format(value["name"])), []byte(kit.Format(value["text"])), 0666) - }) - } - // 插件列表 - m.Cmdy("nfs.dir", p, "time", "line", "hashs", "path") + m.Option("name", arg[0]) + m.Option("help", kit.Select("plugin", arg, 1)) + m.Cmdy("nfs.template", path.Join(m.Conf("project", "plugin.path"), arg[0])+"/", path.Join(m.Conf("project", "plugin.template"))+"/") } return }}, @@ -1228,17 +1224,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", m.Table() return } - m.Cmd("nfs.save", path.Join(m.Conf("runtime", "boot.ctx_home"), "src/contexts/cli/version.go"), fmt.Sprintf(`package cli -var version = struct { - time string - host string - self int -}{ - "%s", "%s", %d, -} -`, m.Time(), m.Conf("runtime", "node.route"), version.self+1)) - m.Append("directory", "") + m.Option("time", m.Time()) + m.Option("host", m.Conf("runtime", "node.route")) + m.Option("self", version.self+1) + m.Cmdy("nfs.template", "force", path.Join(m.Conf("runtime", "boot.ctx_home"), "src/contexts/cli/version.go"), path.Join(m.Conf("project", "template.path"), "version/")) return }}, }, diff --git a/src/contexts/cli/version.go b/src/contexts/cli/version.go index cf85dbaa..b1598c19 100644 --- a/src/contexts/cli/version.go +++ b/src/contexts/cli/version.go @@ -1,8 +1,11 @@ package cli + var version = struct { + init []string time string host string self int }{ - "2019-09-28 15:18:05", "mac", 611, + []string{"2017-11-01 01:02:03", "2019-07-13 18:02:21"}, + `2019-09-29 17:30:47`, `centos`, 612, } diff --git a/src/contexts/ctx/cgi.go b/src/contexts/ctx/cgi.go index a6737b4c..4d6d7988 100644 --- a/src/contexts/ctx/cgi.go +++ b/src/contexts/ctx/cgi.go @@ -2,6 +2,8 @@ package ctx import ( "html/template" + "io" + "path" "strings" ) @@ -85,3 +87,9 @@ var CGI = template.FuncMap{ return "" }, } + +func ExecuteFile(m *Message, w io.Writer, p string) error { + tmpl := template.New("render").Funcs(CGI) + tmpl.ParseGlob(p) + return tmpl.ExecuteTemplate(w, path.Base(p), m) +} diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index 43b0f548..c4cccd6d 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -799,6 +799,38 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", return }}, + "template": &ctx.Command{Name: "template [force] target source...", Help: "生成模板", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + force := false + if arg[0] == "force" { + force, arg = true, arg[1:] + } + + if _, e := os.Stat(arg[0]); os.IsNotExist(e) || force { + if strings.HasSuffix(arg[0], "/") && m.Assert(os.MkdirAll(arg[0], 0777)) { + + kit.List(arg[1:], func(p string) { + m.Cmd("nfs.dir", p, "name", "path").Table(func(line map[string]string) { + if w, _, e := kit.Create(path.Join(arg[0], line["name"])); m.Assert(e) { + defer w.Close() + + m.Assert(ctx.ExecuteFile(m, w, line["path"])) + } + }) + }) + } else if w, _, e := kit.Create(arg[0]); m.Assert(e) { + defer w.Close() + + kit.List(arg[1:], func(p string) { + m.Cmd("nfs.dir", p).Table(func(line map[string]string) { + m.Assert(ctx.ExecuteFile(m, w, line["path"])) + }) + }) + } + } + + m.Cmdy("nfs.dir", arg[0], "time", "line", "hashs", "path") + return + }}, "temp": &ctx.Command{Name: "temp data", Help: "查找文件路径", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { if f, p, e := kit.Create(path.Join(m.Conf("dir", "temp"), kit.Hashs(arg[0]))); m.Assert(e) { defer f.Close() diff --git a/src/toolkit/misc.go b/src/toolkit/misc.go index cb526cbe..2ac7c69c 100644 --- a/src/toolkit/misc.go +++ b/src/toolkit/misc.go @@ -206,3 +206,15 @@ func Linex(p string) map[string]string { }) return meta } + +func List(arg interface{}, cb interface{}) { + list := Trans(arg) + for i, v := range list { + switch cb := cb.(type) { + case func(string): + cb(v) + case func(string, int): + cb(v, i) + } + } +} diff --git a/usr/template/plugin/index.css b/usr/template/plugin/index.css new file mode 100644 index 00000000..062894fc --- /dev/null +++ b/usr/template/plugin/index.css @@ -0,0 +1,3 @@ +fieldset.item.{{options . "name"}} div.output { +} + diff --git a/src/contexts/cli/template.go b/usr/template/plugin/index.go similarity index 50% rename from src/contexts/cli/template.go rename to usr/template/plugin/index.go index 46d5759a..2e4e5c74 100644 --- a/src/contexts/cli/template.go +++ b/usr/template/plugin/index.go @@ -1,19 +1,15 @@ -package cli - -var Template = []interface{}{ - map[string]interface{}{"name": "index.go", "text": -`package main +package main import ( - "contexts/cli" - "contexts/ctx" - "toolkit" + "contexts/cli" + "contexts/ctx" + "toolkit" - "fmt" - "os" + "fmt" + "os" ) -var Index = &ctx.Context{Name: "test", Help: "测试工具", +var Index = &ctx.Context{Name: `{{options . "name"}}`, Help: `{{options . "help"}}`, Caches: map[string]*ctx.Cache{}, Configs: map[string]*ctx.Config{ "_index": &ctx.Config{Name: "index", Value: []interface{}{ @@ -29,8 +25,8 @@ var Index = &ctx.Context{Name: "test", Help: "测试工具", }, Commands: map[string]*ctx.Command{ "demo": {Name: "demo", Help: "demo", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - m.Echo(kit.Select("hello world", arg, 0)) - return + m.Echo(kit.Select("hello world", arg, 0)) + return }}, }, } @@ -38,20 +34,3 @@ var Index = &ctx.Context{Name: "test", Help: "测试工具", func main() { fmt.Print(cli.Index.Plugin(Index, os.Args[1:])) } -`}, map[string]interface{}{"name": "index.shy", "text": -`fun hello world "" "" public \ - text "" \ - button "执行" - copy pwd -end -`}, map[string]interface{}{"name": "index.css", "text": -`fieldset.item.demo div.output { -} -`}, map[string]interface{}{"name": "index.js", "text": -`{init: function(run, field, option, output) { - kit.Log("hello world") - return {} -}} -`}, map[string]interface{}{"name": "local.shy", "text": -``}, -} diff --git a/usr/template/plugin/index.js b/usr/template/plugin/index.js new file mode 100644 index 00000000..ab13e13f --- /dev/null +++ b/usr/template/plugin/index.js @@ -0,0 +1,5 @@ +{init: function(run, field, option, output) { + kit.Log("hello world") + return {} +}} + diff --git a/usr/template/plugin/index.shy b/usr/template/plugin/index.shy new file mode 100644 index 00000000..95aa8fae --- /dev/null +++ b/usr/template/plugin/index.shy @@ -0,0 +1,6 @@ +fun hello world "" "" public \ + text "" \ + button "执行" + copy pwd + +end diff --git a/usr/template/version/version.go b/usr/template/version/version.go new file mode 100644 index 00000000..8dba4be0 --- /dev/null +++ b/usr/template/version/version.go @@ -0,0 +1,11 @@ +package cli + +var version = struct { + init []string + time string + host string + self int +}{ + []string{"2017-11-01 01:02:03", "2019-07-13 18:02:21"}, + `{{options . "time"}}`, `{{options . "host"}}`, {{options . "self"}}, +}