Skip to content

Commit 99ba1a4

Browse files
committed
Support for rollback of failed installation #522
1 parent 551aeaa commit 99ba1a4

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

cmd/uniget/install.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,12 @@ func installTools(w io.Writer, requestedTools tool.Tools, check bool, plan bool,
375375
if installSpinner != nil {
376376
installSpinner.Fail()
377377
}
378-
logging.Warning.Printfln("Unable to install %s: %s", plannedTool.Name, err)
378+
logging.Error.Printfln("Unable to install %s: %s", plannedTool.Name, err)
379+
logging.Warning.Printfln("Removing partial installation")
380+
err = uninstallFiles(installedFiles)
381+
if err != nil {
382+
logging.Warning.Printfln("Unable to remove partial installation: %s", err)
383+
}
379384
continue
380385
}
381386
logging.Debugf("Installed files: %d", len(installedFiles))

cmd/uniget/uninstall.go

+37-29
Original file line numberDiff line numberDiff line change
@@ -109,36 +109,13 @@ func uninstallTool(toolName string) error {
109109
}
110110
return fmt.Errorf("unable to read file %s: %s", filename, err)
111111
}
112-
for _, line := range strings.Split(string(data), "\n") {
113-
line = strings.TrimSuffix(line, ".go-template")
114-
logging.Debugf("processing %s", line)
115-
strippedLine := strings.TrimPrefix(line, "./")
116-
strippedLine = strings.TrimPrefix(strippedLine, "usr/local/")
117-
logging.Debugf("stripped line %s", strippedLine)
118-
if strippedLine == "" {
119-
continue
120-
}
121-
if strings.HasPrefix(strippedLine, "/") {
122-
logging.Warning.Printfln("Skipping %s because it is not safe to remove", strippedLine)
123-
continue
124-
}
125-
126-
prefixedLine := viper.GetString("prefix") + "/" + viper.GetString("target") + "/" + strippedLine
127-
logging.Debugf("prefixed line %s", prefixedLine)
128-
129-
_, err := os.Lstat(prefixedLine)
130-
if err != nil {
131-
logging.Debugf("Unable to stat %s: %s", prefixedLine, err)
132-
continue
133-
}
134-
135-
err = os.Remove(prefixedLine)
136-
if err != nil {
137-
if uninstallSpinner != nil {
138-
uninstallSpinner.Fail()
139-
}
140-
return fmt.Errorf("unable to remove %s: %s", prefixedLine, err)
112+
installedFiles := strings.Split(string(data), "\n")
113+
err = uninstallFiles(installedFiles)
114+
if err != nil {
115+
if uninstallSpinner != nil {
116+
uninstallSpinner.Fail()
141117
}
118+
return fmt.Errorf("unable to uninstall files: %s", err)
142119
}
143120

144121
} else {
@@ -215,3 +192,34 @@ func uninstallTool(toolName string) error {
215192

216193
return nil
217194
}
195+
196+
func uninstallFiles(installedFiles []string) error {
197+
for _, file := range installedFiles {
198+
logging.Debugf("processing %s", file)
199+
200+
logging.Debugf("stripped line %s", file)
201+
if file == "" {
202+
continue
203+
}
204+
if strings.HasPrefix(file, "/") {
205+
logging.Warning.Printfln("Skipping %s because it is not safe to remove", file)
206+
continue
207+
}
208+
209+
prefixedFile := viper.GetString("prefix") + "/" + viper.GetString("target") + "/" + file
210+
logging.Debugf("prefixed line %s", prefixedFile)
211+
212+
_, err := os.Lstat(prefixedFile)
213+
if err != nil {
214+
logging.Debugf("Unable to stat %s: %s", prefixedFile, err)
215+
continue
216+
}
217+
218+
err = os.Remove(prefixedFile)
219+
if err != nil {
220+
return fmt.Errorf("unable to remove %s: %s", prefixedFile, err)
221+
}
222+
}
223+
224+
return nil
225+
}

0 commit comments

Comments
 (0)