-
Notifications
You must be signed in to change notification settings - Fork 13
/
uninstaller.go
67 lines (53 loc) · 1.86 KB
/
uninstaller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/pkg/errors"
)
type uninstaller interface {
Uninstall(projectDirectory, dockerComposeFileName, projectName string) error
}
type systemdUninstaller struct {
systemdDirectory string
commandExecutor Executor
dryRun bool
}
func (uninstaller *systemdUninstaller) Uninstall(projectDirectory, dockerComposeFileName, projectName string) error {
serviceName := getServiceName(projectName)
serviceViewModel := serviceDefinition{
ProjectName: projectName,
ProjectDirectory: projectDirectory,
DockerComposeFile: dockerComposeFileName,
}
stopError := uninstaller.commandExecutor.Run("systemctl", "stop", serviceName)
if stopError != nil {
return errors.Wrap(stopError, fmt.Sprintf("Failed to stop %q", serviceName))
}
disableError := uninstaller.commandExecutor.Run("systemctl", "disable", serviceName)
if disableError != nil {
return errors.Wrap(disableError, fmt.Sprintf("Failed to disable %q", serviceName))
}
removeError := uninstaller.removeSystemdService(serviceViewModel)
if removeError != nil {
return errors.Wrap(removeError, fmt.Sprintf("Failed to remove the systemd service %q", serviceViewModel.ProjectName))
}
reloadError := uninstaller.commandExecutor.Run("systemctl", "daemon-reload")
if reloadError != nil {
return errors.Wrap(reloadError, "Failed to reload the systemd configuration")
}
return nil
}
func (uninstaller *systemdUninstaller) removeSystemdService(service serviceDefinition) error {
serviceFilePath := filepath.Join(uninstaller.systemdDirectory, getServiceName(service.ProjectName))
if uninstaller.dryRun {
log.Println("Would remove file:", serviceFilePath)
} else {
removeError := os.Remove(serviceFilePath)
if removeError != nil {
return errors.Wrap(removeError, fmt.Sprintf("Failed to remove %q", serviceFilePath))
}
}
return nil
}