-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (70 loc) · 1.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
flag "github.com/spf13/pflag"
"log"
"os"
"strings"
"text/tabwriter"
)
func main() {
id := flag.BoolP("id", "i", false, "show instance-ID")
status := flag.BoolP("status", "s", false, "show instance status")
instanceType := flag.BoolP("type", "t", false, "show instance type")
zone := flag.BoolP("zone", "z", false, "show availability-zone")
help := flag.BoolP("help", "h", false, "show help")
detailed := flag.BoolP("details", "d", false, "show all information")
flag.Parse()
if *help {
flag.PrintDefaults()
os.Exit(0)
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
ec2Svc := ec2.New(sess)
result, err := ec2Svc.DescribeInstances(nil)
if err != nil {
log.Fatalf("error describing instances: %v", err)
}
query := strings.Join(flag.Args(), " ")
b := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
for _, r := range result.Reservations {
for _, i := range r.Instances {
for _, t := range i.Tags {
if *t.Key == "Name" && matches(*t.Value, query) {
s := *t.Value
if i.PrivateIpAddress != nil {
s = s + "\t" + *i.PrivateIpAddress
}
if (*id || *detailed) && i.InstanceId != nil {
s = s + "\t" + *i.InstanceId
}
if (*status || *detailed) && i.State.Name != nil {
s = s + "\t" + *i.State.Name
}
if (*instanceType || *detailed) && i.InstanceType != nil {
s = s + "\t" + *i.InstanceType
}
if (*zone || *detailed) && i.Placement.AvailabilityZone != nil {
s = s + "\t" + *i.Placement.AvailabilityZone
}
fmt.Fprintln(b, s)
}
}
}
}
b.Flush()
}
func matches(value string, query string) bool {
if query == "" {
return true
}
l, q := strings.ToLower(value), strings.ToLower(query)
if strings.Contains(l, q) {
return true
}
return false
}