Skip to content

Commit

Permalink
apply go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
buger committed Mar 10, 2014
1 parent 74c2266 commit 5887d19
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 163 deletions.
320 changes: 159 additions & 161 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,201 +2,199 @@ package main

import (
"fmt"
"github.com/go-yaml/yaml"
"github.com/buger/cloud-ssh/provider"
"github.com/go-yaml/yaml"
"io/ioutil"
"log"
"os"
"os/exec"
"log"
"strings"
"runtime"
"io/ioutil"
"regexp"
"strconv"
"regexp"
"runtime"
"strconv"
"strings"
)

type CloudInstances map[string]provider.Instances
type StrMap map[string]string
type Config map[string]StrMap

func splitHostname(str string) (user string, hostname string) {
if arr := strings.Split(str, "@"); len(arr) > 1 {
return arr[0], arr[1]
} else {
return "", str
}
if arr := strings.Split(str, "@"); len(arr) > 1 {
return arr[0], arr[1]
} else {
return "", str
}
}

func joinHostname(user string, hostname string) string {
if user != "" {
return user + "@" + hostname
} else {
return hostname
}
if user != "" {
return user + "@" + hostname
} else {
return hostname
}
}

func getTargetHostname(args []string) (user string, hostname string, arg_idx int) {
for idx, arg := range args {
if !strings.HasPrefix(arg, "-") {
if idx == 0 {
hostname = arg
arg_idx = idx
break
} else {
if !strings.HasPrefix(args[idx-1], "-") {
hostname = arg
arg_idx = idx
break
}
}
}
}

user, hostname = splitHostname(hostname)

return
for idx, arg := range args {
if !strings.HasPrefix(arg, "-") {
if idx == 0 {
hostname = arg
arg_idx = idx
break
} else {
if !strings.HasPrefix(args[idx-1], "-") {
hostname = arg
arg_idx = idx
break
}
}
}
}

user, hostname = splitHostname(hostname)

return
}

func getInstances(config Config) (clouds CloudInstances) {
clouds = make(CloudInstances)

for name, cfg := range config {
for k,v := range cfg {
cfg["name"] = name

if k == "provider" {
switch v {
case "aws":
clouds[name] = provider.GetEC2Instances(cfg)
default:
log.Println("Unknown provider: ", v)
}
}
}
}

return
func getInstances(config Config) (clouds CloudInstances) {
clouds = make(CloudInstances)

for name, cfg := range config {
for k, v := range cfg {
cfg["name"] = name

if k == "provider" {
switch v {
case "aws":
clouds[name] = provider.GetEC2Instances(cfg)
default:
log.Println("Unknown provider: ", v)
}
}
}
}

return
}

func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}

func readConfig() (config Config) {
config = make(Config)

prefferedPaths := []string{
"./cloud-ssh.yaml",
userHomeDir() + "/.ssh/cloud-ssh.yaml",
"/etc/cloud-ssh.yaml",
}

var content []byte

for _, path := range prefferedPaths {
if _, err := os.Stat(path); err == nil {
fmt.Println("Found config:", path)
content, err = ioutil.ReadFile(path)

if err != nil {
log.Fatal("Error while reading config: ", err)
}
}
}

if os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" {
config["default"] = make(StrMap)
config["default"]["access_key"] = os.Getenv("AWS_ACCESS_KEY_ID")
config["default"]["secret_key"] = os.Getenv("AWS_SECRET_ACCESS_KEY")
config["default"]["region"] = os.Getenv("AWS_REGION")
config["default"]["provider"] = "aws"
}

if len(content) == 0 {
if len(config) == 0 {
fmt.Println("Can't find any configuration or ENV variables. Check http://github.com/buger/cloud-ssh for documentation.")
}
return
} else if err := yaml.Unmarshal(content, &config); err != nil {
log.Fatal(err)
}

return
config = make(Config)

prefferedPaths := []string{
"./cloud-ssh.yaml",
userHomeDir() + "/.ssh/cloud-ssh.yaml",
"/etc/cloud-ssh.yaml",
}

var content []byte

for _, path := range prefferedPaths {
if _, err := os.Stat(path); err == nil {
fmt.Println("Found config:", path)
content, err = ioutil.ReadFile(path)

if err != nil {
log.Fatal("Error while reading config: ", err)
}
}
}

if os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" {
config["default"] = make(StrMap)
config["default"]["access_key"] = os.Getenv("AWS_ACCESS_KEY_ID")
config["default"]["secret_key"] = os.Getenv("AWS_SECRET_ACCESS_KEY")
config["default"]["region"] = os.Getenv("AWS_REGION")
config["default"]["provider"] = "aws"
}

if len(content) == 0 {
if len(config) == 0 {
fmt.Println("Can't find any configuration or ENV variables. Check http://github.com/buger/cloud-ssh for documentation.")
}
return
} else if err := yaml.Unmarshal(content, &config); err != nil {
log.Fatal(err)
}

return
}


func getMatchedInstances(clouds CloudInstances, filter string) (matched []StrMap) {

// Fuzzy matching (like SublimeText)
filter = strings.Join(strings.Split(filter,""), ".*?")

rHost := regexp.MustCompile(filter)

for cloud, instances := range clouds {
for addr, tags := range instances {
for _, tag := range tags {
if rHost.MatchString(tag.Value) {
matched = append(matched, StrMap{
"cloud": cloud,
"addr": addr,
"tag_name": tag.Name,
"tag_value": tag.Value,
})

break
}
}
}
}

return
// Fuzzy matching (like SublimeText)
filter = strings.Join(strings.Split(filter, ""), ".*?")

rHost := regexp.MustCompile(filter)

for cloud, instances := range clouds {
for addr, tags := range instances {
for _, tag := range tags {
if rHost.MatchString(tag.Value) {
matched = append(matched, StrMap{
"cloud": cloud,
"addr": addr,
"tag_name": tag.Name,
"tag_value": tag.Value,
})

break
}
}
}
}

return
}

func formatMatchedInstance(inst StrMap) string {
return "Cloud: " + inst["cloud"] + "\tMatched by: " + inst["tag_name"] + "=" + inst["tag_value"] + "\tAddr: " + inst["addr"]
return "Cloud: " + inst["cloud"] + "\tMatched by: " + inst["tag_name"] + "=" + inst["tag_value"] + "\tAddr: " + inst["addr"]
}

func main() {
config := readConfig()
instances := getInstances(config)

args := os.Args[1:len(os.Args)]

user, hostname, arg_idx := getTargetHostname(args)

match := getMatchedInstances(instances, hostname)


if len(match) == 0 {
fmt.Println("Can't find cloud instance, trying to connect anyway")
} else if len(match) == 1 {
hostname = match[0]["addr"]
fmt.Println("Found clound instance:")
fmt.Println(formatMatchedInstance(match[0]))
} else {
fmt.Println("Found multiple instances:")
for i, host := range match {
fmt.Println(strconv.Itoa(i+1)+") ", formatMatchedInstance(host))
}
fmt.Print("Choose instance: ")

var i int
_, err := fmt.Scanf("%d", &i)

if err != nil || i > len(match)+1 {
log.Fatal("Wrong index")
}

hostname = match[i-1]["addr"]
}

args[arg_idx] = joinHostname(user, hostname)
config := readConfig()
instances := getInstances(config)

args := os.Args[1:len(os.Args)]

user, hostname, arg_idx := getTargetHostname(args)

match := getMatchedInstances(instances, hostname)

if len(match) == 0 {
fmt.Println("Can't find cloud instance, trying to connect anyway")
} else if len(match) == 1 {
hostname = match[0]["addr"]
fmt.Println("Found clound instance:")
fmt.Println(formatMatchedInstance(match[0]))
} else {
fmt.Println("Found multiple instances:")
for i, host := range match {
fmt.Println(strconv.Itoa(i+1)+") ", formatMatchedInstance(host))
}
fmt.Print("Choose instance: ")

var i int
_, err := fmt.Scanf("%d", &i)

if err != nil || i > len(match)+1 {
log.Fatal("Wrong index")
}

hostname = match[i-1]["addr"]
}

args[arg_idx] = joinHostname(user, hostname)

cmd := exec.Command("ssh", args...)
cmd.Stdin = os.Stdin
Expand Down
4 changes: 2 additions & 2 deletions provider/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func GetEC2Instances(config map[string]string) (instances Instances) {

if inst.DNSName != "" {
var tags []Tag

for _, tag := range inst.Tags {
tags = append(tags, Tag{"Tag", tag.Value})
tags = append(tags, Tag{tag.Key, tag.Value})
}

for _, sg := range inst.SecurityGroups {
Expand Down

0 comments on commit 5887d19

Please sign in to comment.