1. 前言
接上篇,本次添加镜像查看、删除功能
2. 效果图
下载地址 https://github.com/LeoBest2/my-docker-gui/releases/tag/v0.0.1
3. 代码
直接调用官方库,稍作处理格式化,没什么代码量
TODO:
- types.ImageRemoveOptions
Force
和PruneChildren
具体含义
app.go
func (a *App) ImageList() ([]Image, error) {
images, err := Cli.ImageList(context.Background(), types.ImageListOptions{})
if err != nil {
return nil, err
}
var imagesRet []Image
for _, i := range images {
iID := strings.Index(i.ID, ":")
iRepo := strings.LastIndex(i.RepoTags[0], ":")
imagesRet = append(imagesRet, Image{
ID: i.ID[iID+1 : iID+13],
Repository: i.RepoTags[0][:iRepo],
Tag: i.RepoTags[0][iRepo+1:],
Created: time.Unix(i.Created, 0).Format("2006-01-02 15:04:05"),
Size: fmt.Sprintf("%dMB", i.Size/1024/1024),
})
}
return imagesRet, nil
}
func (a *App) ImageDelete(ID string) error {
_, err := Cli.ImageRemove(context.Background(), ID, types.ImageRemoveOptions{Force: true})
return err
}
type Image struct {
ID string `json:"ID"`
Repository string `json:"Repository"`
Tag string `json:"Tag"`
Created string `json:"Created"`
Size string `json:"Size"`
}