Skip to content

Commit

Permalink
Merge pull request #8 from rwillard/master
Browse files Browse the repository at this point in the history
Made connection method configurable
  • Loading branch information
buger committed May 26, 2016
2 parents a0536bd + b6632d8 commit 71299a1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
16 changes: 14 additions & 2 deletions aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func getEC2Instances(config map[string]string) (instances Instances) {
config["region"] = "us-east-1"
}

if _, ok := config["output_format"]; !ok {
config["output_format"] = "Cloud: {cloud} \tMatched by: {tag_name} = {tag_value} \tAddr: {addr}"
}

auth := aws.Auth{AccessKey: config["access_key"], SecretKey: config["secret_key"]}

e := ec2.New(auth, aws.Regions[config["region"]])
Expand All @@ -44,8 +48,16 @@ func getEC2Instances(config map[string]string) (instances Instances) {
for _, sg := range inst.SecurityGroups {
tags = append(tags, Tag{"Security group", sg.Name})
}

instances[inst.DNSName] = tags
ci := config["connection_interface"]
if ci == "private_ip" {
instances[inst.PrivateIPAddress] = tags
} else if ci == "public_ip" {
instances[inst.IPAddress] = tags
} else if ci == "private_dns" {
instances[inst.PrivateDNSName] = tags
} else {
instances[inst.DNSName] = tags
}
}
}
}
Expand Down
29 changes: 25 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func getMatchedInstances(clouds CloudInstances, filter string) (matched []StrMap
"addr": addr,
"tag_name": tag.Name,
"tag_value": tag.Value,
"instance_name": getInstanceName(tags),
})

break
Expand All @@ -91,8 +92,28 @@ func getMatchedInstances(clouds CloudInstances, filter string) (matched []StrMap
return
}

func formatMatchedInstance(inst StrMap) string {
return "Cloud: " + inst["cloud"] + "\tMatched by: " + inst["tag_name"] + "=" + inst["tag_value"] + "\tAddr: " + inst["addr"]
func formatMatchedInstance(inst StrMap, output string) string {
c := strings.SplitAfter(output, "{")
for i := 1; i < len(c); i++ {
s := strings.SplitN(c[i], "}", 2)
c[i] = getStringValue(inst, s[0])
output = strings.Replace(output, "{" + s[0] + "}", c[i], -1)
}
return output
}

func getStringValue(inst StrMap, s string) string{
if len(inst[s]) > 0 {
return inst[s]
}
return "{" + s + "}"
}

func getInstanceName(tags []Tag) string {
for _, tag := range tags {
if tag.Name == "Name" { return tag.Value }
}
return ""
}

func main() {
Expand All @@ -113,7 +134,7 @@ func main() {
matched_instance = match[0]
} else {
for i, host := range match {
fmt.Println(strconv.Itoa(i+1)+") ", formatMatchedInstance(host))
fmt.Println(strconv.Itoa(i+1)+") ", formatMatchedInstance(host, config[host["cloud"]]["output_format"]))
}
fmt.Print("Choose instance: ")

Expand All @@ -136,7 +157,7 @@ func main() {
}

fmt.Println("Connecting to instance:")
fmt.Println(formatMatchedInstance(matched_instance))
fmt.Println(formatMatchedInstance(matched_instance, config[matched_instance["cloud"]]["output_format"]))
}

if len(args) == 0 {
Expand Down

0 comments on commit 71299a1

Please sign in to comment.