From 3f6358dd61f16662a6009da6cf746745cd28c439 Mon Sep 17 00:00:00 2001 From: shaoying Date: Mon, 2 Aug 2021 08:49:57 +0800 Subject: [PATCH] add cmd.go --- core/chat/action.go | 34 +++++++++++++++++++++--- core/chat/cmd.go | 63 +++++++++++++++++++++++++++++++++++++++++++++ go.sum | 2 ++ 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 core/chat/cmd.go diff --git a/core/chat/action.go b/core/chat/action.go index dd53d282..6f70abdd 100644 --- a/core/chat/action.go +++ b/core/chat/action.go @@ -9,6 +9,7 @@ import ( "github.com/shylinux/icebergs/base/cli" "github.com/shylinux/icebergs/base/ctx" "github.com/shylinux/icebergs/base/mdb" + "github.com/shylinux/icebergs/base/nfs" "github.com/shylinux/icebergs/base/web" kit "github.com/shylinux/toolkits" ) @@ -208,9 +209,8 @@ func init() { "/cmd/": {Name: "/cmd/", Help: "命令", Action: map[string]*ice.Action{ ctx.COMMAND: {Name: "command", Help: "命令", Hand: func(m *ice.Message, arg ...string) { if len(arg) == 0 { - arg = strings.Split(strings.TrimPrefix(m.Option("_names"), "/chat/cmd/"), "/") - m.Push("index", arg[0]) - m.Push("args", kit.Format(arg[1:])) + m.Push("index", kit.Select("cmd")) + m.Push("args", "") return } m.Cmdy(ctx.COMMAND, arg[0]) @@ -219,7 +219,33 @@ func init() { m.Cmdy(arg) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.RenderDownload(path.Join(m.Conf(web.SERVE, kit.Keym(ice.VOLCANOS, kit.MDB_PATH)), "page/cmd.html")) + sp := m.Conf(web.SERVE, kit.Keym(ice.VOLCANOS, kit.MDB_PATH)) + if strings.HasSuffix(m.R.URL.Path, "/") { + m.RenderDownload(path.Join(sp, "page/cmd.html")) + return + } + m.RenderDownload(path.Join(sp, path.Join(arg...))) + }}, + "cmd": {Name: "cmd path auto up", Help: "命令", Action: map[string]*ice.Action{ + "up": {Name: "up", Help: "上一级", Hand: func(m *ice.Message, arg ...string) { + if strings.TrimPrefix(m.R.URL.Path, "/cmd") == "/" { + m.Cmdy("cmd") + return + } + if strings.HasSuffix(m.R.URL.Path, "/") { + m.Process("_location", "../") + } else { + m.Process("_location", "./") + } + }}, + }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if len(arg) > 0 { + m.Process("_location", arg[0]) + return + } + sup := m.Conf(web.SERVE, kit.Keym(ice.VOLCANOS, kit.MDB_PATH)) + m.Option(nfs.DIR_ROOT, path.Join(sup, strings.TrimPrefix(path.Dir(m.R.URL.Path), "/cmd"))) + m.Cmdy(nfs.DIR, arg) }}, }}) } diff --git a/core/chat/cmd.go b/core/chat/cmd.go new file mode 100644 index 00000000..36dd269d --- /dev/null +++ b/core/chat/cmd.go @@ -0,0 +1,63 @@ +package chat + +import ( + "path" + "strings" + + ice "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/cli" + "github.com/shylinux/icebergs/base/ctx" + "github.com/shylinux/icebergs/base/nfs" + "github.com/shylinux/icebergs/base/web" + kit "github.com/shylinux/toolkits" +) + +const CMD = "cmd" + +func init() { + Index.Merge(&ice.Context{ + Configs: map[string]*ice.Config{ + CMD: {Name: CMD, Help: "命令", Value: kit.Data(kit.MDB_PATH, "./")}, + }, + Commands: map[string]*ice.Command{ + "/cmd/": {Name: "/cmd/", Help: "命令", Action: map[string]*ice.Action{ + ctx.COMMAND: {Name: "command", Help: "命令", Hand: func(m *ice.Message, arg ...string) { + if len(arg) == 0 { + m.Push("index", "cmd") + m.Push("args", "") + return + } + m.Cmdy(ctx.COMMAND, arg[0]) + }}, + cli.RUN: {Name: "command", Help: "执行", Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(arg) + }}, + }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if strings.HasSuffix(m.R.URL.Path, "/") { + m.RenderDownload(path.Join(m.Conf(web.SERVE, kit.Keym(ice.VOLCANOS, kit.MDB_PATH)), "page/cmd.html")) + return + } + m.RenderDownload(path.Join(m.Conf(CMD, kit.META_PATH), path.Join(arg...))) + }}, + "cmd": {Name: "cmd path auto up", Help: "命令", Action: map[string]*ice.Action{ + "up": {Name: "up", Help: "上一级", Hand: func(m *ice.Message, arg ...string) { + if strings.TrimPrefix(m.R.URL.Path, "/cmd") == "/" { + m.Cmdy("cmd") + return + } + if strings.HasSuffix(m.R.URL.Path, "/") { + m.Process("_location", "../") + } else { + m.Process("_location", "./") + } + }}, + }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if len(arg) > 0 { + m.Process("_location", arg[0]) + return + } + m.Option(nfs.DIR_ROOT, path.Join(m.Conf(CMD, kit.META_PATH), strings.TrimPrefix(path.Dir(m.R.URL.Path), "/cmd"))) + m.Cmdy(nfs.DIR, arg) + }}, + }}) +} diff --git a/go.sum b/go.sum index 62ac4f17..e07c6149 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/shylinux/toolkits v0.2.4 h1:2TaRBYdt15dE9vTUTO++6aF66vOZNd+ruP3be1C4z github.com/shylinux/toolkits v0.2.4/go.mod h1:oMwi/N00I2cXenmcAkqikEncTzB4u5JhTQ9mO5xj7sY= github.com/shylinux/toolkits v0.2.5 h1:dAqUrWjUSl/QuhUN9jRrALqmICsX5sFMmWm4LsiIi0s= github.com/shylinux/toolkits v0.2.5/go.mod h1:oMwi/N00I2cXenmcAkqikEncTzB4u5JhTQ9mO5xj7sY= +github.com/shylinux/toolkits v0.2.6 h1:fqoMRKxcyunPYzrcZh3UaogwOv6Zm8TjXK3XDg/xGNE= +github.com/shylinux/toolkits v0.2.6/go.mod h1:oMwi/N00I2cXenmcAkqikEncTzB4u5JhTQ9mO5xj7sY= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w=