-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (108 loc) · 2.94 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
package main
import (
"os/exec"
"regexp"
"fmt"
"strings"
"os"
"github.com/manifoldco/promptui"
"github.com/valyala/fastjson"
"gopkg.in/alecthomas/kingpin.v2"
)
// generated by goreleaser
var version string
var commit string
var date string
func main() {
project := kingpin.Flag("project", "The Google Cloud Platform project name to use for this invocation. If omitted, then the current project is assumed").PlaceHolder("PROJECT_ID").String()
kingpin.Version(fmt.Sprintf("%s %s %s", version, commit, date))
kingpin.Parse()
cmd := &exec.Cmd{
Args: []string{"gcloud"},
}
var err error
cmd.Path, err = exec.LookPath(cmd.Args[0])
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if project != nil && *project != "" {
cmd.Args = append([]string{"--project", *project}, cmd.Args...)
}
cmd.Args = append(cmd.Args, "--format", "json", "compute", "instances", "list")
fmt.Print("Getting instances...\r")
out, err := cmd.Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
fmt.Println(exitError.Error())
fmt.Println(string(exitError.Stderr))
os.Exit(1)
}
fmt.Println(err.Error())
os.Exit(1)
}
v, err := fastjson.ParseBytes(out)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
type Instance struct {
Name string
Zone string
Project string
Status string
}
instances := v.GetArray()
dialogInstances := make([]Instance, len(instances))
re, err := regexp.Compile(`projects/([a-z0-9\-]+)/zones/([a-z0-9\-]+)`)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
for i, instance := range instances {
projectAndZone := instance.Get("zone").String()
matches := re.FindStringSubmatch(projectAndZone)
if len(matches) != 3 {
fmt.Printf("unable to find 3 matches in %s\n", projectAndZone)
os.Exit(1)
}
name := strings.Trim(instance.Get("name").String(), `"`)
status := strings.Trim(instance.Get("status").String(), `"`)
dialogInstances[i] = Instance{
Name: name,
Zone: matches[2],
Project: matches[1],
Status: status,
}
}
prompt := promptui.Select{
Items: dialogInstances,
HideSelected: true,
Templates: &promptui.SelectTemplates{
Label: "Connect to:",
Active: fmt.Sprintf(`%s {{ .Name | underline }} ({{ .Status }})`, promptui.IconSelect),
Inactive: " {{ .Name }} ({{ .Status }})",
Selected: "",
Details: `
--------- Detail ----------
{{ "Name:" | faint }} {{ .Name }}
{{ "Status:" | faint }} {{ .Status }}
{{ "Project:" | faint }} {{ .Project }}
{{ "Zone:" | faint }} {{ .Zone }}`,
},
}
i, _, err := prompt.Run()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
cmd = exec.Command("gcloud", "compute", "--project", dialogInstances[i].Project, "ssh", "--zone", dialogInstances[i].Zone, dialogInstances[i].Name)
fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
if err = cmd.Run(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}