|
@@ -3,50 +3,45 @@ package valueObject
|
|
|
import (
|
|
|
"errors"
|
|
|
|
|
|
+ "golang.org/x/exp/maps"
|
|
|
"golang.org/x/exp/slices"
|
|
|
)
|
|
|
|
|
|
type ServiceName string
|
|
|
|
|
|
-var SupportedServiceNames = []string{
|
|
|
- "openlitespeed",
|
|
|
- "nginx",
|
|
|
- "node",
|
|
|
- "mysql",
|
|
|
- "redis",
|
|
|
-}
|
|
|
-
|
|
|
-var SupportedServiceNamesAliases = []string{
|
|
|
- "litespeed",
|
|
|
- "nodejs",
|
|
|
- "mysqld",
|
|
|
- "mariadb",
|
|
|
- "percona",
|
|
|
- "perconadb",
|
|
|
- "redis-server",
|
|
|
+var SupportedServiceNamesAndAliases = map[string][]string{
|
|
|
+ "openlitespeed": {"litespeed"},
|
|
|
+ "node": {"nodejs"},
|
|
|
+ "mysql": {"mysqld", "mariadb", "percona", "perconadb"},
|
|
|
+ "redis": {"redis-server"},
|
|
|
}
|
|
|
|
|
|
func NewServiceName(value string) (ServiceName, error) {
|
|
|
- ss := ServiceName(value)
|
|
|
- if !ss.isValid() {
|
|
|
- return "", errors.New("InvalidServiceName")
|
|
|
+ supportedServicesCorrectName := maps.Keys(SupportedServiceNamesAndAliases)
|
|
|
+ if slices.Contains(supportedServicesCorrectName, value) {
|
|
|
+ return ServiceName(value), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, serviceName := range supportedServicesCorrectName {
|
|
|
+ if slices.Contains(
|
|
|
+ SupportedServiceNamesAndAliases[serviceName],
|
|
|
+ value,
|
|
|
+ ) {
|
|
|
+ return ServiceName(value), nil
|
|
|
+ }
|
|
|
}
|
|
|
- return ss, nil
|
|
|
+
|
|
|
+ return "", errors.New("InvalidServiceName")
|
|
|
}
|
|
|
|
|
|
func NewServiceNamePanic(value string) ServiceName {
|
|
|
- ss := ServiceName(value)
|
|
|
- if !ss.isValid() {
|
|
|
+ ss, err := NewServiceName(value)
|
|
|
+ if err != nil {
|
|
|
panic("InvalidServiceName")
|
|
|
}
|
|
|
return ss
|
|
|
}
|
|
|
|
|
|
-func (ss ServiceName) isValid() bool {
|
|
|
- supportedServices := append(SupportedServiceNames, SupportedServiceNamesAliases...)
|
|
|
- return slices.Contains(supportedServices, ss.String())
|
|
|
-}
|
|
|
-
|
|
|
func (ss ServiceName) String() string {
|
|
|
return string(ss)
|
|
|
}
|