From 73ba086923f0127efd43e2d52a683f3d8a8747fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20ma=CC=8Ase=CC=81n?= Date: Mon, 24 Jun 2019 21:41:34 +0200 Subject: [PATCH] cleanup: reorder and remove unused code --- pkg/cli/generate.go | 12 +---- pkg/cli/main.go | 1 - pkg/cli/send.go | 15 ++---- pkg/cli/verify.go | 26 ++-------- pkg/format/formatter.go | 5 +- pkg/router/router.go | 7 +-- pkg/services/options.go | 48 ------------------- .../standard => util/queue}/queued_sender.go | 14 ++++-- 8 files changed, 25 insertions(+), 103 deletions(-) delete mode 100644 pkg/services/options.go rename pkg/{services/standard => util/queue}/queued_sender.go (73%) diff --git a/pkg/cli/generate.go b/pkg/cli/generate.go index 2a38149f..71a8b942 100644 --- a/pkg/cli/generate.go +++ b/pkg/cli/generate.go @@ -20,16 +20,6 @@ func generate() action { fmt.Printf("Service: %s\n", serviceSchema) - //logger := DiscardLogger - //if verbose { - // logger = log.New(os.Stderr, "SHOUTRRR ", log.LstdFlags) - //} - // - //opts := PluginOpts { - // Verbose: verbose, - // Logger: logger, - //} - serviceRouter := router.ServiceRouter{} service, err := serviceRouter.Locate(serviceSchema) @@ -46,7 +36,7 @@ func generate() action { return 1 }, FlagSet: *flag.NewFlagSet("generate", flag.ExitOnError), - Usage: "%s send [OPTIONS] \n", + Usage: "%s generate [OPTIONS] \n", } // action.FlagSet.BoolVar(&verbose, "verbose", false, "display additional output") diff --git a/pkg/cli/main.go b/pkg/cli/main.go index 66290e24..3dfad072 100644 --- a/pkg/cli/main.go +++ b/pkg/cli/main.go @@ -20,7 +20,6 @@ func usage(syntax string) { func main() { - if len(os.Args) < 2 { showMainUsage() return diff --git a/pkg/cli/send.go b/pkg/cli/send.go index 0844979c..e36e25fc 100644 --- a/pkg/cli/send.go +++ b/pkg/cli/send.go @@ -3,18 +3,17 @@ package main import ( "flag" "fmt" - "github.com/containrrr/shoutrrr" - "github.com/containrrr/shoutrrr/pkg/services" "log" "os" "strings" + + "github.com/containrrr/shoutrrr" + "github.com/containrrr/shoutrrr/pkg/util" ) var verbose bool func send() action { - - action := action{ run: func(flags *flag.FlagSet) int { @@ -22,24 +21,18 @@ func send() action { return ExitCodeUsage } - fmt.Printf("Args: %d\n", flags.NArg()) - - // Arg #0 is always the action verb url := flags.Arg(0) - fmt.Printf("Url: %s\n", url) message := strings.Join(flags.Args()[1:], " ") - fmt.Printf("Message: %s\n", message) - var logger *log.Logger if verbose { logger = log.New(os.Stderr, "SHOUTRRR ", log.LstdFlags) } else { - logger = services.DiscardLogger + logger = util.DiscardLogger } shoutrrr.SetLogger(logger) diff --git a/pkg/cli/verify.go b/pkg/cli/verify.go index a6882e01..45e8cd82 100644 --- a/pkg/cli/verify.go +++ b/pkg/cli/verify.go @@ -8,30 +8,14 @@ import ( func verify() action { - - action := action{ + return action{ run: func(flags *flag.FlagSet) int { - if flags.NArg() < 1 { + url := flags.Arg(0) + if url == "" { return ExitCodeUsage } - //fmt.Printf("Args: %d\n", flags.NArg()) - - // Arg #0 is always the action verb - url := flags.Arg(0) - - //fmt.Printf("Url: %s\n", url) - - //logger := DiscardLogger - //if verbose { - // logger = log.New(os.Stderr, "SHOUTRRR ", log.LstdFlags) - //} - // - //opts := PluginOpts { - // Verbose: verbose, - // Logger: logger, - //} if err := shoutrrr.Verify(url); err != nil { fmt.Printf("error verifying URL: %s", err) @@ -44,8 +28,4 @@ func verify() action { FlagSet: *flag.NewFlagSet("verify", flag.ExitOnError), Usage: "%s send [OPTIONS] \n", } - - // action.FlagSet.BoolVar(&verbose, "verbose", false, "display additional output") - - return action } \ No newline at end of file diff --git a/pkg/format/formatter.go b/pkg/format/formatter.go index c8df2b65..d9c5413a 100644 --- a/pkg/format/formatter.go +++ b/pkg/format/formatter.go @@ -2,11 +2,12 @@ package format import ( "fmt" - "github.com/containrrr/shoutrrr/pkg/types" - "github.com/containrrr/shoutrrr/pkg/util" "reflect" "strings" "unsafe" + + "github.com/containrrr/shoutrrr/pkg/types" + "github.com/containrrr/shoutrrr/pkg/util" ) // GetConfigMap returns a string map of a given Config struct diff --git a/pkg/router/router.go b/pkg/router/router.go index ab1d223a..c142b828 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -2,6 +2,10 @@ package router import ( "fmt" + "log" + "net/url" + "strings" + "github.com/containrrr/shoutrrr/pkg/services/discord" "github.com/containrrr/shoutrrr/pkg/services/ifttt" "github.com/containrrr/shoutrrr/pkg/services/pushover" @@ -10,9 +14,6 @@ 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 diff --git a/pkg/services/options.go b/pkg/services/options.go deleted file mode 100644 index 460c4ce3..00000000 --- a/pkg/services/options.go +++ /dev/null @@ -1,48 +0,0 @@ -package services - -import ( - "io/ioutil" - "log" -) - -// ServiceOpts contains various properties that can be set by the consumer to alter the behaviour of services -type ServiceOpts struct { - verbose bool - logger *log.Logger - props map[string]string -} - -// Verbose marks whether the consumer want a service to output to the logger -func (svc *ServiceOpts) Verbose() bool { - return svc.verbose -} - -// Logger is the logging interface to be used by a service -func (svc *ServiceOpts) Logger() *log.Logger { - return svc.logger -} - -// Props is a collection of strings that should be used for substitution in fields tagged with `template:"yes"` -func (svc *ServiceOpts) Props() map[string]string { - return svc.props -} - -// GetDefaultOpts creates a default ServiceOpts struct that discards any output written to it's logger -func GetDefaultOpts() *ServiceOpts { - return &ServiceOpts{ - verbose: false, - logger: DiscardLogger, - } -} - -// CreateServiceOpts creates a ServiceOpts struct -func CreateServiceOpts(logger *log.Logger, verbose bool, props map[string]string) *ServiceOpts { - return &ServiceOpts{ - verbose, - logger, - props, - } -} - -// DiscardLogger is a logger that discards any output written to it -var DiscardLogger = log.New(ioutil.Discard, "", 0) \ No newline at end of file diff --git a/pkg/services/standard/queued_sender.go b/pkg/util/queue/queued_sender.go similarity index 73% rename from pkg/services/standard/queued_sender.go rename to pkg/util/queue/queued_sender.go index 06b7b8a9..0652b01c 100644 --- a/pkg/services/standard/queued_sender.go +++ b/pkg/util/queue/queued_sender.go @@ -10,6 +10,7 @@ import ( // QueuedSender implements the standard queue sender interface type QueuedSender struct { queue []string + sender *types.Sender } // Enqueuef adds a formatted message to an internal queue and sends it when SendQueued is invoked @@ -22,11 +23,16 @@ func (qs *QueuedSender) Enqueue(message string) { qs.queue = append(qs.queue, message) } +func Connect(qs *QueuedSender, sender *types.Sender) { + qs.sender = sender +} + // Flush sends all messages that have been queued up as a combined message. This method should be deferred! func (qs *QueuedSender) Flush(params *map[string]string) { var anonService interface{} = qs - service := anonService.(types.Service) - - // Since this method is supposed to be deferred we just have to ignore errors - _ = service.Send(strings.Join(qs.queue, "\n"), params) + service, ok := anonService.(types.Service) + if ok { + // Since this method is supposed to be deferred we just have to ignore errors + _ = service.Send(strings.Join(qs.queue, "\n"), params) + } } \ No newline at end of file