Skip to content

Commit

Permalink
Artifacts (#7)
Browse files Browse the repository at this point in the history
* fix: don't forget to close writer when finished

* Support empty base image

* Support files as well as directories as input

* Update GO version
  • Loading branch information
johnpoth authored Dec 16, 2021
1 parent 40a1e4c commit 8f8efa4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
59 changes: 39 additions & 20 deletions pkg/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -76,22 +77,32 @@ 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
}
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
}
Expand All @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/builder/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8f8efa4

Please sign in to comment.