Skip to content

Commit

Permalink
refactor(services): move logging, add composites
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Jun 11, 2019
1 parent 818e0d7 commit b4b6535
Show file tree
Hide file tree
Showing 23 changed files with 198 additions and 143 deletions.
8 changes: 2 additions & 6 deletions pkg/cli/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ func send() action {
logger = services.DiscardLogger
}

opts := services.CreateServiceOpts(
logger,
verbose,
map[string]string {})

shoutrrr.Send(url, message, opts)
shoutrrr.SetLogger(logger)
shoutrrr.Send(url, message, nil)

return 1
},
Expand Down
21 changes: 15 additions & 6 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import (
"github.com/containrrr/shoutrrr/pkg/services/teams"
"github.com/containrrr/shoutrrr/pkg/services/telegram"
"github.com/containrrr/shoutrrr/pkg/types"
"log"
"net/url"
"strings"
)


// ServiceRouter is responsible for routing a message to a specific notification service using the notification URL
type ServiceRouter struct {}
type ServiceRouter struct {
logger *log.Logger
}

// SetLogger sets the logger that the services will use to write progress logs
func (router *ServiceRouter) SetLogger(logger *log.Logger) {
router.logger = logger
}

// ExtractServiceName from a notification URL
func (router *ServiceRouter) ExtractServiceName(rawURL string) (string, *url.URL, error) {
Expand All @@ -40,10 +47,10 @@ func (router *ServiceRouter) Route(rawURL string, message string, opts types.Ser
return err
}

return service.Send(url, message, opts)
return service.Send(url, message, nil)
}

var services = map[string]types.Service {
var serviceMap = map[string]types.Service {
"discord": &discord.Service{},
"pushover": &pushover.Service{},
"slack": &slack.Service{},
Expand All @@ -52,13 +59,15 @@ var services = map[string]types.Service {
"smtp": &smtp.Service{},
}

// Locate returns the service implementation that corresponds to the given scheme
// Locate returns the service implementation that corresponds to the given
func (router *ServiceRouter) Locate(serviceScheme string) (types.Service, error) {

service, valid := services[strings.ToLower(serviceScheme)]
service, valid := serviceMap[strings.ToLower(serviceScheme)]
if !valid {
return nil, fmt.Errorf("unknown service scheme '%s'", serviceScheme)
}

service.SetLogger(router.logger)

return service, nil
}
7 changes: 5 additions & 2 deletions pkg/services/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ package discord
import (
"bytes"
"fmt"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
"net/http"
"net/url"
)

// Service providing Discord as a notification service
type Service struct {}
type Service struct {
standard.Standard
}

const (
hookURL = "https://discordapp.com/api/webhooks"
maxlength = 2000
)

// Send a notification message to discord
func (plugin *Service) Send(rawURL *url.URL, message string, opts types.ServiceOpts) error {
func (plugin *Service) Send(rawURL *url.URL, message string, params *map[string]string) error {
config, err := plugin.CreateConfigFromURL(rawURL)
if err != nil {
return err
Expand Down
23 changes: 2 additions & 21 deletions pkg/services/discord/discord_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,17 @@ package discord

import (
"errors"
"github.com/containrrr/shoutrrr/pkg/types"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"net/url"
)

// Config is the configuration needed to send discord notifications
type Config struct {
standard.QuerylessConfig
Channel string
Token string
}

// QueryFields returns the fields that are part of the Query of the service URL
func (config Config) QueryFields() []string {
return []string{}
}

// Enums returns the fields that should use a corresponding EnumFormatter to Print/Parse their values
func (config Config) Enums() map[string]types.EnumFormatter {
return map[string]types.EnumFormatter{}
}

// Get returns the value of a Query field
func (config Config) Get(string) (string, error) {
return "", nil
}

// Set updates the value of a Query field
func (config Config) Set(string, string) error {
return nil
}

// GetURL returns a URL representation of it's current field values
func (config Config) GetURL() *url.URL {
return &url.URL{
Expand Down
4 changes: 1 addition & 3 deletions pkg/services/discord/discord_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package discord_test

import (
"github.com/containrrr/shoutrrr/pkg/services"
. "github.com/containrrr/shoutrrr/pkg/services/discord"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -33,11 +32,10 @@ var _ = Describe("the discord service", func() {
}

serviceURL, _ := url.Parse(envDiscordURL.String())
opts := services.GetDefaultOpts()
err := service.Send(
serviceURL,
"this is an integration test",
opts,
nil,
)
Expect(err).NotTo(HaveOccurred())
})
Expand Down
3 changes: 3 additions & 0 deletions pkg/services/pushbullet/pushbullet_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package pushbullet

import "github.com/containrrr/shoutrrr/pkg/services/standard"

// Config ...
type Config struct {
standard.QuerylessConfig
Targets []string
Token string
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/services/pushover/pushover.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pushover

import (
"fmt"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
"net/http"
netUrl "net/url"
Expand All @@ -15,18 +16,20 @@ const (
)

// Service providing the notification service Pushover
type Service struct{}
type Service struct{
standard.Standard
}

// Send a notification message to Pushover
func (plugin *Service) Send(url *netUrl.URL, message string, opts types.ServiceOpts) error {
func (service *Service) Send(url *netUrl.URL, message string, params *map[string]string) error {
config := Config{}
config.SetURL(url)
data := netUrl.Values{}
data.Set("device", config.Devices[0])
data.Set("user", config.User)
data.Set("token", config.Token)
data.Set("message", message)
opts.Logger().Println(data.Encode())
service.Logln(data.Encode())

res, err := http.Post(
hookURL,
Expand All @@ -39,6 +42,6 @@ func (plugin *Service) Send(url *netUrl.URL, message string, opts types.ServiceO
}

// GetConfig returns an empty ServiceConfig for this Service
func (plugin *Service) GetConfig() types.ServiceConfig {
func (service *Service) GetConfig() types.ServiceConfig {
return &Config{}
}
3 changes: 1 addition & 2 deletions pkg/services/pushover/pushover_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pushover_test

import (
"github.com/containrrr/shoutrrr/pkg/services"
"github.com/containrrr/shoutrrr/pkg/services/pushover"
"net/url"
"os"
Expand Down Expand Up @@ -31,7 +30,7 @@ var _ = Describe("the pushover service", func() {
return
}
serviceURL, _ := url.Parse(envPushoverURL.String())
err := service.Send(serviceURL, "this is an integration test", services.GetDefaultOpts())
err := service.Send(serviceURL, "this is an integration test", nil)
Expect(err).NotTo(HaveOccurred())
})
})
Expand Down
17 changes: 10 additions & 7 deletions pkg/services/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"bytes"
"errors"
"fmt"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
"net/http"
"net/url"
)

// Service sends notifications to a pre-configured channel or user
type Service struct {}
type Service struct {
standard.Standard
}

const (
apiURL = "https://hooks.slack.com/services"
Expand All @@ -19,7 +22,7 @@ const (


// Send a notification message to Slack
func (plugin *Service) Send(serviceURL *url.URL, message string, opts types.ServiceOpts) error {
func (service *Service) Send(serviceURL *url.URL, message string, params *map[string]string) error {
config, err := CreateConfigFromURL(serviceURL)
if err != nil {
return err
Expand All @@ -31,16 +34,16 @@ func (plugin *Service) Send(serviceURL *url.URL, message string, opts types.Serv
return errors.New("message exceeds max length")
}

return plugin.doSend(config, message)
return service.doSend(config, message)
}

// GetConfig returns an empty ServiceConfig for this Service
func (plugin *Service) GetConfig() types.ServiceConfig {
func (service *Service) GetConfig() types.ServiceConfig {
return &Config{}
}

func (plugin *Service) doSend(config *Config, message string) error {
url := plugin.getURL(config)
func (service *Service) doSend(config *Config, message string) error {
url := service.getURL(config)
json, _ := CreateJSONPayload(config, message)
res, err := http.Post(url, "application/json", bytes.NewReader(json))

Expand All @@ -50,7 +53,7 @@ func (plugin *Service) doSend(config *Config, message string) error {
return err
}

func (plugin *Service) getURL(config *Config) string {
func (service *Service) getURL(config *Config) string {
return fmt.Sprintf(
"%s/%s/%s/%s",
apiURL,
Expand Down
23 changes: 2 additions & 21 deletions pkg/services/slack/slack_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,18 @@ package slack

import (
"fmt"
"github.com/containrrr/shoutrrr/pkg/types"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"net/url"
"strings"
)

// Config for the slack service
type Config struct {
standard.QuerylessConfig
BotName string
Token Token
}

// QueryFields returns the fields that are part of the Query of the service URL
func (config *Config) QueryFields() []string {
return []string{}
}

// Enums returns the fields that should use a corresponding EnumFormatter to Print/Parse their values
func (config *Config) Enums() map[string]types.EnumFormatter {
return map[string]types.EnumFormatter{}
}

// Get returns the value of a Query field
func (config *Config) Get(string) (string, error) {
return "", nil
}

// Set updates the value of a Query field
func (config *Config) Set(string, string) error {
return nil
}

// GetURL returns a URL representation of it's current field values
func (config *Config) GetURL() *url.URL {
return &url.URL{
Expand Down
5 changes: 2 additions & 3 deletions pkg/services/slack/slack_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package slack_test

import (
"github.com/containrrr/shoutrrr/pkg/services"
. "github.com/containrrr/shoutrrr/pkg/services/slack"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -36,7 +35,7 @@ var _ = Describe("the slack service", func() {

serviceURL, _ := url.Parse(envSlackURL.String())

err := service.Send(serviceURL, "This is an integration test message", services.GetDefaultOpts())
err := service.Send(serviceURL, "This is an integration test message",nil)
Expect(err).NotTo(HaveOccurred())
})
})
Expand Down Expand Up @@ -115,7 +114,7 @@ var _ = Describe("the slack service", func() {
})

func expectErrorMessageGivenUrl(msg ErrorMessage, slackUrl *url.URL) {
err := service.Send(slackUrl, "Hello", services.GetDefaultOpts())
err := service.Send(slackUrl, "Hello", nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(string(msg)))
}
Loading

0 comments on commit b4b6535

Please sign in to comment.