watchtower/pkg/notifications/gotify.go

77 lines
1.9 KiB
Go
Raw Normal View History

package notifications
import (
2021-03-13 08:58:11 +01:00
"net/url"
"strings"
shoutrrrGotify "github.com/containrrr/shoutrrr/pkg/services/gotify"
2019-07-23 09:36:04 +02:00
t "github.com/containrrr/watchtower/pkg/types"
log "github.com/sirupsen/logrus"
2021-06-27 00:19:52 +02:00
"github.com/spf13/viper"
)
const (
gotifyType = "gotify"
)
type gotifyTypeNotifier struct {
gotifyURL string
gotifyAppToken string
gotifyInsecureSkipVerify bool
}
2021-06-27 00:19:52 +02:00
func newGotifyNotifier() t.ConvertibleNotifier {
2021-06-27 00:19:52 +02:00
apiURL := getGotifyURL()
token := getGotifyToken()
2021-06-27 00:19:52 +02:00
skipVerify := viper.GetBool("notification-gotify-tls-skip-verify")
n := &gotifyTypeNotifier{
2021-03-13 08:58:11 +01:00
gotifyURL: apiURL,
gotifyAppToken: token,
gotifyInsecureSkipVerify: skipVerify,
}
return n
}
2021-06-27 00:19:52 +02:00
func getGotifyToken() string {
gotifyToken := viper.GetString("notification-gotify-token")
if len(gotifyToken) < 1 {
log.Fatal("Required argument --notification-gotify-token(cli) or WATCHTOWER_NOTIFICATION_GOTIFY_TOKEN(env) is empty.")
}
return gotifyToken
}
2021-06-27 00:19:52 +02:00
func getGotifyURL() string {
gotifyURL := viper.GetString("notification-gotify-url")
if len(gotifyURL) < 1 {
log.Fatal("Required argument --notification-gotify-url(cli) or WATCHTOWER_NOTIFICATION_GOTIFY_URL(env) is empty.")
} else if !(strings.HasPrefix(gotifyURL, "http://") || strings.HasPrefix(gotifyURL, "https://")) {
log.Fatal("Gotify URL must start with \"http://\" or \"https://\"")
} else if strings.HasPrefix(gotifyURL, "http://") {
log.Warn("Using an HTTP url for Gotify is insecure")
}
return gotifyURL
}
2021-06-27 00:19:52 +02:00
func (n *gotifyTypeNotifier) GetURL(title string) (string, error) {
2021-03-13 08:58:11 +01:00
apiURL, err := url.Parse(n.gotifyURL)
if err != nil {
return "", err
}
config := &shoutrrrGotify.Config{
2021-03-13 08:58:11 +01:00
Host: apiURL.Host,
Path: apiURL.Path,
DisableTLS: apiURL.Scheme == "http",
Title: title,
2021-03-13 08:58:11 +01:00
Token: n.gotifyAppToken,
}
2021-03-13 08:58:11 +01:00
return config.GetURL().String(), nil
}