Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"time"
)
type ImageInfo struct {
Count int `json:"count"`
Next string `json:"next"`
Previous interface{} `json:"previous"`
Results []struct {
Creator int `json:"creator"`
ID int `json:"id"`
ImageID interface{} `json:"image_id"`
Images []struct {
Architecture string `json:"architecture"`
Features string `json:"features"`
Variant interface{} `json:"variant"`
Digest string `json:"digest"`
Os string `json:"os"`
OsFeatures string `json:"os_features"`
OsVersion interface{} `json:"os_version"`
Size int64 `json:"size"`
Status string `json:"status"`
LastPulled interface{} `json:"last_pulled"`
LastPushed time.Time `json:"last_pushed"`
} `json:"images"`
LastUpdated time.Time `json:"last_updated"`
LastUpdater int `json:"last_updater"`
LastUpdaterUsername string `json:"last_updater_username"`
Name string `json:"name"`
Repository int `json:"repository"`
FullSize int64 `json:"full_size"`
V2 bool `json:"v2"`
TagStatus string `json:"tag_status"`
TagLastPulled interface{} `json:"tag_last_pulled"`
TagLastPushed time.Time `json:"tag_last_pushed"`
} `json:"results"`
}
var emptyImageInfo = &ImageInfo{}
func (i *ImageInfo) Reset() {
*i = *emptyImageInfo
}
func fetch(repo string) []byte {
resp, err := http.Get("https://registry.hub.docker.com/v2/repositories/" + repo + "/tags/")
if err != nil {
fmt.Println("Error:", err)
}
body, err := ioutil.ReadAll(resp.Body)
return body
}
func fetchnext(url string) []byte {
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error:", err)
}
body, err := ioutil.ReadAll(resp.Body)
return body
}
func main() {
var image ImageInfo
var repo string
flag.StringVar(&repo, "r", "", "")
flag.Parse()
body := fetch(repo)
json.Unmarshal(body, &image)
for _, version := range image.Results {
fmt.Println(version.Name)
}
for image.Next != "" {
body = fetchnext(image.Next)
image.Reset()
json.Unmarshal(body, &image)
for _, version := range image.Results {
fmt.Println(version.Name)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment