Skip to content

Commit

Permalink
allow to set page_size (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
alv91 authored Aug 10, 2021
1 parent 2c67320 commit c7a310a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ COMMANDS:
GLOBAL OPTIONS:
--username value
--password value
--days value (default: "30")
--days value (default: 30)
--pageSize value (default: 10)
--dry-run
--help, -h show help
```
20 changes: 12 additions & 8 deletions dhlm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/urfave/cli"
"os"
"strconv"
"time"
)

Expand All @@ -19,8 +18,9 @@ func main() {
var dhPassword string
var dhOrg string
var dhRepo string
var days string
var days int
var dryRun bool
var pageSize int

app.Flags = []cli.Flag{
cli.StringFlag{
Expand All @@ -31,10 +31,15 @@ func main() {
Name: "password",
Destination: &dhPassword,
},
cli.StringFlag{
cli.IntFlag{
Name: "days",
Destination: &days,
Value: "30",
Value: 30,
},
cli.IntFlag{
Name: "pageSize",
Destination: &pageSize,
Value: 10,
},
cli.BoolFlag{
Name: "dry-run",
Expand All @@ -57,11 +62,10 @@ func main() {
Password: dhPassword,
})

daysInt, _ := strconv.Atoi(days)
timeBefore := time.Now().Add(-time.Hour * 24 * time.Duration(daysInt))
timeBefore := time.Now().Add(-time.Hour * 24 * time.Duration(days))

pageNumber := 1
for tagsList := dh.GetImages(dhOrg, dhRepo, pageNumber, timeBefore); len(tagsList.Next) > 0; pageNumber++ {
for tagsList := dh.GetImages(dhOrg, dhRepo, pageNumber, timeBefore, pageSize); len(tagsList.Next) > 0; pageNumber++ {
fmt.Println("Checking page:", pageNumber)
var digests []string
var ignoreList []*dockerhub.IgnoreWarnings
Expand Down Expand Up @@ -90,7 +94,7 @@ func main() {
fmt.Printf("Summary of deleted images ➡ manifest_deletes: %d, manifest_errors: %d, tag_deletes: %d, tag_errors: %d \n",
deletedImages.Metrics.ManifestDeletes, deletedImages.Metrics.ManifestErrors, deletedImages.Metrics.TagDeletes, deletedImages.Metrics.TagDeletes)

tagsList = dh.GetImages(dhOrg, dhRepo, pageNumber, timeBefore)
tagsList = dh.GetImages(dhOrg, dhRepo, pageNumber, timeBefore, pageSize)
}

return nil
Expand Down
6 changes: 2 additions & 4 deletions dockerhub/dockerhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ type client struct {
token string
}

const pageSize = "10"

func NewClient(auth Auth) *client {
c := &client{}
c.authorize(auth)
Expand Down Expand Up @@ -99,11 +97,11 @@ func (client *client) DeleteImages(organization string, repository string, diges
return deletedImages
}

func (client *client) GetImages(organization string, repository string, page int, timeBefore time.Time) (imageList *ImageList) {
func (client *client) GetImages(organization string, repository string, page int, timeBefore time.Time, pageSize int) (imageList *ImageList) {
pageString := strconv.Itoa(page)
timeFrom := url.QueryEscape(timeBefore.Format(time.RFC3339))

req, err := http.NewRequest("GET", "https://hub.docker.com/v2/namespaces/"+organization+"/repositories/"+repository+"/images?page="+pageString+"&page_size="+pageSize+"&ordering=last_activity&status=inactive&active_from="+timeFrom, nil)
req, err := http.NewRequest("GET", "https://hub.docker.com/v2/namespaces/"+organization+"/repositories/"+repository+"/images?page="+pageString+"&page_size="+string(pageSize)+"&ordering=last_activity&status=inactive&active_from="+timeFrom, nil)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit c7a310a

Please sign in to comment.