Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made connection method configurable #8

Merged
merged 6 commits into from
May 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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