From 85109288aaea4a7a769fde6fbd66087468b9b257 Mon Sep 17 00:00:00 2001 From: riteshsonawane1372 Date: Fri, 16 Jun 2023 21:12:18 +0530 Subject: [PATCH 1/4] List Security grp --- cmd/sg.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ pkg/ec2/getSG.go | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 cmd/sg.go create mode 100644 pkg/ec2/getSG.go diff --git a/cmd/sg.go b/cmd/sg.go new file mode 100644 index 0000000..9b22787 --- /dev/null +++ b/cmd/sg.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "fmt" + "log" + "os" + "text/tabwriter" + + "github.com/spf13/cobra" + "github.com/surajincloud/awsctl/pkg/ec2" +) + + + +var sgCmd = &cobra.Command{ + + Use: "sgrp", + Short: "Print Security Groups", + Long: ` + For Example: $ awsctl get sgrp + `, + Run: getSG, + +} + +func getSG(cmd *cobra.Command, args[]string){ + + var sgroup []ec2.SECURITYGRP + + sgroup,err:= ec2.DescribeSecurityGroup(cmd,args) + if err!=nil{ + log.Fatal("Unable to get Security Group") + } + + w:= tabwriter.NewWriter(os.Stdout,18,5,3,' ',tabwriter.TabIndent) + defer w.Flush() + + fmt.Fprintln(w,"NAME","\t","GROUP ID","\t","DESCRIPTION") + + for _,i:= range sgroup{ + + fmt.Fprintln(w,i.SGName,"\t", + i.SGId,"\t", + i.SGDescription,"\t", + ) + + } + + + +} + +func init(){ + getCmd.AddCommand(sgCmd) +} \ No newline at end of file diff --git a/pkg/ec2/getSG.go b/pkg/ec2/getSG.go new file mode 100644 index 0000000..54bea75 --- /dev/null +++ b/pkg/ec2/getSG.go @@ -0,0 +1,42 @@ +package ec2 + +import ( + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/spf13/cobra" +) + + + +type SECURITYGRP struct { + + SGName string + SGId string + SGDescription string + +} + + +func DescribeSecurityGroup(cmd *cobra.Command, args []string) ([]SECURITYGRP,error){ + + var securityGrp []SECURITYGRP + + ctx,client:= Ec2Client(cmd,args) + input:= &ec2.DescribeSecurityGroupsInput{} + info,err:= client.DescribeSecurityGroups(ctx,input) + + + for _, i:= range info.SecurityGroups{ + securityGrp=append(securityGrp, SECURITYGRP{ + SGName: aws.ToString(i.GroupName), + SGId: aws.ToString(i.GroupId), + SGDescription: aws.ToString(i.Description), + + }) + } + + return securityGrp,err + +} + + From 4b5ccfdd5ff51befcf2f4681ae114b184fc7410a Mon Sep 17 00:00:00 2001 From: riteshsonawane1372 Date: Mon, 19 Jun 2023 13:52:25 +0530 Subject: [PATCH 2/4] +/n --- cmd/sg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/sg.go b/cmd/sg.go index 9b22787..caae1d0 100644 --- a/cmd/sg.go +++ b/cmd/sg.go @@ -52,4 +52,4 @@ func getSG(cmd *cobra.Command, args[]string){ func init(){ getCmd.AddCommand(sgCmd) -} \ No newline at end of file +} From 0f7ff9c77100f09300cd9347236db4388eebc2ae Mon Sep 17 00:00:00 2001 From: riteshsonawane1372 Date: Sat, 1 Jul 2023 10:57:35 +0530 Subject: [PATCH 3/4] camel case sg --- cmd/sg.go | 33 ++++++++++++++------------------- pkg/ec2/getSG.go | 36 ++++++++++++++---------------------- 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/cmd/sg.go b/cmd/sg.go index caae1d0..9d9cc2f 100644 --- a/cmd/sg.go +++ b/cmd/sg.go @@ -10,46 +10,41 @@ import ( "github.com/surajincloud/awsctl/pkg/ec2" ) - - var sgCmd = &cobra.Command{ - Use: "sgrp", + Use: "sg", Short: "Print Security Groups", Long: ` - For Example: $ awsctl get sgrp + For Example: $ awsctl get sg `, Run: getSG, - } -func getSG(cmd *cobra.Command, args[]string){ +func getSG(cmd *cobra.Command, args []string) { - var sgroup []ec2.SECURITYGRP + var sgroup []ec2.SecurityGroup - sgroup,err:= ec2.DescribeSecurityGroup(cmd,args) - if err!=nil{ + sgroup, err := ec2.DescribeSecurityGroup(cmd, args) + if err != nil { log.Fatal("Unable to get Security Group") } - w:= tabwriter.NewWriter(os.Stdout,18,5,3,' ',tabwriter.TabIndent) + w := tabwriter.NewWriter(os.Stdout, 18, 5, 3, ' ', tabwriter.TabIndent) defer w.Flush() - fmt.Fprintln(w,"NAME","\t","GROUP ID","\t","DESCRIPTION") + fmt.Fprintln(w, "NAME", "\t", "GROUP ID", "\t", "DESCRIPTION") - for _,i:= range sgroup{ + for _, i := range sgroup { - fmt.Fprintln(w,i.SGName,"\t", - i.SGId,"\t", - i.SGDescription,"\t", - ) + fmt.Fprintln(w, i.SGName, "\t", + i.SGId, "\t", + i.SGDescription, "\t", + ) } - - } -func init(){ +func init() { getCmd.AddCommand(sgCmd) } diff --git a/pkg/ec2/getSG.go b/pkg/ec2/getSG.go index 54bea75..68596b2 100644 --- a/pkg/ec2/getSG.go +++ b/pkg/ec2/getSG.go @@ -6,37 +6,29 @@ import ( "github.com/spf13/cobra" ) - - -type SECURITYGRP struct { - - SGName string - SGId string - SGDescription string - +type SecurityGroup struct { + SGName string + SGId string + SGDescription string } +func DescribeSecurityGroup(cmd *cobra.Command, args []string) ([]SecurityGroup, error) { -func DescribeSecurityGroup(cmd *cobra.Command, args []string) ([]SECURITYGRP,error){ + var securityGrp []SecurityGroup - var securityGrp []SECURITYGRP + ctx, client := Ec2Client(cmd, args) + input := &ec2.DescribeSecurityGroupsInput{} + info, err := client.DescribeSecurityGroups(ctx, input) - ctx,client:= Ec2Client(cmd,args) - input:= &ec2.DescribeSecurityGroupsInput{} - info,err:= client.DescribeSecurityGroups(ctx,input) - - - for _, i:= range info.SecurityGroups{ - securityGrp=append(securityGrp, SECURITYGRP{ - SGName: aws.ToString(i.GroupName), - SGId: aws.ToString(i.GroupId), + for _, i := range info.SecurityGroups { + securityGrp = append(securityGrp, SecurityGroup{ + SGName: aws.ToString(i.GroupName), + SGId: aws.ToString(i.GroupId), SGDescription: aws.ToString(i.Description), - }) } - return securityGrp,err + return securityGrp, err } - From cbb08997ac951b86d3ec40fd7d93360c9bdd48ec Mon Sep 17 00:00:00 2001 From: riteshsonawane1372 Date: Wed, 5 Jul 2023 14:57:27 +0530 Subject: [PATCH 4/4] sg -> securitygroup --- cmd/sg.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/sg.go b/cmd/sg.go index 9d9cc2f..b305a7b 100644 --- a/cmd/sg.go +++ b/cmd/sg.go @@ -15,11 +15,30 @@ var sgCmd = &cobra.Command{ Use: "sg", Short: "Print Security Groups", Long: ` - For Example: $ awsctl get sg + For Example: $ awsctl get securitygroup + $ awsctl get securitygroups + $ awsctl get sg `, Run: getSG, } +var securitygrp=&cobra.Command{ + Use: "securitygroup", + Short: "Print Security Groups", + Run: getSG, +} +var securitygrps=&cobra.Command{ + Use:"securitygroups", + Short: "Print Security Groups", + Run:getSG, +} + +var group=[]*cobra.Command{ + securitygrp, + securitygrps, + sgCmd, +} + func getSG(cmd *cobra.Command, args []string) { var sgroup []ec2.SecurityGroup @@ -45,6 +64,8 @@ func getSG(cmd *cobra.Command, args []string) { } + + func init() { - getCmd.AddCommand(sgCmd) + getCmd.AddCommand(group...) }