diff --git a/pkg/cloudfront.go b/pkg/cloudfront.go index df2f1c1..2a94e8a 100644 --- a/pkg/cloudfront.go +++ b/pkg/cloudfront.go @@ -2,12 +2,10 @@ package pkg import ( "fmt" - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/cloudfront" "github.com/aws/aws-sdk-go/service/cloudfront/cloudfrontiface" + "log" ) // getDistributions return all cloudfront distributions from specified region @@ -25,7 +23,7 @@ func getDistributions(client cloudfrontiface.CloudFrontAPI) *cloudfront.ListDist // ParseDistributionsTags parse output from getDistributions and return distribution arn and specified tags. func ParseDistributionsTags(tagsToRead string, client cloudfrontiface.CloudFrontAPI) [][]string { instancesOutput := getDistributions(client) - rows := addHeaders(tagsToRead, "Arn") + rows := addHeadersToCsv(tagsToRead, "Arn") for _, distribution := range instancesOutput.DistributionList.Items { input := &cloudfront.ListTagsForResourceInput{ @@ -39,12 +37,7 @@ func ParseDistributionsTags(tagsToRead string, client cloudfrontiface.CloudFront for _, tag := range distributionTags.Tags.Items { tags[*tag.Key] = *tag.Value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*distribution.ARN}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *distribution.ARN) } return rows } diff --git a/pkg/cloudwatch.go b/pkg/cloudwatch.go index e569ddd..d449a8e 100644 --- a/pkg/cloudwatch.go +++ b/pkg/cloudwatch.go @@ -2,14 +2,12 @@ package pkg import ( "fmt" - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface" "github.com/aws/aws-sdk-go/service/cloudwatchlogs" "github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface" + "log" ) // getCWAlarm return all CloudWatch alarms from specified region @@ -33,7 +31,7 @@ func getCWAlarm(client cloudwatchiface.CloudWatchAPI) []*cloudwatch.MetricAlarm // ParseCwAlarmTags parse output from getCWAlarm and return alarm arn and specified tags. func ParseCwAlarmTags(tagsToRead string, client cloudwatchiface.CloudWatchAPI) [][]string { instancesOutput := getCWAlarm(client) - rows := addHeaders(tagsToRead, "Arn") + rows := addHeadersToCsv(tagsToRead, "Arn") for _, alarm := range instancesOutput { input := &cloudwatch.ListTagsForResourceInput{ @@ -47,12 +45,7 @@ func ParseCwAlarmTags(tagsToRead string, client cloudwatchiface.CloudWatchAPI) [ for _, tag := range cwLogTags.Tags { tags[*tag.Key] = *tag.Value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*alarm.AlarmArn}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *alarm.AlarmArn) } return rows } @@ -78,7 +71,7 @@ func getCWLogGroups(client cloudwatchlogsiface.CloudWatchLogsAPI) []*cloudwatchl // ParseCwLogGroupTags parse output from getInstances and return logGroupName and specified tags. func ParseCwLogGroupTags(tagsToRead string, client cloudwatchlogsiface.CloudWatchLogsAPI) [][]string { instancesOutput := getCWLogGroups(client) - rows := addHeaders(tagsToRead, "LogGroupName") + rows := addHeadersToCsv(tagsToRead, "LogGroupName") for _, logGroup := range instancesOutput { input := &cloudwatchlogs.ListTagsLogGroupInput{ @@ -92,12 +85,7 @@ func ParseCwLogGroupTags(tagsToRead string, client cloudwatchlogsiface.CloudWatc for key, value := range cwLogTags.Tags { tags[key] = *value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*logGroup.LogGroupName}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *logGroup.LogGroupName) } return rows } diff --git a/pkg/config_rule.go b/pkg/config_rule.go index ebad49a..c4d8b69 100644 --- a/pkg/config_rule.go +++ b/pkg/config_rule.go @@ -2,12 +2,10 @@ package pkg import ( "fmt" - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/configservice" "github.com/aws/aws-sdk-go/service/configservice/configserviceiface" + "log" ) // getConfigRules return all config rules from specified region @@ -25,7 +23,7 @@ func getConfigRules(client configserviceiface.ConfigServiceAPI) *configservice.D // ParseConfigRuleTags parse output from getCWAlarm and return alarm arn and specified tags. func ParseConfigRuleTags(tagsToRead string, client configserviceiface.ConfigServiceAPI) [][]string { instancesOutput := getConfigRules(client) - rows := addHeaders(tagsToRead, "Arn") + rows := addHeadersToCsv(tagsToRead, "Arn") for _, rule := range instancesOutput.ConfigRules { input := &configservice.ListTagsForResourceInput{ @@ -39,12 +37,7 @@ func ParseConfigRuleTags(tagsToRead string, client configserviceiface.ConfigServ for _, tag := range configTags.Tags { tags[*tag.Key] = *tag.Value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*rule.ConfigRuleArn}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *rule.ConfigRuleArn) } return rows } diff --git a/pkg/csv.go b/pkg/csv.go index 57a73b2..69ac0cc 100644 --- a/pkg/csv.go +++ b/pkg/csv.go @@ -40,10 +40,19 @@ func ReadCsv(filename string) [][]string { return csvLines } -func addHeaders(tagsToRead string, resourceIdHeader string) [][]string { +func addHeadersToCsv(tagsToRead string, resourceIdHeader string) [][]string { var rows [][]string headers := []string{resourceIdHeader} headers = append(headers, strings.Split(tagsToRead, ",")...) rows = append(rows, headers) return rows } + +func addTagsToCsv(tagsToRead string, tags map[string]string, rows [][]string, resourceId string) [][]string { + var resultTags []string + for _, key := range strings.Split(tagsToRead, ",") { + resultTags = append(resultTags, tags[key]) + } + rows = append(rows, append([]string{resourceId}, resultTags...)) + return rows +} diff --git a/pkg/ec2.go b/pkg/ec2.go index 6044483..8303b6c 100644 --- a/pkg/ec2.go +++ b/pkg/ec2.go @@ -1,12 +1,10 @@ package pkg import ( - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2/ec2iface" + "log" ) // getEC2Instances return all ec2 instances from specified region @@ -29,18 +27,14 @@ func getEC2Instances(client ec2iface.EC2API) []*ec2.Reservation { // ParseEC2Tags parse output from getEC2Instances and return instances id and specified tags. func ParseEC2Tags(tagsToRead string, client ec2iface.EC2API) [][]string { instancesOutput := getEC2Instances(client) - rows := addHeaders(tagsToRead, "Id") + rows := addHeadersToCsv(tagsToRead, "Id") for _, reservation := range instancesOutput { for _, instance := range reservation.Instances { tags := map[string]string{} for _, tag := range instance.Tags { tags[*tag.Key] = *tag.Value } - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*instance.InstanceId}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *instance.InstanceId) } } return rows diff --git a/pkg/ecr.go b/pkg/ecr.go index e1669c0..a9ab1ab 100644 --- a/pkg/ecr.go +++ b/pkg/ecr.go @@ -6,7 +6,6 @@ import ( "github.com/aws/aws-sdk-go/service/ecr" "github.com/aws/aws-sdk-go/service/ecr/ecriface" "log" - "strings" ) func getEcrRepositories(client ecriface.ECRAPI) []*ecr.Repository { @@ -29,7 +28,7 @@ func getEcrRepositories(client ecriface.ECRAPI) []*ecr.Repository { // ParseEcrRepositoriesTags parse output from getEcrRepositories and return repo arn and specified tags. func ParseEcrRepositoriesTags(tagsToRead string, client ecriface.ECRAPI) [][]string { repoList := getEcrRepositories(client) - rows := addHeaders(tagsToRead, "Arn") + rows := addHeadersToCsv(tagsToRead, "Arn") for _, repo := range repoList { repoTags, err := client.ListTagsForResource(&ecr.ListTagsForResourceInput{ResourceArn: repo.RepositoryArn}) if err != nil { @@ -39,12 +38,7 @@ func ParseEcrRepositoriesTags(tagsToRead string, client ecriface.ECRAPI) [][]str for _, tag := range repoTags.Tags { tags[*tag.Key] = *tag.Value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*repo.RepositoryArn}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *repo.RepositoryArn) } return rows } diff --git a/pkg/elastic_search.go b/pkg/elastic_search.go index ed4651c..0397181 100644 --- a/pkg/elastic_search.go +++ b/pkg/elastic_search.go @@ -2,14 +2,12 @@ package pkg import ( "fmt" - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/elasticsearchservice" "github.com/aws/aws-sdk-go/service/elasticsearchservice/elasticsearchserviceiface" "github.com/aws/aws-sdk-go/service/sts" "github.com/aws/aws-sdk-go/service/sts/stsiface" + "log" ) // getElasticSearchDomains return all elasticsearch from specified region @@ -30,7 +28,7 @@ func ParseElasticSearchTags(tagsToRead string, client elasticsearchserviceiface. if err != nil { log.Fatal("Not able to get account id", err) } - rows := addHeaders(tagsToRead, "Arn") + rows := addHeadersToCsv(tagsToRead, "Arn") for _, elasticCacheInstance := range instancesOutput.DomainNames { clusterArn := fmt.Sprintf("arn:aws:es:%s:%s:domain/%s", @@ -47,12 +45,7 @@ func ParseElasticSearchTags(tagsToRead string, client elasticsearchserviceiface. for _, tag := range elasticSearchTags.TagList { tags[*tag.Key] = *tag.Value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{clusterArn}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, clusterArn) } return rows } diff --git a/pkg/elasticache.go b/pkg/elasticache.go index 0ae6ba7..cd74ed9 100644 --- a/pkg/elasticache.go +++ b/pkg/elasticache.go @@ -2,14 +2,12 @@ package pkg import ( "fmt" - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/elasticache" "github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface" "github.com/aws/aws-sdk-go/service/sts" "github.com/aws/aws-sdk-go/service/sts/stsiface" + "log" ) // getElastiCacheClusters return all ElastiCache from specified region @@ -37,7 +35,7 @@ func ParseElastiCacheClusterTags(tagsToRead string, client elasticacheiface.Elas if err != nil { log.Fatal("Not able to get account id", err) } - rows := addHeaders(tagsToRead, "Arn") + rows := addHeadersToCsv(tagsToRead, "Arn") for _, elasticCacheInstance := range instancesOutput { clusterArn := fmt.Sprintf("arn:aws:elasticache:%s:%s:cluster:%s", @@ -54,12 +52,7 @@ func ParseElastiCacheClusterTags(tagsToRead string, client elasticacheiface.Elas for _, tag := range elasticCacheTag.TagList { tags[*tag.Key] = *tag.Value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{clusterArn}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, clusterArn) } return rows } diff --git a/pkg/elb.go b/pkg/elb.go index db78d0f..16b96cc 100644 --- a/pkg/elb.go +++ b/pkg/elb.go @@ -31,7 +31,7 @@ func getElbV2(client elbv2iface.ELBV2API) []*elbv2.LoadBalancer { // ParseElbV2Tags parse output from getInstances and return instances id and specified tags. func ParseElbV2Tags(tagsToRead string, client elbv2iface.ELBV2API) [][]string { instancesOutput := getElbV2(client) - rows := addHeaders(tagsToRead, "Arn") + rows := addHeadersToCsv(tagsToRead, "Arn") for _, elb := range instancesOutput { elbTags, err := client.DescribeTags(&elbv2.DescribeTagsInput{ResourceArns: []*string{elb.LoadBalancerArn}}) if err != nil { @@ -48,7 +48,7 @@ func ParseElbV2Tags(tagsToRead string, client elbv2iface.ELBV2API) [][]string { for _, key := range strings.Split(tagsToRead, ",") { resultTags = append(resultTags, tags[key]) } - rows = append(rows, append([]string{*elb.LoadBalancerArn}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *elb.LoadBalancerArn) } return rows } diff --git a/pkg/iam.go b/pkg/iam.go index 87d41fb..904bbd3 100644 --- a/pkg/iam.go +++ b/pkg/iam.go @@ -42,12 +42,7 @@ func ParseIamUserTags(tagsToRead string, client iamiface.IAMAPI) [][]string { for _, tag := range userTags.Tags { tags[*tag.Key] = *tag.Value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*user.UserName}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *user.UserName) } return rows } @@ -95,7 +90,7 @@ func getIamRoles(client iamiface.IAMAPI) []*iam.Role { // ParseIamRolesTags parse output from getIamRoles and return roles and specified tags. func ParseIamRolesTags(tagsToRead string, client iamiface.IAMAPI) [][]string { usersList := getIamRoles(client) - rows := addHeaders(tagsToRead, "RoleName") + rows := addHeadersToCsv(tagsToRead, "RoleName") for _, role := range usersList { roleTags, err := client.ListRoleTags(&iam.ListRoleTagsInput{RoleName: role.RoleName}) if err != nil { @@ -105,12 +100,7 @@ func ParseIamRolesTags(tagsToRead string, client iamiface.IAMAPI) [][]string { for _, tag := range roleTags.Tags { tags[*tag.Key] = *tag.Value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*role.RoleName}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *role.RoleName) } return rows } diff --git a/pkg/kinesis.go b/pkg/kinesis.go index 6b52478..48c2a4d 100644 --- a/pkg/kinesis.go +++ b/pkg/kinesis.go @@ -2,14 +2,12 @@ package pkg import ( "fmt" - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/firehose" "github.com/aws/aws-sdk-go/service/firehose/firehoseiface" "github.com/aws/aws-sdk-go/service/kinesis" "github.com/aws/aws-sdk-go/service/kinesis/kinesisiface" + "log" ) // getFirehoses return all firehoses from specified region @@ -26,7 +24,7 @@ func getFirehoses(client firehoseiface.FirehoseAPI) *firehose.ListDeliveryStream // ParseKinesisTags parse output from getFirehoses and return firehose name and specified tags. func ParseFirehoseTags(tagsToRead string, client firehoseiface.FirehoseAPI) [][]string { instancesOutput := getFirehoses(client) - rows := addHeaders(tagsToRead, "Name") + rows := addHeadersToCsv(tagsToRead, "Name") for _, stream := range instancesOutput.DeliveryStreamNames { input := &firehose.ListTagsForDeliveryStreamInput{ @@ -40,12 +38,7 @@ func ParseFirehoseTags(tagsToRead string, client firehoseiface.FirehoseAPI) [][] for _, tag := range distributionTags.Tags { tags[*tag.Key] = *tag.Value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*stream}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *stream) } return rows } @@ -71,7 +64,7 @@ func getStreams(client kinesisiface.KinesisAPI) []*string { // ParseKinesisTags parse output from getStreams and return kinesis arn and specified tags. func ParseKinesisTags(tagsToRead string, client kinesisiface.KinesisAPI) [][]string { instancesOutput := getStreams(client) - rows := addHeaders(tagsToRead, "Name") + rows := addHeadersToCsv(tagsToRead, "Name") for _, stream := range instancesOutput { input := &kinesis.ListTagsForStreamInput{ @@ -85,12 +78,7 @@ func ParseKinesisTags(tagsToRead string, client kinesisiface.KinesisAPI) [][]str for _, tag := range distributionTags.Tags { tags[*tag.Key] = *tag.Value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*stream}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *stream) } return rows } diff --git a/pkg/lambda.go b/pkg/lambda.go index d17a3fa..9af61f8 100644 --- a/pkg/lambda.go +++ b/pkg/lambda.go @@ -2,12 +2,10 @@ package pkg import ( "fmt" - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/lambda" "github.com/aws/aws-sdk-go/service/lambda/lambdaiface" + "log" ) // getLambdaFunctions return all lambdas from specified region @@ -31,7 +29,7 @@ func getLambdaFunctions(client lambdaiface.LambdaAPI) []*lambda.FunctionConfigur // ParseLambdaFunctionTags parse output from getLambdaFunctions and return arn and specified tags. func ParseLambdaFunctionTags(tagsToRead string, client lambdaiface.LambdaAPI) [][]string { instancesOutput := getLambdaFunctions(client) - rows := addHeaders(tagsToRead, "Arn") + rows := addHeadersToCsv(tagsToRead, "Arn") for _, lambdaOutput := range instancesOutput { lambdaTags, err := client.ListTags(&lambda.ListTagsInput{Resource: lambdaOutput.FunctionArn}) if err != nil { @@ -41,12 +39,7 @@ func ParseLambdaFunctionTags(tagsToRead string, client lambdaiface.LambdaAPI) [] for key, value := range lambdaTags.Tags { tags[key] = *value } - - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*lambdaOutput.FunctionArn}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *lambdaOutput.FunctionArn) } return rows } diff --git a/pkg/rds.go b/pkg/rds.go index 441772a..20ab491 100644 --- a/pkg/rds.go +++ b/pkg/rds.go @@ -2,12 +2,10 @@ package pkg import ( "fmt" - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/rds" "github.com/aws/aws-sdk-go/service/rds/rdsiface" + "log" ) // getRDSInstances return all rds instances from specified region @@ -31,7 +29,7 @@ func getRDSInstances(client rdsiface.RDSAPI) []*rds.DBInstance { // ParseRDSTags parse output from getRDSInstances and return arn and specified tags. func ParseRDSTags(tagsToRead string, client rdsiface.RDSAPI) [][]string { instancesOutput := getRDSInstances(client) - rows := addHeaders(tagsToRead, "Arn") + rows := addHeadersToCsv(tagsToRead, "Arn") for _, dbInstances := range instancesOutput { rdsTags, err := client.ListTagsForResource(&rds.ListTagsForResourceInput{ResourceName: dbInstances.DBInstanceArn}) if err != nil { @@ -41,11 +39,7 @@ func ParseRDSTags(tagsToRead string, client rdsiface.RDSAPI) [][]string { for _, tag := range rdsTags.TagList { tags[*tag.Key] = *tag.Value } - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*dbInstances.DBInstanceArn}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *dbInstances.DBInstanceArn) } return rows } diff --git a/pkg/redshift.go b/pkg/redshift.go index 322d973..f90e97c 100644 --- a/pkg/redshift.go +++ b/pkg/redshift.go @@ -2,14 +2,12 @@ package pkg import ( "fmt" - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/redshift" "github.com/aws/aws-sdk-go/service/redshift/redshiftiface" "github.com/aws/aws-sdk-go/service/sts" "github.com/aws/aws-sdk-go/service/sts/stsiface" + "log" ) // getRedshiftInstances return all redshift instances from specified region @@ -38,7 +36,7 @@ func ParseRedshiftTags(tagsToRead string, client redshiftiface.RedshiftAPI, stsC if err != nil { log.Fatal("Not able to get account id", err) } - rows := addHeaders(tagsToRead, "Arn") + rows := addHeadersToCsv(tagsToRead, "Arn") for _, redshiftInstances := range instancesOutput { clusterArn := fmt.Sprintf("arn:aws:redshift:%s:%s:cluster:%s", region, *callerIdentity.Account, *redshiftInstances.ClusterIdentifier) @@ -50,11 +48,7 @@ func ParseRedshiftTags(tagsToRead string, client redshiftiface.RedshiftAPI, stsC for _, tag := range redshiftTags.TaggedResources { tags[*tag.Tag.Key] = *tag.Tag.Value } - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{clusterArn}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, clusterArn) } return rows } diff --git a/pkg/s3.go b/pkg/s3.go index 7f0c72d..9961a9e 100644 --- a/pkg/s3.go +++ b/pkg/s3.go @@ -2,13 +2,11 @@ package pkg import ( "fmt" - "log" - "strings" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3/s3iface" + "log" ) // getBuckets return all s3 buckets from specified region @@ -26,7 +24,7 @@ func getBuckets(client s3iface.S3API) *s3.ListBucketsOutput { // ParseS3Tags parse output from getBuckets and return instances id and specified tags. func ParseS3Tags(tagsToRead string, client s3iface.S3API) [][]string { s3Output := getBuckets(client) - rows := addHeaders(tagsToRead, "Name") + rows := addHeadersToCsv(tagsToRead, "Name") for _, bucket := range s3Output.Buckets { s3Tags, err := client.GetBucketTagging(&s3.GetBucketTaggingInput{Bucket: bucket.Name}) if err != nil { @@ -42,11 +40,7 @@ func ParseS3Tags(tagsToRead string, client s3iface.S3API) [][]string { for _, tag := range s3Tags.TagSet { tags[*tag.Key] = *tag.Value } - var resultTags []string - for _, key := range strings.Split(tagsToRead, ",") { - resultTags = append(resultTags, tags[key]) - } - rows = append(rows, append([]string{*bucket.Name}, resultTags...)) + rows = addTagsToCsv(tagsToRead, tags, rows, *bucket.Name) } return rows }