forked from meshery/meshkit
/
utils.go
65 lines (53 loc) · 1.56 KB
/
utils.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
package oci
import (
"fmt"
"os"
"path/filepath"
"github.com/fluxcd/pkg/tar"
"github.com/google/go-containerregistry/pkg/crane"
gcrv1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/google/go-containerregistry/pkg/v1/validate"
"github.com/layer5io/meshkit/utils"
)
func CreateTempOCIContentDir() (tempDir string, err error) {
wd := utils.GetHome()
wd = filepath.Join(wd, ".meshery", "content")
tempDir, err = os.MkdirTemp(wd, "oci")
if err != nil {
return "", err
}
return tempDir, nil
}
// saves the oci artifact to the given path as tarball type
func SaveOCIArtifact(img gcrv1.Image, artifactPath, name string) error {
repoWithTag := fmt.Sprintf("%s:%s", name, "latest") // TODO: Add support to make this dynamic from user input
err := crane.Save(img, repoWithTag, artifactPath)
if err != nil {
return err
}
return nil
}
// uncompress the OCI Artifact tarball at the given destination path
func UnCompressOCIArtifact(source, destination string) error {
img, err := tarball.ImageFromPath(source, nil)
if err != nil {
return ErrGettingImage(err)
}
opt := []validate.Option{}
if err = validate.Image(img, opt...); err != nil {
return ErrValidatingImage(err)
}
layers, err := img.Layers()
if err != nil {
return ErrGettingLayer(err)
}
blob, err := layers[0].Compressed()
if err != nil {
return ErrCompressingLayer(err)
}
if err = tar.Untar(blob, destination, tar.WithMaxUntarSize(-1), tar.WithSkipSymlinks()); err != nil {
return ErrUnTaringLayer(err)
}
return nil
}