Documentation
¶
Index ¶
- Variables
- type NFS
- func (nfs *NFS) Begin(m *ctx.Message, arg ...string) ctx.Server
- func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool
- func (nfs *NFS) Read(p []byte) (n int, err error)
- func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
- func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool
- func (nfs *NFS) View(buf []string, top int, height int)
Constants ¶
This section is empty.
Variables ¶
View Source
var FileNotExist = errors.New("file not exist")
View Source
var Index = &ctx.Context{Name: "nfs", Help: "存储中心", Caches: map[string]*ctx.Cache{ "nfile": &ctx.Cache{Name: "nfile", Value: "-1", Help: "已经打开的文件数量"}, }, Configs: map[string]*ctx.Config{ "nfs_name": &ctx.Config{Name: "nfs_name", Value: "file", Help: "默认模块命名", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string { if len(arg) > 0 { return arg[0] } return fmt.Sprintf("%s%d", x.Value, m.Capi("nfile", 1)) }}, "nfs_help": &ctx.Config{Name: "nfs_help", Value: "file", Help: "默认模块帮助"}, "buf_size": &ctx.Config{Name: "buf_size", Value: "1024", Help: "读取文件的缓存区的大小"}, "qr_size": &ctx.Config{Name: "qr_size", Value: "256", Help: "生成二维码的图片的大小"}, "dir_name": &ctx.Config{Name: "dir_name(name/tree/path/full)", Value: "name", Help: "dir命令输出文件名的类型, name: 文件名, tree: 带缩进的文件名, path: 相对路径, full: 绝对路径"}, "dir_info": &ctx.Config{Name: "dir_info(sizes/lines/files/dirs)", Value: "sizes lines files dirs", Help: "dir命令输出目录的统计信息, info: 输出统计信息, 否则输出"}, "dir_type": &ctx.Config{Name: "dir_type(file/dir)", Value: "file", Help: "dir命令输出的文件类型, file: 只输出普通文件, dir: 只输出目录文件, 否则输出所有文件"}, "sort_field": &ctx.Config{Name: "sort_field", Value: "line", Help: "表格排序字段"}, "sort_order": &ctx.Config{Name: "sort_order(int/int_r/string/string_r/time/time_r)", Value: "int", Help: "表格排序类型"}, "git_branch": &ctx.Config{Name: "git_branch", Value: "--list", Help: "版本控制状态参数"}, "git_status": &ctx.Config{Name: "git_status", Value: "-sb", Help: "版本控制状态参数"}, "git_diff": &ctx.Config{Name: "git_diff", Value: "--stat", Help: "版本控制状态参数"}, "git_log": &ctx.Config{Name: "git_log", Value: "--pretty=%h %an(%ad) %s --date=format:%m/%d %H:%M --graph", Help: "版本控制状态参数"}, "git_log_form": &ctx.Config{Name: "git_log", Value: "stat", Help: "版本控制状态参数"}, "git_log_skip": &ctx.Config{Name: "git_log", Value: "0", Help: "版本控制状态参数"}, "git_log_line": &ctx.Config{Name: "git_log", Value: "5", Help: "版本控制状态参数"}, "git_path": &ctx.Config{Name: "git_path", Value: ".", Help: "版本控制默认路径"}, "git_info": &ctx.Config{Name: "git_info", Value: "branch status diff", Help: "命令集合"}, }, 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{ Name: "scan filename [nfs_name [nfs_help]]", Help: "扫描文件, filename: 文件名, nfs_name: 模块名, nfs_help: 模块帮助", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) { if arg[0] == "stdio" { m.Optionv("in", os.Stdin) m.Optionv("out", os.Stdout) } else { if f, e := nfs.open(arg[0]); m.Assert(e) { m.Optionv("in", f) } } m.Start(m.Confx("nfs_name", arg, 1), m.Confx("nfs_help", arg, 2), key, arg[0]) } }}, "history": &ctx.Command{ Name: "history [save|load filename [lines [pos]]] [find|search key]", Help: "扫描记录, save: 保存记录, load: 加载记录, filename: 文件名, lines: 加载或保存记录数量, pos: 加载或保存的起始位置, find: 查找记录, search: 搜索记录, key: 查找或搜索的参数", 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.history { m.Echo("%d: %s\n", i, v) } return } switch arg[0] { case "load": f, e := nfs.open(arg[1]) m.Assert(e) defer f.Close() pos, lines := 0, -1 if len(arg) > 3 { i, e := strconv.Atoi(arg[3]) m.Assert(e) pos = i } if len(arg) > 2 { i, e := strconv.Atoi(arg[2]) m.Assert(e) lines = i } bio := bufio.NewScanner(f) for i := 0; bio.Scan(); i++ { if i < pos { continue } if lines != -1 && (i-pos) >= lines { break } nfs.history = append(nfs.history, bio.Text()) } m.Capi("nline", 0, len(nfs.history)) case "save": f, e := os.Create(arg[1]) m.Assert(e) defer f.Close() pos, lines := 0, -1 if len(arg) > 3 { i, e := strconv.Atoi(arg[3]) m.Assert(e) pos = i } if len(arg) > 2 { i, e := strconv.Atoi(arg[2]) m.Assert(e) lines = i } for i, v := range nfs.history { if i < pos { continue } if lines != -1 && (i-pos) >= lines { break } fmt.Fprintln(f, v) } case "find": for i, v := range nfs.history { if strings.HasPrefix(v, arg[1]) { m.Echo("%d: %s\n", i, v) } } case "search": default: if i, e := strconv.Atoi(arg[0]); e == nil && i < len(nfs.history) { m.Echo(nfs.history[i]) } } } }}, "open": &ctx.Command{ Name: "open filename [nfs_name [nfs_help]]", Help: "打开文件, filename: 文件名, nfs_name: 模块名, nfs_help: 模块帮助", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if m.Has("io") { } else if f, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm); m.Assert(e) { m.Put("option", "in", f).Put("option", "out", f) } m.Start(m.Confx("nfs_name", arg, 1), m.Confx("nfs_help", arg, 2), "open", arg[0]) m.Echo(m.Target().Name) }}, "append": &ctx.Command{ Name: "append filename [nfs_name [nfs_help]]", Help: "打开文件, filename: 文件名, nfs_name: 模块名, nfs_help: 模块帮助", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if m.Has("io") { } else if f, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm); m.Assert(e) { m.Put("option", "in", f).Put("option", "out", f) } m.Start(m.Confx("nfs_name", arg, 1), m.Confx("nfs_help", arg, 2), "append", arg[0]) m.Echo(m.Target().Name) }}, "read": &ctx.Command{Name: "read [buf_size [pos]]", Help: "读取文件, buf_size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) && nfs.in != nil { n, e := strconv.Atoi(m.Confx("buf_size", arg, 0)) m.Assert(e) if len(arg) > 1 { m.Cap("pos", arg[1]) } buf := make([]byte, n) if n, e = nfs.in.ReadAt(buf, int64(m.Capi("pos"))); e != io.EOF { m.Assert(e) } m.Capi("nread", n) m.Echo(string(buf)) if m.Capi("pos", n); n == 0 { m.Cap("pos", "0") } } }}, "write": &ctx.Command{Name: "write string [pos]", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) && nfs.out != nil { if len(arg) > 1 { m.Cap("pos", arg[1]) } if len(arg[0]) == 0 { m.Assert(nfs.out.Truncate(int64(m.Capi("pos")))) m.Cap("size", m.Cap("pos")) m.Cap("pos", "0") } else { n, e := nfs.out.WriteAt([]byte(arg[0]), int64(m.Capi("pos"))) if m.Capi("nwrite", n); m.Assert(e) && m.Capi("pos", n) > m.Capi("size") { m.Cap("size", m.Cap("pos")) } nfs.out.Sync() } m.Echo(m.Cap("pos")) } }}, "load": &ctx.Command{Name: "load file [buf_size [pos]]", Help: "加载文件, buf_size: 加载大小, pos: 加载位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if f, e := os.Open(arg[0]); m.Assert(e) { defer f.Close() pos := 0 if len(arg) > 2 { i, e := strconv.Atoi(arg[2]) m.Assert(e) pos = i } s, e := strconv.Atoi(m.Confx("buf_size", arg, 1)) m.Assert(e) buf := make([]byte, s) if l, e := f.ReadAt(buf, int64(pos)); e == io.EOF || m.Assert(e) { m.Log("info", "read %d", l) m.Echo(string(buf[:l])) } } }}, "save": &ctx.Command{Name: "save file string...", Help: "保存文件, file: 保存的文件, string: 保存的内容", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if f, e := os.Create(arg[0]); m.Assert(e) { defer f.Close() for _, v := range arg[1:] { fmt.Fprint(f, v) } } }}, "print": &ctx.Command{Name: "print file string...", Help: "输出文件, file: 输出的文件, string: 输出的内容", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if f, e := os.OpenFile(arg[0], os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); m.Assert(e) { defer f.Close() for _, v := range arg[1:] { fmt.Fprint(f, v) } fmt.Fprint(f, "\n") } }}, "genqr": &ctx.Command{Name: "genqr [qr_size size] filename string...", Help: "生成二维码图片, qr_size: 图片大小, filename: 文件名, string: 输出内容", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if size, e := strconv.Atoi(m.Confx("qr_size")); m.Assert(e) { qrcode.WriteFile(strings.Join(arg[1:], ""), qrcode.Medium, size, arg[0]) } }}, "json": &ctx.Command{Name: "json [key value]...", Help: "生成格式化内容, key: 参数名, value: 参数值", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { data := map[string]interface{}{} for _, k := range m.Meta["option"] { if v, ok := m.Data[k]; ok { data[k] = v continue } data[k] = m.Meta[k] } for i := 1; i < len(arg)-1; i += 2 { data[arg[i]] = arg[i+1] } buf, e := json.Marshal(data) m.Assert(e) m.Echo(string(buf)) }}, "pwd": &ctx.Command{Name: "pwd", Help: "查看当前路径", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { wd, e := os.Getwd() m.Assert(e) m.Echo(wd) }}, "dir": &ctx.Command{ Name: "dir dir [dir_info info] [dir_name name|tree|path|full] [dir_type file|dir] [sort_field name] [sort_order type]", Help: "查看目录, dir: 目录名, dir_info: 显示统计信息, dir_name: 文件名类型, dir_type: 文件类型, sort_field: 排序字段, sort_order: 排序类型", Form: map[string]int{"dir_info": 1, "dir_name": 1, "dir_type": 1, "sort_field": 1, "sort_order": 1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { d := "." if len(arg) > 0 { d = arg[0] } trip := 0 if m.Confx("dir_name") == "path" { wd, e := os.Getwd() m.Assert(e) trip = len(wd) + 1 } info := strings.Split(m.Confx("dir_info"), " ") for _, v := range info { m.Option(v, 0) } dir(m, d, 0) m.Sort(m.Confx("sort_field"), m.Confx("sort_order")) m.Table(func(maps map[string]string, list []string, line int) bool { for i, v := range list { key := m.Meta["append"][i] switch key { case "filename": if trip > 0 { v = v[trip:] } case "dir": continue } m.Echo("%s\t", v) } m.Echo("\n") return true }) for _, v := range info { m.Echo("%s: %s\n", v, m.Option(v)) } }}, "git": &ctx.Command{ Name: "git branch|status|diff|log|info arg... [git_path path]...", Help: "版本控制, branch: 分支管理, status: 查看状态, info: 查看分支与状态, git_path: 指定路径", Form: map[string]int{"git_path": 1, "git_info": 1, "git_log": 1, "git_log_form": 1}, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if len(arg) == 0 { arg = []string{"info"} } cmds := []string{arg[0]} switch arg[0] { case "s": arg[0] = "status" case "b": arg[0] = "branch" case "d": arg[0] = "diff" } if arg[0] == "info" { cmds = strings.Split(m.Confx("git_info"), " ") } wd, e := os.Getwd() m.Assert(e) if !m.Has("git_path") { m.Option("git_path", m.Conf("git_path")) } for _, p := range m.Meta["git_path"] { if !path.IsAbs(p) { p = path.Join(wd, p) } m.Echo("path: %s\n", p) for _, c := range cmds { args := []string{} switch c { case "branch", "status", "diff": if c != "status" { args = append(args, "--color") } args = append(args, strings.Split(m.Confx("git_"+c, arg, 1), " ")...) if len(arg) > 2 { args = append(args, arg[2:]...) } case "difftool": cmd := exec.Command("git", "difftool", "-y") m.Log("info", "cmd: %s %v", "git", "difftool", "-y") cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr if e := cmd.Start(); e != nil { m.Echo("error: ") m.Echo("%s\n", e) } else if e := cmd.Wait(); e != nil { m.Echo("error: ") m.Echo("%s\n", e) } continue case "csv": cmd := exec.Command("git", "log", "--shortstat", "--pretty=commit: %ad", "--date=format:%Y-%m-%d") if out, e := cmd.CombinedOutput(); e != nil { m.Echo("error: ") m.Echo("%s\n", e) } else { f, e := os.Create(arg[1]) m.Assert(e) defer f.Close() type stat struct { date string adds int dels int } stats := []*stat{} list := strings.Split(string(out), "commit: ") for _, v := range list { l := strings.Split(v, "\n") if len(l) > 2 { fs := strings.Split(strings.Trim(l[2], " "), ", ") stat := &stat{date: l[0]} if len(fs) > 2 { adds := strings.Split(fs[1], " ") dels := strings.Split(fs[2], " ") a, e := strconv.Atoi(adds[0]) m.Assert(e) stat.adds = a d, e := strconv.Atoi(dels[0]) m.Assert(e) stat.dels = d } else { adds := strings.Split(fs[1], " ") a, e := strconv.Atoi(adds[0]) m.Assert(e) if adds[1] == "insertions(+)" { stat.adds = a } else { stat.dels = a } } stats = append(stats, stat) } } fmt.Fprintf(f, "order,date,adds,dels,sum,top,bottom,last\n") l := len(stats) for i := 0; i < l/2; i++ { stats[i], stats[l-i-1] = stats[l-i-1], stats[i] } sum := 0 for i, v := range stats { fmt.Fprintf(f, "%d,%s,%d,%d,%d,%d,%d,%d\n", i, v.date, v.adds, v.dels, sum, sum+v.adds, sum-v.dels, sum+v.adds-v.dels) sum += v.adds - v.dels } } continue case "log": args = append(args, "--color") args = append(args, strings.Split(m.Confx("git_log"), " ")...) args = append(args, fmt.Sprintf("--%s", m.Confx("git_log_form"))) args = append(args, m.Confx("git_log_skip", arg, 1, "--skip=%s")) args = append(args, m.Confx("git_log_line", arg, 2, "-n %s")) default: args = append(args, arg[1:]...) } m.Log("info", "cmd: %s %v", "git", ctx.Trans("-C", p, c, args)) cmd := exec.Command("git", ctx.Trans("-C", p, c, args)...) if out, e := cmd.CombinedOutput(); e != nil { m.Echo("error: ") m.Echo("%s\n", e) } else { if m.Echo(string(out)); len(out) > 0 { m.Echo("\n") } } } } }}, "listen": &ctx.Command{Name: "listen args...", Help: "启动文件服务, args: 参考tcp模块, listen命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if _, ok := m.Target().Server.(*NFS); m.Assert(ok) { m.Find("tcp").Call(func(com *ctx.Message) *ctx.Message { sub := com.Spawn(m.Target()) sub.Put("option", "target", m.Source()) sub.Put("option", "io", com.Data["io"]) sub.Start(fmt.Sprintf("file%d", m.Capi("nfile", 1)), "打开文件") return sub }, m.Meta["detail"]) } }}, "dial": &ctx.Command{Name: "dial args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if _, ok := m.Target().Server.(*NFS); m.Assert(ok) { m.Find("tcp").Call(func(com *ctx.Message) *ctx.Message { sub := com.Spawn(m.Target()) sub.Put("option", "target", m.Source()) sub.Put("option", "io", com.Data["io"]) sub.Start(fmt.Sprintf("file%d", m.Capi("nfile", 1)), "打开文件") return sub }, m.Meta["detail"]) } }}, "send": &ctx.Command{Name: "send [file] args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) { if m.Has("nrecv") { if len(arg) > 1 && arg[0] == "file" { info, e := os.Stat(arg[1]) m.Assert(e) m.Append("name", info.Name()) m.Append("size", info.Size()) m.Append("time", info.ModTime()) m.Append("mode", info.Mode()) f, e := os.Open(arg[1]) m.Assert(e) m.Put("append", "io", f) } } else { nfs.send[m.Optioni("nsend", m.Capi("nsend", 1))] = m if len(arg) > 1 && arg[0] == "file" { info, e := os.Stat(arg[1]) m.Assert(e) m.Option("name", info.Name()) m.Option("size", info.Size()) m.Option("time", info.ModTime()) m.Option("mode", info.Mode()) n, e := fmt.Fprintf(nfs.Writer, "detail: recv\n") m.Capi("nbytes", n) m.Assert(e) } for _, v := range arg { n, e := fmt.Fprintf(nfs.Writer, "detail: %v\n", v) m.Capi("nbytes", n) m.Assert(e) } for _, k := range m.Meta["option"] { if k == "args" { continue } for _, v := range m.Meta[k] { n, e := fmt.Fprintf(nfs.Writer, "%s: %s\n", k, v) m.Capi("nbytes", n) m.Assert(e) } } m.Log("info", "%d send", m.Optioni("nsend")) m.Log("info", "detail: %v", m.Meta["detail"]) m.Log("info", "option: %v", m.Meta["option"]) n, e := fmt.Fprintf(nfs.Writer, "\n") m.Capi("nbytes", n) m.Assert(e) nfs.Writer.Flush() if len(arg) > 1 && arg[0] == "file" { f, e := os.Open(arg[1]) m.Assert(e) defer f.Close() _, e = io.Copy(nfs.Writer, f) } } } }}, "recv": &ctx.Command{Name: "recv [file] args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if nfs, ok := m.Target().Server.(*NFS); m.Assert(ok) { if m.Has("nrecv") { if len(arg) > 1 && arg[0] == "file" { f, e := os.Create(arg[1]) m.Assert(e) defer f.Close() io.CopyN(f, nfs.Reader, int64(m.Optioni("size"))) } return } nfs.send[m.Optioni("nrecv", m.Capi("nsend", 1))] = m if len(arg) > 1 && arg[0] == "file" { f, e := os.Create(arg[1]) m.Assert(e) m.Put("option", "io", f) fmt.Fprintf(nfs.Writer, "detail: send\n") } for _, v := range arg { fmt.Fprintf(nfs.Writer, "detail: %v\n", v) } for _, k := range m.Meta["option"] { if k == "args" { continue } for _, v := range m.Meta[k] { fmt.Fprintf(nfs.Writer, "%s: %s\n", k, v) } } fmt.Fprintf(nfs.Writer, "\n") nfs.Writer.Flush() } }}, }, }
Functions ¶
This section is empty.
Types ¶
Click to show internal directories.
Click to hide internal directories.