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"
"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"
)
@ -12,7 +20,7 @@ import (
// Version variable is used for semVer
var Version string
// main with combile all the function into commands
// main with combine all the function into commands
func main() {
app := cli.NewApp()
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.`
// app.Commands contains the subcommands as functions which return []*cli.Command.
app.Commands = []*cli.Command{
cmd.SendToTelegram(),
cmd.SendToRocketChat(),
cmd.SendToSlack(),
cmd.SendToDiscord(),
cmd.SendToTeams(),
cmd.SendToPushOver(),
cmd.SendToEmail(),
cmd.SendToMattermost(),
cmd.SendToPushBullet(),
telegram.Send(),
rocketchat.Send(),
slack.Send(),
discord.Send(),
msteams.Send(),
pushover.Send(),
email.Send(),
mattermost.Send(),
pushbullet.Send(),
}
err := app.Run(os.Args)

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

@ -1,11 +1,12 @@
package cmd
package email
import (
"context"
"fmt"
"log"
"strings"
"github.com/kha7iq/pingme/service/helpers"
"github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/mail"
"github.com/urfave/cli/v2"
@ -23,12 +24,12 @@ type email struct {
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
// 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
// each ReceiverAddress is added to receiver.
func SendToEmail() *cli.Command {
func Send() *cli.Command {
var emailOpts email
return &cli.Command{
Name: "email",
@ -98,7 +99,7 @@ All configuration options are also available via environment variables.`,
&cli.StringFlag{
Destination: &emailOpts.Subject,
Name: "sub",
Value: TimeValue,
Value: helpers.TimeValue,
Usage: "Subject of the email",
EnvVars: []string{"EMAIL_SUBJECT"},
},
@ -111,7 +112,7 @@ All configuration options are also available via environment variables.`,
chn := strings.Split(emailOpts.ReceiverAddress, ",")
for _, v := range chn {
if len(v) <= 0 {
return fmt.Errorf(EmptyChannel)
return helpers.ErrChannel
}
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 (
"bytes"
@ -9,6 +9,8 @@ import (
"strings"
"time"
"github.com/kha7iq/pingme/service/helpers"
"github.com/urfave/cli/v2"
)
@ -47,11 +49,11 @@ type matterMostResponse struct {
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.
// If multiple channel ids are provided then the string is split with "," separator and
// message is sent to each channel.
func SendToMattermost() *cli.Command {
func Send() *cli.Command {
var mattermostOpts matterMost
return &cli.Command{
Name: "mattermost",
@ -87,7 +89,7 @@ You can specify multiple channels by separating the value with ','.`,
&cli.StringFlag{
Destination: &mattermostOpts.Title,
Name: "title",
Value: TimeValue,
Value: helpers.TimeValue,
Usage: "Title of the message.",
EnvVars: []string{"MATTERMOST_TITLE"},
},
@ -126,7 +128,7 @@ You can specify multiple channels by separating the value with ','.`,
ids := strings.Split(mattermostOpts.ChanIDs, ",")
for _, v := range ids {
if len(v) == 0 {
return fmt.Errorf(EmptyChannel)
return helpers.ErrChannel
}
jsonData, err := toJSON(v, fullMessage)

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

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

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

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

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

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