Skip to content

Commit

Permalink
Merge pull request #113 from danhutchings/main
Browse files Browse the repository at this point in the history
Prevent "update" items from installing if app is not already installed
  • Loading branch information
1dustindavis authored Jun 10, 2021
2 parents 7eb82ac + 5300775 commit 359a3a3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
56 changes: 27 additions & 29 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func checkRegistry(catalogItem catalog.Item, installType string) (actionNeeded b
gorillalog.Warn("Unable to parse new version: ", checkReg.Version, err)
}

gorillalog.Debug("Check registry version:", checkReg.Version)
// If needed, populate applications status from the registry
if len(RegistryItems) == 0 {
RegistryItems, checkErr = getUninstallKeys()
Expand Down Expand Up @@ -136,44 +137,41 @@ func checkPath(catalogItem catalog.Item, installType string) (actionNeeded bool,
// Iterate through all file provided paths
for _, checkFile := range catalogItem.Check.File {
path := filepath.Clean(checkFile.Path)
gorillalog.Debug("Check File Path", path)

// When doing an install confirm that path exists
// if we get an error, we need to install
if installType == "update" || installType == "install" {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
gorillalog.Debug("Path check failed:", path)
gorillalog.Debug("Check file path:", path)
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {

// when doing an install, and the file path does not exist
// perform an install
if installType == "install" {
actionStore = append(actionStore, true)
break
}
gorillalog.Warn("Unable to check path:", path, err)
break
}
}
// When doing an uninstall confirm that path does not exist
if installType == "uninstall" {
_, err := os.Stat(path)
if err == nil {
gorillalog.Debug("Path exists and doing an uninstall")
actionStore = append(actionStore, true)
break
} else if err != nil {
if os.IsNotExist(err) {
gorillalog.Debug("Path does not exist and doing an uninstall")
actionStore = append(actionStore, false)

// When doing an update or uninstall, and the file path does
// not exist, do nothing
if installType == "update" || installType == "uninstall" {
gorillalog.Debug("No action needed: Install type is", installType)
break
}
}
gorillalog.Warn("Unable to check path:", path, err)
break

} else if err == nil {

// When doing an uninstall, and the path exists
// perform uninstall
if installType == "uninstall" {
actionStore = append(actionStore, true)
}
}

// If a hash is not blank, verify it matches the file
// if the hash does not match, we need to install
if checkFile.Hash != "" {
gorillalog.Debug("Check File Hash:", checkFile.Hash)
gorillalog.Debug("Check file hash:", checkFile.Hash)
hashMatch := download.Verify(path, checkFile.Hash)
if !hashMatch {
actionStore = append(actionStore, true)
Expand All @@ -182,7 +180,7 @@ func checkPath(catalogItem catalog.Item, installType string) (actionNeeded bool,
}

if checkFile.Version != "" {
gorillalog.Debug("Check File Version:", checkFile.Version)
gorillalog.Debug("Check file version:", checkFile.Version)

// Get the file metadata, and check that it has a value
metadata := GetFileMetadata(path)
Expand Down Expand Up @@ -228,15 +226,15 @@ func checkPath(catalogItem catalog.Item, installType string) (actionNeeded bool,
func CheckStatus(catalogItem catalog.Item, installType, cachePath string) (actionNeeded bool, checkErr error) {

if catalogItem.Check.Script != "" {
gorillalog.Info("Checking status via Script:", catalogItem.DisplayName)
gorillalog.Info("Checking status via script:", catalogItem.DisplayName)
return checkScript(catalogItem, cachePath, installType)

} else if catalogItem.Check.File != nil {
gorillalog.Info("Checking status via File:", catalogItem.DisplayName)
gorillalog.Info("Checking status via file:", catalogItem.DisplayName)
return checkPath(catalogItem, installType)

} else if catalogItem.Check.Registry.Version != "" {
gorillalog.Info("Checking status via Registry:", catalogItem.DisplayName)
gorillalog.Info("Checking status via registry:", catalogItem.DisplayName)
return checkRegistry(catalogItem, installType)
}

Expand Down
24 changes: 21 additions & 3 deletions pkg/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ var (
}},
},
}
pathMissing = catalog.Item{
Check: catalog.InstallCheck{
File: []catalog.FileCheck{{
Path: `testdata/bogus.msi`,
Hash: `ba7d5a895f1c500aa3b4ae35f3878595f4587054a32fa6d7e9f46363525c59e8`,
}},
},
}
pathMetadataInstalled = catalog.Item{
Check: catalog.InstallCheck{
File: []catalog.FileCheck{{
Expand Down Expand Up @@ -293,6 +301,16 @@ func TestCheckPath(t *testing.T) {
t.Errorf("actionNeeded: %v; Expected checkPath to return false", actionNeeded)
}

// Run checkPath for file that doesn't exist
// We expect action is not needed; Only error if action needed is true
actionNeeded, err = checkPath(pathMissing, "update")
if err != nil {
t.Errorf("checkPath failed: %v", err)
}
if actionNeeded {
t.Errorf("actionNeeded: %v; Expected checkPath to return false", actionNeeded)
}

// Run checkPath for pathNotInstalled
// We expect action is needed; Only error if actionNeeded is false
actionNeeded, err = checkPath(pathNotInstalled, "install")
Expand Down Expand Up @@ -339,7 +357,7 @@ func ExampleCheckStatus_script() {
CheckStatus(scriptCheckItem, "install", "testdata/")

// Output:
// Checking status via Script: scriptCheckItem
// Checking status via script: scriptCheckItem
}

// ExampleCheckStatus_file validates that a file check is ran
Expand All @@ -356,7 +374,7 @@ func ExampleCheckStatus_file() {
CheckStatus(fileCheckItem, "install", "testdata/")

// Output:
// Checking status via File: fileCheckItem
// Checking status via file: fileCheckItem
}

// ExampleCheckStatus_registry validates that a registry check is ran
Expand All @@ -373,7 +391,7 @@ func ExampleCheckStatus_registry() {
CheckStatus(registryCheckItem, "install", "testdata/")

// Output:
// Checking status via Registry: registryCheckItem
// Checking status via registry: registryCheckItem
}

// ExampleCheckStatus_none validates that no check is ran
Expand Down

0 comments on commit 359a3a3

Please sign in to comment.