Move archive helpers to artifact

This allow in the future to swap and provide archive/compression methods without hijacking the code.

Refers to #33
This commit is contained in:
Ettore Di Giacinto
2019-12-28 16:32:32 +01:00
parent 8fffae31c7
commit 2fa9c754ae
4 changed files with 55 additions and 34 deletions

View File

@@ -16,6 +16,8 @@
package compiler
import (
"archive/tar"
"io"
"io/ioutil"
"os"
"path"
@@ -148,6 +150,48 @@ func (a *PackageArtifact) SetPath(p string) {
a.Path = p
}
// Compress Archives and compress (TODO) to the artifact path
func (a *PackageArtifact) Compress(src string) error {
return helpers.Tar(src, a.Path)
}
// Unpack Untar and decompress (TODO) to the given path
func (a *PackageArtifact) Unpack(dst string, keepPerms bool) error {
return helpers.Untar(a.GetPath(), dst, keepPerms)
}
func (a *PackageArtifact) FileList() ([]string, error) {
tarFile, err := os.Open(a.GetPath())
if err != nil {
return []string{}, errors.Wrap(err, "Could not open package archive")
}
defer tarFile.Close()
tr := tar.NewReader(tarFile)
var files []string
// untar each segment
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return []string{}, err
}
// determine proper file path info
finfo := hdr.FileInfo()
fileName := hdr.Name
if finfo.Mode().IsDir() {
continue
}
files = append(files, fileName)
// if a dir, create it, then go to next segment
}
return files, nil
}
type CopyJob struct {
Src, Dst string
Artifact string

View File

@@ -339,13 +339,12 @@ func (cs *LuetCompiler) compileWithImage(image, buildertaggedImage, packageImage
// strip from includes
cs.stripIncludesFromRootfs(p.GetIncludes(), rootfs)
}
err = helpers.Tar(rootfs, p.Rel(p.GetPackage().GetFingerPrint()+".package.tar"))
artifact = NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar"))
err = artifact.Compress(rootfs)
if err != nil {
return nil, errors.Wrap(err, "Error met while creating package archive")
}
artifact = NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar"))
artifact.SetCompileSpec(p)
} else {
Info(pkgTag, "Generating delta")
@@ -404,8 +403,10 @@ func (cs *LuetCompiler) packageFromImage(p CompilationSpec, tag string, keepPerm
if err != nil {
return nil, errors.Wrap(err, "Could not extract rootfs")
}
artifact := NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar"))
artifact.SetCompileSpec(p)
err = helpers.Tar(rootfs, p.Rel(p.GetPackage().GetFingerPrint()+".package.tar"))
err = artifact.Compress(rootfs)
if err != nil {
return nil, errors.Wrap(err, "Error met while creating package archive")
}
@@ -422,8 +423,7 @@ func (cs *LuetCompiler) packageFromImage(p CompilationSpec, tag string, keepPerm
}
Info(pkgTag, " :white_check_mark: Done")
artifact := NewPackageArtifact(p.Rel(p.GetPackage().GetFingerPrint() + ".package.tar"))
artifact.SetCompileSpec(p)
err = artifact.WriteYaml(p.GetOutputPath())
if err != nil {
return artifact, err

View File

@@ -62,6 +62,9 @@ type Artifact interface {
SetCompileSpec(as CompilationSpec)
GetCompileSpec() CompilationSpec
WriteYaml(dst string) error
Unpack(dst string, keepPerms bool) error
Compress(src string) error
FileList() ([]string, error)
}
type ArtifactNode struct {