1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00

feat: using filepath.Walk to link files and folders

This commit is contained in:
Carlos Alexandro Becker 2017-12-24 10:12:51 -02:00
parent 478c5dced1
commit bceb42d50b
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940

View File

@ -7,6 +7,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"text/template" "text/template"
"github.com/apex/log" "github.com/apex/log"
@ -16,7 +17,6 @@ import (
"github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pipeline" "github.com/goreleaser/goreleaser/pipeline"
"io/ioutil"
) )
// ErrNoDocker is shown when docker cannot be found in $PATH // ErrNoDocker is shown when docker cannot be found in $PATH
@ -144,60 +144,24 @@ func process(ctx *context.Context, docker config.Docker, artifact artifact.Artif
return publish(ctx, docker, image, latest) return publish(ctx, docker, image, latest)
} }
// link a file or directory hard // walks the src, recreating dirs and hard-linking files
func link(src, dest string) error { func link(src, dest string) error {
info, err := os.Stat(src) return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil { // We have the following:
return err // - src = "a/b"
} // - dest = "dist/linuxamd64/b"
if info.IsDir() { // - path = "a/b/c.txt"
return directoryLink(src, dest, info) // So we join "a/b" with "c.txt" and use it as the destination.
} var dst = filepath.Join(dest, strings.Replace(path, src, "", 1))
return fileLink(src, dest) log.WithFields(log.Fields{
} "src": path,
"dst": dst,
// directoryLink recursively creates all subdirectories and links all files hard }).Info("extra file")
func directoryLink(src, dest string, info os.FileInfo) error {
if info == nil {
i, err := os.Stat(src)
if err != nil {
return err
}
info = i
}
if err := os.MkdirAll(dest, info.Mode()); err != nil {
return err
}
infos, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, info := range infos {
if info.IsDir() { if info.IsDir() {
err := directoryLink( return os.MkdirAll(dst, info.Mode())
filepath.Join(src, info.Name()),
filepath.Join(dest, info.Name()),
info,
)
if err != nil {
return err
}
} else {
err := fileLink(
filepath.Join(src, info.Name()),
filepath.Join(dest, info.Name()),
)
if err != nil {
return err
}
} }
} return os.Link(path, dst)
return nil })
}
// fileLink links a file hard
func fileLink(src, dest string) error {
return os.Link(src, dest)
} }
func publish(ctx *context.Context, docker config.Docker, image, latest string) error { func publish(ctx *context.Context, docker config.Docker, image, latest string) error {