Debian Codegraph @

pkg/services/push/pushover/pushover_config.go

raw
1 package pushover
2
3 import (
4 "fmt"
5 "net/url"
6
7 "github.com/nicholas-fedor/shoutrrr/pkg/format"
8 "github.com/nicholas-fedor/shoutrrr/pkg/types"
9 )
10
11 // Config for the Pushover notification service.
12 type Config struct {
13 Token string `desc:"API Token/Key" url:"pass"`
14 User string `desc:"User Key" url:"host"`
15 Devices []string ` key:"devices" optional:""`
16 Priority int8 ` key:"priority" default:"0"`
17 Title string ` key:"title" optional:""`
18 }
19
20 // Scheme is the identifying part of this service's configuration URL.
21 const Scheme = "pushover"
22
23 // Enums returns the fields that should use a corresponding EnumFormatter to Print/Parse their values.
24 func (c *Config) Enums() map[string]types.EnumFormatter {
25 return map[string]types.EnumFormatter{}
26 }
27
28 // GetURL returns a URL representation of its current field values.
29 func (c *Config) GetURL() *url.URL {
30 resolver := format.NewPropKeyResolver(c)
31
32 return c.getURL(&resolver)
33 }
34
35 // SetURL updates the Config from a URL representation of its field values.
36 func (c *Config) SetURL(serviceURL *url.URL) error {
37 resolver := format.NewPropKeyResolver(c)
38
39 return c.setURL(&resolver, serviceURL)
40 }
41
42 // getURL constructs a URL from the Config's fields using the provided resolver.
43 func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
44 return &url.URL{
45 User: url.UserPassword("Token", c.Token),
46 Host: c.User,
47 Scheme: Scheme,
48 ForceQuery: true,
49 RawQuery: format.BuildQuery(resolver),
50 }
51 }
52
53 // setURL updates the Config from a URL using the provided resolver.
54 func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
55 password, _ := serviceURL.User.Password()
56 c.User = serviceURL.Host
57 c.Token = password
58
59 for key, vals := range serviceURL.Query() {
60 if err := resolver.Set(key, vals[0]); err != nil {
61 return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err)
62 }
63 }
64
65 if serviceURL.String() != "pushover://[email protected]" {
66 if len(c.User) < 1 {
67 return ErrUserMissing
68 }
69
70 if len(c.Token) < 1 {
71 return ErrTokenMissing
72 }
73 }
74
75 return nil
76 }