From 1ba3a0fa62dfb8567cd17e22a8446c4fe9450ebe Mon Sep 17 00:00:00 2001 From: harveyshao Date: Sun, 20 Mar 2022 12:21:28 +0800 Subject: [PATCH] add mirror --- base/cli/cli.go | 2 +- base/cli/mirrors.go | 57 ++++++++++++++++++++++++++++++++++++++++++++ base/cli/runtime.go | 5 ---- base/cli/system.go | 50 ++++++++++++++++++++++---------------- core/code/compile.go | 10 +------- core/code/js.go | 3 --- core/code/py.go | 9 +------ go.sum | 1 + misc/git/git.go | 2 +- misc/git/repos.go | 1 + 10 files changed, 92 insertions(+), 48 deletions(-) create mode 100644 base/cli/mirrors.go diff --git a/base/cli/cli.go b/base/cli/cli.go index 9c36209f..d3927eaf 100644 --- a/base/cli/cli.go +++ b/base/cli/cli.go @@ -6,4 +6,4 @@ const CLI = "cli" var Index = &ice.Context{Name: CLI, Help: "命令模块"} -func init() { ice.Index.Register(Index, nil, RUNTIME, QRCODE, SYSTEM, DAEMON, FOREVER) } +func init() { ice.Index.Register(Index, nil, RUNTIME, QRCODE, MIRROR, SYSTEM, DAEMON, FOREVER) } diff --git a/base/cli/mirrors.go b/base/cli/mirrors.go new file mode 100644 index 00000000..137a30f0 --- /dev/null +++ b/base/cli/mirrors.go @@ -0,0 +1,57 @@ +package cli + +import ( + "strings" + + ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/mdb" + kit "shylinux.com/x/toolkits" +) + +func IsAlpine(m *ice.Message, arg ...string) bool { + if strings.Contains(m.Conf(RUNTIME, "host.OSID"), ALPINE) { + if len(arg) > 0 { + m.Cmd(MIRROR, mdb.CREATE, "cli", arg[0], "cmd", arg[1]) + } + return true + } + return false +} + +const ( + OSID = "OSID" + ALPINE = "alpine" + CENTOS = "centos" + UBUNTU = "ubuntu" +) + +const MIRROR = "mirror" + +func init() { + Index.Merge(&ice.Context{Commands: map[string]*ice.Command{ + MIRROR: {Name: "mirror cli auto", Help: "软件镜像", Action: ice.MergeAction(map[string]*ice.Action{ + ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { + m.Go(func() { + m.Sleep("1s") + IsAlpine(m, "curl", "system apk add curl") + IsAlpine(m, "make", "system apk add make") + IsAlpine(m, "gcc", "system apk add gcc") + IsAlpine(m, "vim", "system apk add vim") + IsAlpine(m, "tmux", "system apk add tmux") + + if IsAlpine(m, "git", "system apk add git"); !IsAlpine(m, "go", "system apk add git go") { + m.Cmd(MIRROR, mdb.CREATE, kit.SimpleKV("cli,cmd", "go", "install download https://golang.google.cn/dl/go1.15.5.linux-amd64.tar.gz usr/local")) + } + + IsAlpine(m, "node", "system apk add nodejs") + IsAlpine(m, "java", "system apk add openjdk8") + IsAlpine(m, "javac", "system apk add openjdk8") + IsAlpine(m, "python", "system apk add python2") + IsAlpine(m, "python2", "system apk add python2") + IsAlpine(m, "python3", "system apk add python3") + }) + }}, + mdb.CREATE: {Name: "create cli cmd", Help: "创建"}, + }, mdb.HashAction(mdb.SHORT, "cli", mdb.FIELD, "time,cli,cmd"))}, + }}) +} diff --git a/base/cli/runtime.go b/base/cli/runtime.go index d9feb4b0..fbd81e86 100644 --- a/base/cli/runtime.go +++ b/base/cli/runtime.go @@ -112,11 +112,6 @@ const ( LINUX = "linux" DARWIN = "darwin" WINDOWS = "windows" - - OSID = "OSID" - ALPINE = "alpine" - CENTOS = "centos" - UBUNTU = "ubuntu" ) const ( SHELL = "SHELL" diff --git a/base/cli/system.go b/base/cli/system.go index 660cfafa..08dad4c4 100644 --- a/base/cli/system.go +++ b/base/cli/system.go @@ -15,6 +15,34 @@ import ( ) func _system_cmd(m *ice.Message, arg ...string) *exec.Cmd { + // 定制目录 + if text := kit.ReadFile(ice.ETC_PATH); len(text) > 0 { + if file := _system_find(m, arg[0], strings.Split(text, ice.NL)...); file != "" { + m.Debug("cmd: %v", file) + arg[0] = file + } + } + // 环境变量 + env := kit.Simple(m.Optionv(CMD_ENV)) + for i := 0; i < len(env)-1; i += 2 { + if env[i] == PATH { + if file := _system_find(m, arg[0], strings.Split(env[i+1], ice.DF)...); file != "" { + m.Debug("cmd: %v", file) + arg[0] = file + } + } + } + // 自动安装 + if _system_find(m, arg[0]) == "" { + if cmds := m.Cmd(MIRROR, arg[0]).Append("cmd"); cmds != "" { + m.Cmd(kit.Split(cmds)) + if file := _system_find(m, arg[0]); file != "" { + m.Debug("cmd: %v", file) + arg[0] = file + } + } + } + cmd := exec.Command(arg[0], arg[1:]...) // 运行目录 @@ -23,29 +51,10 @@ func _system_cmd(m *ice.Message, arg ...string) *exec.Cmd { nfs.MkdirAll(m, cmd.Dir) } } - // 环境变量 - env := kit.Simple(m.Optionv(CMD_ENV)) for i := 0; i < len(env)-1; i += 2 { - if cmd.Env = append(cmd.Env, kit.Format("%s=%s", env[i], env[i+1])); env[i] == PATH { - if file := _system_find(m, arg[0], strings.Split(env[i+1], ice.DF)...); file != "" { - m.Debug("cmd: %v", file) - cmd.Path = file - } - } + cmd.Env = append(cmd.Env, kit.Format("%s=%s", env[i], env[i+1])) } - - // 定制目录 - if text := kit.ReadFile(ice.ETC_PATH); len(text) > 0 { - if file := _system_find(m, arg[0], strings.Split(text, ice.NL)...); file != "" { - m.Debug("cmd: %v", file) - cmd.Path = file - } - } - if _system_find(m, cmd.Path) == "" { - } - m.Debug("cmd: %v", cmd.Path) - if len(cmd.Env) > 0 { m.Log_EXPORT(CMD_ENV, cmd.Env) } @@ -157,7 +166,6 @@ func init() { if strings.HasSuffix(arg[0], ".sh") { arg = []string{"sh", path.Join("src", arg[0])} } - m.Debug("arg %v", arg) _system_exec(m, _system_cmd(m, arg...)) }}, }}) diff --git a/core/code/compile.go b/core/code/compile.go index f8309338..a3e39b90 100644 --- a/core/code/compile.go +++ b/core/code/compile.go @@ -2,14 +2,12 @@ package code import ( "path" - "strings" ice "shylinux.com/x/icebergs" "shylinux.com/x/icebergs/base/cli" "shylinux.com/x/icebergs/base/mdb" "shylinux.com/x/icebergs/base/nfs" "shylinux.com/x/icebergs/base/tcp" - "shylinux.com/x/icebergs/base/web" kit "shylinux.com/x/toolkits" ) @@ -53,10 +51,9 @@ func init() { m.Cmdy(nfs.DIR, ice.SRC, nfs.DIR_CLI_FIELDS, kit.Dict(nfs.DIR_REG, `.*\.go$`)).Sort(nfs.PATH) }}, INSTALL: {Name: "compile", Help: "安装", Hand: func(m *ice.Message, arg ...string) { - if strings.Contains(m.Cmdx(cli.RUNTIME, kit.Keys(tcp.HOST, cli.OSID)), cli.ALPINE) { + if cli.IsAlpine(m) { cli.PushStream(m) m.Cmd(cli.SYSTEM, "apk", "add", GIT, GO) - m.Cmd(cli.SYSTEM, GO, "get", "shylinux.com/x/ice") return } if m.Cmdx(cli.SYSTEM, nfs.FIND, GIT) == "" { @@ -64,7 +61,6 @@ func init() { m.Echo(ice.FALSE) return } - m.Cmd(INSTALL, web.DOWNLOAD, "https://golang.google.cn/dl/go1.15.5.linux-amd64.tar.gz", ice.USR_LOCAL) }}, BINPACK: {Name: "binpack", Help: "打包", Hand: func(m *ice.Message, arg ...string) { m.Cmdy(AUTOGEN, BINPACK) @@ -73,10 +69,6 @@ func init() { m.Cmd(COMPILE, ice.SRC_RELAY_GO, path.Join(ice.USR_PUBLISH, RELAY)) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - if m.Cmdx(cli.SYSTEM, nfs.FIND, GO) == "" && m.Cmdx(COMPILE, INSTALL) == ice.FALSE { - return - } - // 下载依赖 _autogen_version(m.Spawn()) m.Cmd(cli.SYSTEM, GO, "get", "shylinux.com/x/ice") diff --git a/core/code/js.go b/core/code/js.go index 14d45989..63b9d158 100644 --- a/core/code/js.go +++ b/core/code/js.go @@ -57,9 +57,6 @@ func init() { m.ProcessCommand(kit.Select("can.code.inner.plugin", key), kit.Simple()) }}, mdb.ENGINE: {Hand: func(m *ice.Message, arg ...string) { - if !InstallSoftware(m.Spawn(), NODE, m.Configv(INSTALL)) { - return - } m.Cmdy(cli.SYSTEM, NODE, "-e", kit.Join(_js_main_script(m, arg...), ice.NL)).SetAppend() m.Echo(ice.NL) }}, diff --git a/core/code/py.go b/core/code/py.go index fa6c41b7..332abad5 100644 --- a/core/code/py.go +++ b/core/code/py.go @@ -15,19 +15,12 @@ const ( ) func init() { - Index.Merge(&ice.Context{Configs: map[string]*ice.Config{ - PY: {Name: "py", Help: "脚本", Value: kit.Data(INSTALL, kit.List(kit.Dict( - cli.OSID, cli.ALPINE, ice.CMD, kit.List("apk", "add", PYTHON2), - )))}, - }, Commands: map[string]*ice.Command{ + Index.Merge(&ice.Context{Commands: map[string]*ice.Command{ PY: {Name: "py", Help: "脚本", Action: map[string]*ice.Action{ ice.CTX_INIT: {Hand: func(m *ice.Message, arg ...string) { m.Cmd(mdb.ENGINE, mdb.CREATE, PY, m.PrefixKey()) }}, mdb.ENGINE: {Hand: func(m *ice.Message, arg ...string) { - if !InstallSoftware(m.Spawn(), PYTHON2, m.Configv(INSTALL)) { - return - } if kit.FileExists(kit.Path(arg[2], arg[1])) { m.Cmdy(cli.SYSTEM, PYTHON2, kit.Path(arg[2], arg[1])) } else if b, ok := ice.Info.Pack[path.Join(arg[2], arg[1])]; ok && len(b) > 0 { diff --git a/go.sum b/go.sum index a7d1e4e4..d260f76e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +shylinux.com/x/go-qrcode v0.0.1 h1:/eOGqMj1qtgs9Ymd12zTUa1gcJZs9S92kj2lb0QzKsE= shylinux.com/x/go-qrcode v0.0.1/go.mod h1:KAbtU+KwiiABMZ/CJ0zh9PI2AX82Uf9rRYcQ4ODm4po= shylinux.com/x/toolkits v0.5.3 h1:DuMHEKiPbaoU+0PEw9CtZldaM8bLLaQ6Dx0Qaqfz3SY= shylinux.com/x/toolkits v0.5.3/go.mod h1:8LbYHe7oxBIqb6s4MSOD+4d28QvPdvkyCVtwB/JW7AA= diff --git a/misc/git/git.go b/misc/git/git.go index a6e70c3d..d1133cec 100644 --- a/misc/git/git.go +++ b/misc/git/git.go @@ -33,4 +33,4 @@ var Index = &ice.Context{Name: GIT, Help: "代码库", Configs: map[string]*ice. }}, }} -func init() { code.Index.Register(Index, &web.Frame{}, REPOS, GIT) } +func init() { code.Index.Register(Index, &web.Frame{}, REPOS) } diff --git a/misc/git/repos.go b/misc/git/repos.go index 43e0ad6a..cf8717cc 100644 --- a/misc/git/repos.go +++ b/misc/git/repos.go @@ -59,6 +59,7 @@ func init() { m.Cmd(nfs.DIR, ice.USR, "name,path").Table(func(index int, value map[string]string, head []string) { _repos_insert(m, value[mdb.NAME], value[nfs.PATH]) }) + cli.IsAlpine(m, "git", "apk add git") }}, mdb.CREATE: {Name: "create repos branch name path", Help: "添加", Hand: func(m *ice.Message, arg ...string) { m.Option(mdb.NAME, kit.Select(strings.TrimSuffix(path.Base(m.Option(REPOS)), ".git"), m.Option(mdb.NAME)))