refactor: restructure project for better integration of new services

pull/26/head
kha7iq 4 years ago
parent 158452591e
commit f1bdc11693

@ -4,7 +4,15 @@ import (
"log" "log"
"os" "os"
"github.com/kha7iq/pingme/cmd" "github.com/kha7iq/pingme/service/discord"
"github.com/kha7iq/pingme/service/email"
"github.com/kha7iq/pingme/service/mattermost"
"github.com/kha7iq/pingme/service/msteams"
"github.com/kha7iq/pingme/service/pushbullet"
"github.com/kha7iq/pingme/service/pushover"
"github.com/kha7iq/pingme/service/rocketchat"
"github.com/kha7iq/pingme/service/slack"
"github.com/kha7iq/pingme/service/telegram"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -12,7 +20,7 @@ import (
// Version variable is used for semVer // Version variable is used for semVer
var Version string var Version string
// main with combile all the function into commands // main with combine all the function into commands
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "PingMe" app.Name = "PingMe"
@ -24,15 +32,15 @@ variables and command line switches.Currently supported platforms include Slack,
RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams and email address.` RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams and email address.`
// app.Commands contains the subcommands as functions which return []*cli.Command. // app.Commands contains the subcommands as functions which return []*cli.Command.
app.Commands = []*cli.Command{ app.Commands = []*cli.Command{
cmd.SendToTelegram(), telegram.Send(),
cmd.SendToRocketChat(), rocketchat.Send(),
cmd.SendToSlack(), slack.Send(),
cmd.SendToDiscord(), discord.Send(),
cmd.SendToTeams(), msteams.Send(),
cmd.SendToPushOver(), pushover.Send(),
cmd.SendToEmail(), email.Send(),
cmd.SendToMattermost(), mattermost.Send(),
cmd.SendToPushBullet(), pushbullet.Send(),
} }
err := app.Run(os.Args) err := app.Run(os.Args)

@ -1,4 +1,4 @@
package cmd package discord
import ( import (
"context" "context"
@ -6,6 +6,8 @@ import (
"log" "log"
"strings" "strings"
"github.com/kha7iq/pingme/service/helpers"
"github.com/nikoksr/notify" "github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/discord" "github.com/nikoksr/notify/service/discord"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -19,11 +21,11 @@ type discordPingMe struct {
Title string Title string
} }
// SendToDiscord parse values from *cli.context and return *cli.Command. // Send parse values from *cli.context and return *cli.Command.
// Values include discord bot token, userID, channelIDs, Message and Title. // Values include discord bot token, userID, channelIDs, Message and Title.
// If multiple channels are provided then the string is split with "," separator and // If multiple channels are provided then the string is split with "," separator and
// each channelID is added to receiver. // each channelID is added to receiver.
func SendToDiscord() *cli.Command { func Send() *cli.Command {
var discordOpts discordPingMe var discordOpts discordPingMe
return &cli.Command{ return &cli.Command{
Name: "discord", Name: "discord",
@ -56,7 +58,7 @@ All configuration options are also available via environment variables.`,
&cli.StringFlag{ &cli.StringFlag{
Destination: &discordOpts.Title, Destination: &discordOpts.Title,
Name: "title", Name: "title",
Value: TimeValue, Value: helpers.TimeValue,
Usage: "Title of the message.", Usage: "Title of the message.",
EnvVars: []string{"DISCORD_MSG_TITLE"}, EnvVars: []string{"DISCORD_MSG_TITLE"},
}, },
@ -72,7 +74,7 @@ All configuration options are also available via environment variables.`,
chn := strings.Split(discordOpts.Channel, ",") chn := strings.Split(discordOpts.Channel, ",")
for _, v := range chn { for _, v := range chn {
if len(v) <= 0 { if len(v) <= 0 {
return fmt.Errorf(EmptyChannel) return helpers.ErrChannel
} }
discordSvc.AddReceivers(v) discordSvc.AddReceivers(v)

@ -1,11 +1,12 @@
package cmd package email
import ( import (
"context" "context"
"fmt"
"log" "log"
"strings" "strings"
"github.com/kha7iq/pingme/service/helpers"
"github.com/nikoksr/notify" "github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/mail" "github.com/nikoksr/notify/service/mail"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -23,12 +24,12 @@ type email struct {
Identity string Identity string
} }
// SendToEmail parse values from *cli.context and return *cli.Command. // Send parses values from *cli.context and return *cli.Command.
// SendAddress is used for authentication with smtp server, host and port is required // SendAddress is used for authentication with smtp server, host and port is required
// the default value for port is set to "587" and host as "smtp.gmail.com" // the default value for port is set to "587" and host as "smtp.gmail.com"
// If multiple ReceiverAddress are provided then the string value is split with "," separator and // If multiple ReceiverAddress are provided then the string value is split with "," separator and
// each ReceiverAddress is added to receiver. // each ReceiverAddress is added to receiver.
func SendToEmail() *cli.Command { func Send() *cli.Command {
var emailOpts email var emailOpts email
return &cli.Command{ return &cli.Command{
Name: "email", Name: "email",
@ -98,7 +99,7 @@ All configuration options are also available via environment variables.`,
&cli.StringFlag{ &cli.StringFlag{
Destination: &emailOpts.Subject, Destination: &emailOpts.Subject,
Name: "sub", Name: "sub",
Value: TimeValue, Value: helpers.TimeValue,
Usage: "Subject of the email", Usage: "Subject of the email",
EnvVars: []string{"EMAIL_SUBJECT"}, EnvVars: []string{"EMAIL_SUBJECT"},
}, },
@ -111,7 +112,7 @@ All configuration options are also available via environment variables.`,
chn := strings.Split(emailOpts.ReceiverAddress, ",") chn := strings.Split(emailOpts.ReceiverAddress, ",")
for _, v := range chn { for _, v := range chn {
if len(v) <= 0 { if len(v) <= 0 {
return fmt.Errorf(EmptyChannel) return helpers.ErrChannel
} }
emailSvc.AddReceivers(v) emailSvc.AddReceivers(v)

@ -0,0 +1,13 @@
package helpers
import (
"errors"
"time"
)
var (
// ErrChannel variable holds default error message if no channel is provided.
ErrChannel = errors.New("target channel or id can not be empty")
// TimeValue holds current date and time in unix format.
TimeValue = "⏰ " + time.Now().Format(time.UnixDate)
)

@ -1,4 +1,4 @@
package cmd package mattermost
import ( import (
"bytes" "bytes"
@ -9,6 +9,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/kha7iq/pingme/service/helpers"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -47,11 +49,11 @@ type matterMostResponse struct {
Metadata struct{} `json:"metadata"` Metadata struct{} `json:"metadata"`
} }
// SendToMattermost parse values from *cli.context and return *cli.Command // Send parse values from *cli.context and return *cli.Command
// and send messages to target channels. // and send messages to target channels.
// If multiple channel ids are provided then the string is split with "," separator and // If multiple channel ids are provided then the string is split with "," separator and
// message is sent to each channel. // message is sent to each channel.
func SendToMattermost() *cli.Command { func Send() *cli.Command {
var mattermostOpts matterMost var mattermostOpts matterMost
return &cli.Command{ return &cli.Command{
Name: "mattermost", Name: "mattermost",
@ -87,7 +89,7 @@ You can specify multiple channels by separating the value with ','.`,
&cli.StringFlag{ &cli.StringFlag{
Destination: &mattermostOpts.Title, Destination: &mattermostOpts.Title,
Name: "title", Name: "title",
Value: TimeValue, Value: helpers.TimeValue,
Usage: "Title of the message.", Usage: "Title of the message.",
EnvVars: []string{"MATTERMOST_TITLE"}, EnvVars: []string{"MATTERMOST_TITLE"},
}, },
@ -126,7 +128,7 @@ You can specify multiple channels by separating the value with ','.`,
ids := strings.Split(mattermostOpts.ChanIDs, ",") ids := strings.Split(mattermostOpts.ChanIDs, ",")
for _, v := range ids { for _, v := range ids {
if len(v) == 0 { if len(v) == 0 {
return fmt.Errorf(EmptyChannel) return helpers.ErrChannel
} }
jsonData, err := toJSON(v, fullMessage) jsonData, err := toJSON(v, fullMessage)

@ -1,11 +1,12 @@
package cmd package msteams
import ( import (
"context" "context"
"fmt"
"log" "log"
"strings" "strings"
"github.com/kha7iq/pingme/service/helpers"
"github.com/nikoksr/notify" "github.com/nikoksr/notify"
msteams2 "github.com/nikoksr/notify/service/msteams" msteams2 "github.com/nikoksr/notify/service/msteams"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -18,11 +19,11 @@ type msTeams struct {
Title string Title string
} }
// SendToTeams parse values from *cli.context and return *cli.Command. // Send parse values from *cli.context and return *cli.Command.
// Values include Ms Teams Webhook, Message and Title. // Values include Ms Teams Webhook, Message and Title.
// If multiple webhooks are provided then the string is split with "," separator and // If multiple webhooks are provided then the string is split with "," separator and
// each webhook is added to receiver. // each webhook is added to receiver.
func SendToTeams() *cli.Command { func Send() *cli.Command {
var msTeamOpt msTeams var msTeamOpt msTeams
return &cli.Command{ return &cli.Command{
Name: "teams", Name: "teams",
@ -48,7 +49,7 @@ you can add permissions for multiple channels to single webhook.`,
&cli.StringFlag{ &cli.StringFlag{
Destination: &msTeamOpt.Title, Destination: &msTeamOpt.Title,
Name: "title", Name: "title",
Value: TimeValue, Value: helpers.TimeValue,
Usage: "Title of the message.", Usage: "Title of the message.",
EnvVars: []string{"TEAMS_MSG_TITLE"}, EnvVars: []string{"TEAMS_MSG_TITLE"},
}, },
@ -60,7 +61,7 @@ you can add permissions for multiple channels to single webhook.`,
chn := strings.Split(msTeamOpt.Webhook, ",") chn := strings.Split(msTeamOpt.Webhook, ",")
for _, v := range chn { for _, v := range chn {
if len(v) <= 0 { if len(v) <= 0 {
return fmt.Errorf(EmptyChannel) return helpers.ErrChannel
} }
teamsSvc.AddReceivers(v) teamsSvc.AddReceivers(v)
} }

@ -1,11 +1,12 @@
package cmd package pushbullet
import ( import (
"context" "context"
"fmt"
"log" "log"
"strings" "strings"
"github.com/kha7iq/pingme/service/helpers"
"github.com/nikoksr/notify" "github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/pushbullet" "github.com/nikoksr/notify/service/pushbullet"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -21,11 +22,11 @@ type pushBullet struct {
SMS bool SMS bool
} }
// SendToPushBullet parse values from *cli.context and return *cli.Command. // Send parse values from *cli.context and return *cli.Command.
// Values include pushbullet token, Device, phone number, Message and Title. // Values include pushbullet token, Device, phone number, Message and Title.
// If multiple devices are provided they the string is split with "," separator and // If multiple devices are provided they the string is split with "," separator and
// each device is added to receiver. // each device is added to receiver.
func SendToPushBullet() *cli.Command { func Send() *cli.Command {
var pushBulletOpts pushBullet var pushBulletOpts pushBullet
return &cli.Command{ return &cli.Command{
Name: "pushbullet", Name: "pushbullet",
@ -68,7 +69,7 @@ Multiple device nicknames or numbers can be used separated by comma.`,
&cli.StringFlag{ &cli.StringFlag{
Destination: &pushBulletOpts.Title, Destination: &pushBulletOpts.Title,
Name: "title", Name: "title",
Value: TimeValue, Value: helpers.TimeValue,
Usage: "Title of the message.", Usage: "Title of the message.",
EnvVars: []string{"PUSHBULLET_TITLE"}, EnvVars: []string{"PUSHBULLET_TITLE"},
}, },
@ -92,7 +93,7 @@ Multiple device nicknames or numbers can be used separated by comma.`,
devices := strings.Split(pushBulletOpts.PhoneNumber, ",") devices := strings.Split(pushBulletOpts.PhoneNumber, ",")
for _, v := range devices { for _, v := range devices {
if len(v) <= 0 { if len(v) <= 0 {
return fmt.Errorf(EmptyChannel) return helpers.ErrChannel
} }
pushBulletSmsSvc.AddReceivers(v) pushBulletSmsSvc.AddReceivers(v)
@ -112,7 +113,7 @@ Multiple device nicknames or numbers can be used separated by comma.`,
devices := strings.Split(pushBulletOpts.Device, ",") devices := strings.Split(pushBulletOpts.Device, ",")
for _, v := range devices { for _, v := range devices {
if len(v) <= 0 { if len(v) <= 0 {
return fmt.Errorf(EmptyChannel) return helpers.ErrChannel
} }
pushBulletSvc.AddReceivers(v) pushBulletSvc.AddReceivers(v)
} }

@ -1,10 +1,11 @@
package cmd package pushover
import ( import (
"fmt"
"log" "log"
"strings" "strings"
"github.com/kha7iq/pingme/service/helpers"
"github.com/gregdel/pushover" "github.com/gregdel/pushover"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -17,11 +18,11 @@ type pushOver struct {
Title string Title string
} }
// SendToPushOver parse values from *cli.context and return *cli.Command. // Send parse values from *cli.context and return *cli.Command.
// Values include token, users, Message and Title. // Values include token, users, Message and Title.
// If multiple users are provided then the string is split with "," separator and // If multiple users are provided then the string is split with "," separator and
// each user is added to receiver. // each user is added to receiver.
func SendToPushOver() *cli.Command { func Send() *cli.Command {
var pushOverOpts pushOver var pushOverOpts pushOver
return &cli.Command{ return &cli.Command{
Name: "pushover", Name: "pushover",
@ -56,7 +57,7 @@ All configuration options are also available via environment variables.`,
&cli.StringFlag{ &cli.StringFlag{
Destination: &pushOverOpts.Title, Destination: &pushOverOpts.Title,
Name: "title", Name: "title",
Value: TimeValue, Value: helpers.TimeValue,
Usage: "Title of the message.", Usage: "Title of the message.",
EnvVars: []string{"PUSHOVER_TITLE"}, EnvVars: []string{"PUSHOVER_TITLE"},
}, },
@ -68,7 +69,7 @@ All configuration options are also available via environment variables.`,
for _, v := range users { for _, v := range users {
if len(v) == 0 { if len(v) == 0 {
return fmt.Errorf(EmptyChannel) return helpers.ErrChannel
} }
recipient := pushover.NewRecipient(v) recipient := pushover.NewRecipient(v)
responsePushOver, err := app.SendMessage(message, recipient) responsePushOver, err := app.SendMessage(message, recipient)

@ -1,11 +1,10 @@
package cmd package rocketchat
import ( import (
"context" "context"
"fmt"
"strings" "strings"
"time"
"github.com/kha7iq/pingme/service/helpers"
"github.com/nikoksr/notify" "github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/rocketchat" "github.com/nikoksr/notify/service/rocketchat"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -21,17 +20,11 @@ type rocketChat struct {
Scheme string Scheme string
} }
var ( // Send parse values from *cli.context and return *cli.Command.
// EmptyChannel variable holds default error message if no channel is provided.
EmptyChannel = "channel name or id can not be empty"
TimeValue = "⏰ " + time.Now().Format(time.UnixDate)
)
// SendToRocketChat parse values from *cli.context and return *cli.Command.
// Values include rocketchat token, , UserId, channelIDs, ServerURL, Scheme, Message and Title. // Values include rocketchat token, , UserId, channelIDs, ServerURL, Scheme, Message and Title.
// If multiple channels are provided then the string is split with "," separator and // If multiple channels are provided then the string is split with "," separator and
// each channelID is added to receiver. // each channelID is added to receiver.
func SendToRocketChat() *cli.Command { func Send() *cli.Command {
var rocketChatOpts rocketChat var rocketChatOpts rocketChat
return &cli.Command{ return &cli.Command{
Name: "rocketchat", Name: "rocketchat",
@ -90,7 +83,7 @@ All configuration options are also available via environment variables.`,
&cli.StringFlag{ &cli.StringFlag{
Destination: &rocketChatOpts.Title, Destination: &rocketChatOpts.Title,
Name: "title", Name: "title",
Value: TimeValue, Value: helpers.TimeValue,
Usage: "Title of the message", Usage: "Title of the message",
EnvVars: []string{"ROCKETCHAT_TITLE"}, EnvVars: []string{"ROCKETCHAT_TITLE"},
}, },
@ -106,7 +99,7 @@ All configuration options are also available via environment variables.`,
chn := strings.Split(rocketChatOpts.Channel, ",") chn := strings.Split(rocketChatOpts.Channel, ",")
for _, v := range chn { for _, v := range chn {
if len(v) <= 0 { if len(v) <= 0 {
return fmt.Errorf(EmptyChannel) return helpers.ErrChannel
} }
rocketChatSvc.AddReceivers(v) rocketChatSvc.AddReceivers(v)

@ -1,11 +1,12 @@
package cmd package slack
import ( import (
"context" "context"
"fmt"
"log" "log"
"strings" "strings"
"github.com/kha7iq/pingme/service/helpers"
"github.com/nikoksr/notify" "github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/slack" "github.com/nikoksr/notify/service/slack"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -19,11 +20,11 @@ type slackPingMe struct {
Title string Title string
} }
// SendToSlack parse values from *cli.context and return *cli.Command. // Send parse values from *cli.context and return *cli.Command.
// Values include slack token, channelIDs, Message and Title. // Values include slack token, channelIDs, Message and Title.
// If multiple channels are provided then the string is split with "," separator and // If multiple channels are provided then the string is split with "," separator and
// each channelID is added to receiver. // each channelID is added to receiver.
func SendToSlack() *cli.Command { func Send() *cli.Command {
var slackOpts slackPingMe var slackOpts slackPingMe
return &cli.Command{ return &cli.Command{
Name: "slack", Name: "slack",
@ -59,7 +60,7 @@ All configuration options are also available via environment variables.`,
&cli.StringFlag{ &cli.StringFlag{
Destination: &slackOpts.Title, Destination: &slackOpts.Title,
Name: "title", Name: "title",
Value: TimeValue, Value: helpers.TimeValue,
Usage: "Title of the message.", Usage: "Title of the message.",
EnvVars: []string{"SLACK_MSG_TITLE"}, EnvVars: []string{"SLACK_MSG_TITLE"},
}, },
@ -70,7 +71,7 @@ All configuration options are also available via environment variables.`,
chn := strings.Split(slackOpts.Channel, ",") chn := strings.Split(slackOpts.Channel, ",")
for _, v := range chn { for _, v := range chn {
if len(v) <= 0 { if len(v) <= 0 {
return fmt.Errorf(EmptyChannel) return helpers.ErrChannel
} }
slackSvc.AddReceivers(v) slackSvc.AddReceivers(v)

@ -1,12 +1,13 @@
package cmd package telegram
import ( import (
"context" "context"
"fmt"
"log" "log"
"strconv" "strconv"
"strings" "strings"
"github.com/kha7iq/pingme/service/helpers"
"github.com/nikoksr/notify" "github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/telegram" "github.com/nikoksr/notify/service/telegram"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -20,11 +21,11 @@ type teleGram struct {
Title string Title string
} }
// SendToTelegram parse values from *cli.context and return *cli.Command. // Send parse values from *cli.context and return *cli.Command.
// Values include telegram token, channelIDs, Message and Title. // Values include telegram token, channelIDs, Message and Title.
// If multiple channels are provided they the string is split with "," separator and // If multiple channels are provided they the string is split with "," separator and
// each channelID is added to receiver. // each channelID is added to receiver.
func SendToTelegram() *cli.Command { func Send() *cli.Command {
var telegramOpts teleGram var telegramOpts teleGram
return &cli.Command{ return &cli.Command{
Name: "telegram", Name: "telegram",
@ -60,7 +61,7 @@ All configuration options are also available via environment variables.`,
&cli.StringFlag{ &cli.StringFlag{
Destination: &telegramOpts.Title, Destination: &telegramOpts.Title,
Name: "title", Name: "title",
Value: TimeValue, Value: helpers.TimeValue,
Usage: "Title of the message.", Usage: "Title of the message.",
EnvVars: []string{"TELEGRAM_TITLE"}, EnvVars: []string{"TELEGRAM_TITLE"},
}, },
@ -75,7 +76,7 @@ All configuration options are also available via environment variables.`,
chn := strings.Split(telegramOpts.Channel, ",") chn := strings.Split(telegramOpts.Channel, ",")
for _, v := range chn { for _, v := range chn {
if len(v) <= 0 { if len(v) <= 0 {
return fmt.Errorf(EmptyChannel) return helpers.ErrChannel
} }
k, errStr := strconv.Atoi(v) k, errStr := strconv.Atoi(v)
if errStr != nil { if errStr != nil {
Loading…
Cancel
Save