forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
128 lines (105 loc) · 2.84 KB
/
client.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package pack
import (
"os"
"path/filepath"
"github.com/buildpack/imgutil"
dockerClient "github.com/docker/docker/client"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/pkg/errors"
"github.com/buildpack/pack/blob"
"github.com/buildpack/pack/build"
"github.com/buildpack/pack/config"
"github.com/buildpack/pack/image"
"github.com/buildpack/pack/logging"
)
type Client struct {
logger logging.Logger
imageFetcher ImageFetcher
downloader Downloader
lifecycle Lifecycle
docker *dockerClient.Client
imageFactory ImageFactory
}
type ClientOption func(c *Client)
// WithLogger supply your own logger.
func WithLogger(l logging.Logger) ClientOption {
return func(c *Client) {
c.logger = l
}
}
// WithImageFactory supply your own image factory.
func WithImageFactory(f ImageFactory) ClientOption {
return func(c *Client) {
c.imageFactory = f
}
}
// WithFetcher supply your own fetcher.
func WithFetcher(f ImageFetcher) ClientOption {
return func(c *Client) {
c.imageFetcher = f
}
}
// WithDownloader supply your own downloader.
func WithDownloader(d Downloader) ClientOption {
return func(c *Client) {
c.downloader = d
}
}
// WithCacheDir supply your own cache directory.
//
// Deprecated: use WithDownloader instead.
func WithCacheDir(path string) ClientOption {
return func(c *Client) {
c.downloader = blob.NewDownloader(c.logger, path)
}
}
// WithDockerClient supply your own docker client.
func WithDockerClient(docker *dockerClient.Client) ClientOption {
return func(c *Client) {
c.docker = docker
}
}
func NewClient(opts ...ClientOption) (*Client, error) {
var client Client
for _, opt := range opts {
opt(&client)
}
if client.logger == nil {
client.logger = logging.New(os.Stderr)
}
if client.docker == nil {
var err error
client.docker, err = dockerClient.NewClientWithOpts(dockerClient.FromEnv, dockerClient.WithVersion("1.38"))
if err != nil {
return nil, err
}
}
if client.downloader == nil {
packHome, err := config.PackHome()
if err != nil {
return nil, errors.Wrap(err, "getting pack home")
}
client.downloader = blob.NewDownloader(client.logger, filepath.Join(packHome, "download-cache"))
}
if client.imageFetcher == nil {
client.imageFetcher = image.NewFetcher(client.logger, client.docker)
}
if client.imageFactory == nil {
client.imageFactory = &DefaultImageFactory{
dockerClient: client.docker,
keychain: authn.DefaultKeychain,
}
}
client.lifecycle = build.NewLifecycle(client.docker, client.logger)
return &client, nil
}
type DefaultImageFactory struct {
dockerClient *dockerClient.Client
keychain authn.Keychain
}
func (f *DefaultImageFactory) NewImage(repoName string, local bool) (imgutil.Image, error) {
if local {
return imgutil.EmptyLocalImage(repoName, f.dockerClient), nil
}
return imgutil.NewRemoteImage(repoName, f.keychain)
}