From d46cc20c35226f17edaf047b0b729c2ef63b21b0 Mon Sep 17 00:00:00 2001 From: brunopadz Date: Fri, 8 Oct 2021 16:48:00 -0300 Subject: [PATCH 1/3] feat: support pterm to handle outputs Signed-off-by: brunopadz --- cmd/awsListAll.go | 15 +++++++++++++-- cmd/awsListUnused.go | 27 +++++++++++++++++++++------ go.mod | 1 + go.sum | 23 +++++++++++++++++++++++ 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/cmd/awsListAll.go b/cmd/awsListAll.go index d63070d..3556298 100644 --- a/cmd/awsListAll.go +++ b/cmd/awsListAll.go @@ -6,6 +6,9 @@ import ( "log" "github.com/aws/aws-sdk-go-v2/aws" + + "github.com/pterm/pterm" + "github.com/aws/aws-sdk-go-v2/service/ec2" cfg "github.com/brunopadz/amictl/config" aaws "github.com/brunopadz/amictl/pkg/providers/aws" @@ -36,6 +39,10 @@ func runAll(cmd *cobra.Command, _ []string) error { r := viper.GetStringSlice("aws.regions") + td := pterm.TableData{ + {"AMI ID", "REGION"}, + } + for _, v := range r { s, err := aaws.New(v) if err != nil { @@ -56,10 +63,14 @@ func runAll(cmd *cobra.Command, _ []string) error { } for _, i := range output.Images { - fmt.Println(aws.ToString(i.ImageId)) + td = append(td, []string{aws.ToString(i.ImageId), v}) } - fmt.Println("Total of", len(output.Images), "AMIs in", v) + } + + err = pterm.DefaultTable.WithHasHeader().WithData(td).Render() + if err != nil { + log.Fatalln("Couldn't render the results.") } return nil diff --git a/cmd/awsListUnused.go b/cmd/awsListUnused.go index 225ae45..6ce2a85 100644 --- a/cmd/awsListUnused.go +++ b/cmd/awsListUnused.go @@ -4,7 +4,8 @@ import ( "context" "fmt" "log" - "strings" + + "github.com/pterm/pterm" "github.com/brunopadz/amictl/pkg/commons" @@ -40,6 +41,12 @@ func runUnused(cmd *cobra.Command, _ []string) error { r := viper.GetStringSlice("aws.regions") + d := pterm.TableData{ + {"AMI ID", "REGION"}, + } + + var x []string + for _, v := range r { var AllImages []string @@ -78,7 +85,7 @@ func runUnused(cmd *cobra.Command, _ []string) error { o, err := client.DescribeInstances(context.TODO(), criteria) if err != nil { - log.Fatalln("Deu ruim") + log.Fatalln("Couldn't load instances data.") } for _, a := range o.Reservations { @@ -87,11 +94,19 @@ func runUnused(cmd *cobra.Command, _ []string) error { } } - x := commons.Compare(AllImages, UsedImages) - y := strings.Join(x, "\n") - fmt.Println(y) - fmt.Println("Total of", len(x), "not used AMIs in", v) + x = commons.Compare(AllImages, UsedImages) + + for _, id := range x { + d = append(d, []string{id, v}) + } + } + l := len(d) - 1 + + err = pterm.DefaultTable.WithHasHeader().WithData(d).Render() + + fmt.Println(l, "AMIs are not being utilized.") + return nil } diff --git a/go.mod b/go.mod index 8381b5e..b36ef5f 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/aws/aws-sdk-go-v2 v1.9.1 github.com/aws/aws-sdk-go-v2/config v1.8.2 github.com/aws/aws-sdk-go-v2/service/ec2 v1.17.0 + github.com/pterm/pterm v0.12.31 // indirect github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.8.1 ) diff --git a/go.sum b/go.sum index cf8f7e0..4bfdf34 100644 --- a/go.sum +++ b/go.sum @@ -39,10 +39,15 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs= +github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3kkcZX4hv9Rp8= +github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBExvZqnKYWlII= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/atomicgo/cursor v0.0.1 h1:xdogsqa6YYlLfM+GyClC/Lchf7aiMerFiZQn7soTOoU= +github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk= github.com/aws/aws-sdk-go-v2 v1.9.1 h1:ZbovGV/qo40nrOJ4q8G33AGICzaPI45FHQWJ9650pF4= github.com/aws/aws-sdk-go-v2 v1.9.1/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4= github.com/aws/aws-sdk-go-v2/config v1.8.2 h1:Dqy4ySXFmulRmZhfynm/5CD4Y6aXiTVhDtXLIuUe/r0= @@ -158,6 +163,8 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4 github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gookit/color v1.4.2 h1:tXy44JFSFkKnELV6WaMo/lLfu/meqITX3iAV52do7lk= +github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= @@ -207,6 +214,8 @@ github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaW github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= +github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -229,6 +238,13 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/pterm/pterm v0.12.27/go.mod h1:PhQ89w4i95rhgE+xedAoqous6K9X+r6aSOI2eFF7DZI= +github.com/pterm/pterm v0.12.29/go.mod h1:WI3qxgvoQFFGKGjGnJR849gU0TsEOvKn5Q8LlY1U7lg= +github.com/pterm/pterm v0.12.30/go.mod h1:MOqLIyMOgmTDz9yorcYbcw+HsgoZo3BQfg2wtl3HEFE= +github.com/pterm/pterm v0.12.31 h1:+UFxhtv9Kuz0nIBH7Aqc2AF0QeOlNChb38L2sYPtXiU= +github.com/pterm/pterm v0.12.31/go.mod h1:32ZAWZVXD7ZfG0s8qqHXePte42kdz8ECtRyEejaWgXU= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -261,6 +277,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -422,7 +440,12 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE= +golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= From b394b30e15d7adb212c84882b7f96271e426d771 Mon Sep 17 00:00:00 2001 From: brunopadz Date: Fri, 8 Oct 2021 16:51:31 -0300 Subject: [PATCH 2/3] chore: sort import packages Signed-off-by: brunopadz --- cmd/awsListAll.go | 4 +--- cmd/awsListUnused.go | 6 ++---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/cmd/awsListAll.go b/cmd/awsListAll.go index 3556298..aa07ec9 100644 --- a/cmd/awsListAll.go +++ b/cmd/awsListAll.go @@ -6,12 +6,10 @@ import ( "log" "github.com/aws/aws-sdk-go-v2/aws" - - "github.com/pterm/pterm" - "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/pterm/pterm" "github.com/spf13/cobra" "github.com/spf13/viper" ) diff --git a/cmd/awsListUnused.go b/cmd/awsListUnused.go index 6ce2a85..4f687eb 100644 --- a/cmd/awsListUnused.go +++ b/cmd/awsListUnused.go @@ -5,15 +5,13 @@ import ( "fmt" "log" - "github.com/pterm/pterm" - - "github.com/brunopadz/amictl/pkg/commons" - "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" cfg "github.com/brunopadz/amictl/config" + "github.com/brunopadz/amictl/pkg/commons" aaws "github.com/brunopadz/amictl/pkg/providers/aws" + "github.com/pterm/pterm" "github.com/spf13/cobra" "github.com/spf13/viper" ) From 9aa5b4a2ab5d51a6de74e9ec7ea37c7408bc4b39 Mon Sep 17 00:00:00 2001 From: brunopadz Date: Fri, 8 Oct 2021 17:39:22 -0300 Subject: [PATCH 3/3] chore: set output for aws root command Signed-off-by: brunopadz --- cmd/aws.go | 28 +++------------------------- cmd/root.go | 21 --------------------- 2 files changed, 3 insertions(+), 46 deletions(-) diff --git a/cmd/aws.go b/cmd/aws.go index 6705293..5de1108 100644 --- a/cmd/aws.go +++ b/cmd/aws.go @@ -1,38 +1,16 @@ -/* -Copyright © 2021 NAME HERE - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ package cmd import ( - "fmt" - "github.com/spf13/cobra" ) // awsCmd represents the aws command var awsCmd = &cobra.Command{ Use: "aws", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + Short: "Manage AWS AMIs", + Long: "Manages AWS AMIs by account and region", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Gonna run on") + cmd.Println(cmd.Long) }, } diff --git a/cmd/root.go b/cmd/root.go index 550db58..d407cb4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,24 +1,3 @@ -/* -Copyright © 2021 NAME HERE - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ package cmd import (