fix some linting and add comments

fix/snakeswap
nils måsén 2 years ago
parent b97de9dd0b
commit 8a9b1ab945

@ -23,17 +23,14 @@ func EnvConfig() error {
host := GetString(DockerHost) host := GetString(DockerHost)
tls := GetBool(DockerTlSVerify) tls := GetBool(DockerTlSVerify)
version := GetString(DockerApiVersion) version := GetString(DockerAPIVersion)
if err = setEnvOptStr("DOCKER_HOST", host); err != nil { if err = setEnvOptStr("DOCKER_HOST", host); err != nil {
return err return err
} }
if err = setEnvOptBool("DOCKER_TLS_VERIFY", tls); err != nil { if err = setEnvOptBool("DOCKER_TLS_VERIFY", tls); err != nil {
return err return err
} }
if err = setEnvOptStr("DOCKER_API_VERSION", version); err != nil { return setEnvOptStr("DOCKER_API_VERSION", version)
return err
}
return nil
} }
func setEnvOptStr(env string, opt string) error { func setEnvOptStr(env string, opt string) error {
@ -59,8 +56,8 @@ func setEnvOptBool(env string, opt bool) error {
func GetSecretsFromFiles() { func GetSecretsFromFiles() {
secrets := []string{ secrets := []string{
string(NotificationEmailServerPassword), string(NotificationEmailServerPassword),
string(NotificationSlackHookUrl), string(NotificationSlackHookURL),
string(NotificationMsteamsHook), string(NotificationMSTeamsHook),
string(NotificationGotifyToken), string(NotificationGotifyToken),
} }
for _, secret := range secrets { for _, secret := range secrets {

@ -25,14 +25,14 @@ const (
RollingRestart boolConfKey = "rolling-restart" RollingRestart boolConfKey = "rolling-restart"
WarnOnHeadFailure stringConfKey = "warn-on-head-failure" WarnOnHeadFailure stringConfKey = "warn-on-head-failure"
HttpApiUpdate boolConfKey = "http-api-update" HTTPAPIUpdate boolConfKey = "http-api-update"
HttpApiMetrics boolConfKey = "http-api-metrics" HTTPAPIMetrics boolConfKey = "http-api-metrics"
HttpApiPeriodicPolls boolConfKey = "http-api-periodic-polls" HTTPAPIPeriodicPolls boolConfKey = "http-api-periodic-polls"
HttpApiToken stringConfKey = "HttpApiToken" HTTPAPIToken stringConfKey = "HTTPAPIToken"
NoColor boolConfKey = "no-color" NoColor boolConfKey = "no-color"
NotificationGotifyTlsSkipVerify boolConfKey = "notification-gotify-tls-skip-verify" NotificationGotifyTLSSkipVerify boolConfKey = "notification-gotify-tls-skip-verify"
Schedule stringConfKey = "schedule" Schedule stringConfKey = "schedule"
Interval intConfKey = "interval" Interval intConfKey = "interval"
@ -43,7 +43,7 @@ const (
/* Docker v*/ /* Docker v*/
DockerHost stringConfKey = "host" DockerHost stringConfKey = "host"
DockerApiVersion stringConfKey = "api-version" DockerAPIVersion stringConfKey = "api-version"
DockerTlSVerify boolConfKey = "tlsverify" DockerTlSVerify boolConfKey = "tlsverify"
Notifications sliceConfKey = "notifications" Notifications sliceConfKey = "notifications"
@ -52,27 +52,27 @@ const (
NotificationsHostname stringConfKey = "notifications-hostname" NotificationsHostname stringConfKey = "notifications-hostname"
NotificationTemplate stringConfKey = "notification-template" NotificationTemplate stringConfKey = "notification-template"
NotificationReport boolConfKey = "notification-report" NotificationReport boolConfKey = "notification-report"
NotificationUrl sliceConfKey = "notification-url" NotificationURL sliceConfKey = "notification-url"
NotificationEmailFrom stringConfKey = "notification-email-from" NotificationEmailFrom stringConfKey = "notification-email-from"
NotificationEmailTo stringConfKey = "notification-email-to" NotificationEmailTo stringConfKey = "notification-email-to"
NotificationEmailServer stringConfKey = "notification-email-server" NotificationEmailServer stringConfKey = "notification-email-server"
NotificationEmailServerUser stringConfKey = "notification-email-server-user" NotificationEmailServerUser stringConfKey = "notification-email-server-user"
NotificationEmailServerPassword stringConfKey = "notification-email-server-password" NotificationEmailServerPassword stringConfKey = "notification-email-server-password"
NotificationEmailSubjecttag stringConfKey = "notification-email-subjecttag" NotificationEmailSubjectTag stringConfKey = "notification-email-subjecttag"
NotificationEmailDelay intConfKey = "notification-email-delay" NotificationEmailDelay intConfKey = "notification-email-delay"
NotificationEmailServerPort intConfKey = "notification-email-server-port" NotificationEmailServerPort intConfKey = "notification-email-server-port"
NotificationEmailServerTlsSkipVerify boolConfKey = "notification-email-server-tls-skip-verify" NotificationEmailServerTLSSkipVerify boolConfKey = "notification-email-server-tls-skip-verify"
NotificationSlackHookUrl stringConfKey = "notification-slack-hook-url" NotificationSlackHookURL stringConfKey = "notification-slack-hook-url"
NotificationSlackIdentifier stringConfKey = "notification-slack-identifier" NotificationSlackIdentifier stringConfKey = "notification-slack-identifier"
NotificationSlackChannel stringConfKey = "notification-slack-channel" NotificationSlackChannel stringConfKey = "notification-slack-channel"
NotificationSlackIconEmoji stringConfKey = "notification-slack-icon-emoji" NotificationSlackIconEmoji stringConfKey = "notification-slack-icon-emoji"
NotificationSlackIconUrl stringConfKey = "notification-slack-icon-url" NotificationSlackIconURL stringConfKey = "notification-slack-icon-url"
NotificationMsteamsHook stringConfKey = "notification-msteams-hook" NotificationMSTeamsHook stringConfKey = "notification-msteams-hook"
NotificationMsteamsData boolConfKey = "notification-msteams-data" NotificationMSTeamsData boolConfKey = "notification-msteams-data"
NotificationGotifyUrl stringConfKey = "notification-gotify-url" NotificationGotifyURL stringConfKey = "notification-gotify-url"
NotificationGotifyToken stringConfKey = "notification-gotify-token" NotificationGotifyToken stringConfKey = "notification-gotify-token"
) )

@ -1,12 +1,12 @@
package config package config
import ( import (
"github.com/spf13/pflag" "github.com/spf13/cobra"
) )
// RegisterLegacyNotificationFlags registers all the flags related to the old notification system // RegisterLegacyNotificationFlags registers all the flags related to the old notification system
func RegisterLegacyNotificationFlags(flags *pflag.FlagSet) { func RegisterLegacyNotificationFlags(cmd *cobra.Command) {
ob := OptBuilder(flags) ob := NewOptBuilder(cmd.PersistentFlags())
// Hide all legacy notification flags from the `--help` to reduce clutter // Hide all legacy notification flags from the `--help` to reduce clutter
ob.Hide = true ob.Hide = true
@ -27,7 +27,7 @@ func RegisterLegacyNotificationFlags(flags *pflag.FlagSet) {
ob.Int(NotificationEmailServerPort, 25, ob.Int(NotificationEmailServerPort, 25,
"SMTP server port to send notification emails through", "WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT") "SMTP server port to send notification emails through", "WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT")
ob.Bool(NotificationEmailServerTlsSkipVerify, false, ob.Bool(NotificationEmailServerTLSSkipVerify, false,
`Controls whether watchtower verifies the SMTP server's certificate chain and host name. `Controls whether watchtower verifies the SMTP server's certificate chain and host name.
Should only be used for testing.`, Should only be used for testing.`,
"WATCHTOWER_NOTIFICATION_EMAIL_SERVER_TLS_SKIP_VERIFY") "WATCHTOWER_NOTIFICATION_EMAIL_SERVER_TLS_SKIP_VERIFY")
@ -40,11 +40,11 @@ Should only be used for testing.`,
"SMTP server password for sending notifications", "SMTP server password for sending notifications",
"WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD") "WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD")
ob.String(NotificationEmailSubjecttag, "", ob.String(NotificationEmailSubjectTag, "",
"Subject prefix tag for notifications via mail", "Subject prefix tag for notifications via mail",
"WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG") "WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG")
ob.String(NotificationSlackHookUrl, "", ob.String(NotificationSlackHookURL, "",
"The Slack Hook URL to send notifications to", "The Slack Hook URL to send notifications to",
"WATCHTOWER_NOTIFICATION_SLACK_HOOK_URL") "WATCHTOWER_NOTIFICATION_SLACK_HOOK_URL")
@ -60,25 +60,25 @@ Should only be used for testing.`,
"An emoji code string to use in place of the default icon", "An emoji code string to use in place of the default icon",
"WATCHTOWER_NOTIFICATION_SLACK_ICON_EMOJI") "WATCHTOWER_NOTIFICATION_SLACK_ICON_EMOJI")
ob.String(NotificationSlackIconUrl, "", ob.String(NotificationSlackIconURL, "",
"An icon image URL string to use in place of the default icon", "An icon image URL string to use in place of the default icon",
"WATCHTOWER_NOTIFICATION_SLACK_ICON_URL") "WATCHTOWER_NOTIFICATION_SLACK_ICON_URL")
ob.String(NotificationMsteamsHook, "", ob.String(NotificationMSTeamsHook, "",
"The MSTeams WebHook URL to send notifications to", "The MSTeams WebHook URL to send notifications to",
"WATCHTOWER_NOTIFICATION_MSTEAMS_HOOK_URL") "WATCHTOWER_NOTIFICATION_MSTEAMS_HOOK_URL")
ob.Bool(NotificationMsteamsData, false, ob.Bool(NotificationMSTeamsData, false,
"The MSTeams notifier will try to extract log entry fields as MSTeams message facts", "The MSTeams notifier will try to extract log entry fields as MSTeams message facts",
"WATCHTOWER_NOTIFICATION_MSTEAMS_USE_LOG_DATA") "WATCHTOWER_NOTIFICATION_MSTEAMS_USE_LOG_DATA")
ob.String(NotificationGotifyUrl, "", ob.String(NotificationGotifyURL, "",
"The Gotify URL to send notifications to", "WATCHTOWER_NOTIFICATION_GOTIFY_URL") "The Gotify URL to send notifications to", "WATCHTOWER_NOTIFICATION_GOTIFY_URL")
ob.String(NotificationGotifyToken, "", ob.String(NotificationGotifyToken, "",
"The Gotify Application required to query the Gotify API", "WATCHTOWER_NOTIFICATION_GOTIFY_TOKEN") "The Gotify Application required to query the Gotify API", "WATCHTOWER_NOTIFICATION_GOTIFY_TOKEN")
ob.Bool(NotificationGotifyTlsSkipVerify, false, ob.Bool(NotificationGotifyTLSSkipVerify, false,
`Controls whether watchtower verifies the Gotify server's certificate chain and host name. `Controls whether watchtower verifies the Gotify server's certificate chain and host name.
Should only be used for testing.`, Should only be used for testing.`,
"WATCHTOWER_NOTIFICATION_GOTIFY_TLS_SKIP_VERIFY") "WATCHTOWER_NOTIFICATION_GOTIFY_TLS_SKIP_VERIFY")

@ -6,82 +6,98 @@ import (
"time" "time"
) )
type optBuilder struct { // OptBuilder is a helper for registering options to both pflags, viper and env
type OptBuilder struct {
Flags *pflag.FlagSet Flags *pflag.FlagSet
Hide bool Hide bool
} }
func OptBuilder(flags *pflag.FlagSet) *optBuilder { // NewOptBuilder returns a new OptBuilder with the supplied flags
return &optBuilder{ func NewOptBuilder(flags *pflag.FlagSet) *OptBuilder {
return &OptBuilder{
Flags: flags, Flags: flags,
} }
} }
func (ob *optBuilder) register(key string, env string) { func (ob *OptBuilder) register(key string, env string) {
_ = viper.BindEnv(key, env) _ = viper.BindEnv(key, env)
if ob.Hide { if ob.Hide {
_ = ob.Flags.MarkHidden(key) _ = ob.Flags.MarkHidden(key)
} }
} }
func (ob *optBuilder) StringP(key stringConfKey, short string, defaultValue string, usage string, env string) { // StringP registers a string option with a shorthand
func (ob *OptBuilder) StringP(key stringConfKey, short string, defaultValue string, usage string, env string) {
ob.Flags.StringP(string(key), short, defaultValue, usage) ob.Flags.StringP(string(key), short, defaultValue, usage)
ob.register(string(key), env) ob.register(string(key), env)
} }
func (ob *optBuilder) BoolP(key boolConfKey, short string, defaultValue bool, usage string, env string) { // BoolP registers a bool option with a shorthand
func (ob *OptBuilder) BoolP(key boolConfKey, short string, defaultValue bool, usage string, env string) {
ob.Flags.BoolP(string(key), short, defaultValue, usage) ob.Flags.BoolP(string(key), short, defaultValue, usage)
ob.register(string(key), env) ob.register(string(key), env)
} }
func (ob *optBuilder) IntP(key intConfKey, short string, defaultValue int, usage string, env string) { // IntP registers an int option with a shorthand
func (ob *OptBuilder) IntP(key intConfKey, short string, defaultValue int, usage string, env string) {
ob.Flags.IntP(string(key), short, defaultValue, usage) ob.Flags.IntP(string(key), short, defaultValue, usage)
ob.register(string(key), env) ob.register(string(key), env)
} }
func (ob *optBuilder) DurationP(key durationConfKey, short string, defaultValue time.Duration, usage string, env string) { // DurationP registers a duration option with a shorthand
func (ob *OptBuilder) DurationP(key durationConfKey, short string, defaultValue time.Duration, usage string, env string) {
ob.Flags.DurationP(string(key), short, defaultValue, usage) ob.Flags.DurationP(string(key), short, defaultValue, usage)
ob.register(string(key), env) ob.register(string(key), env)
} }
func (ob *optBuilder) String(key stringConfKey, defaultValue string, usage string, env string) { // String registers a string option
func (ob *OptBuilder) String(key stringConfKey, defaultValue string, usage string, env string) {
ob.StringP(key, "", defaultValue, usage, env) ob.StringP(key, "", defaultValue, usage, env)
} }
func (ob *optBuilder) Bool(key boolConfKey, defaultValue bool, usage string, env string) { // Bool registers a bool option
func (ob *OptBuilder) Bool(key boolConfKey, defaultValue bool, usage string, env string) {
ob.BoolP(key, "", defaultValue, usage, env) ob.BoolP(key, "", defaultValue, usage, env)
} }
func (ob *optBuilder) Int(key intConfKey, defaultValue int, usage string, env string) { // Int registers a int option
func (ob *OptBuilder) Int(key intConfKey, defaultValue int, usage string, env string) {
ob.IntP(key, "", defaultValue, usage, env) ob.IntP(key, "", defaultValue, usage, env)
} }
func (ob *optBuilder) StringArray(key sliceConfKey, defaultValue []string, usage string, env string) { // StringArray registers a string array option
func (ob *OptBuilder) StringArray(key sliceConfKey, defaultValue []string, usage string, env string) {
ob.Flags.StringArray(string(key), defaultValue, usage) ob.Flags.StringArray(string(key), defaultValue, usage)
ob.register(string(key), env) ob.register(string(key), env)
} }
func (ob *optBuilder) StringSliceP(key sliceConfKey, short string, defaultValue []string, usage string, env string) { // StringSliceP registers a string slice option with a shorthand
func (ob *OptBuilder) StringSliceP(key sliceConfKey, short string, defaultValue []string, usage string, env string) {
ob.Flags.StringSliceP(string(key), short, defaultValue, usage) ob.Flags.StringSliceP(string(key), short, defaultValue, usage)
ob.register(string(key), env) ob.register(string(key), env)
} }
// GetString returns the string option value for the given key
func GetString(key stringConfKey) string { func GetString(key stringConfKey) string {
return viper.GetString(string(key)) return viper.GetString(string(key))
} }
// GetBool returns the bool option value for the given key
func GetBool(key boolConfKey) bool { func GetBool(key boolConfKey) bool {
return viper.GetBool(string(key)) return viper.GetBool(string(key))
} }
// GetInt returns the int option value for the given key
func GetInt(key intConfKey) int { func GetInt(key intConfKey) int {
return viper.GetInt(string(key)) return viper.GetInt(string(key))
} }
// GetDuration returns the duration option value for the given key
func GetDuration(key durationConfKey) time.Duration { func GetDuration(key durationConfKey) time.Duration {
return viper.GetDuration(string(key)) return viper.GetDuration(string(key))
} }
// GetSlice returns the slice option value for the given key
func GetSlice(key sliceConfKey) []string { func GetSlice(key sliceConfKey) []string {
return viper.GetStringSlice(string(key)) return viper.GetStringSlice(string(key))
} }

@ -15,7 +15,7 @@ const DefaultInterval = int(time.Hour * 24 / time.Second)
// RegisterDockerOptions that are used directly by the docker api client // RegisterDockerOptions that are used directly by the docker api client
func RegisterDockerOptions(rootCmd *cobra.Command) { func RegisterDockerOptions(rootCmd *cobra.Command) {
ob := OptBuilder(rootCmd.PersistentFlags()) ob := NewOptBuilder(rootCmd.PersistentFlags())
ob.StringP(DockerHost, "H", "unix:///var/run/docker.sock", ob.StringP(DockerHost, "H", "unix:///var/run/docker.sock",
"daemon socket to connect to", "daemon socket to connect to",
@ -25,14 +25,14 @@ func RegisterDockerOptions(rootCmd *cobra.Command) {
"use TLS and verify the remote", "use TLS and verify the remote",
"DOCKER_TLS_VERIFY") "DOCKER_TLS_VERIFY")
ob.StringP(DockerApiVersion, "a", DockerAPIMinVersion, ob.StringP(DockerAPIVersion, "a", DockerAPIMinVersion,
"api version to use by docker client", "api version to use by docker client",
"DOCKER_API_VERSION") "DOCKER_API_VERSION")
} }
// RegisterSystemOptions that are used by watchtower to modify the program flow // RegisterSystemOptions that are used by watchtower to modify the program flow
func RegisterSystemOptions(rootCmd *cobra.Command) { func RegisterSystemOptions(rootCmd *cobra.Command) {
ob := OptBuilder(rootCmd.PersistentFlags()) ob := NewOptBuilder(rootCmd.PersistentFlags())
ob.IntP(Interval, "i", DefaultInterval, ob.IntP(Interval, "i", DefaultInterval,
"poll interval (in seconds)", "poll interval (in seconds)",
@ -113,19 +113,19 @@ func RegisterSystemOptions(rootCmd *cobra.Command) {
"Restart containers one at a time", "Restart containers one at a time",
"WATCHTOWER_ROLLING_RESTART") "WATCHTOWER_ROLLING_RESTART")
ob.Bool(HttpApiUpdate, false, ob.Bool(HTTPAPIUpdate, false,
"Runs Watchtower in HTTP API mode, so that image updates must to be triggered by a request", "Runs Watchtower in HTTP API mode, so that image updates must to be triggered by a request",
"WATCHTOWER_HTTP_API_UPDATE") "WATCHTOWER_HTTP_API_UPDATE")
ob.Bool(HttpApiMetrics, false, ob.Bool(HTTPAPIMetrics, false,
"Runs Watchtower with the Prometheus metrics API enabled", "Runs Watchtower with the Prometheus metrics API enabled",
"WATCHTOWER_HTTP_API_METRICS") "WATCHTOWER_HTTP_API_METRICS")
ob.String(HttpApiToken, "", ob.String(HTTPAPIToken, "",
"Sets an authentication token to HTTP API requests.", "Sets an authentication token to HTTP API requests.",
"WATCHTOWER_HTTP_API_TOKEN") "WATCHTOWER_HTTP_API_TOKEN")
ob.Bool(HttpApiPeriodicPolls, false, ob.Bool(HTTPAPIPeriodicPolls, false,
"Also run periodic updates (specified with --interval and --schedule) if HTTP API is enabled", "Also run periodic updates (specified with --interval and --schedule) if HTTP API is enabled",
"WATCHTOWER_HTTP_API_PERIODIC_POLLS") "WATCHTOWER_HTTP_API_PERIODIC_POLLS")
@ -141,7 +141,7 @@ func RegisterSystemOptions(rootCmd *cobra.Command) {
// RegisterNotificationOptions that are used by watchtower to send notifications // RegisterNotificationOptions that are used by watchtower to send notifications
func RegisterNotificationOptions(cmd *cobra.Command) { func RegisterNotificationOptions(cmd *cobra.Command) {
ob := OptBuilder(cmd.PersistentFlags()) ob := NewOptBuilder(cmd.PersistentFlags())
ob.StringSliceP(Notifications, "n", []string{}, ob.StringSliceP(Notifications, "n", []string{},
" Notification types to send (valid: email, slack, msteams, gotify, shoutrrr)", " Notification types to send (valid: email, slack, msteams, gotify, shoutrrr)",
@ -163,7 +163,7 @@ func RegisterNotificationOptions(cmd *cobra.Command) {
"The shoutrrr text/template for the messages", "The shoutrrr text/template for the messages",
"WATCHTOWER_NOTIFICATION_TEMPLATE") "WATCHTOWER_NOTIFICATION_TEMPLATE")
ob.StringArray(NotificationUrl, []string{}, ob.StringArray(NotificationURL, []string{},
"The shoutrrr URL to send notifications to", "The shoutrrr URL to send notifications to",
"WATCHTOWER_NOTIFICATION_URL") "WATCHTOWER_NOTIFICATION_URL")
@ -175,5 +175,5 @@ func RegisterNotificationOptions(cmd *cobra.Command) {
"When to warn about HEAD pull requests failing. Possible values: always, auto or never", "When to warn about HEAD pull requests failing. Possible values: always, auto or never",
"WATCHTOWER_WARN_ON_HEAD_FAILURE") "WATCHTOWER_WARN_ON_HEAD_FAILURE")
RegisterLegacyNotificationFlags(ob.Flags) RegisterLegacyNotificationFlags(cmd)
} }

Loading…
Cancel
Save