1
0
forked from x/ContextOS

add nfs.grep

This commit is contained in:
shaoying 2019-06-18 18:18:10 +08:00
parent d5f056f797
commit 1ff338f367
6 changed files with 123 additions and 12 deletions

View File

@ -41,7 +41,11 @@ install() {
&& ./${ctx_app} upgrade ${target} && ${md5} ${ctx_app} \
&& mv ${ctx_app} bin/${ctx_app}
}
hup() {
echo "term hup"
}
main() {
trap HUP hup
while true; do
${ctx_bin} "$@" && break
log "restarting..." && sleep 3

View File

@ -478,6 +478,15 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
// 解析结果
switch m.Option("cmd_parse") {
case "format":
var data interface{}
if json.Unmarshal(out.Bytes(), &data) == nil {
if b, e := json.MarshalIndent(data, "", " "); e == nil {
m.Echo(string(b))
break
}
}
m.Echo(out.String())
case "json":
var data interface{}
if json.Unmarshal(out.Bytes(), &data) == nil {

View File

@ -134,6 +134,14 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
m.Assert(e)
mdb.conn = redis.NewConn(c, time.Second*10, time.Second*10)
default:
if mdb.conn == nil {
m.Echo("not open")
break
}
if mdb.conn.Err() != nil {
m.Echo("%v", mdb.conn.Err())
return
}
args := []interface{}{}
for _, v:=range arg[1:] {
args = append(args, v)

View File

@ -1126,6 +1126,10 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
"dir_type": &ctx.Config{Name: "dir_type(file/dir/both/all)", Value: "both", Help: "dir命令输出的文件类型, file: 只输出普通文件, dir: 只输出目录文件, 否则输出所有文件"},
"dir_fields": &ctx.Config{Name: "dir_fields(time/type/name/size/line/hash)", Value: "time size line filename", Help: "dir命令输出文件名的类型, name: 文件名, tree: 带缩进的文件名, path: 相对路径, full: 绝对路径"},
"grep": &ctx.Config{Name: "grep", Value: map[string]interface{}{
"list": map[string]interface{}{
},
}, Help: "dir命令输出文件名的类型, name: 文件名, tree: 带缩进的文件名, path: 相对路径, full: 绝对路径"},
"git": &ctx.Config{Name: "git", Value: map[string]interface{}{
"args": []interface{}{"-C", "@git_dir"},
"info": map[string]interface{}{"cmds": []interface{}{"log", "status", "branch"}},
@ -1287,6 +1291,73 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
}
return
}},
"grep": &ctx.Command{Name: "grep", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
if len(arg) > 0 {
switch arg[0]{
case "add":
m.Confv("grep", []string{"list", arg[1]}, map[string]interface{}{
"pos": 0,
"offset": 0,
"file": arg[2],
})
return
case "head":
m.Confm("grep", "list", func(key string, value map[string]interface{}) {
if len(arg) == 1 || key == arg[1] {
value["pos"] = 0
value["offset"] = 0
}
})
return
case "tail":
m.Confm("grep", "list", func(key string, value map[string]interface{}) {
if len(arg) == 1 || key == arg[1] {
value["pos"] = -1
value["offset"] = 0
}
})
return
}
}
m.Confm("grep", "list", func(key string, value map[string]interface{}) {
f, e := os.Open(kit.Format(value["file"]))
m.Assert(e)
defer f.Close()
// s, e := f.Stat()
// m.Assert(e)
begin, e := f.Seek(int64(kit.Int(value["pos"])), 0)
if kit.Int(value["pos"]) == -1 {
begin, e = f.Seek(0, 2)
}
m.Assert(e)
n := 0
bio := bufio.NewScanner(f)
for i := 0; i < m.Optioni("page.limit") && bio.Scan(); i++ {
text := bio.Text()
if len(arg) == 0 || strings.Contains(text, arg[0]){
m.Add("append", "key", key)
// m.Add("append", "pos",begin+int64(n))
// m.Add("append", "len",len(text))
// m.Add("append", "end",s.Size())
m.Add("append", "line", kit.Int(value["offset"]))
m.Add("append", "text", text)
} else {
i--
}
n+=len(text)+1
value["offset"] = kit.Int(value["offset"]) + 1
}
value["pos"] = begin + int64(n)
})
m.Table()
return
}},
"hash": &ctx.Command{Name: "hash filename", Help: "查找文件路径", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
dir, name := path.Split(arg[0])

View File

@ -62,16 +62,27 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
map[string]interface{}{"type": "text", "name": "pod", "imports": "plugin_pod"},
map[string]interface{}{"type": "select", "name": "sub", "values": []interface{}{"deploygo", "deploy"}},
map[string]interface{}{"type": "text", "name": "sub", "imports": "plugin_dir"},
map[string]interface{}{"type": "text", "name": "sub", "imports": "plugin_branch"},
map[string]interface{}{"type": "text", "name": "sub", "imports": "plugin_branch", "view": "long"},
map[string]interface{}{"type": "button", "value": "执行"},
},
},
map[string]interface{}{"componet_name": "test", "componet_help": "test",
"componet_tmpl": "componet", "componet_view": "Compile", "componet_init": "",
"componet_type": "private", "componet_ctx": "ssh", "componet_cmd": "_route",
"componet_args": []interface{}{"$$", "context", "cli", "upgrade"}, "inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "pod", "imports": "plugin_pod"},
map[string]interface{}{"type": "select", "name": "action", "values": []interface{}{"script", "portal", "system", "bench"}},
map[string]interface{}{"type": "text", "name": "action", "view": "long"},
map[string]interface{}{"type": "button", "value": "升级"},
},
"display": map[string]interface{}{"show_result": true},
},
map[string]interface{}{"componet_name": "php", "componet_help": "php",
"componet_tmpl": "componet", "componet_view": "Company", "componet_init": "",
"componet_type": "private", "componet_ctx": "ssh", "componet_cmd": "_route",
"componet_args": []interface{}{"$$", "php"}, "inputs": []interface{}{
"componet_args": []interface{}{"$$", "php", "cmd_parse", "format"}, "inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "pod", "imports": "plugin_pod"},
map[string]interface{}{"type": "text", "name": "cmd", "value": "usr/script/test.php"},
map[string]interface{}{"type": "text", "name": "cmd", "value": "usr/script/test.php", "view": "long"},
map[string]interface{}{"type": "button", "value": "执行"},
},
},
@ -79,9 +90,9 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
"componet_tmpl": "componet", "componet_view": "Company", "componet_init": "",
"componet_type": "private", "componet_ctx": "ssh", "componet_cmd": "_route",
"componet_args": []interface{}{"$$", "context", "mdb", "redis"}, "inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "pod"},
map[string]interface{}{"type": "text", "name": "cmd", "values": []interface{}{"get", "del"}},
map[string]interface{}{"type": "text", "name": "cmd", "value": "test"},
map[string]interface{}{"type": "text", "name": "pod", "value": "redis"},
map[string]interface{}{"type": "select", "name": "cmd", "values": []interface{}{"get", "del"}},
map[string]interface{}{"type": "text", "name": "cmd", "value": "test", "view": "long"},
map[string]interface{}{"type": "button", "value": "执行"},
},
},
@ -122,9 +133,10 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
},
map[string]interface{}{"componet_name": "upgrade", "componet_help": "upgrade",
"componet_tmpl": "componet", "componet_view": "Compile", "componet_init": "",
"componet_type": "private", "componet_ctx": "cli", "componet_cmd": "upgrade",
"componet_args": []interface{}{}, "inputs": []interface{}{
map[string]interface{}{"type": "select", "name": "action", "values": []interface{}{"bench", "system", "portal", "script"}},
"componet_type": "private", "componet_ctx": "ssh", "componet_cmd": "_route",
"componet_args": []interface{}{"$$", "context", "cli", "upgrade"}, "inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "pod", "imports": "plugin_pod"},
map[string]interface{}{"type": "select", "name": "action", "values": []interface{}{"script", "portal", "system", "bench"}},
map[string]interface{}{"type": "text", "name": "action"},
map[string]interface{}{"type": "button", "value": "升级"},
},
@ -159,7 +171,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
"componet_tmpl": "componet", "componet_view": "Context", "componet_init": "",
"componet_type": "public", "componet_ctx": "aaa", "componet_cmd": "clip",
"componet_args": []interface{}{}, "inputs": []interface{}{
map[string]interface{}{"label": "content", "type": "text", "name": "content"},
map[string]interface{}{"type": "text", "name": "content", "view": "long"},
map[string]interface{}{"type": "button", "value": "运行"},
},
},
@ -244,7 +256,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
"componet_args": []interface{}{"$$", "context", "web", "post", "$$", "content_type", "application/json", "parse", "json"}, "inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "pod", "imports": "plugin_pod"},
map[string]interface{}{"type": "text", "name": "spide", "imports": "plugin_site"},
map[string]interface{}{"type": "text", "name": "url", "value": "/"},
map[string]interface{}{"type": "text", "name": "url", "value": "/", "view": "long"},
map[string]interface{}{"type": "button", "value": "执行"},
},
},
@ -254,7 +266,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
"componet_args": []interface{}{"$$", "context", "web", "get"}, "inputs": []interface{}{
map[string]interface{}{"type": "text", "name": "pod", "imports": "plugin_pod"},
map[string]interface{}{"type": "text", "name": "spide", "imports": "plugin_site"},
map[string]interface{}{"type": "text", "name": "url", "value": "/"},
map[string]interface{}{"type": "text", "name": "url", "value": "/", "view": "long"},
map[string]interface{}{"type": "button", "value": "执行"},
},
},

View File

@ -30,6 +30,9 @@ fieldset form.option div.cmd input.args {
background-color: black;
width:160px;
}
fieldset form.option div.long input.args {
width:320px;
}
fieldset form.option label {
margin-right:6px;
}
@ -59,6 +62,10 @@ fieldset div.output div.code span.red {
fieldset div.output div.code span.green {
color:green;
}
fieldset div.output table td {
word-break:break-all;
min-width:40px;
}
fieldset.toast {
background-color:#ffffff00;