From d4d3d7e4956c1a9cda52ce778a03e7f1c5f7edbc Mon Sep 17 00:00:00 2001 From: Tomoya Amachi Date: Thu, 26 Dec 2019 03:00:57 +0900 Subject: [PATCH] stop using pointer in ImageTag (#7) --- internal/report/table.go | 13 +++++++------ internal/types/types.go | 24 ++++++++++++++---------- pkg/provider/dockerhub/dockerhub.go | 8 ++++---- pkg/provider/ecr/aws.go | 15 +++++++++------ pkg/provider/gcr/gcr.go | 10 +++++----- 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/internal/report/table.go b/internal/report/table.go index 8412f76..c56dfbe 100644 --- a/internal/report/table.go +++ b/internal/report/table.go @@ -19,6 +19,7 @@ type TableWriter struct { func (w TableWriter) Write(tags types.ImageTags) (err error) { table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Tag", "Size", "Created At", "Uploaded At"}) + for _, tag := range tags { table.Append([]string{ strings.Join(tag.Tags, ","), @@ -33,16 +34,16 @@ func (w TableWriter) Write(tags types.ImageTags) (err error) { return nil } -func getBytesize(b *int) string { - if b == nil { +func getBytesize(b int) string { + if b == 0 { return "-" } - return utils.ByteSize(*b) + return utils.ByteSize(b) } -func ttos(t *time.Time) string { - if t == nil { +func ttos(t time.Time) string { + if t.IsZero() { return "NULL" } - return (*t).Format(time.RFC3339) + return t.Format(time.RFC3339) } diff --git a/internal/types/types.go b/internal/types/types.go index 1d8cb5f..b697e8c 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -2,8 +2,10 @@ package types import "time" -const ITEM_PER_PAGE = 10 +// ImagePerPage : +const ImagePerPage = 10 +// RequestOption : container registry information type RequestOption struct { MaxCount int AuthURL string @@ -17,29 +19,31 @@ type RequestOption struct { Timeout time.Duration } +// FilterOption : tag pattern type FilterOption struct { Contain string } +// ImageTag : tag information type ImageTag struct { - Tags []string `json:"tags"` - Byte *int `json:"byte"` - CreatedAt *time.Time `json:"created_at"` - UploadedAt *time.Time `json:"uploaded_at"` + Tags []string `json:"tags"` + Byte int `json:"byte"` + CreatedAt time.Time `json:"created_at"` + UploadedAt time.Time `json:"uploaded_at"` } +// ImageTags : tag information slice type ImageTags []ImageTag func (t ImageTags) Len() int { return len(t) } func (t ImageTags) Swap(i, j int) { t[i], t[j] = t[j], t[i] } func (t ImageTags) Less(i, j int) bool { - if t[i].UploadedAt != nil && t[j].UploadedAt != nil { - return (*(t[i].UploadedAt)).After(*(t[j].UploadedAt)) + if !t[i].UploadedAt.IsZero() && !t[j].UploadedAt.IsZero() { + return (t[i].UploadedAt).After((t[j].UploadedAt)) } - if t[i].CreatedAt != nil && t[j].CreatedAt != nil { - return (*(t[i].CreatedAt)).After(*(t[j].CreatedAt)) + if !t[i].CreatedAt.IsZero() && !t[j].CreatedAt.IsZero() { + return (t[i].CreatedAt).After((t[j].CreatedAt)) } - return true } diff --git a/pkg/provider/dockerhub/dockerhub.go b/pkg/provider/dockerhub/dockerhub.go index faab9cc..36e03c9 100644 --- a/pkg/provider/dockerhub/dockerhub.go +++ b/pkg/provider/dockerhub/dockerhub.go @@ -92,8 +92,8 @@ func (p *DockerHub) convertResultToTag(summaries []ImageSummary) types.ImageTags } tags = append(tags, types.ImageTag{ Tags: tagNames, - Byte: &detail.FullSize, - CreatedAt: &createdAt, + Byte: detail.FullSize, + CreatedAt: createdAt, }) } return tags @@ -113,11 +113,11 @@ func getTagResponse(ctx context.Context, auth dockertypes.AuthConfig, timeout ti } func calcMaxRequestPage(totalCnt, needCnt int, option *types.FilterOption) int { - maxPage := totalCnt/types.ITEM_PER_PAGE + 1 + maxPage := totalCnt/types.ImagePerPage + 1 if needCnt == 0 || option.Contain != "" { return maxPage } - needPage := needCnt/types.ITEM_PER_PAGE + 1 + needPage := needCnt/types.ImagePerPage + 1 if needPage >= maxPage { return maxPage } diff --git a/pkg/provider/ecr/aws.go b/pkg/provider/ecr/aws.go index cf2472b..542eaee 100644 --- a/pkg/provider/ecr/aws.go +++ b/pkg/provider/ecr/aws.go @@ -76,11 +76,15 @@ func (p *ECR) Run(ctx context.Context, domain, repository string, reqOpt *types. continue } + var pushedAt time.Time + if detail.ImagePushedAt != nil { + pushedAt = *detail.ImagePushedAt + } imageTags = append(imageTags, types.ImageTag{ Tags: tags, Byte: getIntByte(detail.ImageSizeInBytes), - CreatedAt: nil, - UploadedAt: detail.ImagePushedAt, + CreatedAt: time.Time{}, + UploadedAt: pushedAt, }) } @@ -110,10 +114,9 @@ func getSession(option *types.RequestOption) (*session.Session, error) { }) } -func getIntByte(b *int64) *int { +func getIntByte(b *int64) int { if b == nil { - return nil + return 0 } - i := int(*b) - return &i + return int(*b) } diff --git a/pkg/provider/gcr/gcr.go b/pkg/provider/gcr/gcr.go index aadb4de..b4223b1 100644 --- a/pkg/provider/gcr/gcr.go +++ b/pkg/provider/gcr/gcr.go @@ -76,8 +76,8 @@ func (p *GCR) Run(ctx context.Context, domain, repository string, reqOpt *types. imageTags = append(imageTags, types.ImageTag{ Tags: detail.Tag, Byte: getIntByte(detail.ImageSizeBytes), - CreatedAt: &createdAt, - UploadedAt: &uploadedAt, + CreatedAt: createdAt, + UploadedAt: uploadedAt, }) } return imageTags, nil @@ -141,10 +141,10 @@ func (p *GCR) getCredential(ctx context.Context) (username, password string, err return helper.Get(p.domain) } -func getIntByte(strB string) *int { +func getIntByte(strB string) int { b, err := strconv.Atoi(strB) if err != nil { - return nil + return 0 } - return &b + return b }