Skip to content

Commit

Permalink
Support for rollback of failed installation #522
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasdille committed Dec 20, 2024
1 parent 551aeaa commit 99ba1a4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
7 changes: 6 additions & 1 deletion cmd/uniget/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,12 @@ func installTools(w io.Writer, requestedTools tool.Tools, check bool, plan bool,
if installSpinner != nil {
installSpinner.Fail()
}
logging.Warning.Printfln("Unable to install %s: %s", plannedTool.Name, err)
logging.Error.Printfln("Unable to install %s: %s", plannedTool.Name, err)
logging.Warning.Printfln("Removing partial installation")
err = uninstallFiles(installedFiles)
if err != nil {
logging.Warning.Printfln("Unable to remove partial installation: %s", err)
}
continue
}
logging.Debugf("Installed files: %d", len(installedFiles))
Expand Down
66 changes: 37 additions & 29 deletions cmd/uniget/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,36 +109,13 @@ func uninstallTool(toolName string) error {
}
return fmt.Errorf("unable to read file %s: %s", filename, err)
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSuffix(line, ".go-template")
logging.Debugf("processing %s", line)
strippedLine := strings.TrimPrefix(line, "./")
strippedLine = strings.TrimPrefix(strippedLine, "usr/local/")
logging.Debugf("stripped line %s", strippedLine)
if strippedLine == "" {
continue
}
if strings.HasPrefix(strippedLine, "/") {
logging.Warning.Printfln("Skipping %s because it is not safe to remove", strippedLine)
continue
}

prefixedLine := viper.GetString("prefix") + "/" + viper.GetString("target") + "/" + strippedLine
logging.Debugf("prefixed line %s", prefixedLine)

_, err := os.Lstat(prefixedLine)
if err != nil {
logging.Debugf("Unable to stat %s: %s", prefixedLine, err)
continue
}

err = os.Remove(prefixedLine)
if err != nil {
if uninstallSpinner != nil {
uninstallSpinner.Fail()
}
return fmt.Errorf("unable to remove %s: %s", prefixedLine, err)
installedFiles := strings.Split(string(data), "\n")
err = uninstallFiles(installedFiles)
if err != nil {
if uninstallSpinner != nil {
uninstallSpinner.Fail()
}
return fmt.Errorf("unable to uninstall files: %s", err)
}

} else {
Expand Down Expand Up @@ -215,3 +192,34 @@ func uninstallTool(toolName string) error {

return nil
}

func uninstallFiles(installedFiles []string) error {
for _, file := range installedFiles {
logging.Debugf("processing %s", file)

logging.Debugf("stripped line %s", file)
if file == "" {
continue
}
if strings.HasPrefix(file, "/") {
logging.Warning.Printfln("Skipping %s because it is not safe to remove", file)
continue
}

prefixedFile := viper.GetString("prefix") + "/" + viper.GetString("target") + "/" + file
logging.Debugf("prefixed line %s", prefixedFile)

_, err := os.Lstat(prefixedFile)
if err != nil {
logging.Debugf("Unable to stat %s: %s", prefixedFile, err)
continue
}

err = os.Remove(prefixedFile)
if err != nil {
return fmt.Errorf("unable to remove %s: %s", prefixedFile, err)
}
}

return nil
}

0 comments on commit 99ba1a4

Please sign in to comment.