From 972b0b276f6fa0b80cc15e6cae2c7837f6e994a0 Mon Sep 17 00:00:00 2001 From: Simon Aronsson Date: Sun, 23 Jun 2019 00:32:50 +0200 Subject: [PATCH] fix linter errors --- cmd/root.go | 9 ++++++--- internal/flags/flags.go | 7 ++++++- notifications/slack.go | 8 ++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 18e20ad..a3511bf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "github.com/containrrr/watchtower/actions" "github.com/containrrr/watchtower/container" "github.com/containrrr/watchtower/internal/flags" @@ -17,6 +16,8 @@ import ( "github.com/spf13/cobra" ) +// DockerAPIMinVersion is the minimum version of the docker api required to +// use watchtower const DockerAPIMinVersion string = "1.24" var ( @@ -48,13 +49,14 @@ func init() { flags.RegisterNotificationFlags(rootCmd) } +// Execute the root func and exit in case of errors func Execute() { if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) + log.Fatal(err) } } +// PreRun is a lifecycle hook that runs before the command is executed. func PreRun(cmd *cobra.Command, args []string) { f := cmd.PersistentFlags() @@ -99,6 +101,7 @@ func PreRun(cmd *cobra.Command, args []string) { notifier = notifications.NewNotifier(cmd) } +// Run is the main execution flow of the command func Run(c *cobra.Command, names []string) { filter := container.BuildFilter(names, enableLabel) runOnce, _ := c.PersistentFlags().GetBool("run-once") diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 236e4ba..6962b98 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -8,12 +8,14 @@ import ( "time" ) +// RegisterDockerFlags that are used directly by the docker api client func RegisterDockerFlags(rootCmd *cobra.Command) { flags := rootCmd.PersistentFlags() flags.StringP("host", "H", viper.GetString("DOCKER_HOST"), "daemon socket to connect to") flags.BoolP("tlsverify", "v", viper.GetBool("DOCKER_TLS_VERIFY"), "use TLS and verify the remote") } +// RegisterSystemFlags that are used by watchtower to modify the program flow func RegisterSystemFlags(rootCmd *cobra.Command) { flags := rootCmd.PersistentFlags() flags.IntP( @@ -82,6 +84,7 @@ func RegisterSystemFlags(rootCmd *cobra.Command) { "Will also include created and exited containers") } +// RegisterNotificationFlags that are used by watchtower to send notifications func RegisterNotificationFlags(rootCmd *cobra.Command) { flags := rootCmd.PersistentFlags() @@ -185,6 +188,7 @@ Should only be used for testing. "The MSTeams notifier will try to extract log entry fields as MSTeams message facts") } +// SetDefaults provides default values for environment variables func SetDefaults() { viper.AutomaticEnv() viper.SetDefault("DOCKER_HOST", "unix:///var/run/docker.sock") @@ -196,7 +200,7 @@ func SetDefaults() { viper.SetDefault("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", "watchtower") } -// envConfig translates the command-line options into environment variables +// EnvConfig translates the command-line options into environment variables // that will initialize the api client func EnvConfig(cmd *cobra.Command, dockerAPIMinVersion string) error { var err error @@ -223,6 +227,7 @@ func EnvConfig(cmd *cobra.Command, dockerAPIMinVersion string) error { return nil } +// ReadFlags reads common flags used in the main program flow of watchtower func ReadFlags(cmd *cobra.Command) (bool, bool, bool, time.Duration) { flags := cmd.PersistentFlags() diff --git a/notifications/slack.go b/notifications/slack.go index 99ad1dd..709c9ba 100644 --- a/notifications/slack.go +++ b/notifications/slack.go @@ -17,19 +17,19 @@ type slackTypeNotifier struct { func newSlackNotifier(c *cobra.Command, acceptedLogLevels []log.Level) typeNotifier { 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") + iconURL, _ := flags.GetString("notification-slack-icon-url") n := &slackTypeNotifier{ SlackrusHook: slackrus.SlackrusHook{ - HookURL: hookUrl, + HookURL: hookURL, Username: userName, Channel: channel, IconEmoji: emoji, - IconURL: iconUrl, + IconURL: iconURL, AcceptedLevels: acceptedLogLevels, }, }