-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemuri.go
51 lines (44 loc) · 1.87 KB
/
systemuri.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
package systemuri
import (
"fmt"
"regexp"
"strings"
)
var validateScheme = regexp.MustCompile("^[a-zA-Z0-9-]*$")
// RegisterURLHandlerPassingAll registers a custom URL handler for the specified schema and application
func RegisterURLHandlerPassingAll(name string, scheme string, applicationPath string) error {
return registerURLHandler(name, scheme, applicationPath, "%s")
}
// RegisterURLHandler registers a custom URL handler for the specified schema and application. argumentsPattern the %s is replaced by the system-specific WINDOWS_PLACEHOLDER
func RegisterURLHandler(name string, scheme string, applicationPath string, argumentsPattern string) error {
if err := validateURLHandleParameters(name, scheme, applicationPath, argumentsPattern); err != nil {
return err
}
return registerURLHandler(name, scheme, applicationPath, argumentsPattern)
}
// UnregisterURLHandler removes all registered entries based on the scheme
func UnregisterURLHandler(scheme string) error {
return unregisterURLHandler(scheme)
}
// UnregisterURLHandlerByPath removes all registered entries based on the application path
func UnregisterURLHandlerByPath(applicationPath string) error {
return unregisterURLHandlerByPath(applicationPath)
}
func validateURLHandleParameters(name string, scheme string, applicationPath string, argumentsPattern string) error {
if name == "" {
return fmt.Errorf("`name` is required but empty")
}
if scheme == "" {
return fmt.Errorf("`scheme` is required but empty")
}
if !validateScheme.MatchString(scheme) {
return fmt.Errorf("`scheme` must match the regex %v", validateScheme.String())
}
if applicationPath == "" {
return fmt.Errorf("`applicationPath` is required but empty")
}
if argumentsPattern != "" && !strings.Contains(argumentsPattern, "%s") {
return fmt.Errorf("`argumentsPattern` has to contain %%s to be replaced by os-specific placeholder")
}
return nil
}