|
@@ -1,6 +1,7 @@
|
|
|
package plugin_entities
|
|
|
|
|
|
import (
|
|
|
+ "errors"
|
|
|
"regexp"
|
|
|
"strings"
|
|
|
|
|
@@ -12,23 +13,64 @@ type PluginUniqueIdentifier string
|
|
|
|
|
|
var (
|
|
|
// pluginUniqueIdentifierRegexp is a regular expression to validate the plugin unique identifier.
|
|
|
- // It must be in the format of "plugin_id:version@checksum".
|
|
|
+ // It must be in the format of "author/plugin_id:version@checksum".
|
|
|
// all lowercase. the length of plugin_id must be less than 128, and for version part, it must be ^\d{1,4}(\.\d{1,4}){1,3}(-\w{1,16})?$
|
|
|
// for checksum, it must be a 32-character hexadecimal string.
|
|
|
+ // the author part is optional, if not specified, it will be empty.
|
|
|
pluginUniqueIdentifierRegexp = regexp.MustCompile(
|
|
|
- `^[a-z0-9_-]{1,128}:[0-9]{1,4}(\.[0-9]{1,4}){1,3}(-\w{1,16})?@[a-f0-9]{32}$`,
|
|
|
+ `^(?:([a-z0-9_-]{1,64})\/)?([a-z0-9_-]{1,128}):([0-9]{1,4})(\.[0-9]{1,4}){1,3}(-\w{1,16})?@[a-f0-9]{32,64}$`,
|
|
|
)
|
|
|
)
|
|
|
|
|
|
+func NewPluginUniqueIdentifier(identifier string) (PluginUniqueIdentifier, error) {
|
|
|
+ if !pluginUniqueIdentifierRegexp.MatchString(identifier) {
|
|
|
+ return "", errors.New("plugin_unique_identifier is not valid")
|
|
|
+ }
|
|
|
+ return PluginUniqueIdentifier(identifier), nil
|
|
|
+}
|
|
|
+
|
|
|
func (p PluginUniqueIdentifier) PluginID() string {
|
|
|
- // try find @
|
|
|
- split := strings.Split(p.String(), "@")
|
|
|
+ // try find :
|
|
|
+ split := strings.Split(p.String(), ":")
|
|
|
if len(split) == 2 {
|
|
|
return split[0]
|
|
|
}
|
|
|
return p.String()
|
|
|
}
|
|
|
|
|
|
+func (p PluginUniqueIdentifier) Version() string {
|
|
|
+ // extract version part from the string
|
|
|
+ split := strings.Split(p.String(), "@")
|
|
|
+ if len(split) == 2 {
|
|
|
+ split = strings.Split(split[0], ":")
|
|
|
+ if len(split) == 2 {
|
|
|
+ return split[1]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ""
|
|
|
+}
|
|
|
+
|
|
|
+func (p PluginUniqueIdentifier) Author() string {
|
|
|
+ // extract author part from the string
|
|
|
+ split := strings.Split(p.String(), ":")
|
|
|
+ if len(split) == 2 {
|
|
|
+ split = strings.Split(split[0], "/")
|
|
|
+ if len(split) == 2 {
|
|
|
+ return split[0]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ""
|
|
|
+}
|
|
|
+
|
|
|
+func (p PluginUniqueIdentifier) Checksum() string {
|
|
|
+ // extract checksum part from the string
|
|
|
+ split := strings.Split(p.String(), "@")
|
|
|
+ if len(split) == 2 {
|
|
|
+ return split[1]
|
|
|
+ }
|
|
|
+ return ""
|
|
|
+}
|
|
|
+
|
|
|
func (p PluginUniqueIdentifier) String() string {
|
|
|
return string(p)
|
|
|
}
|