1
0
forked from x/icebergs
This commit is contained in:
harveyshao 2022-11-20 23:39:59 +08:00
parent 028a6ba82b
commit f57e198c7e
9 changed files with 156 additions and 181 deletions

View File

@ -16,12 +16,9 @@ import (
) )
func _runtime_init(m *ice.Message) { func _runtime_init(m *ice.Message) {
// 版本信息 make
kit.Fetch(kit.UnMarshal(kit.Format(ice.Info.Make)), func(key string, value ice.Any) { kit.Fetch(kit.UnMarshal(kit.Format(ice.Info.Make)), func(key string, value ice.Any) {
m.Conf(RUNTIME, kit.Keys(MAKE, strings.ToLower(key)), value) m.Conf(RUNTIME, kit.Keys(MAKE, strings.ToLower(key)), value)
}) })
// 主机信息 host
m.Conf(RUNTIME, kit.Keys(HOST, GOARCH), runtime.GOARCH) m.Conf(RUNTIME, kit.Keys(HOST, GOARCH), runtime.GOARCH)
m.Conf(RUNTIME, kit.Keys(HOST, GOOS), runtime.GOOS) m.Conf(RUNTIME, kit.Keys(HOST, GOOS), runtime.GOOS)
m.Conf(RUNTIME, kit.Keys(HOST, PID), os.Getpid()) m.Conf(RUNTIME, kit.Keys(HOST, PID), os.Getpid())
@ -30,36 +27,16 @@ func _runtime_init(m *ice.Message) {
m.Conf(RUNTIME, kit.Keys(HOST, MAXPROCS), runtime.GOMAXPROCS(0)) m.Conf(RUNTIME, kit.Keys(HOST, MAXPROCS), runtime.GOMAXPROCS(0))
m.Conf(RUNTIME, mdb.META, "") m.Conf(RUNTIME, mdb.META, "")
m.Conf(RUNTIME, mdb.HASH, "") m.Conf(RUNTIME, mdb.HASH, "")
osid := runtime.GOOS
// 启动目录 boot m.Cmd(nfs.CAT, "/etc/os-release", func(text string) {
m.Conf(RUNTIME, kit.Keys(BOOT, HOSTNAME), kit.Env("HOSTNAME")) if ls := kit.Split(text, "="); len(ls) > 1 {
if name, e := os.Hostname(); e == nil && name != "" { switch ls[0] {
m.Conf(RUNTIME, kit.Keys(BOOT, HOSTNAME), name) case "ID", "ID_LIKE":
} osid = strings.TrimSpace(ls[1] + ice.SP + osid)
m.Conf(RUNTIME, kit.Keys(BOOT, PATHNAME), path.Base(kit.Env("PWD"))) }
if name, e := os.Getwd(); e == nil && name != "" {
m.Conf(RUNTIME, kit.Keys(BOOT, PATHNAME), path.Base(name))
}
m.Conf(RUNTIME, kit.Keys(BOOT, USERNAME), kit.Select(kit.UserName(), kit.Select(kit.Env("WORKSPACE_GIT_USERNAME"), kit.Env(CTX_USER))))
ice.Info.HostName = m.Conf(RUNTIME, kit.Keys(BOOT, HOSTNAME))
ice.Info.PathName = m.Conf(RUNTIME, kit.Keys(BOOT, PATHNAME))
ice.Info.UserName = m.Conf(RUNTIME, kit.Keys(BOOT, USERNAME))
m.Cmd(aaa.USER, mdb.CREATE, ice.Info.UserName, "", aaa.ROOT)
aaa.UserRoot(ice.Pulse)
// 启动程序 boot
m.Conf(RUNTIME, kit.Keys(BOOT, mdb.COUNT), kit.Int(m.Conf(RUNTIME, kit.Keys(BOOT, mdb.COUNT)))+1)
bin := _system_find(m, os.Args[0])
m.Conf(RUNTIME, kit.Keys(BOOT, ice.BIN), bin)
if s, e := nfs.StatFile(m, bin); e == nil {
m.Conf(RUNTIME, kit.Keys(BOOT, nfs.SIZE), kit.FmtSize(s.Size()))
if f, e := nfs.OpenFile(m, bin); e == nil {
defer f.Close()
m.Conf(RUNTIME, kit.Keys(BOOT, mdb.HASH), kit.Hashs(f))
} }
} })
m.Conf(RUNTIME, kit.Keys(HOST, OSID), osid)
// 环境变量 conf
for _, k := range ENV_LIST { for _, k := range ENV_LIST {
switch m.Conf(RUNTIME, kit.Keys(CONF, k), kit.Env(k)); k { switch m.Conf(RUNTIME, kit.Keys(CONF, k), kit.Env(k)); k {
case CTX_PID: case CTX_PID:
@ -71,25 +48,31 @@ func _runtime_init(m *ice.Message) {
} }
} }
// 系统版本 osid m.Conf(RUNTIME, kit.Keys(BOOT, HOSTNAME), kit.Env("HOSTNAME"))
osid := runtime.GOOS if name, e := os.Hostname(); e == nil && name != "" {
m.Cmd(nfs.CAT, "/etc/os-release", func(text string) { m.Conf(RUNTIME, kit.Keys(BOOT, HOSTNAME), name)
if ls := kit.Split(text, "="); len(ls) > 1 {
switch ls[0] {
case "ID", "ID_LIKE":
osid = strings.TrimSpace(ls[1] + ice.SP + osid)
}
}
})
m.Conf(RUNTIME, kit.Keys(HOST, OSID), osid)
switch strings.Split(os.Getenv(TERM), "-")[0] {
case "xterm", "screen":
ice.Info.Colors = true
default:
ice.Info.Colors = false
} }
m.Conf(RUNTIME, kit.Keys(BOOT, PATHNAME), path.Base(kit.Env("PWD")))
if name, e := os.Getwd(); e == nil && name != "" {
m.Conf(RUNTIME, kit.Keys(BOOT, PATHNAME), path.Base(name))
}
m.Conf(RUNTIME, kit.Keys(BOOT, USERNAME), kit.Select(kit.UserName(), kit.Env(CTX_USER)))
ice.Info.HostName = m.Conf(RUNTIME, kit.Keys(BOOT, HOSTNAME))
ice.Info.PathName = m.Conf(RUNTIME, kit.Keys(BOOT, PATHNAME))
ice.Info.UserName = m.Conf(RUNTIME, kit.Keys(BOOT, USERNAME))
m.Cmd(aaa.USER, mdb.CREATE, ice.Info.UserName, "", aaa.ROOT)
aaa.UserRoot(ice.Pulse)
m.Conf(RUNTIME, kit.Keys(BOOT, mdb.COUNT), kit.Int(m.Conf(RUNTIME, kit.Keys(BOOT, mdb.COUNT)))+1)
bin := _system_find(m, os.Args[0])
m.Conf(RUNTIME, kit.Keys(BOOT, ice.BIN), bin)
if s, e := nfs.StatFile(m, bin); e == nil {
m.Conf(RUNTIME, kit.Keys(BOOT, nfs.SIZE), kit.FmtSize(s.Size()))
if f, e := nfs.OpenFile(m, bin); e == nil {
defer f.Close()
m.Conf(RUNTIME, kit.Keys(BOOT, mdb.HASH), kit.Hashs(f))
}
}
} }
func _runtime_hostinfo(m *ice.Message) { func _runtime_hostinfo(m *ice.Message) {
m.Push("nCPU", strings.Count(m.Cmdx(nfs.CAT, "/proc/cpuinfo"), "processor")) m.Push("nCPU", strings.Count(m.Cmdx(nfs.CAT, "/proc/cpuinfo"), "processor"))
@ -121,8 +104,8 @@ const (
MAKE = "make" MAKE = "make"
TEST = "test" TEST = "test"
HOST = "host" HOST = "host"
BOOT = "boot"
CONF = "conf" CONF = "conf"
BOOT = "boot"
NODE = "node" NODE = "node"
) )
const ( const (
@ -147,6 +130,7 @@ const (
) )
const ( const (
CTX_SHY = "ctx_shy" CTX_SHY = "ctx_shy"
CTX_COM = "ctx_com"
CTX_DEV = "ctx_dev" CTX_DEV = "ctx_dev"
CTX_OPS = "ctx_ops" CTX_OPS = "ctx_ops"
CTX_POD = "ctx_pod" CTX_POD = "ctx_pod"
@ -164,7 +148,7 @@ const (
) )
var ENV_LIST = []string{ var ENV_LIST = []string{
TERM, SHELL, CTX_SHY, CTX_DEV, CTX_OPS, CTX_ARG, CTX_PID, CTX_USER, CTX_SHARE, CTX_RIVER, CTX_DAEMON, TERM, SHELL, CTX_SHY, CTX_COM, CTX_DEV, CTX_OPS, CTX_ARG, CTX_PID, CTX_USER, CTX_SHARE, CTX_RIVER, CTX_DAEMON,
} }
const ( const (
@ -186,81 +170,61 @@ const RUNTIME = "runtime"
func init() { func init() {
Index.MergeCommands(ice.Commands{ Index.MergeCommands(ice.Commands{
RUNTIME: {Name: "runtime info=ifconfig,hostinfo,hostname,userinfo,procinfo,diskinfo,bootinfo,api,cli,cmd,env auto", Help: "运行环境", Actions: ice.Actions{ RUNTIME: {Name: "runtime info=ifconfig,hostinfo,hostname,userinfo,procinfo,diskinfo,bootinfo,api,cli,cmd,env auto", Help: "运行环境", Actions: ice.MergeActions(ice.Actions{
ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { _runtime_init(m) }},
cs := m.Target().Configs IFCONFIG: {Hand: func(m *ice.Message, arg ...string) { m.Cmdy("tcp.host") }},
if _, ok := cs[RUNTIME]; !ok { HOSTINFO: {Hand: func(m *ice.Message, arg ...string) { _runtime_hostinfo(m) }},
cs[RUNTIME] = &ice.Config{Value: kit.Dict()} HOSTNAME: {Hand: func(m *ice.Message, arg ...string) {
}
_runtime_init(m)
}},
IFCONFIG: {Name: "ifconfig", Help: "网卡配置", Hand: func(m *ice.Message, arg ...string) {
m.Cmdy("tcp.host")
}},
HOSTINFO: {Name: "hostinfo", Help: "主机信息", Hand: func(m *ice.Message, arg ...string) {
_runtime_hostinfo(m)
}},
HOSTNAME: {Name: "hostname", Help: "主机域名", Hand: func(m *ice.Message, arg ...string) {
if len(arg) > 0 { if len(arg) > 0 {
ice.Info.HostName = m.Conf(RUNTIME, kit.Keys(BOOT, HOSTNAME), m.Conf(RUNTIME, kit.Keys(NODE, mdb.NAME), arg[0])) ice.Info.HostName = m.Conf(RUNTIME, kit.Keys(BOOT, HOSTNAME), m.Conf(RUNTIME, kit.Keys(NODE, mdb.NAME), arg[0]))
} }
m.Echo(ice.Info.HostName) m.Echo(ice.Info.HostName)
}}, }},
USERINFO: {Name: "userinfo", Help: "用户信息", Hand: func(m *ice.Message, arg ...string) { USERINFO: {Hand: func(m *ice.Message, arg ...string) { m.Split(m.Cmdx(SYSTEM, "who"), "user term time") }},
m.Split(m.Cmdx(SYSTEM, "who"), "user term time") PROCINFO: {Hand: func(m *ice.Message, arg ...string) {
}},
PROCINFO: {Name: "procinfo", Help: "进程信息", Hand: func(m *ice.Message, arg ...string) {
msg := m.Cmd("", HOSTINFO) msg := m.Cmd("", HOSTINFO)
m.Split(m.Cmdx(SYSTEM, "ps", "u")).PushAction(PROCKILL).SortIntR("RSS") m.Split(m.Cmdx(SYSTEM, "ps", "u")).PushAction(PROCKILL).SortIntR("RSS")
m.StatusTimeCount("nCPU", msg.Append("nCPU"), "MemTotal", msg.Append("MemTotal"), "MemFree", msg.Append("MemFree")) m.StatusTimeCount("nCPU", msg.Append("nCPU"), "MemTotal", msg.Append("MemTotal"), "MemFree", msg.Append("MemFree"))
}}, }},
PROCKILL: {Name: "prockill", Help: "结束进程", Hand: func(m *ice.Message, arg ...string) { PROCKILL: {Hand: func(m *ice.Message, arg ...string) { m.Cmdy(gdb.SIGNAL, gdb.STOP, m.Option("PID")).ProcessRefresh() }},
m.Cmdy(gdb.SIGNAL, gdb.STOP, m.Option("PID")) MAXPROCS: {Hand: func(m *ice.Message, arg ...string) {
m.ProcessRefresh()
}},
DISKINFO: {Name: "diskinfo", Help: "磁盘信息", Hand: func(m *ice.Message, arg ...string) {
_runtime_diskinfo(m)
}},
MAXPROCS: {Name: "maxprocs", Help: "最大并发", Hand: func(m *ice.Message, arg ...string) {
if len(arg) > 0 { if len(arg) > 0 {
runtime.GOMAXPROCS(kit.Int(m.Conf(RUNTIME, kit.Keys(HOST, MAXPROCS), kit.Select("1", arg, 0)))) runtime.GOMAXPROCS(kit.Int(m.Conf(RUNTIME, kit.Keys(HOST, MAXPROCS), arg[0])))
} }
m.Echo("%d", runtime.GOMAXPROCS(0)) m.Echo("%d", runtime.GOMAXPROCS(0))
}}, }},
API: {Name: "api", Help: "接口命令", Hand: func(m *ice.Message, arg ...string) { DISKINFO: {Hand: func(m *ice.Message, arg ...string) { _runtime_diskinfo(m) }},
API: {Hand: func(m *ice.Message, arg ...string) {
for k, v := range ice.Info.Route { for k, v := range ice.Info.Route {
m.Push(nfs.PATH, k) m.Push(nfs.PATH, k).Push(nfs.FILE, v)
m.Push(nfs.FILE, v)
} }
m.Sort(nfs.PATH).StatusTimeCount() m.Sort(nfs.PATH).StatusTimeCount()
}}, }},
CLI: {Name: "cli", Help: "文件模块", Hand: func(m *ice.Message, arg ...string) { CLI: {Hand: func(m *ice.Message, arg ...string) {
for k, v := range ice.Info.File { for k, v := range ice.Info.File {
m.Push(nfs.FILE, k) m.Push(nfs.FILE, k).Push(mdb.NAME, v)
m.Push(mdb.NAME, v)
} }
m.Sort(nfs.FILE).StatusTimeCount() m.Sort(nfs.FILE).StatusTimeCount()
}}, }},
CMD: {Name: "cmd", Help: "模块命令", Hand: func(m *ice.Message, arg ...string) { CMD: {Hand: func(m *ice.Message, arg ...string) {
m.OptionFields(ctx.INDEX, mdb.NAME, mdb.HELP) m.OptionFields(ctx.INDEX, mdb.NAME, mdb.HELP)
m.Cmdy(ctx.COMMAND, mdb.SEARCH, ctx.COMMAND).StatusTimeCount() m.Cmdy(ctx.COMMAND, mdb.SEARCH, ctx.COMMAND).StatusTimeCount()
}}, }},
ENV: {Name: "env", Help: "环境变量", Hand: func(m *ice.Message, arg ...string) { ENV: {Hand: func(m *ice.Message, arg ...string) {
for _, v := range os.Environ() { for _, v := range os.Environ() {
ls := strings.SplitN(v, "=", 2) ls := strings.SplitN(v, ice.EQ, 2)
m.Push(mdb.NAME, ls[0]) m.Push(mdb.NAME, ls[0]).Push(mdb.VALUE, ls[1])
m.Push(mdb.VALUE, ls[1])
} }
m.StatusTimeCount() m.StatusTimeCount()
}}, }},
MAKE_DOMAIN: {Name: "make.domain", Help: "编译主机", Hand: func(m *ice.Message, arg ...string) { MAKE_DOMAIN: {Hand: func(m *ice.Message, arg ...string) {
if os.Getenv(CTX_DEV) == "" || os.Getenv(CTX_POD) == "" { if os.Getenv(CTX_DEV) == "" || os.Getenv(CTX_POD) == "" {
m.Echo(m.Conf(RUNTIME, MAKE_DOMAIN)) m.Echo(m.Conf(RUNTIME, MAKE_DOMAIN))
} else { } else {
m.Echo(kit.MergePOD(os.Getenv(CTX_DEV), os.Getenv(CTX_POD))) m.Echo(kit.MergePOD(os.Getenv(CTX_DEV), os.Getenv(CTX_POD)))
} }
}}, }},
}, Hand: func(m *ice.Message, arg ...string) { }, ctx.ConfAction("hi")), Hand: func(m *ice.Message, arg ...string) {
if len(arg) > 0 && arg[0] == BOOTINFO { if len(arg) > 0 && arg[0] == BOOTINFO {
arg = arg[1:] arg = arg[1:]
} }

View File

@ -16,58 +16,48 @@ import (
) )
func _system_cmd(m *ice.Message, arg ...string) *exec.Cmd { func _system_cmd(m *ice.Message, arg ...string) *exec.Cmd {
bin := "" bin, env := "", kit.Simple(m.Optionv(CMD_ENV))
if text := kit.ReadFile(ice.ETC_PATH); len(text) > 0 {
if file := _system_find(m, arg[0], strings.Split(text, ice.NL)...); file != "" {
m.Logs(mdb.SELECT, "etc path cmd", file)
bin = file // 配置目录
}
}
env := kit.Simple(m.Optionv(CMD_ENV))
for i := 0; i < len(env)-1; i += 2 { for i := 0; i < len(env)-1; i += 2 {
if env[i] == PATH { if env[i] == PATH {
if file := _system_find(m, arg[0], strings.Split(env[i+1], ice.DF)...); file != "" { if file := _system_find(m, arg[0], strings.Split(env[i+1], ice.DF)...); file != "" {
m.Logs(mdb.SELECT, "env path cmd", file) m.Logs(mdb.SELECT, "env path cmd", file)
bin = file // 环境变量 bin = file
}
}
}
if bin == "" {
if text := kit.ReadFile(ice.ETC_PATH); len(text) > 0 {
if file := _system_find(m, arg[0], strings.Split(text, ice.NL)...); file != "" {
m.Logs(mdb.SELECT, "etc path cmd", file)
bin = file
} }
} }
} }
// if m.Option(CMD_DIR) != "" {
// if file := _system_find(m, arg[0], m.Option(CMD_DIR)); file != "" {
// m.Logs(mdb.SELECT, "dir path cmd", file)
// bin = file // 当前目录
// }
// }
if bin == "" { if bin == "" {
if file := _system_find(m, arg[0], ice.BIN, nfs.PWD); file != "" { if file := _system_find(m, arg[0], ice.BIN, nfs.PWD); file != "" {
m.Logs(mdb.SELECT, "mirrors cmd", file) m.Logs(mdb.SELECT, "contexts cmd", file)
bin = file // 当前目录 bin = file
} }
} }
if bin == "" && !strings.Contains(arg[0], ice.PS) { if bin == "" && !strings.Contains(arg[0], ice.PS) {
if file := _system_find(m, arg[0]); file != "" { if file := _system_find(m, arg[0]); file != "" {
m.Logs(mdb.SELECT, "systems cmd", file) m.Logs(mdb.SELECT, "systems cmd", file)
bin = file // 系统命令 bin = file
} }
} }
if bin == "" && !strings.Contains(arg[0], ice.PS) { if bin == "" && !strings.Contains(arg[0], ice.PS) {
m.Cmd(MIRRORS, CMD, arg[0]) m.Cmd(MIRRORS, CMD, arg[0])
if file := _system_find(m, arg[0]); file != "" { if file := _system_find(m, arg[0]); file != "" {
m.Logs(mdb.SELECT, "mirrors cmd", file) m.Logs(mdb.SELECT, "mirrors cmd", file)
bin = file // 软件镜像 bin = file
} }
} }
cmd := exec.Command(bin, arg[1:]...) cmd := exec.Command(bin, arg[1:]...)
// 运行目录
if cmd.Dir = m.Option(CMD_DIR); len(cmd.Dir) > 0 { if cmd.Dir = m.Option(CMD_DIR); len(cmd.Dir) > 0 {
if m.Logs(mdb.EXPORT, CMD_DIR, cmd.Dir); !nfs.ExistsFile(m, cmd.Dir) { if m.Logs(mdb.EXPORT, CMD_DIR, cmd.Dir); !nfs.ExistsFile(m, cmd.Dir) {
file.MkdirAll(cmd.Dir, ice.MOD_DIR) file.MkdirAll(cmd.Dir, ice.MOD_DIR)
} }
} }
// 环境变量
for i := 0; i < len(env)-1; i += 2 { for i := 0; i < len(env)-1; i += 2 {
cmd.Env = append(cmd.Env, kit.Format("%s=%s", env[i], env[i+1])) cmd.Env = append(cmd.Env, kit.Format("%s=%s", env[i], env[i+1]))
} }
@ -90,10 +80,10 @@ func _system_out(m *ice.Message, out string) io.Writer {
} }
func _system_exec(m *ice.Message, cmd *exec.Cmd) { func _system_exec(m *ice.Message, cmd *exec.Cmd) {
if r, ok := m.Optionv(CMD_INPUT).(io.Reader); ok { if r, ok := m.Optionv(CMD_INPUT).(io.Reader); ok {
cmd.Stdin = r // 输入流 cmd.Stdin = r
} }
if w := _system_out(m, CMD_OUTPUT); w != nil { if w := _system_out(m, CMD_OUTPUT); w != nil {
cmd.Stdout, cmd.Stderr = w, w // 输出流 cmd.Stdout, cmd.Stderr = w, w
if w := _system_out(m, CMD_ERRPUT); w != nil { if w := _system_out(m, CMD_ERRPUT); w != nil {
cmd.Stderr = w cmd.Stderr = w
} }
@ -110,8 +100,6 @@ func _system_exec(m *ice.Message, cmd *exec.Cmd) {
m.Echo(strings.TrimRight(out.String(), ice.NL)) m.Echo(strings.TrimRight(out.String(), ice.NL))
}() }()
} }
// 执行命令
if e := cmd.Run(); !m.Warn(e, ice.ErrNotFound, cmd.Args) { if e := cmd.Run(); !m.Warn(e, ice.ErrNotFound, cmd.Args) {
m.Cost(CODE, cmd.ProcessState.ExitCode(), ctx.ARGS, cmd.Args) m.Cost(CODE, cmd.ProcessState.ExitCode(), ctx.ARGS, cmd.Args)
} }
@ -157,10 +145,10 @@ const SYSTEM = "system"
func init() { func init() {
Index.MergeCommands(ice.Commands{ Index.MergeCommands(ice.Commands{
SYSTEM: {Name: "system cmd run", Help: "系统命令", Actions: ice.Actions{ SYSTEM: {Name: "system cmd run", Help: "系统命令", Actions: ice.Actions{
nfs.FIND: {Name: "find", Help: "查找", Hand: func(m *ice.Message, arg ...string) { nfs.FIND: {Hand: func(m *ice.Message, arg ...string) {
m.Echo(_system_find(m, arg[0], arg[1:]...)) m.Echo(_system_find(m, arg[0], arg[1:]...))
}}, }},
nfs.PUSH: {Name: "push", Help: "查找", Hand: func(m *ice.Message, arg ...string) { nfs.PUSH: {Hand: func(m *ice.Message, arg ...string) {
for _, p := range arg { for _, p := range arg {
if !strings.Contains(m.Cmdx(nfs.CAT, ice.ETC_PATH), p) { if !strings.Contains(m.Cmdx(nfs.CAT, ice.ETC_PATH), p) {
m.Cmd(nfs.PUSH, ice.ETC_PATH, strings.TrimSpace(p)+ice.NL) m.Cmd(nfs.PUSH, ice.ETC_PATH, strings.TrimSpace(p)+ice.NL)
@ -168,21 +156,18 @@ func init() {
} }
m.Cmdy(nfs.CAT, ice.ETC_PATH) m.Cmdy(nfs.CAT, ice.ETC_PATH)
}}, }},
MAN: {Name: "man", Help: "文档", Hand: func(m *ice.Message, arg ...string) { MAN: {Hand: func(m *ice.Message, arg ...string) {
if len(arg) == 1 { if len(arg) == 1 {
arg = append(arg, "") arg = append(arg, "")
} }
m.Option(CMD_ENV, "COLUMNS", kit.Int(kit.Select("1920", m.Option("width")))/12) m.Option(CMD_ENV, "COLUMNS", kit.Int(kit.Select("1920", m.Option(ice.WIDTH)))/12)
m.Echo(SystemCmds(m, "man %s %s|col -b", kit.Select("", arg[1], arg[1] != "1"), arg[0])) m.Echo(SystemCmds(m, "man %s %s|col -b", kit.Select("", arg[1], arg[1] != "1"), arg[0]))
}}, }},
}, Hand: func(m *ice.Message, arg ...string) { }, Hand: func(m *ice.Message, arg ...string) {
if len(arg) == 0 { if len(arg) == 0 {
return return
} }
if len(arg) == 1 { if _system_exec(m, _system_cmd(m, kit.Simple(kit.Split(arg[0]), arg[1:])...)); IsSuccess(m) && m.Append(CMD_ERR) == "" {
arg = kit.Split(arg[0])
}
if _system_exec(m, _system_cmd(m, arg...)); IsSuccess(m) && m.Append(CMD_ERR) == "" {
m.SetAppend() m.SetAppend()
} }
}}, }},

View File

@ -18,10 +18,8 @@ func _command_list(m *ice.Message, name string) {
case nfs.JS: case nfs.JS:
m.Push(DISPLAY, FileURI(name)) m.Push(DISPLAY, FileURI(name))
name = kit.Select(CAN_PLUGIN, GetFileCmd(name)) name = kit.Select(CAN_PLUGIN, GetFileCmd(name))
case nfs.GO: case nfs.GO:
name = GetFileCmd(name) name = GetFileCmd(name)
default: default:
if msg := m.Cmd(mdb.RENDER, kit.Ext(name)); msg.Length() > 0 { if msg := m.Cmd(mdb.RENDER, kit.Ext(name)); msg.Length() > 0 {
m.Push(ARGS, kit.Format(kit.List(name))) m.Push(ARGS, kit.Format(kit.List(name)))
@ -33,12 +31,11 @@ func _command_list(m *ice.Message, name string) {
m.Push(mdb.INDEX, name) m.Push(mdb.INDEX, name)
return return
} }
if name == "" { // 命令列表 if name == "" {
for k, v := range m.Source().Commands { for k, v := range m.Source().Commands {
if k[0] == '/' || k[0] == '_' { if k[0] == '/' || k[0] == '_' {
continue // 内部命令 continue
} }
m.Push(mdb.KEY, k) m.Push(mdb.KEY, k)
m.Push(mdb.NAME, v.Name) m.Push(mdb.NAME, v.Name)
m.Push(mdb.HELP, v.Help) m.Push(mdb.HELP, v.Help)
@ -46,8 +43,6 @@ func _command_list(m *ice.Message, name string) {
m.Sort(mdb.KEY) m.Sort(mdb.KEY)
return return
} }
// 命令详情
m.Spawn(m.Source()).Search(name, func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) { m.Spawn(m.Source()).Search(name, func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) {
m.Push(mdb.INDEX, kit.Keys(s.Cap(ice.CTX_FOLLOW), key)) m.Push(mdb.INDEX, kit.Keys(s.Cap(ice.CTX_FOLLOW), key))
m.Push(mdb.NAME, kit.Format(cmd.Name)) m.Push(mdb.NAME, kit.Format(cmd.Name))
@ -59,17 +54,15 @@ func _command_list(m *ice.Message, name string) {
func _command_search(m *ice.Message, kind, name, text string) { func _command_search(m *ice.Message, kind, name, text string) {
m.Travel(func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) { m.Travel(func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) {
if key[0] == '/' || key[0] == '_' { if key[0] == '/' || key[0] == '_' {
return // 内部命令 return
} }
if name != "" && !strings.HasPrefix(key, name) && !strings.Contains(s.Name, name) { if name != "" && !strings.HasPrefix(key, name) && !strings.Contains(s.Name, name) {
return return
} }
m.PushSearch(ice.CTX, kit.PathName(1), ice.CMD, kit.FileName(1), m.PushSearch(ice.CTX, kit.PathName(1), ice.CMD, kit.FileName(1),
kit.SimpleKV("", s.Cap(ice.CTX_FOLLOW), cmd.Name, cmd.Help), kit.SimpleKV("", s.Cap(ice.CTX_FOLLOW), cmd.Name, cmd.Help),
CONTEXT, s.Cap(ice.CTX_FOLLOW), COMMAND, key, CONTEXT, s.Cap(ice.CTX_FOLLOW), COMMAND, key, mdb.HELP, cmd.Help,
INDEX, kit.Keys(s.Cap(ice.CTX_FOLLOW), key), INDEX, kit.Keys(s.Cap(ice.CTX_FOLLOW), key),
mdb.HELP, cmd.Help,
) )
}) })
} }
@ -91,17 +84,17 @@ func init() {
COMMAND: {Name: "command key auto", Help: "命令", Actions: ice.MergeActions(ice.Actions{ COMMAND: {Name: "command key auto", Help: "命令", Actions: ice.MergeActions(ice.Actions{
ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) {
TravelCmd(m, func(key, file, line string) { TravelCmd(m, func(key, file, line string) {
if strings.Contains(file, "icebergs") { if strings.Contains(file, ice.ICEBERGS) {
AddFileCmd(file, key) AddFileCmd(file, key)
} }
}) })
}}, }},
mdb.SEARCH: {Name: "search type name text", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { mdb.SEARCH: {Hand: func(m *ice.Message, arg ...string) {
if arg[0] == m.CommandKey() || len(arg) > 1 && arg[1] != "" { if arg[0] == m.CommandKey() || len(arg) > 1 && arg[1] != "" {
_command_search(m, arg[0], kit.Select("", arg, 1), kit.Select("", arg, 2)) _command_search(m, arg[0], kit.Select("", arg, 1), kit.Select("", arg, 2))
} }
}}, }},
"tags": {Name: "tags", Help: "索引", Hand: func(m *ice.Message, arg ...string) { "tags": {Hand: func(m *ice.Message, arg ...string) {
TravelCmd(m, func(key, file, line string) { TravelCmd(m, func(key, file, line string) {
m.Push("name", key) m.Push("name", key)
m.Push("file", file) m.Push("file", file)
@ -124,56 +117,60 @@ func init() {
}) })
} }
var runChecker = []func(*ice.Message, string, string, ...string) bool{}
func AddRunChecker(cb func(*ice.Message, string, string, ...string) bool) {
runChecker = append(runChecker, cb)
}
func init() {
AddRunChecker(func(m *ice.Message, cmd, check string, arg ...string) bool {
switch check {
case mdb.REMOVE:
m.Cmd(CONFIG, mdb.REMOVE, cmd)
return true
case mdb.SELECT:
m.Cmdy(CONFIG, cmd)
return true
default:
return false
}
})
}
func Run(m *ice.Message, arg ...string) { func Run(m *ice.Message, arg ...string) {
if len(arg) > 3 && arg[1] == ACTION && arg[2] == CONFIG { if len(arg) > 3 && arg[1] == ACTION && arg[2] == CONFIG {
switch arg[3] { for _, check := range runChecker {
case "help": if check(m, arg[0], arg[3], arg...) {
if file := strings.ReplaceAll(GetCmdFile(m, arg[0]), ".go", ".shy"); nfs.ExistsFile(m, file) { return
ProcessFloat(m, "web.wiki.word", file)
} }
case "script":
if file := strings.ReplaceAll(GetCmdFile(m, arg[0]), ".go", ".js"); nfs.ExistsFile(m, file) {
ProcessFloat(m, "web.code.inner", file)
}
case "source":
if file := GetCmdFile(m, arg[0]); nfs.ExistsFile(m, file) {
ProcessFloat(m, "web.code.inner", file)
}
case "select":
m.Cmdy(CONFIG, arg[0])
case "reset":
m.Cmd(CONFIG, "reset", arg[0])
} }
return
} }
if !PodCmd(m, arg) && aaa.Right(m, arg) { if !PodCmd(m, arg) && aaa.Right(m, arg) {
m.Cmdy(arg) m.Cmdy(arg)
} }
} }
func PodCmd(m *ice.Message, arg ...ice.Any) bool {
if pod := m.Option(ice.POD); pod != "" {
if m.Option(ice.POD, ""); len(kit.Simple(m.Optionv(ice.MSG_UPLOAD))) == 1 {
m.Cmdy("cache", "upload").Option(ice.MSG_UPLOAD, m.Append(mdb.HASH), m.Append(mdb.NAME), m.Append(nfs.SIZE))
}
m.Cmdy(append(kit.List(ice.SPACE, pod), arg...))
return true
}
return false
}
func CmdHandler(args ...ice.Any) ice.Handler { func CmdHandler(args ...ice.Any) ice.Handler {
return func(m *ice.Message, arg ...string) { m.Cmdy(args...) } return func(m *ice.Message, arg ...string) { m.Cmdy(args...) }
} }
func CmdAction(args ...ice.Any) ice.Actions { func CmdAction(args ...ice.Any) ice.Actions {
return ice.Actions{ice.CTX_INIT: mdb.AutoConfig(args...), return ice.Actions{ice.CTX_INIT: mdb.AutoConfig(args...),
COMMAND: {Name: "command", Help: "命令", Hand: func(m *ice.Message, arg ...string) { COMMAND: {Hand: func(m *ice.Message, arg ...string) {
if !PodCmd(m, COMMAND, arg) { if !PodCmd(m, COMMAND, arg) {
m.Cmdy(COMMAND, arg) m.Cmdy(COMMAND, arg)
} }
}}, }},
ice.RUN: {Name: "run", Help: "执行", Hand: Run}, ice.RUN: {Hand: Run},
} }
} }
func PodCmd(m *ice.Message, arg ...ice.Any) bool {
if pod := m.Option(ice.POD); pod != "" {
if m.Option(ice.POD, ""); m.Option(ice.MSG_UPLOAD) != "" {
msg := m.Cmd("cache", "upload")
m.Option(ice.MSG_UPLOAD, msg.Append(mdb.HASH), msg.Append(mdb.NAME), msg.Append(nfs.SIZE))
}
m.Cmdy(append(kit.List("space", pod), arg...))
return true
}
return false
}
func FileURI(dir string) string { func FileURI(dir string) string {
if strings.Contains(dir, "go/pkg/mod") { if strings.Contains(dir, "go/pkg/mod") {

View File

@ -112,7 +112,7 @@ func init() {
} }
m.Confv(arg[0], arg[1], kit.List(list...)) m.Confv(arg[0], arg[1], kit.List(list...))
}}, }},
"reset": {Name: "reset key sub", Help: "重置", Hand: func(m *ice.Message, arg ...string) { mdb.REMOVE: {Name: "remove key sub", Hand: func(m *ice.Message, arg ...string) {
m.Conf(m.Option("key"), m.Option("sub"), "") m.Conf(m.Option("key"), m.Option("sub"), "")
m.Go(func() { m.Cmd(ice.EXIT, 1) }) m.Go(func() { m.Cmd(ice.EXIT, 1) })
}}, }},

View File

@ -156,6 +156,12 @@ func (f *Frame) scan(m *ice.Message, h, line string) *Frame {
} }
func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server { func (f *Frame) Begin(m *ice.Message, arg ...string) ice.Server {
switch strings.Split(os.Getenv(cli.TERM), "-")[0] {
case "xterm", "screen":
ice.Info.Colors = true
default:
ice.Info.Colors = false
}
return f return f
} }
func (f *Frame) Spawn(m *ice.Message, c *ice.Context, arg ...string) ice.Server { func (f *Frame) Spawn(m *ice.Message, c *ice.Context, arg ...string) ice.Server {

View File

@ -19,15 +19,10 @@ import (
) )
func Upload(m *ice.Message) *ice.Message { func Upload(m *ice.Message) *ice.Message {
if up := kit.Simple(m.Optionv(ice.MSG_UPLOAD)); len(up) == 0 { if len(kit.Simple(m.Optionv(ice.MSG_UPLOAD))) == 1 {
return m m.Cmdy(CACHE, UPLOAD).Option(ice.MSG_UPLOAD, m.Append(mdb.HASH), m.Append(mdb.NAME), m.Append(nfs.SIZE))
} else if len(up) < 2 {
msg := m.Cmd(CACHE, UPLOAD)
m.Option(ice.MSG_UPLOAD, msg.Append(mdb.HASH), msg.Append(mdb.NAME), msg.Append(nfs.SIZE))
return msg
} else {
return m.Cmd(CACHE, up[0])
} }
return m
} }
func PushNotice(m *ice.Message, arg ...ice.Any) { func PushNotice(m *ice.Message, arg ...ice.Any) {
if m.Option(ice.MSG_DAEMON) == "" { if m.Option(ice.MSG_DAEMON) == "" {

View File

@ -120,6 +120,9 @@ const (
SERVE_START = "serve.start" SERVE_START = "serve.start"
SERVE_STOP = "serve.stop" SERVE_STOP = "serve.stop"
WEBSITE = "website" WEBSITE = "website"
CODE_INNER = "web.code.inner"
WIKI_WORD = "web.wiki.word"
) )
const WEB = "web" const WEB = "web"

View File

@ -12,6 +12,7 @@ import (
"shylinux.com/x/icebergs/base/lex" "shylinux.com/x/icebergs/base/lex"
"shylinux.com/x/icebergs/base/mdb" "shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/nfs" "shylinux.com/x/icebergs/base/nfs"
"shylinux.com/x/icebergs/base/web"
kit "shylinux.com/x/toolkits" kit "shylinux.com/x/toolkits"
) )
@ -160,6 +161,19 @@ func init() {
ctx.DisplayLocal(m, "") ctx.DisplayLocal(m, "")
}}, }},
}) })
ctx.AddRunChecker(func(m *ice.Message, cmd, check string, arg ...string) bool {
switch check {
case nfs.SCRIPT:
if file := kit.ExtChange(ctx.GetCmdFile(m, cmd), nfs.JS); nfs.ExistsFile(m, file) {
ctx.ProcessFloat(m, web.CODE_INNER, file)
}
case nfs.SOURCE:
if file := ctx.GetCmdFile(m, cmd); nfs.ExistsFile(m, file) {
ctx.ProcessFloat(m, web.CODE_INNER, file)
}
}
return false
})
} }
func PlugAction() ice.Actions { func PlugAction() ice.Actions {
return ice.Actions{ return ice.Actions{

View File

@ -7,6 +7,7 @@ import (
"shylinux.com/x/icebergs/base/mdb" "shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/nfs" "shylinux.com/x/icebergs/base/nfs"
"shylinux.com/x/icebergs/base/ssh" "shylinux.com/x/icebergs/base/ssh"
"shylinux.com/x/icebergs/base/web"
"shylinux.com/x/icebergs/misc/git" "shylinux.com/x/icebergs/misc/git"
kit "shylinux.com/x/toolkits" kit "shylinux.com/x/toolkits"
) )
@ -50,6 +51,16 @@ func init() {
} }
}}, }},
}) })
ctx.AddRunChecker(func(m *ice.Message, cmd, check string, arg ...string) bool {
switch check {
case ice.HELP:
if file := kit.ExtChange(ctx.GetCmdFile(m, cmd), nfs.SHY); nfs.ExistsFile(m, file) {
ctx.ProcessFloat(m, web.WIKI_WORD, file)
}
return true
}
return false
})
} }
func WordAction(template string, arg ...ice.Any) ice.Actions { func WordAction(template string, arg ...ice.Any) ice.Actions {
return ice.Actions{ice.CTX_INIT: mdb.AutoConfig(append([]ice.Any{nfs.TEMPLATE, template}, arg...)...)} return ice.Actions{ice.CTX_INIT: mdb.AutoConfig(append([]ice.Any{nfs.TEMPLATE, template}, arg...)...)}