From c7a310a155f1622c3a1a1a45be77e0b082012628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Robak?= Date: Tue, 10 Aug 2021 11:30:17 +0200 Subject: [PATCH] allow to set page_size (#3) --- README.md | 3 ++- dhlm.go | 20 ++++++++++++-------- dockerhub/dockerhub.go | 6 ++---- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 46a840a..ee5e796 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/dhlm.go b/dhlm.go index 0ef8838..265d821 100644 --- a/dhlm.go +++ b/dhlm.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/urfave/cli" "os" - "strconv" "time" ) @@ -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{ @@ -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", @@ -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 @@ -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 diff --git a/dockerhub/dockerhub.go b/dockerhub/dockerhub.go index 7886832..44f48de 100644 --- a/dockerhub/dockerhub.go +++ b/dockerhub/dockerhub.go @@ -14,8 +14,6 @@ type client struct { token string } -const pageSize = "10" - func NewClient(auth Auth) *client { c := &client{} c.authorize(auth) @@ -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) }