@@ -24,6 +24,7 @@ import (
2424 v1 "github.com/elastic/elastic-agent/pkg/api/v1"
2525 "github.com/elastic/elastic-agent/pkg/component"
2626 "github.com/elastic/elastic-agent/pkg/core/logger"
27+ manifestutils "github.com/elastic/elastic-agent/pkg/utils/manifest"
2728)
2829
2930// UnpackResult contains the location and hash of the unpacked agent files
@@ -115,7 +116,7 @@ func unzip(log *logger.Logger, archivePath, dataDir string, flavor string, copy
115116
116117 fileNamePrefix := strings .TrimSuffix (filepath .Base (archivePath ), ".zip" ) + "/" // omitting `elastic-agent-{version}-{os}-{arch}/` in filename
117118
118- pm := pathMapper {}
119+ var pm * manifestutils. PathMapper
119120 var versionedHome string
120121
121122 metadata , err := getPackageMetadataFromZipReader (r , fileNamePrefix )
@@ -126,10 +127,11 @@ func unzip(log *logger.Logger, archivePath, dataDir string, flavor string, copy
126127 hash = metadata .hash [:hashLen ]
127128 var registry map [string ][]string
128129 if metadata .manifest != nil {
129- pm . mappings = metadata .manifest .Package .PathMappings
130+ pm = manifestutils . NewPathMapper ( metadata .manifest .Package .PathMappings )
130131 versionedHome = filepath .FromSlash (pm .Map (metadata .manifest .Package .VersionedHome ))
131132 registry = metadata .manifest .Package .Flavors
132133 } else {
134+ pm = manifestutils .NewPathMapper (nil )
133135 // if at this point we didn't load the manifest, set the versioned to the backup value
134136 versionedHome = createVersionedHomeFromHash (hash )
135137 }
@@ -361,7 +363,7 @@ func untar(log *logger.Logger, archivePath, dataDir string, flavor string, copy
361363 var hash string
362364
363365 // Look up manifest in the archive and prepare path mappings, if any
364- pm := pathMapper {}
366+ var pm * manifestutils. PathMapper
365367
366368 metadata , err := getPackageMetadataFromTar (archivePath )
367369 if err != nil {
@@ -373,10 +375,11 @@ func untar(log *logger.Logger, archivePath, dataDir string, flavor string, copy
373375
374376 if metadata .manifest != nil {
375377 // set the path mappings
376- pm . mappings = metadata .manifest .Package .PathMappings
378+ pm = manifestutils . NewPathMapper ( metadata .manifest .Package .PathMappings )
377379 versionedHome = filepath .FromSlash (pm .Map (metadata .manifest .Package .VersionedHome ))
378380 registry = metadata .manifest .Package .Flavors
379381 } else {
382+ pm = manifestutils .NewPathMapper (nil )
380383 // set default value of versioned home if it wasn't set by reading the manifest
381384 versionedHome = createVersionedHomeFromHash (metadata .hash )
382385 }
@@ -661,21 +664,6 @@ func validFileName(p string) bool {
661664 return true
662665}
663666
664- type pathMapper struct {
665- mappings []map [string ]string
666- }
667-
668- func (pm pathMapper ) Map (packagePath string ) string {
669- for _ , mapping := range pm .mappings {
670- for pkgPath , mappedPath := range mapping {
671- if strings .HasPrefix (packagePath , pkgPath ) {
672- return path .Join (mappedPath , packagePath [len (pkgPath ):])
673- }
674- }
675- }
676- return packagePath
677- }
678-
679667type tarCloser struct {
680668 tarFile * os.File
681669 gzipReader * gzip.Reader
0 commit comments