Consolidate logic for isolating artifacts

This commit is contained in:
Andy Feller 2024-12-03 13:33:00 -05:00
parent cdfc12caf5
commit 8720479b0b

View file

@ -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 {