-
Notifications
You must be signed in to change notification settings - Fork 61
/
zulip_config.go
102 lines (81 loc) · 2.46 KB
/
zulip_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package zulip
import (
"errors"
"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
"net/url"
)
// Config for the zulip service
type Config struct {
standard.EnumlessConfig
BotMail string `url:"user" desc:"Bot e-mail address"`
BotKey string `url:"pass" desc:"API Key"`
Host string `url:"host,port" desc:"API server hostname"`
Stream string `key:"stream" optional:"" description:"Target stream name"`
Topic string `key:"topic,title" default:""`
}
// 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(_ types.ConfigQueryResolver) *url.URL {
query := &url.Values{}
if config.Stream != "" {
query.Set("stream", config.Stream)
}
if config.Topic != "" {
query.Set("topic", config.Topic)
}
return &url.URL{
User: url.UserPassword(config.BotMail, config.BotKey),
Host: config.Host,
RawQuery: query.Encode(),
Scheme: Scheme,
}
}
// SetURL updates a ServiceConfig from a URL representation of it's field values
func (config *Config) setURL(_ types.ConfigQueryResolver, serviceURL *url.URL) error {
var ok bool
config.BotMail = serviceURL.User.Username()
if config.BotMail == "" {
return errors.New(string(MissingBotMail))
}
config.BotKey, ok = serviceURL.User.Password()
if !ok {
return errors.New(string(MissingAPIKey))
}
config.Host = serviceURL.Hostname()
if config.Host == "" {
return errors.New(string(MissingHost))
}
config.Stream = serviceURL.Query().Get("stream")
config.Topic = serviceURL.Query().Get("topic")
return nil
}
// Clone the config to a new Config struct
func (config *Config) Clone() *Config {
return &Config{
BotMail: config.BotMail,
BotKey: config.BotKey,
Host: config.Host,
Stream: config.Stream,
Topic: config.Topic,
}
}
const (
// Scheme is the identifying part of this service's configuration URL
Scheme = "zulip"
)
// CreateConfigFromURL to use within the zulip service
func CreateConfigFromURL(serviceURL *url.URL) (*Config, error) {
config := Config{}
err := config.setURL(nil, serviceURL)
return &config, err
}