From 02f488a694dadbee576f592cd729654e0c05d25d Mon Sep 17 00:00:00 2001 From: Prima Adi Pradana Date: Fri, 22 Mar 2024 09:32:40 +0700 Subject: [PATCH] =?UTF-8?q?Fix=20#3032:=20Remove=20query=20parameters=20in?= =?UTF-8?q?=20ADD=20command=20when=20the=20destinatio=E2=80=A6=20(#3053)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix #3032: Remove query parameters in ADD command when the destination is a directory * fixing linter URL sorry forget to lint * add error in extractFilename and realize that ResolveEnvironmentReplacement better execute before getting the filename --- pkg/util/command_util.go | 20 ++++++++++++++++++-- pkg/util/command_util_test.go | 6 ++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/pkg/util/command_util.go b/pkg/util/command_util.go index a0a7ca686..f2866770b 100644 --- a/pkg/util/command_util.go +++ b/pkg/util/command_util.go @@ -216,11 +216,17 @@ func URLDestinationFilepath(rawurl, dest, cwd string, envs []string) (string, er } return dest, nil } - urlBase := filepath.Base(rawurl) - urlBase, err := ResolveEnvironmentReplacement(urlBase, envs, true) + + urlBase, err := ResolveEnvironmentReplacement(rawurl, envs, true) if err != nil { return "", err } + + urlBase, err = extractFilename(urlBase) + if err != nil { + return "", err + } + destPath := filepath.Join(dest, urlBase) if !filepath.IsAbs(dest) { @@ -470,3 +476,13 @@ func getUID(userStr string) (uint32, error) { } return uint32(uid), nil } + +// ExtractFilename extracts the filename from a URL without its query url +func extractFilename(rawURL string) (string, error) { + parsedURL, err := url.Parse(rawURL) + if err != nil { + return "", err + } + filename := filepath.Base(parsedURL.Path) + return filename, nil +} diff --git a/pkg/util/command_util_test.go b/pkg/util/command_util_test.go index 19915cfd7..963427820 100644 --- a/pkg/util/command_util_test.go +++ b/pkg/util/command_util_test.go @@ -226,6 +226,12 @@ var urlDestFilepathTests = []struct { dest: "/test", expectedDest: "/test", }, + { + url: "https://something/something.tar?foo=bar", + cwd: "/cwd", + dest: "/dir/", + expectedDest: "/dir/something.tar", + }, { url: "https://something/something", cwd: "/test",