From b84daa360fda511718509b614e44630e62c053b3 Mon Sep 17 00:00:00 2001 From: Nathan Hammond Date: Wed, 27 Jul 2022 05:30:18 -0400 Subject: [PATCH 1/3] Always strip parents. --- internal/archivefiles/archivefiles.go | 16 +++++++++++----- internal/archivefiles/archivefiles_test.go | 20 ++++++++++++++++++++ www/docs/customization/archive.md | 3 --- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/internal/archivefiles/archivefiles.go b/internal/archivefiles/archivefiles.go index 7be9e35dd88..968cc230706 100644 --- a/internal/archivefiles/archivefiles.go +++ b/internal/archivefiles/archivefiles.go @@ -64,11 +64,17 @@ func unique(in []config.File) []config.File { } func destinationFor(f config.File, path string) string { - if f.Destination == "" { - return path - } + var filePath string + if f.StripParent { - return filepath.Join(f.Destination, filepath.Base(path)) + filePath = filepath.Base(path) + } else { + filePath = path + } + + if f.Destination == "" { + return filePath + } else { + return filepath.Join(f.Destination, filePath) } - return filepath.Join(f.Destination, path) } diff --git a/internal/archivefiles/archivefiles_test.go b/internal/archivefiles/archivefiles_test.go index a67cf4e68f6..3c99662c6e4 100644 --- a/internal/archivefiles/archivefiles_test.go +++ b/internal/archivefiles/archivefiles_test.go @@ -31,6 +31,26 @@ func TestEval(t *testing.T) { }, result) }) + t.Run("strip parent plays nicely with destination omitted", func(t *testing.T) { + result, err := Eval(tmpl, []config.File{{Source: "./testdata/a/b", StripParent: true}}) + + require.NoError(t, err) + require.Equal(t, []config.File{ + {Source: "testdata/a/b/a.txt", Destination: "a.txt"}, + {Source: "testdata/a/b/c/d.txt", Destination: "d.txt"}, + }, result) + }) + + t.Run("strip parent plays nicely with destination as an empty string", func(t *testing.T) { + result, err := Eval(tmpl, []config.File{{Source: "./testdata/a/b", Destination: "", StripParent: true}}) + + require.NoError(t, err) + require.Equal(t, []config.File{ + {Source: "testdata/a/b/a.txt", Destination: "a.txt"}, + {Source: "testdata/a/b/c/d.txt", Destination: "d.txt"}, + }, result) + }) + t.Run("match multiple files within tree without destination", func(t *testing.T) { result, err := Eval(tmpl, []config.File{{Source: "./testdata/a"}}) diff --git a/www/docs/customization/archive.md b/www/docs/customization/archive.md index 83d1ee80105..01645bd88e3 100644 --- a/www/docs/customization/archive.md +++ b/www/docs/customization/archive.md @@ -142,9 +142,6 @@ files: # ... ``` -!!! warning - `strip_parent` is only effective if `dst` is not empty. - ## Packaging only the binaries Since GoReleaser will always add the `README` and `LICENSE` files to the From ba6e658d42ccf6316b6316e1342252762ad675fe Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Wed, 27 Jul 2022 09:31:33 -0300 Subject: [PATCH 2/3] refactor: improve code a bit Signed-off-by: Carlos A Becker --- internal/archivefiles/archivefiles.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/internal/archivefiles/archivefiles.go b/internal/archivefiles/archivefiles.go index 968cc230706..19f19b8fb42 100644 --- a/internal/archivefiles/archivefiles.go +++ b/internal/archivefiles/archivefiles.go @@ -64,17 +64,11 @@ func unique(in []config.File) []config.File { } func destinationFor(f config.File, path string) string { - var filePath string + result := path if f.StripParent { - filePath = filepath.Base(path) - } else { - filePath = path + result = filepath.Base(path) } - if f.Destination == "" { - return filePath - } else { - return filepath.Join(f.Destination, filePath) - } + return filepath.Join(f.Destination, result) } From fc70b5463bc47c0bc15fd99c1620893825ee65d8 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Wed, 27 Jul 2022 09:36:07 -0300 Subject: [PATCH 3/3] refactor: even simpler Signed-off-by: Carlos A Becker --- internal/archivefiles/archivefiles.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/internal/archivefiles/archivefiles.go b/internal/archivefiles/archivefiles.go index 19f19b8fb42..4852e977aef 100644 --- a/internal/archivefiles/archivefiles.go +++ b/internal/archivefiles/archivefiles.go @@ -64,11 +64,8 @@ func unique(in []config.File) []config.File { } func destinationFor(f config.File, path string) string { - result := path - if f.StripParent { - result = filepath.Base(path) + return filepath.Join(f.Destination, filepath.Base(path)) } - - return filepath.Join(f.Destination, result) + return filepath.Join(f.Destination, path) }