refactor: move actions into internal

pull/350/head^2
Simon Aronsson 5 years ago
parent 62f603bb25
commit a425bf1024

@ -7,10 +7,10 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/containrrr/watchtower/internal/actions"
"github.com/containrrr/watchtower/internal/flags" "github.com/containrrr/watchtower/internal/flags"
"github.com/containrrr/watchtower/notifications"
"github.com/containrrr/watchtower/pkg/actions"
"github.com/containrrr/watchtower/pkg/container" "github.com/containrrr/watchtower/pkg/container"
"github.com/containrrr/watchtower/pkg/notifications"
t "github.com/containrrr/watchtower/pkg/types" t "github.com/containrrr/watchtower/pkg/types"
"github.com/robfig/cron" "github.com/robfig/cron"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"

@ -2,6 +2,7 @@ package actions_test
import ( import (
"errors" "errors"
"github.com/containrrr/watchtower/internal/actions"
"testing" "testing"
"time" "time"
@ -43,7 +44,7 @@ var _ = Describe("the actions package", func() {
When("given an empty array", func() { When("given an empty array", func() {
It("should not do anything", func() { It("should not do anything", func() {
client.TestData.Containers = []container.Container{} client.TestData.Containers = []container.Container{}
err := CheckForMultipleWatchtowerInstances(client, false) err := actions.CheckForMultipleWatchtowerInstances(client, false)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
}) })
}) })
@ -56,7 +57,7 @@ var _ = Describe("the actions package", func() {
"watchtower", "watchtower",
time.Now()), time.Now()),
} }
err := CheckForMultipleWatchtowerInstances(client, false) err := actions.CheckForMultipleWatchtowerInstances(client, false)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
}) })
}) })
@ -84,7 +85,7 @@ var _ = Describe("the actions package", func() {
} }
}) })
It("should stop all but the latest one", func() { It("should stop all but the latest one", func() {
err := CheckForMultipleWatchtowerInstances(client, false) err := actions.CheckForMultipleWatchtowerInstances(client, false)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
}) })
}) })
@ -111,12 +112,12 @@ var _ = Describe("the actions package", func() {
} }
}) })
It("should try to delete the image if the cleanup flag is true", func() { It("should try to delete the image if the cleanup flag is true", func() {
err := CheckForMultipleWatchtowerInstances(client, true) err := actions.CheckForMultipleWatchtowerInstances(client, true)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(client.TestData.TriedToRemoveImage).To(BeTrue()) Expect(client.TestData.TriedToRemoveImage).To(BeTrue())
}) })
It("should not try to delete the image if the cleanup flag is false", func() { It("should not try to delete the image if the cleanup flag is false", func() {
err := CheckForMultipleWatchtowerInstances(client, false) err := actions.CheckForMultipleWatchtowerInstances(client, false)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(client.TestData.TriedToRemoveImage).To(BeFalse()) Expect(client.TestData.TriedToRemoveImage).To(BeFalse())
}) })

@ -1,5 +1,6 @@
package util package util
// SliceEqual compares two slices and checks whether they have equal content
func SliceEqual(s1, s2 []string) bool { func SliceEqual(s1, s2 []string) bool {
if len(s1) != len(s2) { if len(s1) != len(s2) {
return false return false
@ -14,6 +15,7 @@ func SliceEqual(s1, s2 []string) bool {
return true return true
} }
// SliceSubtract subtracts the content of slice a2 from slice a1
func SliceSubtract(a1, a2 []string) []string { func SliceSubtract(a1, a2 []string) []string {
a := []string{} a := []string{}
@ -35,6 +37,7 @@ func SliceSubtract(a1, a2 []string) []string {
return a return a
} }
// StringMapSubtract subtracts the content of structmap m2 from structmap m1
func StringMapSubtract(m1, m2 map[string]string) map[string]string { func StringMapSubtract(m1, m2 map[string]string) map[string]string {
m := map[string]string{} m := map[string]string{}
@ -51,6 +54,7 @@ func StringMapSubtract(m1, m2 map[string]string) map[string]string {
return m return m
} }
// StructMapSubtract subtracts the content of structmap m2 from structmap m1
func StructMapSubtract(m1, m2 map[string]struct{}) map[string]struct{} { func StructMapSubtract(m1, m2 map[string]struct{}) map[string]struct{} {
m := map[string]struct{}{} m := map[string]struct{}{}

@ -5,8 +5,6 @@ import (
"testing" "testing"
) )
func TestSliceEqual_True(t *testing.T) { func TestSliceEqual_True(t *testing.T) {
s1 := []string{"a", "b", "c"} s1 := []string{"a", "b", "c"}
s2 := []string{"a", "b", "c"} s2 := []string{"a", "b", "c"}

@ -17,5 +17,5 @@ func init() {
} }
func main() { func main() {
cmd.Execute() cmd.Execute()
} }

@ -23,8 +23,7 @@ var _ = Describe("the container", func() {
server := mocks.NewMockAPIServer() server := mocks.NewMockAPIServer()
docker, _ = cli.NewClientWithOpts( docker, _ = cli.NewClientWithOpts(
cli.WithHost(server.URL), cli.WithHost(server.URL),
cli.WithHTTPClient(server.Client(), cli.WithHTTPClient(server.Client()))
))
client = dockerClient{ client = dockerClient{
api: docker, api: docker,
pullImages: false, pullImages: false,

@ -3,8 +3,8 @@ package container
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/containrrr/watchtower/pkg/container/mocks" "github.com/containrrr/watchtower/pkg/container/mocks"
"github.com/stretchr/testify/assert"
) )
func TestWatchtowerContainersFilter(t *testing.T) { func TestWatchtowerContainersFilter(t *testing.T) {

@ -1,15 +1,11 @@
package container package container
import ( import (
"github.com/stretchr/testify/assert"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestEncodedEnvAuth_ShouldReturnAnErrorIfRepoEnvsAreUnset(t *testing.T) { func TestEncodedEnvAuth_ShouldReturnAnErrorIfRepoEnvsAreUnset(t *testing.T) {
os.Unsetenv("REPO_USER") os.Unsetenv("REPO_USER")
os.Unsetenv("REPO_PASS") os.Unsetenv("REPO_PASS")

@ -8,9 +8,9 @@ import (
"os" "os"
"time" "time"
"strconv"
t "github.com/containrrr/watchtower/pkg/types" t "github.com/containrrr/watchtower/pkg/types"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strconv"
) )
const ( const (
@ -35,8 +35,8 @@ func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifie
flags := c.PersistentFlags() flags := c.PersistentFlags()
from, _ := flags.GetString("notification-email-from") from, _ := flags.GetString("notification-email-from")
to, _ := flags.GetString("notification-email-to") to, _ := flags.GetString("notification-email-to")
server, _ := flags.GetString("notification-email-server") server, _ := flags.GetString("notification-email-server")
user, _ := flags.GetString("notification-email-server-user") user, _ := flags.GetString("notification-email-server-user")
password, _ := flags.GetString("notification-email-server-password") password, _ := flags.GetString("notification-email-server-password")
port, _ := flags.GetInt("notification-email-server-port") port, _ := flags.GetInt("notification-email-server-port")
@ -70,7 +70,7 @@ func (e *emailTypeNotifier) buildMessage(entries []*log.Entry) []byte {
} }
t := time.Now() t := time.Now()
header := make(map[string]string) header := make(map[string]string)
header["From"] = e.From header["From"] = e.From
header["To"] = e.To header["To"] = e.To

@ -7,8 +7,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// Notifier can send log output as notification to admins, with optional batching. // Notifier can send log output as notification to admins, with optional batching.
type Notifier struct { type Notifier struct {
types []ty.Notifier types []ty.Notifier

@ -1,10 +1,10 @@
package notifications package notifications
import ( import (
t "github.com/containrrr/watchtower/pkg/types"
"github.com/johntdyer/slackrus" "github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
t "github.com/containrrr/watchtower/pkg/types"
) )
const ( const (
@ -18,11 +18,11 @@ type slackTypeNotifier struct {
func newSlackNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifier { func newSlackNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifier {
flags := c.PersistentFlags() flags := c.PersistentFlags()
hookURL, _ := flags.GetString("notification-slack-hook-url") hookURL, _ := flags.GetString("notification-slack-hook-url")
userName, _ := flags.GetString("notification-slack-identifier") userName, _ := flags.GetString("notification-slack-identifier")
channel, _ := flags.GetString("notification-slack-channel") channel, _ := flags.GetString("notification-slack-channel")
emoji, _ := flags.GetString("notification-slack-icon-emoji") emoji, _ := flags.GetString("notification-slack-icon-emoji")
iconURL, _ := flags.GetString("notification-slack-icon-url") iconURL, _ := flags.GetString("notification-slack-icon-url")
n := &slackTypeNotifier{ n := &slackTypeNotifier{
SlackrusHook: slackrus.SlackrusHook{ SlackrusHook: slackrus.SlackrusHook{

@ -1,5 +1,6 @@
package types package types
// Notifier is the interface that all notification services have in common
type Notifier interface { type Notifier interface {
StartNotification() StartNotification()
SendNotification() SendNotification()

Loading…
Cancel
Save