swirl/docker/image.go

71 lines
1.9 KiB
Go
Raw Normal View History

2021-12-06 12:24:22 +00:00
package docker
import (
"context"
"github.com/cuigh/swirl/misc"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
)
// ImageList return images on the host.
2021-12-20 06:28:43 +00:00
func (d *Docker) ImageList(ctx context.Context, node, name string, pageIndex, pageSize int) (images []types.ImageSummary, total int, err error) {
c, err := d.agent(node)
2021-12-06 12:24:22 +00:00
if err != nil {
return nil, 0, err
}
opts := types.ImageListOptions{}
if name != "" {
opts.Filters = filters.NewArgs()
opts.Filters.Add("reference", name)
}
images, err = c.ImageList(ctx, opts)
if err != nil {
return nil, 0, err
}
total = len(images)
start, end := misc.Page(total, pageIndex, pageSize)
images = images[start:end]
return
}
// ImageInspect returns image information.
2021-12-20 06:28:43 +00:00
func (d *Docker) ImageInspect(ctx context.Context, node, id string) (image types.ImageInspect, raw []byte, err error) {
2021-12-06 12:24:22 +00:00
var c *client.Client
2021-12-20 06:28:43 +00:00
if c, err = d.agent(node); err == nil {
2021-12-06 12:24:22 +00:00
return c.ImageInspectWithRaw(ctx, id)
}
return
}
// ImageHistory returns the changes in an image in history format.
2021-12-20 06:28:43 +00:00
func (d *Docker) ImageHistory(ctx context.Context, node, id string) (histories []image.HistoryResponseItem, err error) {
2021-12-06 12:24:22 +00:00
var c *client.Client
2021-12-20 06:28:43 +00:00
if c, err = d.agent(node); err == nil {
2021-12-06 12:24:22 +00:00
return c.ImageHistory(ctx, id)
}
return
}
// ImageRemove remove a image.
2021-12-20 06:28:43 +00:00
func (d *Docker) ImageRemove(ctx context.Context, node, id string) error {
c, err := d.agent(node)
if err == nil {
2021-12-06 12:24:22 +00:00
_, err = c.ImageRemove(ctx, id, types.ImageRemoveOptions{})
2021-12-20 06:28:43 +00:00
}
return err
2021-12-06 12:24:22 +00:00
}
// ImagePrune remove all unused images.
func (d *Docker) ImagePrune(ctx context.Context, node string) (report types.ImagesPruneReport, err error) {
var c *client.Client
if c, err = d.agent(node); err == nil {
report, err = c.ImagesPrune(ctx, filters.NewArgs(filters.Arg("dangling", "false")))
}
return
}