-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
101 lines (80 loc) · 1.92 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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
)
const (
pageSize = 12
)
var (
version string
commit string
)
var execCommand = exec.Command
func main() {
var projectFilter string
var vmFilter string
var printVersion bool
flag.StringVar(&projectFilter, "p", "", "Project name, can be partial match")
flag.StringVar(&vmFilter, "m", "", "VM name, can be partial match")
flag.BoolVar(&printVersion, "v", false, "Print the version")
flag.Parse()
if printVersion {
fmt.Printf("gssh, version %s", version)
return
}
var selectedProject projectDetails
var selectedVM vmDetails
projects, err := getGcloudProjects()
if err != nil {
fmt.Print(err)
os.Exit(1)
}
filteredProjects := filterProjects(projects, projectFilter)
if len(filteredProjects) == 0 {
fmt.Print("No projects found based on project filter.")
return
} else if len(filteredProjects) == 1 {
selectedProject = filteredProjects[0]
} else {
selectedProject = selectProject(filteredProjects)
}
vms, err := getGcloudVMs(selectedProject)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
filteredVMs := filterVMs(vms, vmFilter)
if len(filteredVMs) == 0 {
fmt.Print("No VMs found based on project and VM filter.")
return
} else if len(filteredVMs) == 1 {
selectedVM = filteredVMs[0]
} else {
selectedVM = selectVM(filteredVMs)
}
fmt.Printf("Using %s as selected project.\n", selectedProject.ProjectID)
fmt.Printf("Using %s as selected VM in zone %s.\n", selectedVM.Name, selectedVM.Zone)
args := buildGcloudArgs(selectedVM, selectedProject)
cmd := exec.Command("gcloud", args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Run()
}
func buildGcloudArgs(selectedVM vmDetails, selectedProject projectDetails) (args []string) {
args = []string{
"beta",
"compute",
"ssh",
"--tunnel-through-iap",
"--zone",
selectedVM.Zone,
"--project",
selectedProject.ProjectID,
selectedVM.Name,
}
return
}