diff --git a/aws.go b/aws.go index bc65cf6..9b1f641 100644 --- a/aws.go +++ b/aws.go @@ -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"]]) @@ -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 + } } } } diff --git a/main.go b/main.go index aaccf82..28e5c0f 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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() { @@ -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: ") @@ -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 {