Skip to content

Commit

Permalink
move codes to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Sep 13, 2024
1 parent 3a34b89 commit 1ef39ce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
13 changes: 0 additions & 13 deletions ecrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/Songmu/prompter"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecr"
ecrTypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
Expand Down Expand Up @@ -157,15 +156,3 @@ func imageTag(d ecrTypes.ImageDetail) (string, bool) {
return untaggedStr, false
}
}

func arnToName(name, removePrefix string) string {
if arn.IsARN(name) {
a, _ := arn.Parse(name)
return strings.Replace(a.Resource, removePrefix, "", 1)
}
return name
}

func clusterArnToName(arn string) string {
return arnToName(arn, "cluster/")
}
16 changes: 1 addition & 15 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/Songmu/prompter"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecr"
ecrTypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/goccy/go-yaml"
Expand Down Expand Up @@ -170,7 +169,7 @@ func (g *Generator) generateLambdaConfig(ctx context.Context, config *Config) er
}

func (g *Generator) generateRepositoryConfig(ctx context.Context, config *Config) error {
repos, err := g.repositories(ctx)
repos, err := ecrRepositories(ctx, ecr.NewFromConfig(g.awsCfg))
if err != nil {
return err
}
Expand Down Expand Up @@ -202,16 +201,3 @@ func (g *Generator) generateRepositoryConfig(ctx context.Context, config *Config
})
return nil
}

func (g *Generator) repositories(ctx context.Context) ([]ecrTypes.Repository, error) {
repos := make([]ecrTypes.Repository, 0)
p := ecr.NewDescribeRepositoriesPaginator(ecr.NewFromConfig(g.awsCfg), &ecr.DescribeRepositoriesInput{})
for p.HasMorePages() {
repo, err := p.NextPage(ctx)
if err != nil {
return nil, err
}
repos = append(repos, repo.Repositories...)
}
return repos, nil
}
29 changes: 29 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"context"
"log"

"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/ecr"
ecrTypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/lambda"
Expand Down Expand Up @@ -79,3 +83,28 @@ func lambdaFunctions(ctx context.Context, client *lambda.Client) ([]lambdaTypes.
}
return fns, nil
}

func ecrRepositories(ctx context.Context, client *ecr.Client) ([]ecrTypes.Repository, error) {
repos := make([]ecrTypes.Repository, 0)
p := ecr.NewDescribeRepositoriesPaginator(client, &ecr.DescribeRepositoriesInput{})
for p.HasMorePages() {
repo, err := p.NextPage(ctx)
if err != nil {
return nil, err
}
repos = append(repos, repo.Repositories...)
}
return repos, nil
}

func arnToName(name, removePrefix string) string {
if arn.IsARN(name) {
a, _ := arn.Parse(name)
return strings.Replace(a.Resource, removePrefix, "", 1)
}
return name
}

func clusterArnToName(arn string) string {
return arnToName(arn, "cluster/")
}

0 comments on commit 1ef39ce

Please sign in to comment.