switch urfave to cobra

This commit is contained in:
Simon Aronsson 2019-06-22 22:04:36 +02:00
parent 339df55cc6
commit 998e8052c5
10 changed files with 581 additions and 398 deletions

View file

@ -3,6 +3,7 @@ package notifications
import (
"encoding/base64"
"fmt"
"github.com/spf13/cobra"
"net/smtp"
"os"
"time"
@ -10,7 +11,6 @@ import (
"strconv"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const (
@ -31,15 +31,25 @@ type emailTypeNotifier struct {
logLevels []log.Level
}
func newEmailNotifier(c *cli.Context, acceptedLogLevels []log.Level) typeNotifier {
func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) typeNotifier {
flags := c.PersistentFlags()
from, _ := flags.GetString("notification-email-from")
to, _ := flags.GetString("notification-email-to")
server, _ := flags.GetString("notification-email-server")
user, _ := flags.GetString("notification-email-server-user")
password, _ := flags.GetString("notification-email-server-password")
port, _ := flags.GetInt("notification-email-server-port")
tlsSkipVerify, _ := flags.GetBool("notification-email-server-tls-skip-verify")
n := &emailTypeNotifier{
From: c.GlobalString("notification-email-from"),
To: c.GlobalString("notification-email-to"),
Server: c.GlobalString("notification-email-server"),
User: c.GlobalString("notification-email-server-user"),
Password: c.GlobalString("notification-email-server-password"),
Port: c.GlobalInt("notification-email-server-port"),
tlsSkipVerify: c.GlobalBool("notification-email-server-tls-skip-verify"),
From: from,
To: to,
Server: server,
User: user,
Password: password,
Port: port,
tlsSkipVerify: tlsSkipVerify,
logLevels: acceptedLogLevels,
}

View file

@ -4,10 +4,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"io/ioutil"
)
@ -21,17 +21,20 @@ type msTeamsTypeNotifier struct {
data bool
}
func newMsTeamsNotifier(c *cli.Context, acceptedLogLevels []log.Level) typeNotifier {
func newMsTeamsNotifier(cmd *cobra.Command, acceptedLogLevels []log.Level) typeNotifier {
webHookURL := c.GlobalString("notification-msteams-hook")
flags := cmd.PersistentFlags()
webHookURL, _ := flags.GetString("notification-msteams-hook")
if len(webHookURL) <= 0 {
log.Fatal("Required argument --notification-msteams-hook(cli) or WATCHTOWER_NOTIFICATION_MSTEAMS_HOOK_URL(env) is empty.")
}
withData, _ := flags.GetBool("notification-msteams-data")
n := &msTeamsTypeNotifier{
levels: acceptedLogLevels,
webHookURL: webHookURL,
data: c.GlobalBool("notification-msteams-data"),
data: withData,
}
log.AddHook(n)

View file

@ -3,7 +3,7 @@ package notifications
import (
"github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/spf13/cobra"
)
type typeNotifier interface {
@ -17,10 +17,13 @@ type Notifier struct {
}
// NewNotifier creates and returns a new Notifier, using global configuration.
func NewNotifier(c *cli.Context) *Notifier {
func NewNotifier(c *cobra.Command) *Notifier {
n := &Notifier{}
logLevel, err := log.ParseLevel(c.GlobalString("notifications-level"))
f := c.PersistentFlags()
level, _ := f.GetString("notifications-level")
logLevel, err := log.ParseLevel(level)
if err != nil {
log.Fatalf("Notifications invalid log level: %s", err.Error())
}
@ -28,7 +31,8 @@ func NewNotifier(c *cli.Context) *Notifier {
acceptedLogLevels := slackrus.LevelThreshold(logLevel)
// Parse types and create notifiers.
types := c.GlobalStringSlice("notifications")
types, _ := f.GetStringSlice("notifications")
for _, t := range types {
var tn typeNotifier
switch t {

View file

@ -3,7 +3,7 @@ package notifications
import (
"github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/spf13/cobra"
)
const (
@ -14,20 +14,27 @@ type slackTypeNotifier struct {
slackrus.SlackrusHook
}
func newSlackNotifier(c *cli.Context, acceptedLogLevels []log.Level) typeNotifier {
func newSlackNotifier(c *cobra.Command, acceptedLogLevels []log.Level) typeNotifier {
flags := c.PersistentFlags()
hookUrl, _ := flags.GetString("notification-slack-hook-url")
userName, _ := flags.GetString("notification-slack-identifier")
channel, _ := flags.GetString("notification-slack-channel")
emoji, _ := flags.GetString("notification-slack-icon-emoji")
iconUrl, _ := flags.GetString("notification-slack-icon-url")
n := &slackTypeNotifier{
SlackrusHook: slackrus.SlackrusHook{
HookURL: c.GlobalString("notification-slack-hook-url"),
Username: c.GlobalString("notification-slack-identifier"),
Channel: c.GlobalString("notification-slack-channel"),
IconEmoji: c.GlobalString("notification-slack-icon-emoji"),
IconURL: c.GlobalString("notification-slack-icon-url"),
HookURL: hookUrl,
Username: userName,
Channel: channel,
IconEmoji: emoji,
IconURL: iconUrl,
AcceptedLevels: acceptedLogLevels,
},
}
log.AddHook(n)
return n
}