From 8f8efa4592a2891dd9c1efa3f031f73a36862a93 Mon Sep 17 00:00:00 2001 From: John Poth Date: Thu, 16 Dec 2021 22:41:06 +0100 Subject: [PATCH] Artifacts (#7) * fix: don't forget to close writer when finished * Support empty base image * Support files as well as directories as input * Update GO version --- .github/workflows/release.yml | 4 +-- go.mod | 2 +- pkg/builder/build.go | 59 +++++++++++++++++++++++------------ pkg/builder/image.go | 4 +++ 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7f55800..d1bd1c2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -38,7 +38,7 @@ jobs: - name: Set Go uses: actions/setup-go@v1 with: - go-version: 1.13.x + go-version: 1.16.x - name: Start Docker Registry run: | docker run -d -p "5000:5000" --restart=always --name "registry" registry:2 @@ -69,7 +69,7 @@ jobs: - name: Set Go uses: actions/setup-go@v1 with: - go-version: 1.13.x + go-version: 1.16.x - name: Tag Release uses: mathieudutour/github-tag-action@v4 with: diff --git a/go.mod b/go.mod index 3c3d5c7..937fc31 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/container-tools/spectrum -go 1.13 +go 1.16 require ( github.com/docker/cli v20.10.11+incompatible diff --git a/pkg/builder/build.go b/pkg/builder/build.go index b7f4337..5bd0bbd 100644 --- a/pkg/builder/build.go +++ b/pkg/builder/build.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-containerregistry/pkg/v1/mutate" "github.com/google/go-containerregistry/pkg/v1/tarball" "io" + "io/fs" "io/ioutil" "log" "os" @@ -76,7 +77,7 @@ func configureLogging(options Options) { logs.Warn = log.New(stderr, LogPrefix, log.LstdFlags) } -func tarPackage(dirName, targetPath string, recursive bool) (file string, err error) { +func tarPackage(name, targetPath string, recursive bool) (file string, err error) { layerFile, err := ioutil.TempFile("", "spectrum-layer-*.tar") if err != nil { return "", err @@ -84,14 +85,24 @@ func tarPackage(dirName, targetPath string, recursive bool) (file string, err er defer layerFile.Close() writer := tar.NewWriter(layerFile) + defer writer.Close() + fileInfo, err := os.Stat(name) + if err != nil { + return "", err + } - if recursive { - err = tarPackageRecursive(dirName, targetPath, writer) + if !fileInfo.IsDir() { + err := writeFileToTar(name, targetPath, writer, fileInfo) + if err != nil { + return "", err + } + } else if recursive { + err = tarPackageRecursive(name, targetPath, writer) if err != nil { return "", err } } else { - err = tarPackageNonRecursive(dirName, targetPath, writer) + err = tarPackageNonRecursive(name, targetPath, writer) if err != nil { return "", err } @@ -117,28 +128,36 @@ func tarPackageNonRecursive(dirName, targetPath string, writer *tar.Writer) erro continue } - file, err := os.Open(dir.Name() + string(filepath.Separator) + fileInfo.Name()) + err := writeFileToTar(dir.Name()+string(filepath.Separator)+fileInfo.Name(), targetPath, writer, fileInfo) if err != nil { return err } - defer file.Close() + } + return nil +} + +func writeFileToTar(name, targetPath string, writer *tar.Writer, fileInfo fs.FileInfo) error { + file, err := os.Open(name) + if err != nil { + return err + } + defer file.Close() - // prepare the tar header - header := new(tar.Header) - header.Name = path.Join(targetPath, filepath.Base(file.Name())) - header.Size = fileInfo.Size() - header.Mode = int64(fileInfo.Mode()) - header.ModTime = fileInfo.ModTime() + // prepare the tar header + header := new(tar.Header) + header.Name = path.Join(targetPath, filepath.Base(file.Name())) + header.Size = fileInfo.Size() + header.Mode = int64(fileInfo.Mode()) + header.ModTime = fileInfo.ModTime() - err = writer.WriteHeader(header) - if err != nil { - return err - } + err = writer.WriteHeader(header) + if err != nil { + return err + } - _, err = io.Copy(writer, file) - if err != nil { - return err - } + _, err = io.Copy(writer, file) + if err != nil { + return err } return nil } diff --git a/pkg/builder/image.go b/pkg/builder/image.go index 587fb36..9ad2c5e 100644 --- a/pkg/builder/image.go +++ b/pkg/builder/image.go @@ -6,10 +6,14 @@ import ( "github.com/google/go-containerregistry/pkg/authn" "github.com/google/go-containerregistry/pkg/name" v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/google/go-containerregistry/pkg/v1/empty" "github.com/google/go-containerregistry/pkg/v1/remote" ) func Pull(options Options) (v1.Image, error) { + if options.Base == "" || options.Base == "scratch" { + return empty.Image, nil + } nameOptions := makeNameOptions(options.PullInsecure) ref, err := name.ParseReference(options.Base, nameOptions...) if err != nil {