From 8720479b0bfc95450abb2ba88489f2893e4838a9 Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Tue, 3 Dec 2024 13:33:00 -0500 Subject: [PATCH] Consolidate logic for isolating artifacts --- pkg/cmd/run/download/download.go | 34 ++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/run/download/download.go b/pkg/cmd/run/download/download.go index 04ce74340..8f25e84a2 100644 --- a/pkg/cmd/run/download/download.go +++ b/pkg/cmd/run/download/download.go @@ -151,8 +151,10 @@ func runDownload(opts *DownloadOptions) error { opts.IO.StartProgressIndicator() defer opts.IO.StopProgressIndicator() - // track downloaded artifacts and avoid re-downloading any of the same name + // track downloaded artifacts and avoid re-downloading any of the same name, isolate if multiple artifacts downloaded := set.NewStringSet() + isolateArtifacts := isolateArtifacts(wantNames, wantPatterns) + for _, a := range artifacts { if a.Expired { continue @@ -165,16 +167,9 @@ func runDownload(opts *DownloadOptions) error { continue } } - destDir := opts.DestinationDir - // Isolate the downloaded artifact file to avoid potential conflicts from other downloaded artifacts when: - // - // 1. len(wantPatterns) > 0: Any pattern can result in 2+ artifacts - // 2. len(wantNames) == 0: User wants all artifacts regardless what they are named - // 3. len(wantNames) > 1: User wants multiple, specific artifacts - // - // Otherwise if a single artifact is wanted, then the protective subdirectory is an unnecessary inconvenience. - if len(wantPatterns) > 0 || len(wantNames) != 1 { + destDir := opts.DestinationDir + if isolateArtifacts { destDir = filepath.Join(destDir, a.Name) } @@ -196,6 +191,25 @@ func runDownload(opts *DownloadOptions) error { return nil } +func isolateArtifacts(wantNames []string, wantPatterns []string) bool { + if len(wantPatterns) > 0 { + // Patterns can match multiple artifacts + return true + } + + if len(wantNames) == 0 { + // All artifacts wanted regardless what they are named + return true + } + + if len(wantNames) > 1 { + // Multiple, specific artifacts wanted + return true + } + + return false +} + func matchAnyName(names []string, name string) bool { for _, n := range names { if name == n {