mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-16 15:10:12 +01:00
fix tests, upgrade snakes
This commit is contained in:
parent
aa50cdf9bc
commit
1165f31ca0
15 changed files with 669 additions and 283 deletions
77
internal/flags/deprecated.go
Normal file
77
internal/flags/deprecated.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package flags
|
||||
|
||||
import "github.com/spf13/pflag"
|
||||
|
||||
// RegisterLegacyNotificationFlags registers all the flags related to the old notification system
|
||||
func RegisterLegacyNotificationFlags(flags *pflag.FlagSet) {
|
||||
depFlags := NewDeprecator(flags, "use notification-url instead")
|
||||
depFlags.Deprecate = false
|
||||
|
||||
depFlags.Prefix = "notification-email-"
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_FROM"),
|
||||
depFlags.String("from", "", "Address to send notification emails from")
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_TO"),
|
||||
depFlags.String("to", "", "Address to send notification emails to")
|
||||
|
||||
//viper.GetInt("WATCHTOWER_NOTIFICATION_EMAIL_DELAY"),
|
||||
depFlags.Int("delay", 0, "Delay before sending notifications, expressed in seconds")
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER"),
|
||||
depFlags.String("server", "", "SMTP server to send notification emails through")
|
||||
|
||||
// viper.GetInt("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT"),
|
||||
depFlags.Int("server-port", 25, "SMTP server port to send notification emails through")
|
||||
|
||||
// viper.GetBool("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_TLS_SKIP_VERIFY"),
|
||||
depFlags.Bool("server-tls-skip-verify", false, `Controls whether watchtower verifies the SMTP server's certificate chain and host name.
|
||||
Should only be used for testing.`)
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER"),
|
||||
depFlags.String("server-user", "", "SMTP server user for sending notifications")
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD"),
|
||||
depFlags.String("server-password", "", "SMTP server password for sending notifications")
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG"),
|
||||
depFlags.String("subjecttag", "", "Subject prefix tag for notifications via mail")
|
||||
|
||||
depFlags.Prefix = "notification-slack-"
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_HOOK_URL"),
|
||||
depFlags.String("hook-url", "", "The Slack Hook URL to send notifications to")
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER"),
|
||||
depFlags.String("identifier", "watchtower", "A string which will be used to identify the messages coming from this watchtower instance")
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_CHANNEL"),
|
||||
depFlags.String("channel", "", "A string which overrides the webhook's default channel. Example: #my-custom-channel")
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_ICON_EMOJI"),
|
||||
depFlags.String("icon-emoji", "", "An emoji code string to use in place of the default icon")
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_ICON_URL"),
|
||||
depFlags.String("icon-url", "", "An icon image URL string to use in place of the default icon")
|
||||
|
||||
depFlags.Prefix = "notification-msteams-"
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_MSTEAMS_HOOK_URL"),
|
||||
depFlags.String("hook", "", "The MSTeams WebHook URL to send notifications to")
|
||||
|
||||
// viper.GetBool("WATCHTOWER_NOTIFICATION_MSTEAMS_USE_LOG_DATA"),
|
||||
depFlags.Bool("data", false, "The MSTeams notifier will try to extract log entry fields as MSTeams message facts")
|
||||
|
||||
depFlags.Prefix = "notification-gotify-"
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_GOTIFY_URL"),
|
||||
depFlags.String("url", "", "The Gotify URL to send notifications to")
|
||||
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_GOTIFY_TOKEN"),
|
||||
depFlags.String("token", "", "The Gotify Application required to query the Gotify API")
|
||||
|
||||
// viper.GetBool("WATCHTOWER_NOTIFICATION_GOTIFY_TLS_SKIP_VERIFY"),
|
||||
depFlags.Bool("tls-skip-verify", false, `Controls whether watchtower verifies the Gotify server's certificate chain and host name.
|
||||
Should only be used for testing.`)
|
||||
|
||||
}
|
||||
106
internal/flags/flaghelper.go
Normal file
106
internal/flags/flaghelper.go
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
package flags
|
||||
|
||||
import (
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
type flagBuilder struct {
|
||||
flags *pflag.FlagSet
|
||||
Deprecate bool
|
||||
Hide bool
|
||||
DeprecationMessage string
|
||||
Prefix string
|
||||
}
|
||||
|
||||
func NewDeprecator(flags *pflag.FlagSet, message string) *flagBuilder {
|
||||
return &flagBuilder{
|
||||
flags: flags,
|
||||
Deprecate: true,
|
||||
Hide: false,
|
||||
DeprecationMessage: message,
|
||||
Prefix: "",
|
||||
}
|
||||
}
|
||||
|
||||
//goland:noinspection GoUnusedExportedFunction
|
||||
func New(flags *pflag.FlagSet) *flagBuilder {
|
||||
return &flagBuilder{
|
||||
flags: flags,
|
||||
Deprecate: false,
|
||||
Hide: false,
|
||||
DeprecationMessage: "",
|
||||
Prefix: "",
|
||||
}
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) MarkFlag(name string) {
|
||||
if fb.Deprecate {
|
||||
must(fb.flags.MarkDeprecated(name, fb.DeprecationMessage))
|
||||
}
|
||||
if fb.Hide {
|
||||
must(fb.flags.MarkHidden(name))
|
||||
}
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) ResolveName(name string) string {
|
||||
if fb.Prefix == "" {
|
||||
return name
|
||||
}
|
||||
return fb.Prefix + name
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) StringP(name string, shorthand string, value string, usage string) {
|
||||
fullName := fb.ResolveName(name)
|
||||
fb.flags.StringP(fullName, shorthand, value, usage)
|
||||
fb.MarkFlag(fullName)
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) StringSliceP(name string, shorthand string, value []string, usage string) {
|
||||
fullName := fb.ResolveName(name)
|
||||
fb.flags.StringSliceP(fullName, shorthand, value, usage)
|
||||
fb.MarkFlag(fullName)
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) StringArrayP(name string, shorthand string, value []string, usage string) {
|
||||
fullName := fb.ResolveName(name)
|
||||
fb.flags.StringArrayP(fullName, shorthand, value, usage)
|
||||
fb.MarkFlag(fullName)
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) BoolP(name string, shorthand string, value bool, usage string) {
|
||||
fullName := fb.ResolveName(name)
|
||||
fb.flags.BoolP(fullName, shorthand, value, usage)
|
||||
fb.MarkFlag(fullName)
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) IntP(name string, shorthand string, value int, usage string) {
|
||||
fullName := fb.ResolveName(name)
|
||||
fb.flags.IntP(fullName, shorthand, value, usage)
|
||||
fb.MarkFlag(fullName)
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) String(name string, value string, usage string) {
|
||||
fb.StringP(name, "", value, usage)
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) StringSlice(name string, value []string, usage string) {
|
||||
fb.flags.StringSliceP(name, "", value, usage)
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) StringArray(name string, value []string, usage string) {
|
||||
fb.flags.StringArrayP(name, "", value, usage)
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) Bool(name string, value bool, usage string) {
|
||||
fb.BoolP(name, "", value, usage)
|
||||
}
|
||||
|
||||
func (fb *flagBuilder) Int(name string, value int, usage string) {
|
||||
fb.IntP(name, "", value, usage)
|
||||
}
|
||||
|
||||
func must(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,6 @@ import (
|
|||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
|
|
@ -36,7 +35,7 @@ func RegisterSystemFlags(rootCmd *cobra.Command) {
|
|||
DefaultInterval, // viper.GetInt("WATCHTOWER_POLL_INTERVAL"),
|
||||
"poll interval (in seconds)")
|
||||
|
||||
flags.String(
|
||||
flags.StringP(
|
||||
"schedule",
|
||||
"s",
|
||||
"",
|
||||
|
|
@ -189,122 +188,6 @@ func RegisterNotificationFlags(rootCmd *cobra.Command) {
|
|||
// viper.GetString("WATCHTOWER_NOTIFICATIONS_HOSTNAME"),
|
||||
"Custom hostname for notification titles")
|
||||
|
||||
flags.String(
|
||||
"notification-email-from",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_FROM"),
|
||||
"Address to send notification emails from")
|
||||
|
||||
flags.String(
|
||||
"notification-email-to",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_TO"),
|
||||
"Address to send notification emails to")
|
||||
|
||||
flags.Int(
|
||||
"notification-email-delay",
|
||||
0,
|
||||
//viper.GetInt("WATCHTOWER_NOTIFICATION_EMAIL_DELAY"),
|
||||
"Delay before sending notifications, expressed in seconds")
|
||||
|
||||
flags.String(
|
||||
"notification-email-server",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER"),
|
||||
"SMTP server to send notification emails through")
|
||||
|
||||
flags.Int(
|
||||
"notification-email-server-port",
|
||||
25,
|
||||
// viper.GetInt("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT"),
|
||||
"SMTP server port to send notification emails through")
|
||||
|
||||
flags.Bool(
|
||||
"notification-email-server-tls-skip-verify",
|
||||
false,
|
||||
// viper.GetBool("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_TLS_SKIP_VERIFY"),
|
||||
`Controls whether watchtower verifies the SMTP server's certificate chain and host name.
|
||||
Should only be used for testing.`)
|
||||
|
||||
flags.String(
|
||||
"notification-email-server-user",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER"),
|
||||
"SMTP server user for sending notifications")
|
||||
|
||||
flags.String(
|
||||
"notification-email-server-password",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD"),
|
||||
"SMTP server password for sending notifications")
|
||||
|
||||
flags.String(
|
||||
"notification-email-subjecttag",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG"),
|
||||
"Subject prefix tag for notifications via mail")
|
||||
|
||||
flags.String(
|
||||
"notification-slack-hook-url",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_HOOK_URL"),
|
||||
"The Slack Hook URL to send notifications to")
|
||||
|
||||
flags.String(
|
||||
"notification-slack-identifier",
|
||||
"watchtower",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER"),
|
||||
"A string which will be used to identify the messages coming from this watchtower instance")
|
||||
|
||||
flags.String(
|
||||
"notification-slack-channel",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_CHANNEL"),
|
||||
"A string which overrides the webhook's default channel. Example: #my-custom-channel")
|
||||
|
||||
flags.String(
|
||||
"notification-slack-icon-emoji",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_ICON_EMOJI"),
|
||||
"An emoji code string to use in place of the default icon")
|
||||
|
||||
flags.String(
|
||||
"notification-slack-icon-url",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_SLACK_ICON_URL"),
|
||||
"An icon image URL string to use in place of the default icon")
|
||||
|
||||
flags.String(
|
||||
"notification-msteams-hook",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_MSTEAMS_HOOK_URL"),
|
||||
"The MSTeams WebHook URL to send notifications to")
|
||||
|
||||
flags.Bool(
|
||||
"notification-msteams-data",
|
||||
false,
|
||||
// viper.GetBool("WATCHTOWER_NOTIFICATION_MSTEAMS_USE_LOG_DATA"),
|
||||
"The MSTeams notifier will try to extract log entry fields as MSTeams message facts")
|
||||
|
||||
flags.String(
|
||||
"notification-gotify-url",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_GOTIFY_URL"),
|
||||
"The Gotify URL to send notifications to")
|
||||
|
||||
flags.String(
|
||||
"notification-gotify-token",
|
||||
"",
|
||||
// viper.GetString("WATCHTOWER_NOTIFICATION_GOTIFY_TOKEN"),
|
||||
"The Gotify Application required to query the Gotify API")
|
||||
|
||||
flags.Bool(
|
||||
"notification-gotify-tls-skip-verify",
|
||||
false,
|
||||
// viper.GetBool("WATCHTOWER_NOTIFICATION_GOTIFY_TLS_SKIP_VERIFY"),
|
||||
`Controls whether watchtower verifies the Gotify server's certificate chain and host name.
|
||||
Should only be used for testing.`)
|
||||
|
||||
flags.String(
|
||||
"notification-template",
|
||||
"",
|
||||
|
|
@ -328,6 +211,7 @@ Should only be used for testing.`)
|
|||
// viper.GetString("WATCHTOWER_WARN_ON_HEAD_FAILURE"),
|
||||
"When to warn about HEAD pull requests failing. Possible values: always, auto or never")
|
||||
|
||||
RegisterLegacyNotificationFlags(flags)
|
||||
}
|
||||
|
||||
func mustBindEnv(flag string, env string) {
|
||||
|
|
@ -368,8 +252,8 @@ func EnvConfig() error {
|
|||
var err error
|
||||
|
||||
host := viper.GetString("host")
|
||||
tls = viper.GetBool("tlsverify")
|
||||
version = viper.GetString("api-version")
|
||||
tls := viper.GetBool("tlsverify")
|
||||
version := viper.GetString("api-version")
|
||||
if err = setEnvOptStr("DOCKER_HOST", host); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,16 +6,17 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEnvConfig_Defaults(t *testing.T) {
|
||||
cmd := new(cobra.Command)
|
||||
SetDefaults()
|
||||
RegisterDockerFlags(cmd)
|
||||
BindViperFlags(cmd)
|
||||
|
||||
err := EnvConfig(cmd)
|
||||
err := EnvConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "unix:///var/run/docker.sock", os.Getenv("DOCKER_HOST"))
|
||||
|
|
@ -26,13 +27,13 @@ func TestEnvConfig_Defaults(t *testing.T) {
|
|||
|
||||
func TestEnvConfig_Custom(t *testing.T) {
|
||||
cmd := new(cobra.Command)
|
||||
SetDefaults()
|
||||
RegisterDockerFlags(cmd)
|
||||
BindViperFlags(cmd)
|
||||
|
||||
err := cmd.ParseFlags([]string{"--host", "some-custom-docker-host", "--tlsverify", "--api-version", "1.99"})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = EnvConfig(cmd)
|
||||
err = EnvConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "some-custom-docker-host", os.Getenv("DOCKER_HOST"))
|
||||
|
|
@ -56,7 +57,10 @@ func TestGetSecretsFromFilesWithFile(t *testing.T) {
|
|||
// Create the temporary file which will contain a secret.
|
||||
file, err := ioutil.TempFile(os.TempDir(), "watchtower-")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(file.Name()) // Make sure to remove the temporary file later.
|
||||
defer func() {
|
||||
// Make sure to remove the temporary file later.
|
||||
_ = os.Remove(file.Name())
|
||||
}()
|
||||
|
||||
// Write the secret to the temporary file.
|
||||
secret := []byte(value)
|
||||
|
|
@ -71,11 +75,11 @@ func TestGetSecretsFromFilesWithFile(t *testing.T) {
|
|||
|
||||
func testGetSecretsFromFiles(t *testing.T, flagName string, expected string) {
|
||||
cmd := new(cobra.Command)
|
||||
SetDefaults()
|
||||
RegisterNotificationFlags(cmd)
|
||||
GetSecretsFromFiles(cmd)
|
||||
value, err := cmd.PersistentFlags().GetString(flagName)
|
||||
require.NoError(t, err)
|
||||
BindViperFlags(cmd)
|
||||
SetEnvBindings()
|
||||
GetSecretsFromFiles()
|
||||
value := viper.GetString(flagName)
|
||||
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue