Skip to content

Commit

Permalink
feat: Add EC2 ls
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Oct 27, 2023
1 parent e162710 commit 694f723
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
95 changes: 95 additions & 0 deletions awscmd/ec2.go
@@ -0,0 +1,95 @@
package awscmd

import (
"context"
"io"
"time"

"github.com/aws/aws-sdk-go/service/ec2"
)

type InputEc2List struct {
Region string
}

type OutputEc2List struct {
Region string
Instances []*ec2Instance
}

type ec2Instance struct {
ID string
Name string
IPv4 string
IPv4private string
Type string
State string
AMI string
Zone string
LaunchTime time.Time
}

func Ec2List(ctx context.Context, input *InputEc2List, w io.Writer) (*OutputEc2List, error) {
sess, err := NewSession(input.Region)
if err != nil {
return nil, err
}
svc := ec2.New(sess)
response, err := svc.DescribeInstancesWithContext(ctx, &ec2.DescribeInstancesInput{})
if err != nil {
return nil, err
}
ec2s := []*ec2Instance{}

for _, reservation := range response.Reservations {
for _, instance := range reservation.Instances {
name := ""
for _, tag := range instance.Tags {
if *tag.Key == "Name" {
name = *tag.Value
}
}
row := &ec2Instance{
ID: toS(instance.InstanceId),
Name: name,
IPv4: toS(instance.PublicIpAddress),
IPv4private: toS(instance.PrivateIpAddress),
Type: toS(instance.InstanceType),
State: ec2InstanceStatus(*instance.State.Code),
AMI: toS(instance.ImageId),
Zone: toS(instance.Placement.AvailabilityZone),
LaunchTime: *instance.LaunchTime,
}
ec2s = append(ec2s, row)
}
}

return &OutputEc2List{
Region: input.Region,
Instances: ec2s,
}, nil
}

func toS(s *string) string {
if s == nil {
return ""
}
return *s
}

func ec2InstanceStatus(state int64) string {
switch state {
case 16:
return "running"
case 32:
return "shutting-down"
case 48:
return "terminated"
case 64:
return "stopping"
case 80:
return "stopped"
default: // 0
return "pending"
}
}
40 changes: 40 additions & 0 deletions main.go
Expand Up @@ -374,6 +374,46 @@ func main() {
return nil
},
},
{
Name: "ec2",
Usage: "EC2 instances",
Subcommands: []*cli.Command{
{
Name: "ls",
Usage: "List all EC2 instances in region.",
Flags: []cli.Flag{
&cli.StringFlag{Name: "region", Usage: "AWS region", Required: true},
},
Action: func(c *cli.Context) error {
input := &awscmd.InputEc2List{
Region: c.String("region"),
}
out, err := awscmd.Ec2List(context.Background(), input, c.App.Writer)
if out != nil {
for _, instance := range out.Instances {
fmt.Fprintf(
c.App.Writer,
"%s %s %s %s %s%15s%s %s %s %s\n",
instance.ID,
instance.Name,
instance.State,
instance.IPv4private,
ctc.ForegroundGreen, instance.IPv4, ctc.Reset,
instance.Type,
instance.Zone,
instance.LaunchTime.Format("2006-01-02"),
)
}
}
if err != nil {
return err
}

return nil
},
},
},
},
{
Name: "ecs",
Usage: "Elastic Container Service",
Expand Down

0 comments on commit 694f723

Please sign in to comment.