forked from togettoyou/hub-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
204 lines (173 loc) · 3.9 KB
/
cli.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package pkg
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"errors"
"io"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
)
type Cli struct {
cli *client.Client
repository string
username string
auth string
log io.Writer
}
func NewCli(ctx context.Context, repository, username, password string, log io.Writer) (*Cli, error) {
if username == "" || password == "" {
return nil, errors.New("username or password cannot be empty")
}
authConfig := registry.AuthConfig{
Username: username,
Password: password,
ServerAddress: repository,
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
return nil, err
}
cli, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
return nil, err
}
_, err = cli.RegistryLogin(ctx, authConfig)
if err != nil {
return nil, err
}
return &Cli{
cli: cli,
repository: repository,
username: username,
auth: base64.URLEncoding.EncodeToString(encodedJSON),
log: log,
}, nil
}
type Output struct {
Source string
Target string
}
func (c *Cli) Source2Target(source string, platform string) (*Output, error) {
if source == "" {
return nil, errors.New("source is nil")
}
target := source
if strings.Contains(source, "$") {
parts := strings.Split(source, "$")
source = parts[0]
target = parts[1]
}
if !strings.Contains(target, ":") && strings.Contains(source, ":") {
parts := strings.Split(source, ":")
target += ":" + parts[1]
}
if platform != "" {
if strings.Contains(target, ":") {
parts := strings.Split(target, ":")
target = parts[0] + "-" + strings.ReplaceAll(platform, "/", "-") + ":" + parts[1]
} else {
target += "-" + strings.ReplaceAll(platform, "/", "-")
}
}
if c.repository == "" {
target = "docker.io/" + c.username + "/" + strings.ReplaceAll(target, "/", ".")
} else {
target = c.repository + "/" + strings.ReplaceAll(target, "/", ".")
}
return &Output{
Source: source,
Target: target,
}, nil
}
func (c *Cli) PullTagPushImage(ctx context.Context, source, platform string) (*Output, error) {
output, err := c.Source2Target(source, platform)
if err != nil {
return nil, err
}
err = c.PullImage(ctx, output.Source, platform)
if err != nil {
return nil, err
}
err = c.cli.ImageTag(ctx, output.Source, output.Target)
if err != nil {
return nil, err
}
err = c.PushImage(ctx, output.Target, platform)
if err != nil {
return nil, err
}
return output, nil
}
type errorMessage struct {
Error string `json:"error"`
}
func (c *Cli) PullImage(ctx context.Context, image, platform string) error {
pullOut, err := c.cli.ImagePull(ctx, image, types.ImagePullOptions{Platform: platform})
defer func() {
if pullOut != nil {
pullOut.Close()
}
}()
if err != nil {
return err
}
var e errorMessage
buffIOReader := bufio.NewReader(pullOut)
for {
streamBytes, err := buffIOReader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
return err
}
if c.log != nil {
_, _ = c.log.Write(streamBytes)
}
_ = json.Unmarshal(streamBytes, &e)
if e.Error != "" {
return errors.New(e.Error)
}
}
return nil
}
func (c *Cli) PushImage(ctx context.Context, image, platform string) error {
pushOut, err := c.cli.ImagePush(ctx, image, types.ImagePushOptions{
RegistryAuth: c.auth,
Platform: platform,
})
defer func() {
if pushOut != nil {
pushOut.Close()
}
}()
if err != nil {
return err
}
var e errorMessage
buffIOReader := bufio.NewReader(pushOut)
for {
streamBytes, err := buffIOReader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
return err
}
if c.log != nil {
_, _ = c.log.Write(streamBytes)
}
_ = json.Unmarshal(streamBytes, &e)
if e.Error != "" {
return errors.New(e.Error)
}
}
return nil
}