/
slack_config.go
83 lines (67 loc) · 2.01 KB
/
slack_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package slack
import (
"fmt"
"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
"net/url"
"strings"
)
// Config for the slack service
type Config struct {
standard.EnumlessConfig
BotName string `default:"" optional:""`
Token []string `description:"List of comma separated token parts"`
Color string `key:"color" optional:""`
Title string `key:"title" optional:""`
}
// GetURL returns a URL representation of it's current field values
func (config *Config) GetURL() *url.URL {
resolver := format.NewPropKeyResolver(config)
return config.getURL(&resolver)
}
// SetURL updates a ServiceConfig from a URL representation of it's field values
func (config *Config) SetURL(url *url.URL) error {
resolver := format.NewPropKeyResolver(config)
return config.setURL(&resolver, url)
}
func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
return &url.URL{
User: url.User(config.BotName),
Host: config.Token[0],
Path: fmt.Sprintf("/%s/%s", config.Token[1], config.Token[2]),
Scheme: Scheme,
ForceQuery: false,
RawQuery: format.BuildQuery(resolver),
}
}
func (config *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
botName := serviceURL.User.Username()
host := serviceURL.Hostname()
token := strings.Split(serviceURL.Path, "/")
token[0] = host
if len(token) < 2 {
token = []string{"", "", ""}
}
config.BotName = botName
config.Token = token
if err := ValidateToken(config.Token); err != nil {
return err
}
for key, vals := range serviceURL.Query() {
if err := resolver.Set(key, vals[0]); err != nil {
return err
}
}
return nil
}
const (
// Scheme is the identifying part of this service's configuration URL
Scheme = "slack"
)
// CreateConfigFromURL to use within the slack service
func CreateConfigFromURL(serviceURL *url.URL) (*Config, error) {
config := Config{}
err := config.SetURL(serviceURL)
return &config, err
}