/
labels_images_utils.go
69 lines (57 loc) · 1.67 KB
/
labels_images_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package labels
import (
"context"
"strings"
"github.com/docker/docker/api/types"
dockerclient "github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
"github.com/zanetworker/dockument/pkg/utils"
)
func getImageLabels(imageName string) (map[string]string, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
d, err := dockerclient.NewEnvClient()
if err != nil {
return nil, err
}
images, err := d.ImageList(ctx, types.ImageListOptions{All: false})
if err != nil {
return nil, err
}
for _, image := range images {
if containsImageName(image.RepoTags, imageName) {
return image.Labels, nil
}
}
log.Warn(utils.ColorString("green", "Ooops! Could not find the Image you mentioned, please retry!"))
return nil, nil
}
// Checks if RepoTags of an image contains the requested image tag
func containsImageName(imageRepoTags []string, requestedImage string) bool {
for _, tag := range imageRepoTags {
nameWithoutTag := strings.Split(tag, ":")[0]
if tag == requestedImage || nameWithoutTag == requestedImage {
return true
}
}
return false
}
//ImageExists checks if a certain image exists in the local repo
func ImageExists(imageName string) bool {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
d, err := dockerclient.NewEnvClient()
if err != nil {
log.Fatalf("Failed to create docker socket, error: %s", err.Error())
}
images, err := d.ImageList(ctx, types.ImageListOptions{All: false})
if err != nil {
log.Fatalf("Failed to list docker images, error: %s", err.Error())
}
for _, image := range images {
if containsImageName(image.RepoTags, imageName) {
return true
}
}
return false
}