forked from containrrr/shoutrrr
/
mattermost_config.go
63 lines (53 loc) · 1.48 KB
/
mattermost_config.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
package mattermost
import (
"errors"
"fmt"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"net/url"
"strings"
)
//Config object holding all information
type Config struct {
standard.QuerylessConfig
standard.EnumlessConfig
UserName string
Channel string
Host string
Token string
}
// GetURL returns a URL representation of it's current field values
func (config *Config) GetURL() *url.URL {
return &url.URL{
Host: config.Host,
Path: fmt.Sprintf("/hooks/%s", config.Token),
Scheme: Scheme,
ForceQuery: false,
}
}
// SetURL updates a ServiceConfig from a URL representation of it's field values
func (config *Config) SetURL(serviceURL *url.URL) error {
config.Host = serviceURL.Hostname()
if serviceURL.Path == "" || serviceURL.Path == "/" {
return errors.New(string(NotEnoughArguments))
}
config.UserName = serviceURL.User.Username()
path := strings.Split(serviceURL.Path[1:], "/")
if len(path) < 1 {
return errors.New(string(NotEnoughArguments))
}
config.Token = path[0]
if len(path) > 1 {
if path[1] != "" {
config.Channel = path[1]
}
}
return nil
}
//ErrorMessage for error events within the mattermost service
type ErrorMessage string
const (
// Scheme is the identifying part of this service's configuration URL
Scheme = "mattermost"
// NotEnoughArguments provided in the service URL
NotEnoughArguments ErrorMessage = "the apiURL does not include enough arguments, either provide 1 or 3 arguments (they may be empty)"
)