-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathroutes.go
70 lines (64 loc) · 1.23 KB
/
routes.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
package main
import (
"fmt"
"path/filepath"
"strings"
)
type routes struct {
Separator string
Values []struct {
Route string
Path string
}
Texts []string
}
func (fv *routes) help() string {
separator := "="
if fv.Separator != "" {
separator = fv.Separator
}
return fmt.Sprintf("a route definition ROUTE%sPATH (ROUTE defaults to basename of PATH if omitted)", separator)
}
// Set is flag.Value.Set
func (fv *routes) Set(v string) error {
separator := "="
if fv.Separator != "" {
separator = fv.Separator
}
i := strings.Index(v, separator)
var route, path string
var err error
if i <= 0 {
path = strings.TrimPrefix(v, "=")
path, err = filepath.Abs(path)
if err != nil {
return err
}
route = fmt.Sprintf("/%s/", filepath.Base(path))
} else {
route = v[:i]
path = v[i+len(separator):]
path, err = filepath.Abs(path)
if err != nil {
return err
}
if !strings.HasPrefix(route, "/") {
route = "/" + route
}
if !strings.HasSuffix(route, "/") {
route = route + "/"
}
}
fv.Texts = append(fv.Texts, v)
fv.Values = append(fv.Values, struct {
Route string
Path string
}{
Route: route,
Path: path,
})
return nil
}
func (fv *routes) String() string {
return strings.Join(fv.Texts, ", ")
}