-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
55 lines (48 loc) · 1.3 KB
/
ssh.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 (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
)
type GCPInstance struct {
Name string `json:"name"`
Zone string `json:"zone"`
}
func ssh(project string) error {
instanceCmd := exec.Command("gcloud", "compute", "--project", project, "instances", "list", "--format", "json")
instanceOut, err := instanceCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("gcloud compute instances list: %v, out: %s", err, string(instanceOut))
}
instances := []GCPInstance{}
err = json.NewDecoder(bytes.NewReader(instanceOut)).Decode(&instances)
if err != nil {
return err
}
names := make([]string, 0, len(instances))
for _, instance := range instances {
names = append(names, instance.Name)
}
name := fzf(names)
fmt.Println(name)
for _, instance := range instances {
fmt.Println(instance.Name)
if instance.Name == name {
zoneParts := strings.Split(instance.Zone, "/")
zone := zoneParts[len(zoneParts)-1]
cmd := exec.Command("gcloud", "compute", "ssh", "--tunnel-through-iap", "--project", project, "--zone", zone, name)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("gcloud compute ssh: %v", err)
}
return nil
}
}
return fmt.Errorf("BUG: selected instance not found")
}