File: pushbullet_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 (95 lines) | stat: -rw-r--r-- 2,514 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
package pushbullet

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 scheme part of the service configuration URL.
const Scheme = "pushbullet"

// ExpectedTokenLength is the required length for a valid Pushbullet token.
const ExpectedTokenLength = 34

// ErrTokenIncorrectSize indicates that the token has an incorrect size.
var ErrTokenIncorrectSize = errors.New("token has incorrect size")

// Config holds the configuration for the Pushbullet service.
type Config struct {
	standard.EnumlessConfig
	Targets []string `url:"path"`
	Token   string   `url:"host"`
	Title   string   `           default:"Shoutrrr notification" key:"title"`
}

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

	return config.getURL(&resolver)
}

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

	return config.setURL(&resolver, url)
}

// getURL constructs a URL from the Config's fields using the provided resolver.
func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
	return &url.URL{
		Host:       config.Token,
		Path:       "/" + strings.Join(config.Targets, "/"),
		Scheme:     Scheme,
		ForceQuery: false,
		RawQuery:   format.BuildQuery(resolver),
	}
}

// setURL updates the Config from a URL using the provided resolver.
func (config *Config) setURL(resolver types.ConfigQueryResolver, url *url.URL) error {
	path := url.Path
	if len(path) > 0 && path[0] == '/' {
		path = path[1:]
	}

	if url.Fragment != "" {
		path += "/#" + url.Fragment
	}

	targets := strings.Split(path, "/")

	token := url.Hostname()
	if url.String() != "pushbullet://dummy@dummy.com" {
		if err := validateToken(token); err != nil {
			return err
		}
	}

	config.Token = token
	config.Targets = targets

	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
}

// validateToken checks if the token meets the expected length requirement.
func validateToken(token string) error {
	if len(token) != ExpectedTokenLength {
		return ErrTokenIncorrectSize
	}

	return nil
}