douhuxi4145 2016-07-21 15:09
浏览 51
已采纳

如何在docker Registry API中为映像名称及其标签创建map [string] [] string?

I am using Docker Registry API, to first list all images on the registry using List repositories method and then looping in the images to create a map[string] []string :image []tags using Listing image tags method.

I tried this

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 get 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 to 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")
for i := 0; i < 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 get 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 to json: %s", err)
    return
}
log.Infof("Get Registry Images: success")
fmt.Fprintf(w, string(response))

}

But its not working, can anybody show me my mistakes or show me how can i do this from scratch?

  • 写回答

1条回答 默认 最新

  • douba1498 2016-07-21 19:01
    关注

    As far as I can see you can not unmarshal array into Go value of type map[string][]string. As JimB says you should make interface and then unmarshal to that.

    This is your Docker response from API for 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 is obvious you can not unpack into map[string][]string (Go playground here)

    If you want to use your thingy for this then do make interface and unpack to that interface. Also use this for JSON https://github.com/antonholmquist/jason. It is much simpler that original encoding/json

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路