Skip to content

Commit f77776a

Browse files
committed
refactor(param): move util funcs out of main file
1 parent e3764ff commit f77776a

File tree

2 files changed

+99
-99
lines changed

2 files changed

+99
-99
lines changed

src/param/main.go

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
package param
22

3-
import (
4-
"../util"
5-
"path"
6-
"path/filepath"
7-
"regexp"
8-
"strings"
9-
"unicode/utf8"
10-
)
3+
import "regexp"
114

125
type user struct {
136
Username string
@@ -58,93 +51,3 @@ type Param struct {
5851
AccessLog string
5952
ErrorLog string
6053
}
61-
62-
func splitMapping(input string) (k, v string, ok bool) {
63-
sep, sepLen := utf8.DecodeRuneInString(input)
64-
if sepLen == 0 {
65-
return
66-
}
67-
entry := input[sepLen:]
68-
if len(entry) == 0 {
69-
return
70-
}
71-
72-
sepIndex := strings.IndexRune(entry, sep)
73-
if sepIndex <= 0 || sepIndex+sepLen == len(entry) {
74-
return
75-
}
76-
77-
k = entry[:sepIndex]
78-
v = entry[sepIndex+sepLen:]
79-
return k, v, true
80-
}
81-
82-
func normalizePathMaps(inputs []string) map[string]string {
83-
maps := map[string]string{}
84-
85-
for _, input := range inputs {
86-
urlPath, fsPath, ok := splitMapping(input)
87-
if !ok {
88-
continue
89-
}
90-
91-
urlPath = util.CleanUrlPath(urlPath)
92-
fsPath = path.Clean(fsPath)
93-
maps[urlPath] = fsPath
94-
}
95-
96-
return maps
97-
}
98-
99-
func normalizeUrlPaths(inputs []string) []string {
100-
outputs := make([]string, 0, len(inputs))
101-
102-
for _, input := range inputs {
103-
if len(input) == 0 {
104-
continue
105-
}
106-
outputs = append(outputs, util.CleanUrlPath(input))
107-
}
108-
109-
return outputs
110-
}
111-
112-
func normalizeFsPaths(inputs []string) []string {
113-
outputs := make([]string, 0, len(inputs))
114-
115-
for _, input := range inputs {
116-
if len(input) == 0 {
117-
continue
118-
}
119-
120-
abs, err := filepath.Abs(input)
121-
if err != nil {
122-
continue
123-
}
124-
125-
outputs = append(outputs, abs)
126-
}
127-
128-
return outputs
129-
}
130-
131-
func getWildcardRegexp(wildcards []string, found bool) (*regexp.Regexp, error) {
132-
if !found || len(wildcards) == 0 {
133-
return nil, nil
134-
}
135-
136-
normalizedWildcards := make([]string, 0, len(wildcards))
137-
for _, wildcard := range wildcards {
138-
if len(wildcard) == 0 {
139-
continue
140-
}
141-
normalizedWildcards = append(normalizedWildcards, util.WildcardToRegexp(wildcard))
142-
}
143-
144-
if len(normalizedWildcards) == 0 {
145-
return nil, nil
146-
}
147-
148-
exp := strings.Join(normalizedWildcards, "|")
149-
return regexp.Compile(exp)
150-
}

src/param/util.go

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,82 @@
11
package param
22

3-
import "strings"
3+
import (
4+
"../util"
5+
"path"
6+
"path/filepath"
7+
"regexp"
8+
"strings"
9+
"unicode/utf8"
10+
)
11+
12+
func splitMapping(input string) (k, v string, ok bool) {
13+
sep, sepLen := utf8.DecodeRuneInString(input)
14+
if sepLen == 0 {
15+
return
16+
}
17+
entry := input[sepLen:]
18+
if len(entry) == 0 {
19+
return
20+
}
21+
22+
sepIndex := strings.IndexRune(entry, sep)
23+
if sepIndex <= 0 || sepIndex+sepLen == len(entry) {
24+
return
25+
}
26+
27+
k = entry[:sepIndex]
28+
v = entry[sepIndex+sepLen:]
29+
return k, v, true
30+
}
31+
32+
func normalizePathMaps(inputs []string) map[string]string {
33+
maps := map[string]string{}
34+
35+
for _, input := range inputs {
36+
urlPath, fsPath, ok := splitMapping(input)
37+
if !ok {
38+
continue
39+
}
40+
41+
urlPath = util.CleanUrlPath(urlPath)
42+
fsPath = path.Clean(fsPath)
43+
maps[urlPath] = fsPath
44+
}
45+
46+
return maps
47+
}
48+
49+
func normalizeUrlPaths(inputs []string) []string {
50+
outputs := make([]string, 0, len(inputs))
51+
52+
for _, input := range inputs {
53+
if len(input) == 0 {
54+
continue
55+
}
56+
outputs = append(outputs, util.CleanUrlPath(input))
57+
}
58+
59+
return outputs
60+
}
61+
62+
func normalizeFsPaths(inputs []string) []string {
63+
outputs := make([]string, 0, len(inputs))
64+
65+
for _, input := range inputs {
66+
if len(input) == 0 {
67+
continue
68+
}
69+
70+
abs, err := filepath.Abs(input)
71+
if err != nil {
72+
continue
73+
}
74+
75+
outputs = append(outputs, abs)
76+
}
77+
78+
return outputs
79+
}
480

581
func getUsers(userEntries []string) []*user {
682
users := make([]*user, 0, len(userEntries))
@@ -38,3 +114,24 @@ func getDupUserNames(usersGroups ...[]*user) []string {
38114
}
39115
return dupUsers
40116
}
117+
118+
func getWildcardRegexp(wildcards []string, found bool) (*regexp.Regexp, error) {
119+
if !found || len(wildcards) == 0 {
120+
return nil, nil
121+
}
122+
123+
normalizedWildcards := make([]string, 0, len(wildcards))
124+
for _, wildcard := range wildcards {
125+
if len(wildcard) == 0 {
126+
continue
127+
}
128+
normalizedWildcards = append(normalizedWildcards, util.WildcardToRegexp(wildcard))
129+
}
130+
131+
if len(normalizedWildcards) == 0 {
132+
return nil, nil
133+
}
134+
135+
exp := strings.Join(normalizedWildcards, "|")
136+
return regexp.Compile(exp)
137+
}

0 commit comments

Comments
 (0)