Skip to content

Commit

Permalink
feat: add initial work on inspect subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: brunopadz <[email protected]>
  • Loading branch information
brunopadz committed Oct 11, 2021
1 parent 09b3d4d commit f351621
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ var awsCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(awsCmd)

inspect.Flags().StringVarP(&ami, "ami", "a", "", "AMI ID")
inspect.Flags().StringVarP(&region, "region", "r", "", "Region where the AMI was created")

inspect.MarkFlagRequired("ami")
inspect.MarkFlagRequired("region")

}
85 changes: 85 additions & 0 deletions cmd/awsInspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cmd

import (
"context"
"fmt"
"log"

"github.com/pterm/pterm"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
cfg "github.com/brunopadz/amictl/config"
aaws "github.com/brunopadz/amictl/pkg/providers/aws"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func init() {
awsCmd.AddCommand(inspect)
}

var inspect = &cobra.Command{
Use: "inspect",
Short: "Inspect AMI",
Long: `Inspect command shows additional info about AMIs`,
Example: ` amictl aws inspect`,
RunE: runInspect,
}

var (
ami string
region string
)

func runInspect(cmd *cobra.Command, _ []string) error {

var c cfg.Config

err := viper.Unmarshal(&c)
if err != nil {
fmt.Println(err)
}

s, err := aaws.New(region)
if err != nil {
log.Fatalln("Couldn't create a session to AWS.")
}

a := ec2.NewFromConfig(s)

input := &ec2.DescribeImagesInput{
ImageIds: []string{
ami,
},
Owners: []string{
viper.GetString("aws.account"),
},
}

o, err := a.DescribeImages(context.TODO(), input)
if err != nil {
log.Fatalln("Couldn't get AMI data.")
}

for _, v := range o.Images {
pterm.FgLightCyan.Println("Displaying info for:", pterm.NewStyle(pterm.Bold).Sprint(aws.ToString(v.ImageId)))
pterm.FgDarkGray.Println("----------------------------------------------")
fmt.Println("Name:", aws.ToString(v.Name))
fmt.Println("Description:", aws.ToString(v.Description))
fmt.Println("Creation Date:", aws.ToString(v.CreationDate))
fmt.Println("Deprecation Time:", aws.ToString(v.DeprecationTime))
fmt.Println("Root Device Name:", aws.ToString(v.RootDeviceName))
fmt.Println("RAM Disk ID:", aws.ToString(v.RamdiskId))
fmt.Println("Kernel ID:", aws.ToString(v.KernelId))
fmt.Println("Platform Details:", aws.ToString(v.PlatformDetails))
fmt.Println("Public:", aws.ToBool(v.Public))
fmt.Println("Tags:")
for _, t := range v.Tags {
fmt.Println(" ", aws.ToString(t.Key), "=", aws.ToString(t.Value))
}

}

return nil
}

0 comments on commit f351621

Please sign in to comment.