MCPcopy
hub / github.com/slimtoolkit/slim / SaveImage

Function SaveImage

pkg/docker/dockerutil/dockerutil.go:266–357  ·  view source on GitHub ↗
(dclient *dockerapi.Client, imageRef, local string, extract, removeOrig bool)

Source from the content-addressed store, hash-verified

264}
265
266func SaveImage(dclient *dockerapi.Client, imageRef, local string, extract, removeOrig bool) error {
267 if local == "" {
268 return ErrBadParam
269 }
270
271 var err error
272 if dclient == nil {
273 socketInfo, err := dockerclient.GetUnixSocketAddr()
274 if err != nil {
275 return err
276 }
277
278 if socketInfo == nil || socketInfo.Address == "" {
279 return fmt.Errorf("no unix socket found")
280 }
281
282 dclient, err = dockerapi.NewClient(socketInfo.Address)
283 if err != nil {
284 log.Errorf("dockerutil.SaveImage: dockerapi.NewClient() error = %v", err)
285 return err
286 }
287 }
288
289 imageRef = CleanImageID(imageRef)
290
291 //todo: 'pull' the image if it's not available locally yet
292 //note: HasImage() doesn't work with image IDs
293
294 dir := fsutil.FileDir(local)
295 if !fsutil.DirExists(dir) {
296 err := os.MkdirAll(dir, 0755)
297 if err != nil {
298 return err
299 }
300 }
301
302 dfile, err := os.Create(local)
303 if err != nil {
304 return err
305 }
306
307 options := dockerapi.ExportImageOptions{
308 Name: imageRef,
309 OutputStream: dfile,
310 InactivityTimeout: 20 * time.Second,
311 }
312
313 err = dclient.ExportImage(options)
314 if err != nil {
315 log.Errorf("dockerutil.SaveImage: dclient.ExportImage() error = %v", err)
316 dfile.Close()
317 return err
318 }
319
320 dfile.Close()
321
322 if extract {
323 dstDir := filepath.Dir(local)

Callers 1

OnCommandFunction · 0.92

Calls 6

GetUnixSocketAddrFunction · 0.92
FileDirFunction · 0.92
DirExistsFunction · 0.92
CleanImageIDFunction · 0.85
RemoveMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected