-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
72 lines (61 loc) · 1.44 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
package main
import (
"os"
"fmt"
"github.com/codegangsta/cli"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
const Version string = "0.1.0"
func main() {
app := cli.NewApp()
app.Name = "ec2ls"
app.Usage = "ls for AWS EC2"
app.Version = Version
app.Author = "Naoki Nomoto"
app.Email = "[email protected]"
app.Action = func(c *cli.Context) {
run()
}
app.Run(os.Args)
}
func run() int {
sess, err := session.NewSession()
if err != nil {
panic(err)
}
svc := ec2.New(sess, &aws.Config{Region: aws.String("ap-northeast-1")})
resp, err := svc.DescribeInstances(nil)
if err != nil {
panic(err)
}
for _, res := range resp.Reservations {
for _, inst := range res.Instances {
var name = ""
for _, tag := range inst.Tags {
if *tag.Key == "Name" {
name = *tag.Value
}
}
instanceID := *inst.InstanceId
state := *inst.State.Name
var publicDNSName = ""
var publicIpAddress = ""
var privateIpAddress = ""
if (inst.PublicDnsName != nil) {
publicDNSName = *inst.PublicDnsName
}
if (inst.PublicIpAddress != nil) {
publicIpAddress = *inst.PublicIpAddress
}
if (inst.PrivateIpAddress != nil) {
privateIpAddress = *inst.PrivateIpAddress
}
fmt.Printf(
"\x1b[33m%-8s\x1b[0m\t%-20s\t%-30s\t%-15s\t%-50s\t%-15s\n",
state, instanceID, name, publicIpAddress, publicDNSName, privateIpAddress)
}
}
return 0
}