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

add nfs.template

This commit is contained in:
shaoying 2019-09-29 17:43:34 +08:00
parent a58ece0f72
commit dd200672cb
11 changed files with 103 additions and 52 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
error.log
usr/publish
var/ var/
pkg/ pkg/
*.swp *.swp

View File

@ -9,7 +9,6 @@ import (
"encoding/csv" "encoding/csv"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"os/user" "os/user"
@ -163,8 +162,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
"project": &ctx.Config{Name: "project", Value: map[string]interface{}{ "project": &ctx.Config{Name: "project", Value: map[string]interface{}{
"github": "https://github.com/shylinux/context", "github": "https://github.com/shylinux/context",
"goproxy": "https://goproxy.cn", "goproxy": "https://goproxy.cn",
"template": map[string]interface{}{
"path": "usr/template",
},
"plugin": map[string]interface{}{ "plugin": map[string]interface{}{
"path": "src/plugin", "list": Template, "path": "src/plugin", "template": "usr/template/plugin",
}, "script": map[string]interface{}{ }, "script": map[string]interface{}{
"path": "usr/script", "path": "usr/script",
}, "trash": map[string]interface{}{ }, "trash": map[string]interface{}{
@ -887,15 +889,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
} }
fallthrough fallthrough
default: default:
// 创建插件 m.Option("name", arg[0])
p := path.Join(m.Conf("project", "plugin.path"), arg[0]) m.Option("help", kit.Select("plugin", arg, 1))
if _, e := os.Stat(p); os.IsNotExist(e) && m.Assert(os.MkdirAll(p, 0777)) { m.Cmdy("nfs.template", path.Join(m.Conf("project", "plugin.path"), arg[0])+"/", path.Join(m.Conf("project", "plugin.template"))+"/")
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")
} }
return return
}}, }},
@ -1228,17 +1224,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
m.Table() m.Table()
return 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 return
}}, }},
}, },

View File

@ -1,8 +1,11 @@
package cli package cli
var version = struct { var version = struct {
init []string
time string time string
host string host string
self int 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,
} }

View File

@ -2,6 +2,8 @@ package ctx
import ( import (
"html/template" "html/template"
"io"
"path"
"strings" "strings"
) )
@ -85,3 +87,9 @@ var CGI = template.FuncMap{
return "" 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)
}

View File

@ -799,6 +799,38 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
return 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) { "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) { if f, p, e := kit.Create(path.Join(m.Conf("dir", "temp"), kit.Hashs(arg[0]))); m.Assert(e) {
defer f.Close() defer f.Close()

View File

@ -206,3 +206,15 @@ func Linex(p string) map[string]string {
}) })
return meta 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)
}
}
}

View File

@ -0,0 +1,3 @@
fieldset.item.{{options . "name"}} div.output {
}

View File

@ -1,19 +1,15 @@
package cli package main
var Template = []interface{}{
map[string]interface{}{"name": "index.go", "text":
`package main
import ( import (
"contexts/cli" "contexts/cli"
"contexts/ctx" "contexts/ctx"
"toolkit" "toolkit"
"fmt" "fmt"
"os" "os"
) )
var Index = &ctx.Context{Name: "test", Help: "测试工具", var Index = &ctx.Context{Name: `{{options . "name"}}`, Help: `{{options . "help"}}`,
Caches: map[string]*ctx.Cache{}, Caches: map[string]*ctx.Cache{},
Configs: map[string]*ctx.Config{ Configs: map[string]*ctx.Config{
"_index": &ctx.Config{Name: "index", Value: []interface{}{ "_index": &ctx.Config{Name: "index", Value: []interface{}{
@ -29,8 +25,8 @@ var Index = &ctx.Context{Name: "test", Help: "测试工具",
}, },
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"demo": {Name: "demo", Help: "demo", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { "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)) m.Echo(kit.Select("hello world", arg, 0))
return return
}}, }},
}, },
} }
@ -38,20 +34,3 @@ var Index = &ctx.Context{Name: "test", Help: "测试工具",
func main() { func main() {
fmt.Print(cli.Index.Plugin(Index, os.Args[1:])) 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":
``},
}

View File

@ -0,0 +1,5 @@
{init: function(run, field, option, output) {
kit.Log("hello world")
return {}
}}

View File

@ -0,0 +1,6 @@
fun hello world "" "" public \
text "" \
button "执行"
copy pwd
end

View File

@ -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"}},
}