-
Notifications
You must be signed in to change notification settings - Fork 3
/
pathspec.go
95 lines (83 loc) · 2.41 KB
/
pathspec.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
// Copyright (c) ClaceIO, LLC
// SPDX-License-Identifier: Apache-2.0
package server
import (
"fmt"
"strings"
"github.com/bmatcuk/doublestar/v4"
"github.com/claceio/clace/internal/utils"
)
// createPathDomain creates a slice of AppPathDomain from a slice of AppInfo
func createPathDomain(apps []utils.AppInfo) []utils.AppPathDomain {
ret := make([]utils.AppPathDomain, 0, len(apps))
for _, app := range apps {
ret = append(ret, app.AppPathDomain)
}
return ret
}
// ParseSpecFromInfo parses a path spec in the format of domain:path. If domain is not specified, it will match empty domain.
// glob patters are supported, *:** matches all apps.
func ParseSpecFromInfo(pathSpec string, apps []utils.AppInfo) ([]utils.AppInfo, error) {
appPathDomain := createPathDomain(apps)
pathDomains, error := ParseSpec(pathSpec, appPathDomain)
if error != nil {
return nil, error
}
found := map[string]bool{}
for _, pathDomain := range pathDomains {
found[pathDomain.String()] = true
}
ret := make([]utils.AppInfo, 0, len(found))
for _, app := range apps {
if found[app.AppPathDomain.String()] {
ret = append(ret, app)
}
}
return ret, nil
}
// ParseSpec parses a path spec in the format of domain:path. If domain is not specified, it will match empty domain.
// glob patters are supported, *:** matches all apps.
func ParseSpec(pathSpec string, apps []utils.AppPathDomain) ([]utils.AppPathDomain, error) {
if pathSpec == "" || strings.ToLower(pathSpec) == "all" {
pathSpec = "*:**"
}
split := strings.Split(pathSpec, ":")
if len(split) > 2 {
return nil, fmt.Errorf("path spec has to be in the format of domain:path")
}
var app, domain string
if len(split) == 2 {
domain = split[0]
app = split[1]
} else {
app = split[0]
}
if app == "" {
return nil, fmt.Errorf("app path spec cannot be empty")
}
if app == "*" {
app = "/*"
}
ret := make([]utils.AppPathDomain, 0)
for _, entry := range apps {
appMatch, err := doublestar.Match(app, entry.Path)
if err != nil {
return nil, fmt.Errorf("invalid path spec app value %s: %s", app, err)
}
if !appMatch {
continue
}
if domain == "" && entry.Domain == "" {
ret = append(ret, entry)
} else {
domainMatch, err := doublestar.Match("/"+domain, "/"+entry.Domain)
if err != nil {
return nil, fmt.Errorf("invalid path spec domain value %s: %s", domain, err)
}
if domainMatch {
ret = append(ret, entry)
}
}
}
return ret, nil
}