-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtemplate_funcs.go
88 lines (79 loc) · 1.9 KB
/
template_funcs.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
package transformations
import (
"fmt"
"net/url"
"os"
"strings"
"text/template"
)
var funcMap = template.FuncMap{
"env": GetEnv,
"url_host": URLHost,
"url_port": URLPort,
"url_password": URLPassword,
"url_path": URLPath,
"url_scheme": URLScheme,
"url_user": URLUser,
"trim_prefix": strings.TrimPrefix,
"replace": strings.Replace,
}
// GetEnv gets the environment variable
func GetEnv(input string) (string, error) {
val, ok := os.LookupEnv(input)
if !ok {
return "", fmt.Errorf("can't find %s in the environment variables", input)
}
return val, nil
}
// URLUser extracts user from the URL or returns "" if it's not set
func URLUser(input string) (string, error) {
u, err := url.Parse(input)
if err != nil {
return "", err
}
return u.User.Username(), nil
}
// URLPassword extracts password from the URL or returns "" if it's not set
func URLPassword(input string) (string, error) {
u, err := url.Parse(input)
if err != nil {
return "", err
}
p, ok := u.User.Password()
if !ok {
return "", nil
}
return p, nil
}
// URLScheme extracts password from the URL or returns "" if it's not set
func URLScheme(input string) (string, error) {
u, err := url.Parse(input)
if err != nil {
return "", err
}
return u.Scheme, nil
}
// URLHost extracts host from the URL or returns "" if it's not set. It also strips any port if there is any
func URLHost(input string) (string, error) {
u, err := url.Parse(input)
if err != nil {
return "", err
}
return u.Hostname(), nil
}
// URLHost extracts host from the URL or returns "" if it's not set
func URLPort(input string) (string, error) {
u, err := url.Parse(input)
if err != nil {
return "", err
}
return u.Port(), nil
}
// URLPath extracts path from the URL or returns "" if it's not set
func URLPath(input string) (string, error) {
u, err := url.Parse(input)
if err != nil {
return "", err
}
return u.Path, nil
}