Skip to content

Commit

Permalink
installer: continue listing packs despite of errors (#62)
Browse files Browse the repository at this point in the history
There might be installed packs with names that are out of the standard.
This may happen due to legacy pack-root folders.

Signed-off-by: Charles Oliveira <[email protected]>
  • Loading branch information
chaws authored Apr 14, 2022
1 parent 42e16dd commit 8cb1fb3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
41 changes: 30 additions & 11 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,33 +244,52 @@ func ListInstalledPacks(listCached, listPublic bool) error {
return nil
}

numErrors := 0
sort.Slice(matches, func(i, j int) bool {
return strings.ToLower(matches[i]) < strings.ToLower(matches[j])
})
for _, pdscFilePath := range matches {
log.Debug(pdscFilePath)

// Transform pdscFilePath into packName
// Transform pdscFilePath into packName, which is printed out
pdscFilePath = strings.Replace(pdscFilePath, Installation.PackRoot, "", -1)
packName, _ := filepath.Split(pdscFilePath)
packName = strings.Replace(packName, "/", " ", -1)
packName = strings.Replace(packName, "\\", " ", -1)
packName = strings.Trim(packName, " ")
packName = strings.Replace(packName, " ", ".", -1) + ".pack"
packName = strings.Replace(packName, " ", ".", -1)
message := packName

// Validate names
errors := []string{}
packNameBits := strings.SplitN(packName, ".", 3)
vendor := packNameBits[0]
name := packNameBits[1]
version := packNameBits[2]

if !utils.IsPackVendorNameValid(vendor) {
errors = append(errors, "vendor")
}

packInfo, err := utils.ExtractPackInfo(packName)
if err != nil {
log.Errorf("A pack in the cache folder has malformed pack name: %s", packName)
return errs.ErrUnknownBehavior
if !utils.IsPackNameValid(name) {
errors = append(errors, "pack name")
}

pdscTag := xml.PdscTag{
Vendor: packInfo.Vendor,
Name: packInfo.Pack,
Version: packInfo.Version,
if !utils.IsPackVersionValid(version) {
errors = append(errors, "pack version")
}

log.Info(pdscTag.Key())
if len(errors) > 0 {
message += " - error: " + strings.Join(errors[:], ", ") + " incorrect format"
numErrors += 1
log.Error(message)
} else {
log.Info(message)
}
}

if numErrors > 0 {
log.Warnf("%d error(s) detected", numErrors)
}
}

Expand Down
37 changes: 37 additions & 0 deletions cmd/installer/root_pack_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,40 @@ func ExampleListInstalledPacks_listInstalled() {
// I: Listing installed packs
// I: TheVendor.PublicLocalPack.1.2.4
}

func ExampleListInstalledPacks_listMalformedInstalledPacks() {
localTestingDir := "test-list-malformed-installed-packs"
_ = installer.SetPackRoot(localTestingDir, CreatePackRoot)
defer os.RemoveAll(localTestingDir)

pdscFilePath := strings.Replace(publicLocalPack123, ".1.2.3.pack", ".pdsc", -1)
_ = utils.CopyFile(pdscFilePath, filepath.Join(installer.Installation.WebDir, "TheVendor.PublicLocalPack.pdsc"))
_ = installer.Installation.PublicIndexXML.AddPdsc(xml.PdscTag{
Vendor: "TheVendor",
Name: "PublicLocalPack",
Version: "1.2.3",
})
_ = installer.AddPack(publicLocalPack123, !CheckEula, !ExtractEula)

// Temper with the installation folder
currVendorFolder := filepath.Join(localTestingDir, "TheVendor")
currPackNameFolder := filepath.Join(localTestingDir, "TheVendor", "PublicLocalPack")
currVersionFolder := filepath.Join(localTestingDir, "TheVendor", "PublicLocalPack", "1.2.3")

temperedVendorFolder := filepath.Join(localTestingDir, "_TheVendor")
temperedPackNameFolder := filepath.Join(localTestingDir, "TheVendor", "_PublicLocalPack")
temperedVersionFolder := filepath.Join(localTestingDir, "TheVendor", "PublicLocalPack", "1.2.3.4")

// Order matters
_ = utils.MoveFile(currVersionFolder, temperedVersionFolder)
_ = utils.MoveFile(currPackNameFolder, temperedPackNameFolder)
_ = utils.MoveFile(currVendorFolder, temperedVendorFolder)

log.SetOutput(os.Stdout)
defer log.SetOutput(ioutil.Discard)
_ = installer.ListInstalledPacks(!ListCached, !ListPublic)
// Output:
// I: Listing installed packs
// E: _TheVendor._PublicLocalPack.1.2.3.4 - error: vendor, pack name, pack version incorrect format
// W: 1 error(s) detected
}

0 comments on commit 8cb1fb3

Please sign in to comment.