Skip to content

Commit

Permalink
stop using pointer in ImageTag (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoyamachi authored Dec 25, 2019
1 parent e3cc4e5 commit d4d3d7e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
13 changes: 7 additions & 6 deletions internal/report/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ","),
Expand All @@ -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)
}
24 changes: 14 additions & 10 deletions internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions pkg/provider/dockerhub/dockerhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/provider/ecr/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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)
}
10 changes: 5 additions & 5 deletions pkg/provider/gcr/gcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit d4d3d7e

Please sign in to comment.