From 80eda9d0c8ac03687fc9923869ee7a0cfbc0229c Mon Sep 17 00:00:00 2001 From: harveyshao Date: Sat, 13 Nov 2021 14:22:25 +0800 Subject: [PATCH] add bash.grant --- base/mdb/mdb.go | 1 + core/code/c.go | 4 +-- core/code/python.go | 4 +-- core/team/task.go | 1 + core/wiki/field.go | 9 +++--- misc.go | 12 ++++++-- misc/bash/grant.go | 22 ++++++++++++++ misc/bash/run.go | 74 +++++++++++++++++++++++++++++++++++++++++++++ misc/bash/sess.go | 7 +++-- option.go | 3 ++ 10 files changed, 122 insertions(+), 15 deletions(-) create mode 100644 misc/bash/grant.go create mode 100644 misc/bash/run.go diff --git a/base/mdb/mdb.go b/base/mdb/mdb.go index 4f99a10b..b7b2ae27 100644 --- a/base/mdb/mdb.go +++ b/base/mdb/mdb.go @@ -115,6 +115,7 @@ var Index = &ice.Context{Name: MDB, Help: "数据模块", Commands: map[string]* MODIFY: {Name: "modify key sub type field value arg...", Help: "编辑", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { switch arg[2] { case ZONE: // modify key sub type zone id field value + m.Debug("what %v %v", arg[3], kit.KeyHash(arg[3])) _list_modify(m, arg[0], _domain_chain(m, kit.Keys(arg[1], kit.KeyHash(arg[3]))), kit.MDB_ID, arg[4], arg[5:]...) case HASH: _hash_modify(m, arg[0], _domain_chain(m, arg[1]), arg[3], arg[4], arg[5:]...) diff --git a/core/code/c.go b/core/code/c.go index 73d466fb..32a3a94b 100644 --- a/core/code/c.go +++ b/core/code/c.go @@ -48,7 +48,7 @@ const ( const C = "c" func init() { - Index.Merge(&ice.Context{Name: C, Help: "系统", Commands: map[string]*ice.Command{ + Index.Register(&ice.Context{Name: C, Help: "系统", Commands: map[string]*ice.Command{ ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { for _, cmd := range []string{mdb.PLUGIN, mdb.RENDER, mdb.ENGINE, mdb.SEARCH} { for _, k := range []string{H, C, CC} { @@ -164,5 +164,5 @@ func init() { ), ), KEYWORD, kit.Dict(), ))}, - }}) + }}, nil) } diff --git a/core/code/python.go b/core/code/python.go index e725ab89..fbbfbb3e 100644 --- a/core/code/python.go +++ b/core/code/python.go @@ -12,7 +12,7 @@ import ( const PYTHON = "python" func init() { - Index.Merge(&ice.Context{ + Index.Register(&ice.Context{ Configs: map[string]*ice.Config{ PYTHON: {Name: PYTHON, Help: "脚本命令", Value: kit.Data( cli.SOURCE, "http://mirrors.sohu.com/python/3.5.2/Python-3.5.2.tar.xz", @@ -40,5 +40,5 @@ func init() { m.Cmdy(INSTALL, path.Base(m.Conf(PYTHON, kit.META_SOURCE)), arg) }}, }, - }) + }, nil) } diff --git a/core/team/task.go b/core/team/task.go index 39f2de7f..37fa4b2a 100644 --- a/core/team/task.go +++ b/core/team/task.go @@ -67,6 +67,7 @@ func init() { m.Cmdy(mdb.INSERT, m.Prefix(TASK), "", mdb.ZONE, m.Option(kit.MDB_ZONE), BEGIN_TIME, m.Time(), CLOSE_TIME, m.Time("30m"), STATUS, PREPARE, LEVEL, 3, SCORE, 3, arg) + m.ProcessRefresh30ms() }}, mdb.MODIFY: {Name: "modify", Help: "编辑", Hand: func(m *ice.Message, arg ...string) { _task_modify(m, arg[0], arg[1]) diff --git a/core/wiki/field.go b/core/wiki/field.go index 68b52df1..5d982bcf 100644 --- a/core/wiki/field.go +++ b/core/wiki/field.go @@ -4,6 +4,7 @@ import ( "strings" ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/ctx" kit "shylinux.com/x/toolkits" ) @@ -97,11 +98,9 @@ const FIELD = "field" func init() { Index.Merge(&ice.Context{Commands: map[string]*ice.Command{ - FIELD: {Name: "field [name] cmd", Help: "插件", Action: map[string]*ice.Action{ - ice.RUN: {Name: "run", Help: "执行", Hand: func(m *ice.Message, arg ...string) { - m.Cmdy(arg) - }}, - }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + FIELD: {Name: "field [name] cmd", Help: "插件", Action: ice.MergeAction(map[string]*ice.Action{ + ice.RUN: {Name: "run", Help: "执行"}, + }, ctx.CmdAction()), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if arg = _name(m, arg); strings.Contains(arg[1], ice.NL) { arg = append([]string{arg[0], "web.chat.div", "auto.cmd", "split", "opts.text", arg[1]}, arg[2:]...) } diff --git a/misc.go b/misc.go index 28db5411..15759681 100644 --- a/misc.go +++ b/misc.go @@ -226,9 +226,6 @@ func (c *Context) _cmd(m *Message, cmd *Command, key string, k string, h *Action m.Cmdy(kit.Split(h.Name), arg) return m } - if k == RUN && !m.Right(arg) { - return m - } m.Log(LOG_CMDS, "%s.%s %s %d %v %s", c.Name, key, k, len(arg), arg, kit.FileLine(h.Hand, 3)) if len(h.List) > 0 && k != "search" { @@ -326,6 +323,15 @@ func Display(file string, arg ...string) map[string]string { } return map[string]string{"display": file, kit.MDB_STYLE: kit.Join(arg, " ")} } +func Display0(n int, file string, arg ...string) map[string]string { + if file == "" { + file = kit.FileName(n+1) + ".js" + } + if !strings.HasPrefix(file, "/") { + file = path.Join("/require", kit.ModPath(n+1, file)) + } + return map[string]string{"display": file, kit.MDB_STYLE: kit.Join(arg, SP)} +} func MergeAction(list ...interface{}) map[string]*Action { if len(list) == 0 { return nil diff --git a/misc/bash/grant.go b/misc/bash/grant.go new file mode 100644 index 00000000..f5b17945 --- /dev/null +++ b/misc/bash/grant.go @@ -0,0 +1,22 @@ +package bash + +import ( + ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/mdb" + kit "shylinux.com/x/toolkits" +) + +const GRANT = "grant" + +func init() { + Index.Merge(&ice.Context{Commands: map[string]*ice.Command{ + "grant": {Name: "grant sid auto", Help: "授权", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if len(arg) > 0 { + if m.Confirm("grant "+arg[0]+"?") == ice.TRUE { + m.Cmdy(SESS, mdb.MODIFY, GRANT, m.Option(ice.MSG_USERNAME), ice.Option{kit.MDB_HASH, arg[0]}) + } + } + m.Cmdy(SESS, arg) + }}, + }}) +} diff --git a/misc/bash/run.go b/misc/bash/run.go new file mode 100644 index 00000000..e253cb1c --- /dev/null +++ b/misc/bash/run.go @@ -0,0 +1,74 @@ +package bash + +import ( + "path" + "strings" + + ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/ctx" + "shylinux.com/x/icebergs/base/nfs" + "shylinux.com/x/icebergs/base/web" + kit "shylinux.com/x/toolkits" +) + +const RUN = "run" + +func init() { + Index.Merge(&ice.Context{Commands: map[string]*ice.Command{ + "/run/": {Name: "/run/", Help: "执行", Action: ice.MergeAction(map[string]*ice.Action{ + ctx.COMMAND: {Name: "command", Help: "命令", Hand: func(m *ice.Message, arg ...string) { + m.Search(arg[0], func(_ *ice.Context, s *ice.Context, key string, cmd *ice.Command) { + p := strings.ReplaceAll(kit.Select("/app/cat.sh", cmd.Meta["display"]), ".js", ".sh") + if strings.HasPrefix(p, ice.PS+ice.REQUIRE) { + m.Cmdy(web.SPIDE, ice.DEV, web.SPIDE_RAW, p) + } else { + m.Cmdy(nfs.CAT, path.Join(ice.USR_INTSHELL, p)) + } + m.Debug(kit.Formats(cmd.Meta)) + if m.Result() == "" || m.Result(1) == ice.ErrNotFound { + m.Set(ice.MSG_RESULT) + m.Echo("#/bin/bash\n") + list := []string{} + args := []string{} + kit.Fetch(cmd.Meta["_trans"], func(k string, v string) { + list = append(list, k) + args = append(args, kit.Format(` %s)`, k)) + kit.Fetch(cmd.Meta[k], func(index int, value map[string]interface{}) { + args = append(args, kit.Format(` read -p "read %s: " v; url="$url/%s/$v" `, value[kit.MDB_NAME], value[kit.MDB_NAME])) + }) + args = append(args, kit.Format(` ;;`)) + }) + list = append(list, "quit") + m.Echo(` +ish_sys_dev_run_action() { + select action in %s; do + if [ "$action" = "quit" ]; then break; fi + local url="run/action/run/%s/action/$action" + case $action in +%s + esac + ish_sys_dev_request $url + echo + done +} +`, kit.Join(list, ice.SP), arg[0], kit.Join(args, ice.NL)) + m.Echo("cat $1\n") + m.Debug("what %v", m.Result()) + } + }) + }}, + ice.RUN: {Name: "run", Help: "执行", Hand: func(m *ice.Message, arg ...string) { + if m.Right(arg) && !m.PodCmd(arg) { + m.Cmdy(arg) + } + if m.Result() == "" { + m.Table() + } + }}, + }, ctx.CmdAction()), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if m.Right(arg) { + m.Cmdy(arg) + } + }}}, + }) +} diff --git a/misc/bash/sess.go b/misc/bash/sess.go index 09223cca..6775c7d8 100644 --- a/misc/bash/sess.go +++ b/misc/bash/sess.go @@ -23,7 +23,7 @@ const SESS = "sess" func init() { Index.Merge(&ice.Context{Configs: map[string]*ice.Config{ SESS: {Name: SESS, Help: "会话流", Value: kit.Data( - kit.MDB_FIELD, "time,hash,status,username,hostname,pid,pwd", + kit.MDB_FIELD, "time,hash,status,username,hostname,pid,pwd,grant", )}, }, Commands: map[string]*ice.Command{ web.WEB_LOGIN: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { @@ -44,9 +44,10 @@ func init() { } msg := m.Cmd(SESS, m.Option(SID)) - m.Option(aaa.USERNAME, msg.Append(aaa.USERNAME)) + m.Option(ice.MSG_USERNAME, msg.Append(GRANT)) + m.Option(ice.MSG_USERROLE, aaa.UserRole(m, msg.Append(GRANT))) m.Option(tcp.HOSTNAME, msg.Append(tcp.HOSTNAME)) - m.Warn(m.Option(aaa.USERNAME) == "", ice.ErrNotLogin, arg) + m.Warn(m.Option(ice.MSG_USERNAME) == "", ice.ErrNotLogin, arg) }}, "/qrcode": {Name: "/qrcode", Help: "二维码", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Cmdy(cli.QRCODE, m.Option(kit.MDB_TEXT), m.Option(cli.FG), m.Option(cli.BG)) diff --git a/option.go b/option.go index 5147b513..a81cca5a 100644 --- a/option.go +++ b/option.go @@ -114,6 +114,9 @@ func (m *Message) StatusTimeCountTotal(arg ...interface{}) { m.Status(kit.MDB_TIME, m.Time(), kit.MDB_COUNT, kit.Split(m.FormatSize())[0], kit.MDB_TOTAL, arg, kit.MDB_COST, m.FormatCost()) } +func (m *Message) Confirm(text string) string { + return m.Cmdx("space", m.Option(MSG_DAEMON), "confirm", text) +} func (m *Message) Toast(text string, arg ...interface{}) { // [title [duration [progress]]] if len(arg) > 1 { switch val := arg[1].(type) {