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

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

@ -1,12 +1,12 @@
package config
import (
"github.com/spf13/pflag"
"github.com/spf13/cobra"
)
// RegisterLegacyNotificationFlags registers all the flags related to the old notification system
func RegisterLegacyNotificationFlags(flags *pflag.FlagSet) {
ob := OptBuilder(flags)
func RegisterLegacyNotificationFlags(cmd *cobra.Command) {
ob := NewOptBuilder(cmd.PersistentFlags())
// Hide all legacy notification flags from the `--help` to reduce clutter
ob.Hide = true
@ -27,7 +27,7 @@ func RegisterLegacyNotificationFlags(flags *pflag.FlagSet) {
ob.Int(NotificationEmailServerPort, 25,
"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.
Should only be used for testing.`,
"WATCHTOWER_NOTIFICATION_EMAIL_SERVER_TLS_SKIP_VERIFY")
@ -40,11 +40,11 @@ Should only be used for testing.`,
"SMTP server password for sending notifications",
"WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD")
ob.String(NotificationEmailSubjecttag, "",
ob.String(NotificationEmailSubjectTag, "",
"Subject prefix tag for notifications via mail",
"WATCHTOWER_NOTIFICATION_EMAIL_SUBJECTTAG")
ob.String(NotificationSlackHookUrl, "",
ob.String(NotificationSlackHookURL, "",
"The Slack Hook URL to send notifications to",
"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",
"WATCHTOWER_NOTIFICATION_SLACK_ICON_EMOJI")
ob.String(NotificationSlackIconUrl, "",
ob.String(NotificationSlackIconURL, "",
"An icon image URL string to use in place of the default icon",
"WATCHTOWER_NOTIFICATION_SLACK_ICON_URL")
ob.String(NotificationMsteamsHook, "",
ob.String(NotificationMSTeamsHook, "",
"The MSTeams WebHook URL to send notifications to",
"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",
"WATCHTOWER_NOTIFICATION_MSTEAMS_USE_LOG_DATA")
ob.String(NotificationGotifyUrl, "",
ob.String(NotificationGotifyURL, "",
"The Gotify URL to send notifications to", "WATCHTOWER_NOTIFICATION_GOTIFY_URL")
ob.String(NotificationGotifyToken, "",
"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.
Should only be used for testing.`,
"WATCHTOWER_NOTIFICATION_GOTIFY_TLS_SKIP_VERIFY")

@ -6,82 +6,98 @@ import (
"time"
)
type optBuilder struct {
// OptBuilder is a helper for registering options to both pflags, viper and env
type OptBuilder struct {
Flags *pflag.FlagSet
Hide bool
}
func OptBuilder(flags *pflag.FlagSet) *optBuilder {
return &optBuilder{
// NewOptBuilder returns a new OptBuilder with the supplied flags
func NewOptBuilder(flags *pflag.FlagSet) *OptBuilder {
return &OptBuilder{
Flags: flags,
}
}
func (ob *optBuilder) register(key string, env string) {
func (ob *OptBuilder) register(key string, env string) {
_ = viper.BindEnv(key, env)
if ob.Hide {
_ = 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.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.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.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.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)
}
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)
}
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)
}
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.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.register(string(key), env)
}
// GetString returns the string option value for the given key
func GetString(key stringConfKey) string {
return viper.GetString(string(key))
}
// GetBool returns the bool option value for the given key
func GetBool(key boolConfKey) bool {
return viper.GetBool(string(key))
}
// GetInt returns the int option value for the given key
func GetInt(key intConfKey) int {
return viper.GetInt(string(key))
}
// GetDuration returns the duration option value for the given key
func GetDuration(key durationConfKey) time.Duration {
return viper.GetDuration(string(key))
}
// GetSlice returns the slice option value for the given key
func GetSlice(key sliceConfKey) []string {
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
func RegisterDockerOptions(rootCmd *cobra.Command) {
ob := OptBuilder(rootCmd.PersistentFlags())
ob := NewOptBuilder(rootCmd.PersistentFlags())
ob.StringP(DockerHost, "H", "unix:///var/run/docker.sock",
"daemon socket to connect to",
@ -25,14 +25,14 @@ func RegisterDockerOptions(rootCmd *cobra.Command) {
"use TLS and verify the remote",
"DOCKER_TLS_VERIFY")
ob.StringP(DockerApiVersion, "a", DockerAPIMinVersion,
ob.StringP(DockerAPIVersion, "a", DockerAPIMinVersion,
"api version to use by docker client",
"DOCKER_API_VERSION")
}
// RegisterSystemOptions that are used by watchtower to modify the program flow
func RegisterSystemOptions(rootCmd *cobra.Command) {
ob := OptBuilder(rootCmd.PersistentFlags())
ob := NewOptBuilder(rootCmd.PersistentFlags())
ob.IntP(Interval, "i", DefaultInterval,
"poll interval (in seconds)",
@ -113,19 +113,19 @@ func RegisterSystemOptions(rootCmd *cobra.Command) {
"Restart containers one at a time",
"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",
"WATCHTOWER_HTTP_API_UPDATE")
ob.Bool(HttpApiMetrics, false,
ob.Bool(HTTPAPIMetrics, false,
"Runs Watchtower with the Prometheus metrics API enabled",
"WATCHTOWER_HTTP_API_METRICS")
ob.String(HttpApiToken, "",
ob.String(HTTPAPIToken, "",
"Sets an authentication token to HTTP API requests.",
"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",
"WATCHTOWER_HTTP_API_PERIODIC_POLLS")
@ -141,7 +141,7 @@ func RegisterSystemOptions(rootCmd *cobra.Command) {
// RegisterNotificationOptions that are used by watchtower to send notifications
func RegisterNotificationOptions(cmd *cobra.Command) {
ob := OptBuilder(cmd.PersistentFlags())
ob := NewOptBuilder(cmd.PersistentFlags())
ob.StringSliceP(Notifications, "n", []string{},
" 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",
"WATCHTOWER_NOTIFICATION_TEMPLATE")
ob.StringArray(NotificationUrl, []string{},
ob.StringArray(NotificationURL, []string{},
"The shoutrrr URL to send notifications to",
"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",
"WATCHTOWER_WARN_ON_HEAD_FAILURE")
RegisterLegacyNotificationFlags(ob.Flags)
RegisterLegacyNotificationFlags(cmd)
}

Loading…
Cancel
Save