-
Notifications
You must be signed in to change notification settings - Fork 7
/
ls.go
73 lines (67 loc) · 1.7 KB
/
ls.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
package cliaws
import (
"context"
"fmt"
"github.com/alexflint/go-arg"
"github.com/nathants/libaws/lib"
"os"
)
func init() {
lib.Commands["ec2-ls"] = ec2Ls
lib.Args["ec2-ls"] = ec2LsArgs{}
}
type ec2LsArgs struct {
Selectors []string `arg:"positional" help:"instance-id | dns-name | private-dns-name | tag | vpc-id | subnet-id | security-group-id | ip-address | private-ip-address"`
State string `arg:"-s,--state" default:"" help:"running | pending | terminated | stopped"`
Dns bool `arg:"-d, --dns" help:"include public dns"`
PrivateIP bool `arg:"-p, --private-ip" help:"include private ipv4"`
}
func (ec2LsArgs) Description() string {
return "\nlist ec2 instances\n"
}
func ec2Ls() {
var args ec2LsArgs
arg.MustParse(&args)
ctx := context.Background()
instances, err := lib.EC2ListInstances(ctx, args.Selectors, args.State)
if err != nil {
lib.Logger.Fatal("error: ", err)
}
fmt.Fprintln(os.Stderr, "name", "type", "state", "id", "image", "kind", "security-group", "tags")
count := 0
for _, instance := range instances {
count++
subnet := "-"
if instance.SubnetId != nil {
subnet = *instance.SubnetId
}
dns := "-"
if instance.PublicIpAddress != nil {
dns = *instance.PublicDnsName
}
if args.Dns {
subnet += " " + dns
}
ip := "-"
if instance.PrivateIpAddress != nil {
ip = *instance.PrivateIpAddress
}
if args.PrivateIP {
subnet += " " + ip
}
fmt.Println(
lib.EC2NameColored(instance),
*instance.InstanceType,
*instance.State.Name,
*instance.InstanceId,
*instance.ImageId,
lib.EC2Kind(instance),
subnet,
lib.EC2SecurityGroups(instance.SecurityGroups),
lib.EC2Tags(instance.Tags),
)
}
if count == 0 {
os.Exit(1)
}
}