Skip to content
Open
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
19 changes: 19 additions & 0 deletions instance.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"encoding/json"
"fmt"
"net"
"os"
"sort"
Expand Down Expand Up @@ -54,6 +56,23 @@ func (s instances) printTable() {
table.Render()
}

func (s instances) printJson() {
rows := make([]map[string]string, len(s))

for i := 0; i < len(s); i++ {
rows[i] = map[string]string{
"name": s[i].name,
"id": *s[i].InstanceId,
"publicIp": stringify(s[i].PublicIpAddress),
"privateIp": *s[i].PrivateIpAddress,
"key": stringify(s[i].KeyName),
}
}

bytes, _ := json.MarshalIndent(rows, "", " ")
fmt.Print(string(bytes))
}

func (s instances) sort() {
sort.Sort(s)
}
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
debug = kingpin.Flag("debug", "Enable debug mode.").Bool()
regions = kingpin.Flag("region", "AWS Region to query. Repeat for multiple regions.").Short('r').Default(DEFAULT_REGIONS...).Strings()
retries = kingpin.Flag("retries", "Number of times to retry AWS API calls in case of errors.").Default("10").Int()
jsonOut = kingpin.Flag("json", "Output json.").Bool()
)

func init() {
Expand Down Expand Up @@ -103,7 +104,11 @@ func main() {
// Sort and print instances that were returned to us
if len(insts) > 0 {
insts.sort()
insts.printTable()
if *jsonOut {
insts.printJson()
} else {
insts.printTable()
}
}

if *debug {
Expand Down