Skip to content

Commit

Permalink
Merge pull request #448 from theBeginner86/theBeginner86/chore/16
Browse files Browse the repository at this point in the history
[chore] Add utils functions for OCI
  • Loading branch information
theBeginner86 authored Jan 11, 2024
2 parents 8a971b8 + d71589e commit a836493
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
10 changes: 10 additions & 0 deletions models/oci/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var (
ErrGettingLayerCode = "11110"
ErrCompressingLayerCode = "11111"
ErrUnTaringLayerCode = "11112"
ErrGettingImageCode = "11113"
ErrValidatingImageCode = "11114"
)

func ErrAppendingLayer(err error) error {
Expand All @@ -38,3 +40,11 @@ func ErrCompressingLayer(err error) error {
func ErrUnTaringLayer(err error) error {
return errors.New(ErrUnTaringLayerCode, errors.Alert, []string{"untaring layer failed"}, []string{err.Error()}, []string{"failed to untar the layer"}, []string{"Try using a different layer", "check if image is not malformed"})
}

func ErrGettingImage(err error) error {
return errors.New(ErrGettingImageCode, errors.Alert, []string{"getting image failed"}, []string{err.Error()}, []string{"failed to get the image"}, []string{"Try using a different image", "check if image is not malformed"})
}

func ErrValidatingImage(err error) error {
return errors.New(ErrValidatingImageCode, errors.Alert, []string{"validating image failed"}, []string{err.Error()}, []string{"failed to validate the image"}, []string{"Try using a different image", "check if image is not malformed"})
}
17 changes: 15 additions & 2 deletions models/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"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"
)

Expand All @@ -32,8 +34,19 @@ func SaveOCIArtifact(img gcrv1.Image, artifactPath, name string) error {
return nil
}

// uncompress the OCI Artifact tarball at the given path
func UnCompressOCIArtifact(img gcrv1.Image, destination string) error {
// 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)
Expand Down
44 changes: 44 additions & 0 deletions utils/walker/directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package walker

import (
"io"
"os"
"path/filepath"
)

func WalkLocalDirectory(path string) ([]*File, error) {
var files []*File
err := filepath.WalkDir(path,
func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
file, err := os.OpenFile(path, os.O_RDONLY, 0444)
if err != nil {
return err
}
content, err := io.ReadAll(file)
if err != nil {
return err
}


f := &File{
Content: string(content),
Name: d.Name(),
Path: path,
}

files = append(files, f)
}
return nil
})

if err != nil {
return nil, err
}

return files, nil

}

0 comments on commit a836493

Please sign in to comment.