forked from codelingo/codelingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
47 lines (42 loc) · 1.32 KB
/
image.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
package docker
import (
"fmt"
"log"
"os"
"github.com/fsouza/go-dockerclient"
"github.com/juju/errors"
)
func apiAuth() (docker.AuthConfiguration, error) {
auths, err := docker.NewAuthConfigurationsFromDockerCfg()
if err != nil {
return docker.AuthConfiguration{}, errors.Errorf("error getting auth config: %s", err.Error())
}
if auth, ok := auths.Configs["https://index.docker.io/v1/"]; ok {
return auth, nil
}
// otherwise return first auth found
for _, auth := range auths.Configs {
return auth, nil
}
return docker.AuthConfiguration{}, errors.New("auth not found")
}
func PullImage(client *docker.Client, name string, registry string, tag string) error {
opts := docker.PullImageOptions{
Repository: name,
Registry: registry,
Tag: tag,
OutputStream: os.Stdout,
}
auth, err := apiAuth()
if err != nil {
// just log err. We should be able to pull without auth.
log.Printf("could not get auth config: %s. We'll try pulling image without auth", err.Error())
fmt.Printf("could not get auth config: %s. We'll try pulling image without auth", err.Error())
}
return client.PullImage(opts, auth)
}
// HaveImage returns true if the tenet image can be found locally.
func HaveImage(client *docker.Client, taggedName string) bool {
img, _ := client.InspectImage(taggedName)
return img != nil
}