-
Notifications
You must be signed in to change notification settings - Fork 13
/
dcsg.go
112 lines (86 loc) · 2.82 KB
/
dcsg.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/pkg/errors"
)
func newDscg(dockerComposeFile, projectName string, dryRun bool, doPull bool) (*dcsg, error) {
cleanedFilePath, err := filepath.Abs(dockerComposeFile)
if err != nil {
return nil, err
}
if !fileExists(cleanedFilePath) {
return nil, fmt.Errorf("The Docker Compose file %q does not exist", cleanedFilePath)
}
if projectName == "" {
projectNameFromDirectory, projectNameError := getProjectName(dockerComposeFile)
if projectNameError != nil {
return nil, projectNameError
}
projectName = projectNameFromDirectory
}
projectDirectory, err := getProjectDirectory(dockerComposeFile)
if err != nil {
return nil, err
}
dockerComposeFileName := filepath.Base(dockerComposeFile)
systemdDirectory := "/etc/systemd/system"
commandExecutor := newExecutor(os.Stdin, os.Stdout, os.Stderr, "", dryRun)
return &dcsg{
projectDirectory: projectDirectory,
dockerComposeFileName: dockerComposeFileName,
projectName: projectName,
installer: &systemdInstaller{systemdDirectory, commandExecutor, dryRun, doPull},
uninstaller: &systemdUninstaller{systemdDirectory, commandExecutor, dryRun},
}, nil
}
type dcsg struct {
projectDirectory string
dockerComposeFileName string
projectName string
installer installer
uninstaller uninstaller
}
func (service dcsg) Install() error {
err := service.installer.Install(service.projectDirectory, service.dockerComposeFileName, service.projectName)
if err != nil {
return errors.Wrap(err, "Installation failed")
}
return nil
}
func (service dcsg) Uninstall() error {
err := service.uninstaller.Uninstall(service.projectDirectory, service.dockerComposeFileName, service.projectName)
if err != nil {
return errors.Wrap(err, "Uninstall failed")
}
return nil
}
func getProjectDirectory(dockerComposeFile string) (string, error) {
cleanedFilePath, err := filepath.Abs(dockerComposeFile)
if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("Failed to determine the project directory from %q", dockerComposeFile))
}
return filepath.Dir(cleanedFilePath), nil
}
var invalidProjectNameCharacters = regexp.MustCompile("[^a-z0-9]")
func fileExists(filePath string) bool {
_, err := os.Stat(filePath)
if err != nil {
return false
}
return true
}
func getProjectName(dockerComposeFile string) (string, error) {
directoryPath, err := getProjectDirectory(dockerComposeFile)
if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("Failed to determine the project directory from %q", dockerComposeFile))
}
directoryName := filepath.Base(directoryPath)
projectName := strings.ToLower(directoryName)
projectName = strings.TrimSpace(projectName)
projectName = invalidProjectNameCharacters.ReplaceAllString(projectName, "")
return projectName, nil
}