File: mattermost_config.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,432 kB
  • sloc: sh: 74; makefile: 5
file content (121 lines) | stat: -rw-r--r-- 3,826 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package mattermost

import (
	"errors"
	"fmt"
	"net/url"
	"strings"

	"github.com/nicholas-fedor/shoutrrr/pkg/format"
	"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
	"github.com/nicholas-fedor/shoutrrr/pkg/types"
)

// Scheme is the identifying part of this service's configuration URL.
const Scheme = "mattermost"

// Static errors for configuration validation.
var (
	ErrNotEnoughArguments = errors.New(
		"the apiURL does not include enough arguments, either provide 1 or 3 arguments (they may be empty)",
	)
)

// ErrorMessage represents error events within the Mattermost service.
type ErrorMessage string

// Config holds all configuration information for the Mattermost service.
type Config struct {
	standard.EnumlessConfig
	UserName   string `desc:"Override webhook user"                                             optional:"" url:"user"`
	Icon       string `desc:"Use emoji or URL as icon (based on presence of http(s):// prefix)" optional:""                 default:""   key:"icon,icon_emoji,icon_url"`
	Title      string `desc:"Notification title, optionally set by the sender (not used)"                                   default:""   key:"title"`
	Channel    string `desc:"Override webhook channel"                                          optional:"" url:"path2"`
	Host       string `desc:"Mattermost server host"                                                        url:"host,port"`
	Token      string `desc:"Webhook token"                                                                 url:"path1"`
	DisableTLS bool   `                                                                                                     default:"No" key:"disabletls"`
}

// CreateConfigFromURL creates a new Config instance from a URL representation.
func CreateConfigFromURL(url *url.URL) (*Config, error) {
	config := &Config{}
	if err := config.SetURL(url); err != nil {
		return nil, err
	}

	return config, nil
}

// GetURL returns a URL representation of the Config's current field values.
func (c *Config) GetURL() *url.URL {
	resolver := format.NewPropKeyResolver(c)

	return c.getURL(&resolver) // Pass pointer to resolver
}

// SetURL updates the Config from a URL representation of its field values.
func (c *Config) SetURL(url *url.URL) error {
	resolver := format.NewPropKeyResolver(c)

	return c.setURL(&resolver, url) // Pass pointer to resolver
}

// getURL constructs a URL from the Config's fields using the provided resolver.
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
	paths := []string{"", c.Token, c.Channel}
	if c.Channel == "" {
		paths = paths[:2]
	}

	var user *url.Userinfo
	if c.UserName != "" {
		user = url.User(c.UserName)
	}

	return &url.URL{
		User:       user,
		Host:       c.Host,
		Path:       strings.Join(paths, "/"),
		Scheme:     Scheme,
		ForceQuery: false,
		RawQuery:   format.BuildQuery(resolver),
	}
}

// setURL updates the Config from a URL using the provided resolver.
func (c *Config) setURL(resolver types.ConfigQueryResolver, url *url.URL) error {
	c.Host = url.Host
	c.UserName = url.User.Username()

	if err := c.parsePath(url); err != nil {
		return err
	}

	for key, vals := range url.Query() {
		if err := resolver.Set(key, vals[0]); err != nil {
			return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err)
		}
	}

	return nil
}

// parsePath extracts Token and Channel from the URL path and validates arguments.
func (c *Config) parsePath(url *url.URL) error {
	path := strings.Split(strings.Trim(url.Path, "/"), "/")
	isDummy := url.String() == "mattermost://dummy@dummy.com"

	if !isDummy && (len(path) < 1 || path[0] == "") {
		return ErrNotEnoughArguments
	}

	if len(path) > 0 && path[0] != "" {
		c.Token = path[0]
	}

	if len(path) > 1 && path[1] != "" {
		c.Channel = path[1]
	}

	return nil
}