1
0
forked from x/ContextOS

tce add nfs.paths

This commit is contained in:
shaoying 2018-07-22 21:20:03 +08:00
parent 95e79d9b8a
commit 8345bca7c7
5 changed files with 96 additions and 36 deletions

View File

@ -1,24 +1,10 @@
~file1
history load etc/history.txt
~shell1 ~shell1
alias import nfs alias import nfs
source etc/local.shy source etc/local.shy
return config debug on
var msg <- $(demo)
for index $msg append temp
for index $msg $temp h
echo $temp $h
end
end
for index $msg hi temp
echo $temp
end
var b = 1
for b += 1; $b < 3
echo $msg
end
return return
return "hello" "world" return "hello" "world"

View File

@ -313,6 +313,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
if i, e := strconv.Atoi(arg[0]); e == nil { if i, e := strconv.Atoi(arg[0]); e == nil {
t = time.Unix(int64(i/m.Confi("time_unit")), 0) t = time.Unix(int64(i/m.Confi("time_unit")), 0)
arg = arg[1:] arg = arg[1:]
m.Option("time_format", m.Conf("time_format"))
} }
} }
@ -664,11 +665,17 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
detail = append(detail, arg...) detail = append(detail, arg...)
} }
if detail[0] != "context" {
target := cli.target
defer func() {
cli.target = target
m.Cap("ps_target", cli.target.Name)
}()
}
routes := strings.Split(detail[0], ".") routes := strings.Split(detail[0], ".")
msg := m msg := m
if len(routes) > 1 { if len(routes) > 1 {
target := cli.target
defer func() { cli.target = target }()
route := strings.Join(routes[:len(routes)-1], ".") route := strings.Join(routes[:len(routes)-1], ".")
if msg = m.Find(route, false); msg == nil { if msg = m.Find(route, false); msg == nil {

View File

@ -1943,8 +1943,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
"nmessage": &Cache{Name: "消息数量", Value: "0", Help: "显示模块启动时所创建消息的数量"}, "nmessage": &Cache{Name: "消息数量", Value: "0", Help: "显示模块启动时所创建消息的数量"},
}, },
Configs: map[string]*Config{ Configs: map[string]*Config{
"scan_path": &Config{Name: "调试模式(true/false)", Value: "", Help: "是否打印错误信息off:不打印on:打印)"},
"debug": &Config{Name: "调试模式(true/false)", Value: "true", Help: "是否打印错误信息off:不打印on:打印)"}, "debug": &Config{Name: "调试模式(true/false)", Value: "true", Help: "是否打印错误信息off:不打印on:打印)"},
"default": &Config{Name: "默认的搜索起点(root/back/home)", Value: "root", Help: "模块搜索的默认起点root:从根模块back:从父模块home:从当前模块"}, "default": &Config{Name: "默认的搜索起点(root/back/home)", Value: "root", Help: "模块搜索的默认起点root:从根模块back:从父模块home:从当前模块"},
"start": &Config{Name: "启动模块", Value: "cli", Help: "启动时自动运行的模块"}, "start": &Config{Name: "启动模块", Value: "cli", Help: "启动时自动运行的模块"},
@ -3006,10 +3004,6 @@ func init() {
} }
func Start(args ...string) { func Start(args ...string) {
if len(args) > 0 {
Pulse.Conf("scan_path", args[0])
args = args[1:]
}
if len(args) > 0 { if len(args) > 0 {
Pulse.Conf("init.shy", args[0]) Pulse.Conf("init.shy", args[0])
args = args[1:] args = args[1:]

View File

@ -1,6 +1,7 @@
package mdb // {{{ package mdb // {{{
// }}} // }}}
import ( // {{{ import ( // {{{
"bufio"
"contexts" "contexts"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
@ -395,10 +396,32 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
} }
// }}} // }}}
}}, }},
"demo": &ctx.Command{Name: "get field offset table where [parse func field]", Help: "执行查询语句", "csv": &ctx.Command{Name: "csv scv_file sql_file sql", Help: "执行查询语句",
Form: map[string]int{"parse": 1}, Form: map[string]int{"csv_col_sep": 1},
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Echo("%v", m.Meta) f, e := os.Open(arg[0])
m.Assert(e)
s, e := os.Create(arg[1])
m.Assert(e)
head := false
bio := bufio.NewScanner(f)
for bio.Scan() {
line := bio.Text()
if !head {
head = true
continue
}
args := []interface{}{}
for _, v := range strings.Split(line, m.Confx("csv_col_sep")) {
args = append(args, v)
}
s.WriteString(fmt.Sprintf(arg[2], args...))
s.WriteString(";\n")
}
}}, }},
}, },
Index: map[string]*ctx.Context{ Index: map[string]*ctx.Context{

View File

@ -3,6 +3,7 @@ package nfs // {{{
import ( // {{{ import ( // {{{
"contexts" "contexts"
"encoding/json" "encoding/json"
"errors"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
"github.com/skip2/go-qrcode" "github.com/skip2/go-qrcode"
@ -21,6 +22,8 @@ import ( // {{{
// }}} // }}}
var FileNotExist = errors.New("file not exist")
type NFS struct { type NFS struct {
in *os.File in *os.File
out *os.File out *os.File
@ -28,6 +31,8 @@ type NFS struct {
pages []string pages []string
width, height int width, height int
paths []string
io io.ReadWriteCloser io io.ReadWriteCloser
*bufio.Reader *bufio.Reader
*bufio.Writer *bufio.Writer
@ -39,6 +44,22 @@ type NFS struct {
*ctx.Context *ctx.Context
} }
func (nfs *NFS) open(name string) (*os.File, error) { // {{{
if path.IsAbs(name) {
nfs.Log("info", nil, "open %s", name)
return os.Open(name)
}
for i := len(nfs.paths) - 1; i >= 0; i-- {
if f, e := os.Open(path.Join(nfs.paths[i], name)); e == nil {
nfs.Log("info", nil, "open %s", path.Join(nfs.paths[i], name))
return f, e
}
}
nfs.Log("info", nil, "open %s", name)
return os.Open(name)
}
// }}}
func dir(m *ctx.Message, name string, level int) { // {{{ func dir(m *ctx.Message, name string, level int) { // {{{
back, e := os.Getwd() back, e := os.Getwd()
m.Assert(e) m.Assert(e)
@ -815,20 +836,49 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
"git_info": &ctx.Config{Name: "git_info", Value: "branch status diff", Help: "命令集合"}, "git_info": &ctx.Config{Name: "git_info", Value: "branch status diff", Help: "命令集合"},
}, },
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"paths": &ctx.Command{
Name: "paths [add path]|[del index]|[set index path]|[index]",
Help: "设置文件搜索路径, add: 添加目录, del: 删除目录, set: 修改目录index: 目录序号, path: 目录名",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) { // {{{
if len(arg) == 0 {
for i, v := range nfs.paths {
m.Echo("%d: %s\n", i, v)
}
return
}
switch arg[0] {
case "add":
nfs.paths = append(nfs.paths, arg[1])
case "del":
if i, e := strconv.Atoi(arg[1]); e == nil && i < len(nfs.paths) {
for ; i < len(nfs.paths)-1; i++ {
nfs.paths[i] = nfs.paths[i+1]
}
nfs.paths = nfs.paths[:len(nfs.paths)-1]
}
case "set":
if i, e := strconv.Atoi(arg[1]); e == nil && i < len(nfs.paths) {
nfs.paths[i] = arg[2]
m.Echo("%d: %s\n", i, nfs.paths[i])
}
default:
if i, e := strconv.Atoi(arg[0]); e == nil && i < len(nfs.paths) {
m.Echo("%d: %s\n", i, nfs.paths[i])
}
}
} // }}}
}},
"scan": &ctx.Command{ "scan": &ctx.Command{
Name: "scan filename [nfs_name [nfs_help]]", Name: "scan filename [nfs_name [nfs_help]]",
Help: "扫描文件, filename: 文件名, nfs_name: 模块名, nfs_help: 模块帮助", Help: "扫描文件, filename: 文件名, nfs_name: 模块名, nfs_help: 模块帮助",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if _, ok := m.Target().Server.(*NFS); m.Assert(ok) { // {{{ if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) { // {{{
if arg[0] == "stdio" { if arg[0] == "stdio" {
m.Optionv("in", os.Stdin) m.Optionv("in", os.Stdin)
m.Optionv("out", os.Stdout) m.Optionv("out", os.Stdout)
} else { } else {
p := arg[0] if f, e := nfs.open(arg[0]); m.Assert(e) {
if !path.IsAbs(arg[0]) {
p = path.Join(m.Conf("scan_path"), arg[0])
}
if f, e := os.Open(p); m.Assert(e) {
m.Optionv("in", f) m.Optionv("in", f)
} }
} }
@ -849,7 +899,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
} }
switch arg[0] { switch arg[0] {
case "load": case "load":
f, e := os.Open(arg[1]) f, e := nfs.open(arg[1])
m.Assert(e) m.Assert(e)
defer f.Close() defer f.Close()