Skip to content

Commit

Permalink
feat: strip_parent_binary_folder (#3261)
Browse files Browse the repository at this point in the history
* feat: strip_parent_binary_folder

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* docs: clarify dir

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: fmt

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Aug 16, 2022
1 parent 53ed816 commit 4da595e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
8 changes: 6 additions & 2 deletions internal/pipe/archive/archive.go
Expand Up @@ -190,11 +190,15 @@ func doCreate(ctx *context.Context, arch config.Archive, binaries []*artifact.Ar
}
bins := []string{}
for _, binary := range binaries {
dst := binary.Name
if arch.StripParentBinaryFolder {
dst = filepath.Base(dst)
}
if err := a.Add(config.File{
Source: binary.Path,
Destination: binary.Name,
Destination: dst,
}); err != nil {
return fmt.Errorf("failed to add: '%s' -> '%s': %w", binary.Path, binary.Name, err)
return fmt.Errorf("failed to add: '%s' -> '%s': %w", binary.Path, dst, err)
}
bins = append(bins, binary.Name)
}
Expand Down
49 changes: 41 additions & 8 deletions internal/pipe/archive/archive_test.go
Expand Up @@ -33,9 +33,35 @@ func createFakeBinary(t *testing.T, dist, arch, bin string) {

func TestRunPipe(t *testing.T) {
folder := testlib.Mktmp(t)
for _, format := range []string{"tar.gz", "zip"} {
t.Run("Archive format "+format, func(t *testing.T) {
dist := filepath.Join(folder, format+"_dist")
for _, dets := range []struct {
Format string
Strip bool
}{
{
Format: "tar.gz",
Strip: true,
},
{
Format: "tar.gz",
Strip: false,
},

{
Format: "zip",
Strip: true,
},
{
Format: "zip",
Strip: false,
},
} {
format := dets.Format
name := "archive." + format
if dets.Strip {
name = "strip_" + name
}
t.Run(name, func(t *testing.T) {
dist := filepath.Join(folder, name+"_dist")
require.NoError(t, os.Mkdir(dist, 0o755))
for _, arch := range []string{"darwinamd64v1", "darwinall", "linux386", "linuxarm7", "linuxmipssoftfloat", "linuxamd64v3"} {
createFakeBinary(t, dist, arch, "bin/mybin")
Expand All @@ -56,9 +82,10 @@ func TestRunPipe(t *testing.T) {
ProjectName: "foobar",
Archives: []config.Archive{
{
ID: "myid",
Builds: []string{"default"},
NameTemplate: defaultNameTemplate,
ID: "myid",
Builds: []string{"default"},
NameTemplate: defaultNameTemplate,
StripParentBinaryFolder: dets.Strip,
Files: []config.File{
{Source: "README.{{.Os}}.*"},
{Source: "./foo/**/*"},
Expand Down Expand Up @@ -169,6 +196,7 @@ func TestRunPipe(t *testing.T) {
ctx.Config.Archives[0].Format = format
require.NoError(t, Pipe{}.Run(ctx))
archives := ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableArchive)).List()

for _, arch := range archives {
expectBin := "bin/mybin"
if arch.Goos == "windows" {
Expand All @@ -181,6 +209,11 @@ func TestRunPipe(t *testing.T) {
require.Len(t, archives, 7)
// TODO: should verify the artifact fields here too

expectBin := "bin/mybin"
if dets.Strip {
expectBin = "mybin"
}

if format == "tar.gz" {
// Check archive contents
for name, os := range map[string]string{
Expand All @@ -196,7 +229,7 @@ func TestRunPipe(t *testing.T) {
[]string{
fmt.Sprintf("README.%s.md", os),
"foo/bar/foobar/blah.txt",
"bin/mybin",
expectBin,
},
tarFiles(t, filepath.Join(dist, name)),
)
Expand All @@ -208,7 +241,7 @@ func TestRunPipe(t *testing.T) {
[]string{
"README.windows.md",
"foo/bar/foobar/blah.txt",
"bin/mybin.exe",
expectBin + ".exe",
},
zipFiles(t, filepath.Join(dist, "foobar_0.0.1_windows_amd64.zip")),
)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -493,6 +493,7 @@ type Archive struct {
Format string `yaml:"format,omitempty" json:"format,omitempty"`
FormatOverrides []FormatOverride `yaml:"format_overrides,omitempty" json:"format_overrides,omitempty"`
WrapInDirectory string `yaml:"wrap_in_directory,omitempty" json:"wrap_in_directory,omitempty" jsonschema:"oneof_type=string;boolean"`
StripParentBinaryFolder bool `yaml:"strip_parent_binary_folder,omitempty" json:"strip_parent_binary_folder,omitempty"`
Files []File `yaml:"files,omitempty" json:"files,omitempty"`
Meta bool `yaml:"meta,omitempty" json:"meta,omitempty"`
AllowDifferentBinaryCount bool `yaml:"allow_different_binary_count,omitempty" json:"allow_different_binary_count,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions www/docs/customization/archive.md
Expand Up @@ -56,6 +56,11 @@ archives:
# Default is false.
wrap_in_directory: true

# If set to true, will strip the parent directories away from binary files.
# This might be useful if you have your binary be built with a subdir for some reason, but do no want that subdir inside the archive.
# Default is false.
strip_parent_binary_folder: true

# Can be used to change the archive formats for specific GOOSs.
# Most common use case is to archive as zip on Windows.
# Default is empty.
Expand Down

0 comments on commit 4da595e

Please sign in to comment.