forked from tokibi/undocker
/
image.go
75 lines (64 loc) · 1.55 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
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
package undocker
import (
"encoding/json"
"io"
"github.com/pkg/errors"
"github.com/pepabo/undocker/internal/untar"
)
type Source interface {
Config(repository, tag string) ([]byte, error)
Exists(repository, tag string) bool
LayerBlobs(repository, tag string) ([]io.Reader, error)
Image(repository, tag string) Image
CleanUp() error
}
type Image struct {
Source Source
Repository string
Tag string
}
// Extract extracts docker image as rootfs to the specified directory
func (i Image) Extract(dir string, overwriteSymlink bool) error {
if !i.Exists() {
return errors.New("Image not found")
}
layerBlobs, err := i.LayerBlobs()
if err != nil {
return err
}
for _, blob := range layerBlobs {
err = untar.Untar(blob, dir, untar.Options{
OverwriteSymlinkRefs: overwriteSymlink,
})
if err != nil {
return err
}
}
return nil
}
// Unpack is an alias for Extract()
func (i Image) Unpack(dir string, overwriteSymlink bool) error {
return i.Extract(dir, overwriteSymlink)
}
func (i Image) Config() (*ImageConfig, error) {
bytes, err := i.Source.Config(i.Repository, i.Tag)
if err != nil {
return nil, err
}
config := new(ImageConfig)
if err := json.Unmarshal(bytes, config); err != nil {
return nil, err
}
return config, nil
}
// Exists check the images
func (i Image) Exists() bool {
if i.Source.Exists(i.Repository, i.Tag) {
return true
}
return false
}
// LayerBlobs return the layers of the image in order from the lower
func (i Image) LayerBlobs() ([]io.Reader, error) {
return i.Source.LayerBlobs(i.Repository, i.Tag)
}