feat(shoutrrr): fix notification url files and tests

This commit is contained in:
p1ksel 2020-06-11 18:37:30 +02:00
parent 624e2a744a
commit e2b963e2aa
2 changed files with 31 additions and 5 deletions

View file

@ -390,16 +390,14 @@ func GetSecretsFromFiles(rootCmd *cobra.Command) {
// getSecretFromFile will check if the flag contains a reference to a file; if it does, replaces the value of the flag with the contents of the file.
func getSecretFromFile(flags *pflag.FlagSet, secret string) {
value, err := flags.GetString(secret)
if err != nil {
log.Error(err)
}
flag := flags.Lookup(secret)
value := flag.Value.String()
if value != "" && isFile(value) {
file, err := ioutil.ReadFile(value)
if err != nil {
log.Fatal(err)
}
flag := flags.Lookup(secret)
if flag.Value.Type() == "stringArray" {
rows := bytes.Split(file, []byte{'\n'})

View file

@ -3,6 +3,7 @@ package flags
import (
"io/ioutil"
"os"
"strings"
"testing"
"github.com/spf13/cobra"
@ -69,6 +70,33 @@ func TestGetSecretsFromFilesWithFile(t *testing.T) {
testGetSecretsFromFiles(t, "notification-email-server-password", value)
}
func TestGetSecretsArrayFromFilesWithFile(t *testing.T) {
expected := []string{"first line", "second line"}
// 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.
// Write the secret to the temporary file.
secret := []byte(strings.Join(expected, "\n"))
_, err = file.Write(secret)
require.NoError(t, err)
err = os.Setenv("WATCHTOWER_NOTIFICATION_URL", file.Name())
require.NoError(t, err)
cmd := new(cobra.Command)
SetDefaults()
RegisterNotificationFlags(cmd)
GetSecretsFromFiles(cmd)
actual, err := cmd.PersistentFlags().GetStringArray("notification-url")
require.NoError(t, err)
assert.Equal(t, expected[0], actual[0])
assert.Equal(t, expected[1], actual[1])
}
func testGetSecretsFromFiles(t *testing.T, flagName string, expected string) {
cmd := new(cobra.Command)
SetDefaults()