-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
201 lines (172 loc) · 4.34 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/chzyer/readline"
"github.com/manifoldco/promptui"
"github.com/urfave/cli/v2"
)
var (
connectTo string
version = "v0.1.2"
appAuthor *cli.Author = &cli.Author{
Name: "LuciferInLove",
Email: "[email protected]",
}
)
const (
sshExecutable = "ssh"
)
func main() {
app := &cli.App{
Name: "dynamic-sshmenu-aws",
Usage: "builds dynamic aws instances addresses list like sshmenu",
Authors: []*cli.Author{appAuthor},
Action: action,
UsageText: "dynamic-sshmenu-aws [-- <args>]",
Version: version,
HideHelpCommand: true,
}
app.Commands = []*cli.Command{}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "tags",
Aliases: []string{"t"},
Usage: "instance tags in \"key1:value1,value2;key2:value1\" format. If undefined, full list will be shown",
Value: "",
},
&cli.StringFlag{
Name: "display-name",
Aliases: []string{"d"},
Usage: "key of instance tag to display its values in results",
Value: "Name",
},
&cli.BoolFlag{
Name: "public-ip",
Aliases: []string{"p"},
Usage: "use public ip instead of private",
Value: false,
},
&cli.StringFlag{
Name: "ssh-username",
Aliases: []string{"u"},
Usage: "ssh username. If undefined, the current user will be used",
Value: "",
},
}
app.RunAndExitOnError()
}
func promptSelect(instances []string) (string, error) {
var selectionSymbol string
searcher := func(input string, index int) bool {
instance, err := parseInstance(instances[index])
if err != nil {
return false
}
name := strings.Replace(strings.ToLower(instance.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
promptui.IconInitial = ""
if runtime.GOOS == "windows" {
selectionSymbol = "->"
} else {
selectionSymbol = "➜"
}
var funcMap = promptui.FuncMap
funcMap["parse"] = parseInstance
templates := promptui.SelectTemplates{
Label: "{{ . | cyan }}",
Active: fmt.Sprintf("%s %s", selectionSymbol,
"{{ (printf \"%v.\t%v\t| %v (%v)\" (. | parse).Number (. | parse).IP (. | parse).Name (. | parse).Zone) | green }}",
),
Inactive: " {{ (printf \"%v.\t%v\t| %v (%v)\" (. | parse).Number (. | parse).IP (. | parse).Name (. | parse).Zone) | white }}",
FuncMap: funcMap,
}
prompt := promptui.Select{
Label: "Select a target (press \"q\" for exit)",
Items: instances,
Templates: &templates,
Size: 20,
Searcher: searcher,
HideSelected: true,
Keys: &promptui.SelectKeys{
Next: promptui.Key{
Code: readline.CharNext,
Display: "↓",
},
Prev: promptui.Key{
Code: readline.CharPrev,
Display: "↑",
},
PageUp: promptui.Key{
Code: readline.CharForward,
Display: "→",
},
PageDown: promptui.Key{
Code: readline.CharBackward,
Display: "←",
},
Search: promptui.Key{
Code: '/',
Display: "/",
},
Exit: promptui.Key{
Code: 'q',
Display: "q",
},
},
}
_, result, err := prompt.Run()
if err != nil {
return result, err
}
return result, nil
}
func action(c *cli.Context) error {
username := c.String("ssh-username")
instances, err := getSliceOfInstances(c.String("tags"), c.String("display-name"), c.Bool("public-ip"))
if err != nil {
if err.Error() == "WrongTagDefinition" {
cli.ShowAppHelp(c)
return fmt.Errorf("\nIncorrect Usage. Wrong tag definition in flag -t")
}
return fmt.Errorf("Listing AWS instances:\n%w", err)
}
result, err := promptSelect(instances)
if err != nil {
switch err {
case promptui.ErrInterrupt:
return nil
case promptui.ErrEOF:
return fmt.Errorf("Unexpected end of file: \"%w\"", err)
default:
return err
}
}
instanceFromResult, err := parseInstance(result)
if err != nil {
return err
}
sshPath, err := exec.LookPath(sshExecutable)
if err != nil {
return fmt.Errorf("%w", err)
}
if username == "" {
connectTo = instanceFromResult.IP
} else {
connectTo = username + "@" + instanceFromResult.IP
}
cmd := exec.Command(sshPath, connectTo)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("Command finished with an error, ssh: %w", err)
}
return nil
}