diff --git a/src/contexts/ctx.go b/src/contexts/ctx.go index 7243560d..1aca8448 100644 --- a/src/contexts/ctx.go +++ b/src/contexts/ctx.go @@ -794,7 +794,28 @@ func (m *Message) Color(color int, str string, arg ...interface{}) *Message { // } // }}} -func (m *Message) Table(cb func(maps map[string]string, list []string, line int) (goon bool)) *Message { // {{{ +func (m *Message) Table(cbs ...func(maps map[string]string, list []string, line int) (goon bool)) *Message { // {{{ + var cb func(maps map[string]string, list []string, line int) (goon bool) + if len(cbs) > 0 { + cb = cbs[0] + } else { + row := m.Confx("table_row_sep") + col := m.Confx("table_col_sep") + compact := Right(m.Confx("table_compact")) + cb = func(maps map[string]string, lists []string, line int) bool { + for i, v := range lists { + if k := m.Meta["append"][i]; compact { + v = maps[k] + } + + if m.Echo(v); i < len(lists)-1 { + m.Echo(col) + } + } + m.Echo(row) + return true + } + } if len(m.Meta["append"]) == 0 { return m } @@ -918,7 +939,7 @@ func (m *Message) Matrix(index int, arg ...interface{}) string { // {{{ } // }}} -func (m *Message) Sort(key string, arg ...string) { // {{{ +func (m *Message) Sort(key string, arg ...string) *Message { // {{{ table := []map[string]string{} m.Table(func(line map[string]string, lists []string, index int) bool { if index != -1 { @@ -993,6 +1014,7 @@ func (m *Message) Sort(key string, arg ...string) { // {{{ m.Add("append", k, v[k]) } } + return m } // }}} @@ -1788,7 +1810,10 @@ var Index = &Context{Name: "ctx", Help: "模块中心", "detail_index": &Config{Name: "detail_index", Value: "0", Help: "参数的索引"}, "result_index": &Config{Name: "result_index", Value: "-2", Help: "返回值的索引"}, - "list_help": &Config{Name: "list_help", Value: "list command", Help: "命令列表帮助"}, + "list_help": &Config{Name: "list_help", Value: "list command", Help: "命令列表帮助"}, + "table_compact": &Config{Name: "table_compact", Value: "false", Help: "命令列表帮助"}, + "table_col_sep": &Config{Name: "table_col_sep", Value: "\t", Help: "命令列表帮助"}, + "table_row_sep": &Config{Name: "table_row_sep", Value: "\n", Help: "命令列表帮助"}, }, Commands: map[string]*Command{ "help": &Command{Name: "help topic", Help: "帮助", Hand: func(m *Message, c *Context, key string, arg ...string) { @@ -2321,20 +2346,20 @@ var Index = &Context{Name: "ctx", Help: "模块中心", }) default: msg.Travel(func(msg *Message, i int) bool { - target := msg.target - m.Echo("%s(", target.Name) - - if target.context != nil { - m.Echo("%s", target.context.Name) + m.Add("append", "name", msg.target.Name) + if msg.target.context != nil { + m.Add("append", "context", msg.target.context.Name) + } else { + m.Add("append", "context", "") } - m.Echo(":") - msg.target = target - - m.Echo("%s(%s) ", msg.Cap("status"), msg.Cap("stream")) - m.Echo("%s\n", target.Help) + m.Add("append", "status", msg.Cap("status")) + m.Add("append", "stream", msg.Cap("stream")) + m.Add("append", "help", msg.target.Help) return true }) + + m.Table() } default: msg.Cmd(arg) @@ -2524,14 +2549,12 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Hand: func(m *Message, c *Context, key string, arg ...string) { switch len(arg) { //{{{ case 0: - keys := []string{} - for k, _ := range m.target.Configs { - keys = append(keys, k) - } - sort.Strings(keys) - for _, k := range keys { - m.Echo("%s(%s): %s\n", k, m.Conf(k), m.target.Configs[k].Name) + for k, v := range m.target.Configs { + m.Add("append", "key", k) + m.Add("append", "value", m.Conf(k)) + m.Add("append", "name", v.Name) } + m.Sort("key", "string").Table() return case 1: if arg[0] == "all" { @@ -2573,14 +2596,12 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Hand: func(m *Message, c *Context, key string, arg ...string) { switch len(arg) { //{{{ case 0: - keys := []string{} - for k, _ := range m.target.Caches { - keys = append(keys, k) - } - sort.Strings(keys) - for _, k := range keys { - m.Echo("%s(%s): %s\n", k, m.Cap(k), m.target.Caches[k].Name) + for k, v := range m.target.Caches { + m.Add("append", "key", k) + m.Add("append", "value", m.Cap(k)) + m.Add("append", "name", v.Name) } + m.Sort("key", "string").Table() return case 1: if arg[0] == "all" { diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index c875829b..627f401b 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -5,6 +5,7 @@ import ( // {{{ "encoding/json" "github.com/nsf/termbox-go" "github.com/skip2/go-qrcode" + "net" "bufio" "errors" @@ -34,8 +35,8 @@ type NFS struct { height int paths []string + io net.Conn - io io.ReadWriteCloser *bufio.Reader *bufio.Writer send map[int]*ctx.Message @@ -672,15 +673,22 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{ return false } + nfs.io = m.Optionv("io").(net.Conn) + bio := bufio.NewScanner(nfs.io) + for bio.Scan() { + m.Log("info", "recv: %s", bio.Text()) + } + + return false m.Sess("nfs", m) nfs.Message = m - if socket, ok := m.Data["io"]; ok { + if _, ok := m.Data["io"]; ok { m.Cap("stream", m.Source().Name) // m.Sess("aaa", "aaa").Cmd("login", "demo", "demo") m.Options("stdio", false) - nfs.io = socket.(io.ReadWriteCloser) + // nfs.io = socket.(io.ReadWriteCloser) nfs.Reader = bufio.NewReader(nfs.io) nfs.Writer = bufio.NewWriter(nfs.io) @@ -806,6 +814,7 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{ // }}} func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool { // {{{ + return false switch nfs.Context { case m.Target(): if nfs.in != nil { @@ -1311,10 +1320,8 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", "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)), "打开文件") + sub := com.Spawn(c) + sub.Start(fmt.Sprintf("file%d", m.Capi("nfile", 1)), "远程文件") return sub }, m.Meta["detail"]) } @@ -1323,10 +1330,8 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", "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)), "打开文件") + sub := com.Spawn(c) + sub.Start(fmt.Sprintf("file%d", m.Capi("nfile", 1)), "远程文件") return sub }, m.Meta["detail"]) } @@ -1334,6 +1339,9 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", }}, "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) { // {{{ + m.Log("fuck", "%v %v", arg, nfs.io) + nfs.io.Write([]byte(arg[0])) + return if m.Has("nrecv") { if len(arg) > 1 && arg[0] == "file" { info, e := os.Stat(arg[1]) diff --git a/src/contexts/tcp/tcp.go b/src/contexts/tcp/tcp.go index 643d0154..a76216ad 100644 --- a/src/contexts/tcp/tcp.go +++ b/src/contexts/tcp/tcp.go @@ -61,8 +61,8 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{ } m.Log("info", "%s dial %s", m.Cap("nclient"), - m.Append("stream", m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr())))) - m.Put("append", "io", tcp.Conn).Back(m) + m.Option("stream", m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr())))) + m.Put("option", "io", tcp.Conn).Back(m) return false case "accept": c, e := m.Data["io"].(net.Conn) @@ -71,7 +71,7 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{ m.Log("info", "%s accept %s", m.Cap("nclient"), m.Append("stream", m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr())))) - m.Put("append", "io", tcp.Conn).Back(m) + m.Put("option", "io", tcp.Conn).Back(m) return false default: if m.Cap("security") != "false" { @@ -145,69 +145,54 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心", "protocol": &ctx.Config{Name: "protocol(tcp/tcp4/tcp6)", Value: "tcp4", Help: "网络协议"}, }, Commands: map[string]*ctx.Command{ - "listen": &ctx.Command{Name: "listen address [security [protocol]]", Help: "网络监听", - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - m.Start(fmt.Sprintf("pub%d", m.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...) - }}, - "accept": &ctx.Command{Name: "accept address [security [protocol]]", Help: "网络连接", - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - m.Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...) - }}, - "dial": &ctx.Command{Name: "dial address [security [protocol]]", Help: "网络连接", - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - m.Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...) - }}, - "send": &ctx.Command{Name: "send message", Help: "发送消息", - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - if tcp, ok := m.Target().Server.(*TCP); m.Assert(ok) && tcp.Conn != nil { - tcp.Conn.Write([]byte(arg[0])) - m.Capi("nsend", len(arg[0])) - } - }}, - "recv": &ctx.Command{Name: "recv size", Help: "接收消息", - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - if tcp, ok := m.Target().Server.(*TCP); m.Assert(ok) && tcp.Conn != nil { - size, e := strconv.Atoi(arg[0]) - m.Assert(e) - - buf := make([]byte, size) - n, e := tcp.Conn.Read(buf) - m.Assert(e) - buf = buf[:n] - - m.Echo(string(buf)) - m.Capi("nrecv", n) - } - }}, - "ifconfig": &ctx.Command{Name: "ifconfig", Help: "接收消息", - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - ifs, e := net.Interfaces() + "listen": &ctx.Command{Name: "listen address [security [protocol]]", Help: "网络监听", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Start(fmt.Sprintf("pub%d", m.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...) + }}, + "accept": &ctx.Command{Name: "accept address [security [protocol]]", Help: "网络连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...) + }}, + "dial": &ctx.Command{Name: "dial address [security [protocol]]", Help: "网络连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...) + }}, + "send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + if tcp, ok := m.Target().Server.(*TCP); m.Assert(ok) && tcp.Conn != nil { // {{{ + tcp.Conn.Write([]byte(arg[0])) + m.Capi("nsend", len(arg[0])) + } // }}} + }}, + "recv": &ctx.Command{Name: "recv size", Help: "接收消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + if tcp, ok := m.Target().Server.(*TCP); m.Assert(ok) && tcp.Conn != nil { // {{{ + size, e := strconv.Atoi(arg[0]) m.Assert(e) - for _, v := range ifs { - ips, e := v.Addrs() - m.Assert(e) - for _, x := range ips { - ip := x.String() - if !strings.Contains(ip, ":") && len(ip) > 0 && len(v.HardwareAddr) > 0 { - m.Add("append", "index", v.Index) - m.Add("append", "name", v.Name) - m.Add("append", "hard", v.HardwareAddr) - m.Add("append", "ip", ip) - } + + buf := make([]byte, size) + n, e := tcp.Conn.Read(buf) + m.Assert(e) + buf = buf[:n] + + m.Echo(string(buf)) + m.Capi("nrecv", n) + } // }}} + }}, + "ifconfig": &ctx.Command{Name: "ifconfig", Help: "网络配置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + ifs, e := net.Interfaces() // {{{ + m.Assert(e) + for _, v := range ifs { + ips, e := v.Addrs() + m.Assert(e) + for _, x := range ips { + ip := x.String() + if !strings.Contains(ip, ":") && len(ip) > 0 && len(v.HardwareAddr) > 0 { + m.Add("append", "index", v.Index) + m.Add("append", "name", v.Name) + m.Add("append", "hard", v.HardwareAddr) + m.Add("append", "ip", ip) } } - - m.Table(func(maps map[string]string, list []string, line int) bool { - for i, v := range list { - m.Echo("%s", v) - if i < len(list)-1 { - m.Echo("\t") - } - } - m.Echo("\n") - return true - }) - }}, + } + m.Table() + // }}} + }}, }, Index: map[string]*ctx.Context{ "void": &ctx.Context{ diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 0e45b7dd..005fecf1 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -643,13 +643,6 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", m.Option("dir", m.Cap("directory")) } - check := m.Spawn().Cmd("/share", "/upload", "dir", m.Option("dir")) - if !check.Results(0) { - m.Copy(check, "append") - return - } - aaa := check.Appendv("aaa").(*ctx.Message) - // 输出文件 s, e := os.Stat(m.Option("dir")) if m.Assert(e); !s.IsDir() { @@ -657,26 +650,35 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", return } - // 共享列表 - share := m.Sess("share", m.Target()) - index := share.Target().Index - if index != nil && index[aaa.Append("username")] != nil { - for k, v := range index[aaa.Append("username")].Index { - for _, j := range v.Commands { - for _, n := range j.Shares { - for _, nn := range n { - if match, e := regexp.MatchString(nn, m.Option("dir")); m.Assert(e) && match { - share.Add("append", "group", k) - share.Add("append", "value", nn) - share.Add("append", "delete", "delete") + if false { + check := m.Spawn().Cmd("/share", "/upload", "dir", m.Option("dir")) + if !check.Results(0) { + m.Copy(check, "append") + return + } + aaa := check.Appendv("aaa").(*ctx.Message) + + // 共享列表 + share := m.Sess("share", m.Target()) + index := share.Target().Index + if index != nil && index[aaa.Append("username")] != nil { + for k, v := range index[aaa.Append("username")].Index { + for _, j := range v.Commands { + for _, n := range j.Shares { + for _, nn := range n { + if match, e := regexp.MatchString(nn, m.Option("dir")); m.Assert(e) && match { + share.Add("append", "group", k) + share.Add("append", "value", nn) + share.Add("append", "delete", "delete") + } } } } } } + share.Sort("value", "string") + share.Sort("argument", "string") } - share.Sort("value", "string") - share.Sort("argument", "string") // 输出目录 fs, e := ioutil.ReadDir(m.Option("dir")) @@ -748,7 +750,8 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } m.Append("title", "upload") - m.Append("tmpl", "userinfo", "share", "list", "git", "upload", "create") + // m.Append("tmpl", "userinfo", "share", "list", "git", "upload", "create") + m.Append("tmpl", "list", "git", "upload", "create") m.Append("template", m.Conf("upload_main"), m.Conf("upload_tmpl")) // }}} }}, @@ -908,7 +911,6 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", if len(arg) > 0 { url = arg[0] } - m.Log("fucK", "os %v", runtime.GOOS) switch runtime.GOOS { case "windows": m.Find("cli").Cmd("system", "explorer", url)