| 1 | package signalgrid |
| 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 holds settings for the Signalgrid notification service. |
| 12 | type Config struct { |
| 13 | // ClientKey is the Signalgrid client key used to authenticate Push API requests. |
| 14 | ClientKey string `desc:"Signalgrid client key" url:"user"` |
| 15 | // Channel is the destination channel token. |
| 16 | Channel string `desc:"Signalgrid channel token" url:"host"` |
| 17 | // Title is an optional notification title. It is omitted from the API request when empty. |
| 18 | Title string `desc:"Notification title" key:"title" optional:""` |
| 19 | // Type is the visual severity of the notification. Default: INFO. |
| 20 | Type notificationType `default:"INFO" desc:"Notification severity (CRIT, WARN, INFO, SUCCESS)" key:"type"` |
| 21 | // Critical delivers the notification as a critical alert when true. |
| 22 | Critical bool `default:"No" desc:"Deliver as a critical alert" key:"critical"` |
| 23 | } |
| 24 | |
| 25 | const ( |
| 26 | // Scheme identifies this service in configuration URLs. |
| 27 | Scheme = "signalgrid" |
| 28 | |
| 29 | // dummyServiceURL is the placeholder URL used by documentation generation. |
| 30 | dummyServiceURL = "signalgrid://[email protected]" |
| 31 | ) |
| 32 | |
| 33 | // Enums returns the fields that use an EnumFormatter for their values. |
| 34 | // |
| 35 | // Returns: |
| 36 | // - A map of config field names to their enum formatters. |
| 37 | func (*Config) Enums() map[string]types.EnumFormatter { |
| 38 | return map[string]types.EnumFormatter{ |
| 39 | "Type": NotificationType.Enum, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // GetURL returns a URL representation of the current configuration. |
| 44 | // |
| 45 | // Returns: |
| 46 | // - The service URL encoding the current field values. |
| 47 | func (c *Config) GetURL() *url.URL { |
| 48 | resolver := format.NewPropKeyResolver(c) |
| 49 | |
| 50 | return c.getURL(&resolver) |
| 51 | } |
| 52 | |
| 53 | // SetURL updates the configuration from a URL representation. |
| 54 | // |
| 55 | // Parameters: |
| 56 | // - serviceURL: The service URL to parse. |
| 57 | // |
| 58 | // Returns: |
| 59 | // - An error if the URL is invalid or required fields are missing. |
| 60 | func (c *Config) SetURL(serviceURL *url.URL) error { |
| 61 | resolver := format.NewPropKeyResolver(c) |
| 62 | |
| 63 | return c.setURL(&resolver, serviceURL) |
| 64 | } |
| 65 | |
| 66 | // getURL constructs a service URL from the current configuration. |
| 67 | // |
| 68 | // Parameters: |
| 69 | // - resolver: Resolver used to encode query parameters from config fields. |
| 70 | // |
| 71 | // Returns: |
| 72 | // - The service URL with the client key as userinfo and the channel as host. |
| 73 | func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 74 | return &url.URL{ |
| 75 | Scheme: Scheme, |
| 76 | User: url.User(c.ClientKey), |
| 77 | Host: c.Channel, |
| 78 | RawQuery: format.BuildQuery(resolver), |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // setURL updates the configuration from a service URL. |
| 83 | // |
| 84 | // Parameters: |
| 85 | // - resolver: Resolver used to apply query parameters to config fields. |
| 86 | // - serviceURL: The service URL to parse. |
| 87 | // |
| 88 | // Returns: |
| 89 | // - An error if a query parameter is invalid or required fields are missing. |
| 90 | func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 91 | isDummy := serviceURL.String() == dummyServiceURL |
| 92 | |
| 93 | if serviceURL.User != nil { |
| 94 | c.ClientKey = serviceURL.User.Username() |
| 95 | } else { |
| 96 | c.ClientKey = "" |
| 97 | } |
| 98 | |
| 99 | c.Channel = serviceURL.Hostname() |
| 100 | |
| 101 | for key, vals := range serviceURL.Query() { |
| 102 | if len(vals) == 0 { |
| 103 | continue |
| 104 | } |
| 105 | |
| 106 | if err := resolver.Set(key, vals[0]); err != nil { |
| 107 | return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if isDummy { |
| 112 | return nil |
| 113 | } |
| 114 | |
| 115 | if c.ClientKey == "" { |
| 116 | return ErrClientKeyMissing |
| 117 | } |
| 118 | |
| 119 | if c.Channel == "" { |
| 120 | return ErrChannelMissing |
| 121 | } |
| 122 | |
| 123 | return nil |
| 124 | } |