Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Eun committed Jan 30, 2020
0 parents commit bc48911
Show file tree
Hide file tree
Showing 413 changed files with 211,885 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/.vscode
/dist
68 changes: 68 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
builds:
-
env:
- CGO_ENABLED=0
goos:
- aix
- darwin
- dragonfly
- freebsd
- linux
- netbsd
- openbsd
- solaris
- windows
goarch:
- 386
- arm
- arm64
- amd64
archives:
- name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
replacements:
darwin: darwin
linux: linux
windows: windows
386: i386
amd64: x86_64
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
nfpms:
-
id: gcloud-ssh
package_name: gcloud-ssh

replacements:
amd64: 64-bit
386: 32-bit
darwin: macOS
linux: linux

homepage: https://github.com/Eun/gcloud-ssh
maintainer: Tobias Salzmann <[email protected]>
description: A interactive way to connect to your Google Cloud VM instances

license: MIT

# Formats to be generated.
formats:
- deb
- rpm

# Package epoch.
# Defaults to empty.
epoch: 1

7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2020 Tobias Salzmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# gcloud-ssh
A interactive way to connect to your Google Cloud VM instances


![demo](demo.gif "demo")
Binary file added demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/Eun/gcloud-ssh

go 1.12

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/manifoldco/promptui v0.7.0
github.com/valyala/fastjson v1.4.5
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
34 changes: 34 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/manifoldco/promptui v0.7.0 h1:3l11YT8tm9MnwGFQ4kETwkzpAwY2Jt9lCrumCUW4+z4=
github.com/manifoldco/promptui v0.7.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/valyala/fastjson v1.4.5 h1:uSuLfXk2LzRtzwd3Fy5zGRBe0Vs7zhs11vjdko32xb4=
github.com/valyala/fastjson v1.4.5/go.mod h1:nV6MsjxL2IMJQUoHDIrjEI7oLyeqK6aBD7EFWPsvP8o=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
128 changes: 128 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
}
}
27 changes: 27 additions & 0 deletions vendor/github.com/alecthomas/template/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/alecthomas/template/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bc48911

Please sign in to comment.