/
custom.go
55 lines (44 loc) · 1.14 KB
/
custom.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
package providers
import (
"fmt"
"net/url"
"regexp"
"strings"
)
var alphanums = regexp.MustCompile("^[a-zA-Z0-9_]*$")
// CustomProvider is used for Config based providers.
type CustomProvider struct {
Name string `json:"name"`
URL string `json:"url"`
TagList []string `json:"tags"`
}
// BuildURI builds the URI for custom providers.
func (c *CustomProvider) BuildURI(q string) string {
return fmt.Sprintf(c.URL, url.QueryEscape(q))
}
// Tags returns the tags relevant to this provider.
func (c *CustomProvider) Tags() []string {
return c.TagList
}
// Valid checks if the custom provider is setup correctly.
func (c *CustomProvider) Valid() error {
// Validate name is only alphanums.
if !alphanums.Match([]byte(c.Name)) {
return fmt.Errorf("name must be alphanumeric")
}
u, err := url.Parse(c.URL)
if err != nil {
return err
}
c.URL = u.String()
// Make sure query token is present
hasToken := strings.Contains(c.URL, "%s")
if !hasToken {
return fmt.Errorf("token %q required", "%s")
}
// Validate scheme is set. Don't limit to http.
if u.Scheme == "" {
return fmt.Errorf("url scheme required")
}
return nil
}