| 1 |
package mattermost |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 11 |
) |
| 12 |
|
| 13 |
// Config holds all configuration information for the Mattermost service. |
| 14 |
type Config struct { |
| 15 |
standard.EnumlessConfig |
| 16 |
|
| 17 |
UserName string `desc:"Override webhook user" optional:"" url:"user"` |
| 18 |
Icon string `desc:"Use emoji or URL as icon (based on presence of http(s):// prefix)" optional:"" default:"" key:"icon,icon_emoji,icon_url"` |
| 19 |
Title string `desc:"Notification title, optionally set by the sender" default:"" key:"title"` |
| 20 |
Channel string `desc:"Override webhook channel" optional:"" url:"path2"` |
| 21 |
Host string `desc:"Mattermost server host" url:"host,port"` |
| 22 |
Token string `desc:"Webhook token" url:"path1"` |
| 23 |
DisableTLS bool ` default:"No" key:"disabletls"` |
| 24 |
} |
| 25 |
|
| 26 |
// Scheme is the identifying part of this service's configuration URL. |
| 27 |
const Scheme = "mattermost" |
| 28 |
|
| 29 |
// CreateConfigFromURL creates a new Config instance from a URL representation. |
| 30 |
func CreateConfigFromURL(serviceURL *url.URL) (*Config, error) { |
| 31 |
// Config has embedded EnumlessConfig and URL-tagged fields are set via SetURL |
| 32 |
config := &Config{} |
| 33 |
if err := config.SetURL(serviceURL); err != nil { |
| 34 |
return nil, err |
| 35 |
} |
| 36 |
|
| 37 |
return config, nil |
| 38 |
} |
| 39 |
|
| 40 |
// GetURL returns a URL representation of the Config's current field values. |
| 41 |
func (c *Config) GetURL() *url.URL { |
| 42 |
resolver := format.NewPropKeyResolver(c) |
| 43 |
|
| 44 |
return c.getURL(&resolver) // Pass pointer to resolver |
| 45 |
} |
| 46 |
|
| 47 |
// SetURL updates the Config from a URL representation of its field values. |
| 48 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 49 |
resolver := format.NewPropKeyResolver(c) |
| 50 |
|
| 51 |
return c.setURL(&resolver, serviceURL) // Pass pointer to resolver |
| 52 |
} |
| 53 |
|
| 54 |
// getURL constructs a URL from the Config's fields using the provided resolver. |
| 55 |
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 56 |
paths := []string{"", c.Token, c.Channel} |
| 57 |
if c.Channel == "" { |
| 58 |
paths = paths[:2] |
| 59 |
} |
| 60 |
|
| 61 |
var user *url.Userinfo |
| 62 |
if c.UserName != "" { |
| 63 |
user = url.User(c.UserName) |
| 64 |
} |
| 65 |
|
| 66 |
// URL is constructed with only the fields needed for service URL representation |
| 67 |
return &url.URL{ |
| 68 |
User: user, |
| 69 |
Host: c.Host, |
| 70 |
Path: strings.Join(paths, "/"), |
| 71 |
Scheme: Scheme, |
| 72 |
ForceQuery: false, |
| 73 |
RawQuery: format.BuildQuery(resolver), |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// parsePath extracts Token and Channel from the URL path and validates arguments. |
| 78 |
func (c *Config) parsePath(serviceURL *url.URL) error { |
| 79 |
path := strings.Split(strings.Trim(serviceURL.Path, "/"), "/") |
| 80 |
isDummy := serviceURL.String() == "mattermost://[email protected]" |
| 81 |
|
| 82 |
if !isDummy && (len(path) < 1 || path[0] == "") { |
| 83 |
return ErrNotEnoughArguments |
| 84 |
} |
| 85 |
|
| 86 |
if len(path) > 0 && path[0] != "" { |
| 87 |
c.Token = path[0] |
| 88 |
} |
| 89 |
|
| 90 |
if len(path) > 1 && path[1] != "" { |
| 91 |
c.Channel = path[1] |
| 92 |
} |
| 93 |
|
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
// setURL updates the Config from a URL using the provided resolver. |
| 98 |
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 99 |
c.Host = serviceURL.Host |
| 100 |
c.UserName = serviceURL.User.Username() |
| 101 |
|
| 102 |
if err := c.parsePath(serviceURL); err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
|
| 106 |
for key, vals := range serviceURL.Query() { |
| 107 |
if err := resolver.Set(key, vals[0]); err != nil { |
| 108 |
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return nil |
| 113 |
} |