1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-27 21:39:20 +02:00

Merge pull request #484 from goreleaser/pull-483

feat: docker link for extra folders
This commit is contained in:
Carlos Alexandro Becker 2017-12-24 10:41:02 -02:00 committed by GitHub
commit d204578516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 116 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"github.com/apex/log"
@ -127,7 +128,7 @@ func process(ctx *context.Context, docker config.Docker, artifact artifact.Artif
return errors.Wrap(err, "failed to link dockerfile")
}
for _, file := range docker.Files {
if err := os.Link(file, filepath.Join(root, filepath.Base(file))); err != nil {
if err := link(file, filepath.Join(root, filepath.Base(file))); err != nil {
return errors.Wrapf(err, "failed to link extra file '%s'", file)
}
}
@ -143,6 +144,26 @@ func process(ctx *context.Context, docker config.Docker, artifact artifact.Artif
return publish(ctx, docker, image, latest)
}
// walks the src, recreating dirs and hard-linking files
func link(src, dest string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
// We have the following:
// - src = "a/b"
// - dest = "dist/linuxamd64/b"
// - path = "a/b/c.txt"
// So we join "a/b" with "c.txt" and use it as the destination.
var dst = filepath.Join(dest, strings.Replace(path, src, "", 1))
log.WithFields(log.Fields{
"src": path,
"dst": dst,
}).Info("extra file")
if info.IsDir() {
return os.MkdirAll(dst, info.Mode())
}
return os.Link(path, dst)
})
}
func publish(ctx *context.Context, docker config.Docker, image, latest string) error {
// TODO: improve this so it can log it to stdout
if !ctx.Publish {

View File

@ -12,6 +12,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pipeline"
"github.com/stretchr/testify/assert"
"syscall"
)
func killAndRm(t *testing.T) {
@ -236,3 +237,96 @@ func TestDefaultSet(t *testing.T) {
assert.Equal(t, "{{ .Version }}", docker.TagTemplate)
assert.Equal(t, "Dockerfile.foo", docker.Dockerfile)
}
func TestLinkFile(t *testing.T) {
const srcFile = "/tmp/test"
const dstFile = "/tmp/linked"
err := ioutil.WriteFile(srcFile, []byte("foo"), 0644)
if err != nil {
t.Log("Cannot setup test file")
t.Fail()
}
err = link(srcFile, dstFile)
if err != nil {
t.Log("Failed to link: ", err)
t.Fail()
}
if inode(srcFile) != inode(dstFile) {
t.Log("Inodes do not match, destination file is not a link")
t.Fail()
}
// cleanup
os.Remove(srcFile)
os.Remove(dstFile)
}
func TestLinkDirectory(t *testing.T) {
const srcDir = "/tmp/testdir"
const testFile = "test"
const dstDir = "/tmp/linkedDir"
os.Mkdir(srcDir, 0755)
err := ioutil.WriteFile(srcDir+"/"+testFile, []byte("foo"), 0644)
if err != nil {
t.Log("Cannot setup test file")
t.Fail()
}
err = link(srcDir, dstDir)
if err != nil {
t.Log("Failed to link: ", err)
t.Fail()
}
if inode(srcDir+"/"+testFile) != inode(dstDir+"/"+testFile) {
t.Log("Inodes do not match, destination file is not a link")
t.Fail()
}
// cleanup
os.RemoveAll(srcDir)
os.RemoveAll(dstDir)
}
func TestLinkTwoLevelDirectory(t *testing.T) {
const srcDir = "/tmp/testdir"
const srcLevel2 = srcDir+"/level2"
const testFile = "test"
const dstDir = "/tmp/linkedDir"
os.Mkdir(srcDir, 0755)
os.Mkdir(srcLevel2, 0755)
err := ioutil.WriteFile(srcDir+"/"+testFile, []byte("foo"), 0644)
if err != nil {
t.Log("Cannot setup test file")
t.Fail()
}
err = ioutil.WriteFile(srcLevel2+"/"+testFile, []byte("foo"), 0644)
if err != nil {
t.Log("Cannot setup test file")
t.Fail()
}
err = link(srcDir, dstDir)
if err != nil {
t.Log("Failed to link: ", err)
t.Fail()
}
if inode(srcDir+"/"+testFile) != inode(dstDir+"/"+testFile) {
t.Log("Inodes do not match")
t.Fail()
}
if inode(srcLevel2+"/"+testFile) != inode(dstDir+"/level2/"+testFile) {
t.Log("Inodes do not match")
t.Fail()
}
// cleanup
os.RemoveAll(srcDir)
os.RemoveAll(dstDir)
}
func inode(file string) uint64 {
fileInfo, err := os.Stat(file)
if err != nil {
return 0
}
stat := fileInfo.Sys().(*syscall.Stat_t)
return stat.Ino
}