This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
images.go
92 lines (81 loc) · 2.34 KB
/
images.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2018 SUSE LLC. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"github.com/codegangsta/cli"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
)
// Print all the images based on SUSE. It will print in a format that is as
// close to the `docker` command as possible.
func printImages(images []types.ImageSummary) {
suseImages := make([]types.ImageSummary, 0, len(images))
cache := getCacheFile()
counter := 0
for _, img := range images {
select {
case <-killChannel:
return
default:
fmt.Printf("Inspecting image %d/%d\r", (counter + 1), len(images))
if cache.isSUSE(img.ID) {
suseImages = append(suseImages, img)
}
}
counter++
}
formatAndPrint(suseImages)
cache.flush()
}
// The images command prints all the images that are based on SUSE.
func imagesCmd(ctx *cli.Context) {
client := getDockerClient()
// On "force", just cleanup the cache.
if ctx.GlobalBool("force") {
cd := getCacheFile()
cd.reset()
}
if imgs, err := client.ImageList(context.Background(), types.ImageListOptions{}); err != nil {
logAndFatalf("Cannot proceed safely: %v.", err)
} else {
printImages(imgs)
exitWithCode(0)
}
}
// Looks for a docker image defined by repo:tag
// Returns true if the image already exists, false otherwise
func checkImageExists(repo, tag string) (bool, error) {
client := getDockerClient()
images, err := client.ImageList(context.Background(), types.ImageListOptions{
All: false,
Filters: filters.NewArgs(filters.Arg("reference", repo)),
})
if err != nil {
return false, err
}
if len(images) == 0 {
return false, nil
}
ref := fmt.Sprintf("%s:%s", repo, tag)
for _, image := range images {
for _, t := range image.RepoTags {
if ref == t {
return true, nil
}
}
}
return false, nil
}