i using docker registry api, first list images on registry using list repositories method , looping in images create map[string] []string :image []tags using listing image tags method.
i tried
func getimages(w http.responsewriter, r *http.request) { w.header().set("access-control-allow-origin", "*") res, err := http.get(fmt.sprintf("%s/%s", sconf.registryconf.url, sconf.registryconf.listrepo)) if err != nil { w.writeheader(500) log.errorf("could not repositories: %s", err) return } log.info("registry accessed") js, err := ioutil.readall(res.body) if err != nil { w.writeheader(500) log.errorf("could not read response: %s", err) return } log.infof("response read successfully") var repositories map[string][]string err = json.unmarshal(js, &repositories) if err != nil { w.writeheader(500) log.errorf("could not transform json map: %s", err) return } res.body.close() if err != nil { log.warnf("could not close body") } var images []string images = repositories["repositories"] //map_image_tags := make(map[string][]string) var map_image_tags map[string]interface{} map_images_tags := make(map[string][]string) log.debugf("ok") := 0; < len(images); i++ { log.debugf("ok") res2, err := http.get(fmt.sprintf("%s/v2/%s/tags/lists", sconf.registryconf.url, images[i])) if err != nil { w.writeheader(500) log.errorf("could not tags: %s", err) return } log.debugf("ok") js2, err := ioutil.readall(res2.body) if err != nil { w.writeheader(500) log.errorf("could not read body: %s", err) return } log.debugf("ok") log.debugf("%v", []byte(js2)) err = json.unmarshal(js2, &map_image_tags) log.debugf("map:", map_image_tags) log.debugf("ok") if err != nil { w.writeheader(500) log.errorf("could not unmarshal json: %s", err) return } log.debugf("ok") //map_images_tags[map_image_tags["name"]] = map_image_tags["tags"] } response, err := json.marshalindent(map_images_tags, "", " ") if err != nil { w.writeheader(500) log.errorf("could not transform map json: %s", err) return } log.infof("get registry images: success") fmt.fprintf(w, string(response)) }
but not working, can show me mistakes or show me how can scratch?
as far can see can not unmarshal array go value of type map[string][]string. jimb says should make interface , unmarshal that.
this docker response api images (from: docker api):
j := []byte(`[ { "repotags": [ "ubuntu:12.04", "ubuntu:precise", "ubuntu:latest" ], "id": "8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c", "created": 1365714795, "size": 131506275, "virtualsize": 131506275, "labels": {} }, { "repotags": [ "ubuntu:12.10", "ubuntu:quantal" ], "parentid": "27cf784147099545", "id": "b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc", "created": 1364102658, "size": 24653, "virtualsize": 180116135, "labels": { "com.example.version": "v1" } } ]`)
it obvious can not unpack map[string][]string (go playground here)
if want use thingy make interface , unpack interface. use json https://github.com/antonholmquist/jason. simpler original encoding/json
Comments
Post a Comment