-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
55 lines (51 loc) · 1.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"encoding/base64"
"fmt"
"github.com/deanishe/awgo"
"github.com/hanjm/errors"
"github.com/hanjm/log"
"os"
"strings"
)
var wf = aw.New()
func run() {
// # 解析命令行参数
// ## 第1个参数 查询条件
var query string
if len(os.Args) > 1 {
query = os.Args[1]
}
// ## 第2个参数 用 iTerm的 哪个 profile 打开
var iTermProfileName = "Default"
if len(os.Args) > 2 {
iTermProfileName = os.Args[2]
}
// ## 第3个参数 自定义的 iTerm user var key
var userVarKey = "sshHost"
if len(os.Args) > 3 {
userVarKey = os.Args[3]
}
// # 解析 ssh config
hosts, err := GetSSHHostList()
if err != nil {
err = errors.Errorf(err, "failed to get ssh hosts")
wf.FatalError(err)
return
}
log.Infof("hosts:%+v, query:%s", hosts, query)
for _, v := range hosts {
if strings.Contains(v, query) {
// 设置变量的命令来自于 https://iterm2.com/shell_integration/zsh, 参考: https://www.iterm2.com/documentation-shell-integration.html
userVarValue := base64.StdEncoding.EncodeToString([]byte(v))
cmd := fmt.Sprintf(`echo "\033]1337;SetUserVar=%s=%s\007";clear;ssh %s`, userVarKey, userVarValue, v)
// 用换行符来传递多个参数到 apple script
arg := strings.Join([]string{iTermProfileName, cmd}, "\n")
wf.NewItem(v).Arg(arg).Valid(true)
}
}
wf.SendFeedback()
}
func main() {
wf.Run(run)
}