diff --git a/etc/conf/auto.sh b/etc/conf/auto.sh index 70dda522..2466c3fc 100644 --- a/etc/conf/auto.sh +++ b/etc/conf/auto.sh @@ -8,14 +8,12 @@ ctx_url=$ctx_dev"/code/zsh" ctx_get=${ctx_get:="wget -q"} ctx_curl=${ctx_curl:="curl"} ctx_head=${ctx_head:="Content-Type: application/json"} -ctx_sync=${ctx_sync:=""} ctx_sid=${ctx_sid:=""} ctx_silent=${ctx_silent:=""} +ctx_err=${ctx_err:="/dev/null"} ctx_welcome=${ctx_welcome:="^_^ Welcome to Context world ^_^"} ctx_goodbye=${ctx_goodbye:="^_^ Goodbye to Context world ^_^"} -ctx_bind=${ctx_bind:="bind -x"} -ctx_null=${ctx_null:="false"} ShyRight() { [ "$1" = "" ] && return 1 @@ -27,6 +25,9 @@ ShyRight() { ShyEcho() { ShyRight "$ctx_silent" || echo "$@" } +ShyLog() { + echo "$@" > $ctx_err +} ShyWord() { echo "$*"|sed -e 's/\ /%20/g' -e 's/\n/\\n/g' @@ -64,11 +65,8 @@ ShyPost() { ShyDownload() { ${ctx_curl} -s "${ctx_url}" -F "cmd=download" -F "arg=$1" -F "sid=$ctx_sid" } -ShyUpdate() { - ${ctx_curl} -s "${ctx_dev}/publish/$1" > $1 -} ShyUpload() { - ${ctx_curl} -s "${ctx_url}" -F "cmd=upload" -F "sid=$ctx_sid" -F "upload=@$1" + ${ctx_curl} -s "${ctx_url}" -F "cmd=upload" -F "upload=@$1" -F "sid=$ctx_sid" } ShyBench() { ${ctx_curl} -s "${ctx_dev}/publish/boot.sh" | sh -s installs context @@ -107,9 +105,8 @@ ShyLogin() { } ShyFavor() { [ "$READLINE_LINE" != "" ] && set $READLINE_LINE && READLINE_LINE="" - [ "$1" != "" ] && ctx_tab=$1 - [ "$2" != "" ] && ctx_note=$2 - ShyPost cmd favor arg "`history|tail -n2|head -n1`" tab "${ctx_tab}" note "${ctx_note}" + [ "$1" != "" ] && ctx_tab=$1; [ "$2" != "" ] && ctx_note=$2 + ShyPost cmd favor arg "`history|tail -n1|head -n1`" tab "${ctx_tab}" note "${ctx_note}" } ShyFavors() { [ "$READLINE_LINE" != "" ] && set $READLINE_LINE && READLINE_LINE="" @@ -119,17 +116,12 @@ ShySync() { [ "$ctx_sid" = "" ] && ShyLogin case "$1" in - "historys") tail -n0 -f $HISTFILE | while true; do read line - line=`ShyLine $line` - ShyPost arg "$line" cmd history FORMAT "$HISTTIMEFORMAT" - echo $line - done - ;; "history") ctx_end=`history|tail -n1|awk '{print $1}'` - ctx_tail=`expr $ctx_end - $ctx_begin` - ShyEcho "sync $ctx_begin-$ctx_end count $ctx_tail to $ctx_dev" - history|tail -n $ctx_tail |while read line; do + ctx_begin=${ctx_begin:=$ctx_end} + ctx_count=`expr $ctx_end - $ctx_begin` + ShyEcho "sync $ctx_begin-$ctx_end count $ctx_count to $ctx_dev" + history|tail -n $ctx_count |while read line; do ShySends historys sub "$line" done ctx_begin=$ctx_end @@ -140,29 +132,14 @@ ShySync() { ShySyncs() { case "$1" in "base") - ShySync df - ShySync env - ShySync free + ShySync df &>/dev/null + ShySync env &>/dev/null + ShySync free &>/dev/null ShySync history ;; *) esac } -ShyHistory() { - case "$SHELL" in - "/bin/zsh") - ShySync df - ShySync env - ShySync free - ShySync historys &>/dev/null & - ctx_sync=$! - ;; - *) ;; - esac -} -ShyRecord() { - script $1 -} ShyHelp() { ShyPost cmd help arg "$@" } @@ -171,7 +148,6 @@ ShyInit() { case "$SHELL" in "/bin/zsh") - ctx_bind=${ctx_null} PROMPT='%![%*]%c$ ' ;; *) @@ -182,17 +158,21 @@ ShyInit() { ;; esac - ${ctx_bind} '"\C-g\C-r":ShySyncs base' - ${ctx_bind} '"\C-g\C-f":ShyFavor' - ${ctx_bind} '"\C-gf":ShyFavor' - ${ctx_bind} '"\C-gF":ShyFavors' + if bind &>/dev/null; then + bind -x '"\C-G\C-R":ShySyncs base' + bind -x '"\C-G\C-F":ShyFavor' + bind -x '"\C-Gf":ShyFavor' + bind -x '"\C-GF":ShyFavors' + elif bindkey &>/dev/null; then + bindkey -s '\C-G\C-R' 'ShySyncs base\n' + setopt nosharehistory + fi - echo ${ctx_welcome} echo "url: ${ctx_url}" echo "pid: $$" - echo "pane: $TMUX_PANE" echo "begin: ${ctx_begin}" echo "share: ${ctx_share}" + echo "pane: $TMUX_PANE" } ShyInit && trap ShyLogout EXIT diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index e04b089b..3d38c45c 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -253,8 +253,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", }, }, Help: "服务升级"}, "missyou": {Name: "missyou", Value: map[string]interface{}{ - "path": "usr/local/work", - "local": "usr/local", + "path": "usr/local/work", "local": "usr/local", }, Help: "任务管理"}, "imq": {Name: "imq", Value: map[string]interface{}{ "data": map[string]interface{}{ @@ -1209,7 +1208,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", }}, "missyou": {Name: "missyou [topic] [name [action]]", Help: "任务管理", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { // 任务主题 - topic := "hello" + topic := "index" if len(arg) > 0 && (arg[0] == "" || m.Cmds("nfs.path", path.Join(m.Conf("cli.project", "plugin.path"), arg[0]))) { topic, arg = arg[0], arg[1:] } @@ -1220,14 +1219,14 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", name := strings.TrimSuffix(value["name"], "/") m.Push("create_time", value["time"]) m.Push("you", name) - m.Push("status", kit.Select("stop", "start", m.Confs("nfs.node", name))) + m.Push("status", kit.Select("stop", "start", m.Confs("ssh.node", name))) }) m.Sort("you", "str_r").Sort("status").Table() return } // 任务命名 - if !strings.Contains(arg[0], "-") { + if m.Option("topic", topic); !strings.Contains(arg[0], "-") { arg[0] = m.Time("20060102-") + arg[0] } @@ -1235,8 +1234,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", if m.Option("dream", arg[0]); m.Confs("ssh.node", arg[0]) { switch kit.Select("", arg, 1) { case "stop": - m.Cmdy("ssh._route", arg[0], "context", "cli", "quit", 0) - m.Cmd("web.code.dream", "exit", arg[0]) + m.Cmdy("ssh._route", arg[0], "cli.quit", 0) default: m.Echo(arg[0]) } @@ -1253,7 +1251,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", "cmd_dir", p, "cmd_daemon", "true", "cmd_env", "PATH", os.Getenv("PATH"), - "cmd_env", "ctx_type", m.Option("topic", kit.Select(topic, arg, 1)), + "cmd_env", "ctx_type", m.Option("topic"), "cmd_env", "ctx_home", m.Conf("runtime", "boot.ctx_home"), "cmd_env", "ctx_ups", fmt.Sprintf("127.0.0.1%s", m.Conf("runtime", "boot.ssh_port")), "cmd_env", "ctx_box", fmt.Sprintf("http://127.0.0.1%s", m.Conf("runtime", "boot.web_port")), diff --git a/src/contexts/ctx/type.go b/src/contexts/ctx/type.go index f9859b40..17f7f323 100644 --- a/src/contexts/ctx/type.go +++ b/src/contexts/ctx/type.go @@ -245,22 +245,25 @@ func (m *Message) Result(arg ...interface{}) string { } func (m *Message) Push(key interface{}, arg ...interface{}) *Message { + keys := []string{} switch key := key.(type) { case string: + keys = strings.Split(key, " ") + case []string: + keys = key + } + + for _, key := range keys { switch m.Option("table.format") { case "table": m.Add("append", "key", key) key = "value" } - return m.Add("append", key, arg...) - case []string: - for _, key := range key { - switch m.Option("table.format") { - case "table": - m.Add("append", "key", key) - key = "value" - } - m.Add("append", key, kit.Select(" ", kit.Format(kit.Chain(arg[0], key)))) + switch value := arg[0].(type) { + case map[string]interface{}: + m.Add("append", key, kit.Select(" ", kit.Format(kit.Chain(value, key)))) + default: + m.Add("append", key, arg...) } } return m diff --git a/src/examples/code/code.go b/src/examples/code/code.go index 782b1770..bc13d9da 100644 --- a/src/examples/code/code.go +++ b/src/examples/code/code.go @@ -120,9 +120,9 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", "dream": {Name: "dream", Help: "使命必达", Value: map[string]interface{}{ "layout": map[string]interface{}{ "three": []interface{}{ + "rename-window -t $dream:1 source", "split-window -t $dream:1.1", "split-window -v -t $dream:1.2", - "rename-window -t $dream:1 source", "select-layout -t $dream:1 main-horizontal", "new-window -t $dream:2 -n docker", @@ -137,24 +137,21 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", }, }, "topic": map[string]interface{}{ - "hello": map[string]interface{}{ + "index": map[string]interface{}{ "ship": []interface{}{"tip", "miss.md", "task", "feed"}, "git": []interface{}{"clone https://github.com/shylinux/context"}, - "layout": []interface{}{}, - "tmux": []interface{}{ - "split-window -t $dream:1.1", - "new-window -t $dream:2", - "split-window -t $dream:2.1", - "new-window -t $dream:3", - "split-window -t $dream:3.1", + "layout": []interface{}{"three"}, "tmux": []interface{}{ + "send-keys -t $dream.1.1 pwd", }, }, }, - "share": map[string]interface{}{}, + "share": map[string]interface{}{"meta": map[string]interface{}{ + "fields": "river dream favor story stage order expire", + }}, }}, }, Commands: map[string]*ctx.Command{ - "dream": {Name: "dream", Help: "使命必达", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { + "dream": {Name: "dream init name [topic]", Help: "使命必达", Hand: func(m *ctx.Message, c *ctx.Context, cmd string, arg ...string) (e error) { switch arg[0] { case "init": // 检查会话 @@ -162,71 +159,53 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", if !m.Cmds(tmux, "has-session", "-t", arg[1]) { break } - topic := kit.Select("hello", kit.Select(m.Option("topic"), arg, 2)) // 下载代码 home := path.Join(m.Conf("missyou", "path"), arg[1], m.Conf("missyou", "local")) + topic := kit.Select("index", kit.Select(m.Option("topic"), arg, 2)) git := kit.Trans(m.Confv("prefix", "git"), "cmd_dir", home) m.Confm("dream", []string{"topic", topic, "git"}, func(index int, value string) { value = strings.Replace(value, "$dream", arg[1], -1) - m.Cmdx(git, strings.Split(value, " ")) + m.Cmd(git, strings.Split(value, " ")) }) // 创建终端 - m.Cmds(tmux, "set-environment", "-g", "ctx_share", m.Cmdx("dream", "share")) - m.Cmds(tmux, "new-session", "-ds", arg[1], "cmd_dir", home, "cmd_env", "TMUX", "") - m.Cmds(tmux, "set-environment", "-t", arg[1], "ctx_share", m.Cmdx("dream", "share")) - m.Confm("dream", []string{"layout", m.Conf("dream", []string{"topic", m.Option("topic"), "layout", "0"})}, func(index int, value string) { + share := m.Cmdx("dream", "share", topic) + m.Cmd(tmux, "set-environment", "-g", "ctx_share", share) + m.Cmd(tmux, "new-session", "-ds", arg[1], "cmd_dir", home, "cmd_env", "TMUX", "") + m.Cmd(tmux, "set-environment", "-t", arg[1], "ctx_share", share) + m.Confm("dream", []string{"layout", m.Conf("dream", []string{"topic", topic, "layout", "0"})}, func(index int, value string) { value = strings.Replace(value, "$dream", arg[1], -1) - m.Cmdx(tmux, strings.Split(value, " "), "cmd_dir", home) + m.Cmd(tmux, strings.Split(value, " "), "cmd_dir", home) }) m.Confm("dream", []string{"topic", topic, "tmux"}, func(index int, value string) { value = strings.Replace(value, "$dream", arg[1], -1) - m.Cmdx(tmux, strings.Split(value, " "), "cmd_dir", home) + m.Cmd(tmux, strings.Split(value, " "), "cmd_dir", home) }) + m.Echo(share) - arg = []string{"share", topic} - fallthrough case "share": - topic := kit.Select("hello", kit.Select(m.Option("topic"), arg, 1)) - if len(arg) == 1 { - m.Confm("dream", []string{"topic", kit.Select("hello", topic), "ship"}, func(index int, value string) { + // 模板参数 + topic := kit.Select("index", kit.Select(m.Option("topic"), arg, 1)) + m.Confm("dream", []string{"topic", topic, "ship"}, func(index int, value string) { + if len(arg) < index+3 { arg = append(arg, value) - }) - } - for i := 0; i < 10; i++ { - h := kit.Hashs("uniq")[:6] - if m.Confs("dream", []string{"share", "hash", h}) { - continue + } else if arg[index+2] == "" { + arg[index+2] = value } - m.Conf("dream", []string{"share", "hash", h}, map[string]interface{}{ - "river": m.Option("river"), - "dream": m.Option("dream"), - "favor": kit.Select("tip", arg, 1), - "story": kit.Select("miss.md", arg, 2), - "stage": kit.Select("task", arg, 3), - "order": kit.Select("feed", arg, 4), - "expire": m.Time("10m", "stamp"), - }) - m.Echo(h) - break - } - - case "bind": - m.Confm("dream", []string{"share", "hash", arg[1]}, func(value map[string]interface{}) { - m.Conf("login", []string{"hash", m.Option("sid"), "ship"}, value) }) + // 共享链接 + h := kit.ShortKey(m.Confm("dream", []string{"share", "hash"}), 6) + m.Confv("dream", []string{"share", "hash", h}, map[string]interface{}{ + "river": m.Option("river"), "dream": m.Option("dream"), + "favor": arg[2], "story": arg[3], "stage": arg[4], "order": arg[5], + "expire": m.Time("10m", "stamp"), + }) + m.Echo(h) case "list": m.Confm("dream", "share.hash", func(key string, value map[string]interface{}) { - m.Push("key", key) - m.Push("river", value["river"]) - m.Push("dream", value["dream"]) - m.Push("favor", value["favor"]) - m.Push("story", value["story"]) - m.Push("stage", value["stage"]) - m.Push("order", value["order"]) - m.Push("expire", value["expire"]) + m.Push("key", key).Push(m.Conf("dream", "share.meta.fields"), value) }) m.Table() diff --git a/src/toolkit/core.go b/src/toolkit/core.go index f5406e43..f797bf11 100644 --- a/src/toolkit/core.go +++ b/src/toolkit/core.go @@ -43,6 +43,9 @@ func Duration(arg ...string) time.Duration { return d } func Hash(arg ...interface{}) (string, []string) { + if len(arg) == 0 { + arg = append(arg, "uniq") + } args := []string{} for _, v := range Trans(arg...) { switch v { diff --git a/src/toolkit/misc.go b/src/toolkit/misc.go index b2646133..6d2ad59f 100644 --- a/src/toolkit/misc.go +++ b/src/toolkit/misc.go @@ -289,3 +289,13 @@ func Short(arg interface{}, l int) string { } return "" } + +func ShortKey(list map[string]interface{}, min int, arg ...interface{}) string { + h := Hashs(arg...) + for i := min; i < len(h); i++ { + if _, ok := list[h[:i]]; !ok { + return h[:i] + } + } + return h +}