2024-01-08 13:26:03 -06:00
|
|
|
//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd))
|
2023-01-10 16:08:57 -06:00
|
|
|
|
2019-02-17 23:40:22 -06:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-28 23:08:40 -05:00
|
|
|
"fmt"
|
2019-02-17 23:40:22 -06:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2023-01-13 11:52:54 -06:00
|
|
|
"github.com/docker/docker/client"
|
2019-02-17 23:40:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImageExistsLocally returns a boolean indicating if an image with the
|
2021-03-28 23:08:40 -05:00
|
|
|
// requested name, tag and architecture exists in the local docker image store
|
|
|
|
func ImageExistsLocally(ctx context.Context, imageName string, platform string) (bool, error) {
|
2020-05-03 23:15:42 -05:00
|
|
|
cli, err := GetDockerClient(ctx)
|
2019-02-17 23:40:22 -06:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-10-24 11:50:43 -05:00
|
|
|
defer cli.Close()
|
2019-02-17 23:40:22 -06:00
|
|
|
|
2023-01-13 11:52:54 -06:00
|
|
|
inspectImage, _, err := cli.ImageInspectWithRaw(ctx, imageName)
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return false, nil
|
|
|
|
} else if err != nil {
|
2019-02-17 23:40:22 -06:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-01-13 11:52:54 -06:00
|
|
|
if platform == "" || platform == "any" || fmt.Sprintf("%s/%s", inspectImage.Os, inspectImage.Architecture) == platform {
|
|
|
|
return true, nil
|
2021-03-28 23:08:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-05-02 10:15:13 -05:00
|
|
|
// RemoveImage removes image from local store, the function is used to run different
|
2021-03-28 23:08:40 -05:00
|
|
|
// container image architectures
|
2021-05-02 10:15:13 -05:00
|
|
|
func RemoveImage(ctx context.Context, imageName string, force bool, pruneChildren bool) (bool, error) {
|
2021-03-28 23:08:40 -05:00
|
|
|
cli, err := GetDockerClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-01-13 11:52:54 -06:00
|
|
|
defer cli.Close()
|
2021-03-28 23:08:40 -05:00
|
|
|
|
2023-01-13 11:52:54 -06:00
|
|
|
inspectImage, _, err := cli.ImageInspectWithRaw(ctx, imageName)
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return false, nil
|
|
|
|
} else if err != nil {
|
2021-03-28 23:08:40 -05:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-01-13 11:52:54 -06:00
|
|
|
if _, err = cli.ImageRemove(ctx, inspectImage.ID, types.ImageRemoveOptions{
|
|
|
|
Force: force,
|
|
|
|
PruneChildren: pruneChildren,
|
|
|
|
}); err != nil {
|
|
|
|
return false, err
|
2021-03-28 23:08:40 -05:00
|
|
|
}
|
|
|
|
|
2023-01-13 11:52:54 -06:00
|
|
|
return true, nil
|
2019-02-17 23:40:22 -06:00
|
|
|
}
|