| 1 | package teams |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net/url" |
| 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 represents the configuration for the Teams service. |
| 14 | type Config struct { |
| 15 | standard.EnumlessConfig |
| 16 | |
| 17 | Host string `desc:"The full Power Automate workflow incoming webhook URL" key:"host"` |
| 18 | Title string `desc:"Title displayed as a bold header in the Adaptive Card" key:"title" optional:""` |
| 19 | Color string `desc:"Title text color (accent, good, warning, attention, dark, light, or default)" key:"color" optional:""` |
| 20 | } |
| 21 | |
| 22 | // Scheme is the identifier for the Teams service protocol. |
| 23 | const Scheme = "teams" |
| 24 | |
| 25 | // GetURL constructs a URL from the Config fields. |
| 26 | func (c *Config) GetURL() *url.URL { |
| 27 | resolver := format.NewPropKeyResolver(c) |
| 28 | |
| 29 | return c.getURL(&resolver) |
| 30 | } |
| 31 | |
| 32 | // SetURL updates the Config from a URL. |
| 33 | func (c *Config) SetURL(serviceURL *url.URL) error { |
| 34 | resolver := format.NewPropKeyResolver(c) |
| 35 | |
| 36 | return c.setURL(&resolver, serviceURL) |
| 37 | } |
| 38 | |
| 39 | func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 40 | return &url.URL{ |
| 41 | Scheme: Scheme, |
| 42 | Host: "", |
| 43 | ForceQuery: true, |
| 44 | RawQuery: format.BuildQuery(resolver), |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 49 | c.Color = "" |
| 50 | c.Title = "" |
| 51 | c.Host = "" |
| 52 | |
| 53 | for key, vals := range serviceURL.Query() { |
| 54 | if len(vals) > 0 && vals[0] != "" { |
| 55 | if err := resolver.Set(key, vals[0]); err != nil { |
| 56 | if errors.Is(err, format.ErrInvalidConfigKey) { |
| 57 | continue |
| 58 | } |
| 59 | |
| 60 | return fmt.Errorf("setting config value for key %q: %w", key, err) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return nil |
| 66 | } |