From a425bf102423493d3e9bb71121267a8518573e5b Mon Sep 17 00:00:00 2001 From: Simon Aronsson Date: Sun, 21 Jul 2019 22:22:30 +0200 Subject: [PATCH] refactor: move actions into internal --- cmd/root.go | 4 ++-- {pkg => internal}/actions/actions_suite_test.go | 11 ++++++----- {pkg => internal}/actions/check.go | 0 {pkg => internal}/actions/update.go | 0 internal/util/util.go | 4 ++++ internal/util/util_test.go | 2 -- main.go | 2 +- pkg/container/container_test.go | 3 +-- pkg/container/filters_test.go | 2 +- pkg/container/trust_test.go | 6 +----- {notifications => pkg/notifications}/email.go | 8 ++++---- {notifications => pkg/notifications}/msteams.go | 0 {notifications => pkg/notifications}/notifier.go | 2 -- {notifications => pkg/notifications}/slack.go | 10 +++++----- {notifications => pkg/notifications}/smtp.go | 0 {notifications => pkg/notifications}/util.go | 0 pkg/types/notifier.go | 1 + 17 files changed, 26 insertions(+), 29 deletions(-) rename {pkg => internal}/actions/actions_suite_test.go (91%) rename {pkg => internal}/actions/check.go (100%) rename {pkg => internal}/actions/update.go (100%) rename {notifications => pkg/notifications}/email.go (96%) rename {notifications => pkg/notifications}/msteams.go (100%) rename {notifications => pkg/notifications}/notifier.go (99%) rename {notifications => pkg/notifications}/slack.go (76%) rename {notifications => pkg/notifications}/smtp.go (100%) rename {notifications => pkg/notifications}/util.go (100%) diff --git a/cmd/root.go b/cmd/root.go index 10023f4..195b174 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,10 +7,10 @@ import ( "syscall" "time" + "github.com/containrrr/watchtower/internal/actions" "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/notifications" t "github.com/containrrr/watchtower/pkg/types" "github.com/robfig/cron" log "github.com/sirupsen/logrus" diff --git a/pkg/actions/actions_suite_test.go b/internal/actions/actions_suite_test.go similarity index 91% rename from pkg/actions/actions_suite_test.go rename to internal/actions/actions_suite_test.go index c0e07f7..031f54c 100644 --- a/pkg/actions/actions_suite_test.go +++ b/internal/actions/actions_suite_test.go @@ -2,6 +2,7 @@ package actions_test import ( "errors" + "github.com/containrrr/watchtower/internal/actions" "testing" "time" @@ -43,7 +44,7 @@ var _ = Describe("the actions package", func() { When("given an empty array", func() { It("should not do anything", func() { client.TestData.Containers = []container.Container{} - err := CheckForMultipleWatchtowerInstances(client, false) + err := actions.CheckForMultipleWatchtowerInstances(client, false) Expect(err).NotTo(HaveOccurred()) }) }) @@ -56,7 +57,7 @@ var _ = Describe("the actions package", func() { "watchtower", time.Now()), } - err := CheckForMultipleWatchtowerInstances(client, false) + err := actions.CheckForMultipleWatchtowerInstances(client, false) Expect(err).NotTo(HaveOccurred()) }) }) @@ -84,7 +85,7 @@ var _ = Describe("the actions package", func() { } }) It("should stop all but the latest one", func() { - err := CheckForMultipleWatchtowerInstances(client, false) + err := actions.CheckForMultipleWatchtowerInstances(client, false) 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() { - err := CheckForMultipleWatchtowerInstances(client, true) + err := actions.CheckForMultipleWatchtowerInstances(client, true) Expect(err).NotTo(HaveOccurred()) Expect(client.TestData.TriedToRemoveImage).To(BeTrue()) }) 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(client.TestData.TriedToRemoveImage).To(BeFalse()) }) diff --git a/pkg/actions/check.go b/internal/actions/check.go similarity index 100% rename from pkg/actions/check.go rename to internal/actions/check.go diff --git a/pkg/actions/update.go b/internal/actions/update.go similarity index 100% rename from pkg/actions/update.go rename to internal/actions/update.go diff --git a/internal/util/util.go b/internal/util/util.go index 924e9c2..08c88bc 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -1,5 +1,6 @@ package util +// SliceEqual compares two slices and checks whether they have equal content func SliceEqual(s1, s2 []string) bool { if len(s1) != len(s2) { return false @@ -14,6 +15,7 @@ func SliceEqual(s1, s2 []string) bool { return true } +// SliceSubtract subtracts the content of slice a2 from slice a1 func SliceSubtract(a1, a2 []string) []string { a := []string{} @@ -35,6 +37,7 @@ func SliceSubtract(a1, a2 []string) []string { return a } +// StringMapSubtract subtracts the content of structmap m2 from structmap m1 func StringMapSubtract(m1, m2 map[string]string) map[string]string { m := map[string]string{} @@ -51,6 +54,7 @@ func StringMapSubtract(m1, m2 map[string]string) map[string]string { return m } +// StructMapSubtract subtracts the content of structmap m2 from structmap m1 func StructMapSubtract(m1, m2 map[string]struct{}) map[string]struct{} { m := map[string]struct{}{} diff --git a/internal/util/util_test.go b/internal/util/util_test.go index e5bf6ba..a6dd657 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -5,8 +5,6 @@ import ( "testing" ) - - func TestSliceEqual_True(t *testing.T) { s1 := []string{"a", "b", "c"} s2 := []string{"a", "b", "c"} diff --git a/main.go b/main.go index c2ed833..193e249 100644 --- a/main.go +++ b/main.go @@ -17,5 +17,5 @@ func init() { } func main() { - cmd.Execute() + cmd.Execute() } diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go index 0cb8931..9e1b213 100644 --- a/pkg/container/container_test.go +++ b/pkg/container/container_test.go @@ -23,8 +23,7 @@ var _ = Describe("the container", func() { server := mocks.NewMockAPIServer() docker, _ = cli.NewClientWithOpts( cli.WithHost(server.URL), - cli.WithHTTPClient(server.Client(), - )) + cli.WithHTTPClient(server.Client())) client = dockerClient{ api: docker, pullImages: false, diff --git a/pkg/container/filters_test.go b/pkg/container/filters_test.go index 00ebae9..4118335 100644 --- a/pkg/container/filters_test.go +++ b/pkg/container/filters_test.go @@ -3,8 +3,8 @@ package container import ( "testing" - "github.com/stretchr/testify/assert" "github.com/containrrr/watchtower/pkg/container/mocks" + "github.com/stretchr/testify/assert" ) func TestWatchtowerContainersFilter(t *testing.T) { diff --git a/pkg/container/trust_test.go b/pkg/container/trust_test.go index 6aa807b..7d2ac96 100644 --- a/pkg/container/trust_test.go +++ b/pkg/container/trust_test.go @@ -1,15 +1,11 @@ package container import ( + "github.com/stretchr/testify/assert" "os" "testing" - "github.com/stretchr/testify/assert" ) - - - - func TestEncodedEnvAuth_ShouldReturnAnErrorIfRepoEnvsAreUnset(t *testing.T) { os.Unsetenv("REPO_USER") os.Unsetenv("REPO_PASS") diff --git a/notifications/email.go b/pkg/notifications/email.go similarity index 96% rename from notifications/email.go rename to pkg/notifications/email.go index 8a0ebfe..60db9cb 100644 --- a/notifications/email.go +++ b/pkg/notifications/email.go @@ -8,9 +8,9 @@ import ( "os" "time" - "strconv" t "github.com/containrrr/watchtower/pkg/types" log "github.com/sirupsen/logrus" + "strconv" ) const ( @@ -35,8 +35,8 @@ func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifie flags := c.PersistentFlags() from, _ := flags.GetString("notification-email-from") - to, _ := flags.GetString("notification-email-to") - server, _ := flags.GetString("notification-email-server") + 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") @@ -70,7 +70,7 @@ func (e *emailTypeNotifier) buildMessage(entries []*log.Entry) []byte { } t := time.Now() - + header := make(map[string]string) header["From"] = e.From header["To"] = e.To diff --git a/notifications/msteams.go b/pkg/notifications/msteams.go similarity index 100% rename from notifications/msteams.go rename to pkg/notifications/msteams.go diff --git a/notifications/notifier.go b/pkg/notifications/notifier.go similarity index 99% rename from notifications/notifier.go rename to pkg/notifications/notifier.go index b14ba24..f077c7f 100644 --- a/notifications/notifier.go +++ b/pkg/notifications/notifier.go @@ -7,8 +7,6 @@ import ( "github.com/spf13/cobra" ) - - // Notifier can send log output as notification to admins, with optional batching. type Notifier struct { types []ty.Notifier diff --git a/notifications/slack.go b/pkg/notifications/slack.go similarity index 76% rename from notifications/slack.go rename to pkg/notifications/slack.go index 0bc8ae1..42b7915 100644 --- a/notifications/slack.go +++ b/pkg/notifications/slack.go @@ -1,10 +1,10 @@ package notifications import ( + t "github.com/containrrr/watchtower/pkg/types" "github.com/johntdyer/slackrus" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - t "github.com/containrrr/watchtower/pkg/types" ) const ( @@ -18,11 +18,11 @@ type slackTypeNotifier struct { func newSlackNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifier { flags := c.PersistentFlags() - hookURL, _ := flags.GetString("notification-slack-hook-url") + 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") + 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{ diff --git a/notifications/smtp.go b/pkg/notifications/smtp.go similarity index 100% rename from notifications/smtp.go rename to pkg/notifications/smtp.go diff --git a/notifications/util.go b/pkg/notifications/util.go similarity index 100% rename from notifications/util.go rename to pkg/notifications/util.go diff --git a/pkg/types/notifier.go b/pkg/types/notifier.go index f073552..c8d07d0 100644 --- a/pkg/types/notifier.go +++ b/pkg/types/notifier.go @@ -1,5 +1,6 @@ package types +// Notifier is the interface that all notification services have in common type Notifier interface { StartNotification() SendNotification()