fix(flags): detect schedule set from env (#1373)

pull/1375/head
nils måsén 2 years ago committed by GitHub
parent e04a107694
commit f047d75dcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,6 +19,8 @@ import (
// use watchtower // use watchtower
const DockerAPIMinVersion string = "1.25" const DockerAPIMinVersion string = "1.25"
var defaultInterval = int((time.Hour * 24).Seconds())
// RegisterDockerFlags that are used directly by the docker api client // RegisterDockerFlags that are used directly by the docker api client
func RegisterDockerFlags(rootCmd *cobra.Command) { func RegisterDockerFlags(rootCmd *cobra.Command) {
flags := rootCmd.PersistentFlags() flags := rootCmd.PersistentFlags()
@ -362,11 +364,10 @@ Should only be used for testing.`)
// SetDefaults provides default values for environment variables // SetDefaults provides default values for environment variables
func SetDefaults() { func SetDefaults() {
day := (time.Hour * 24).Seconds()
viper.AutomaticEnv() viper.AutomaticEnv()
viper.SetDefault("DOCKER_HOST", "unix:///var/run/docker.sock") viper.SetDefault("DOCKER_HOST", "unix:///var/run/docker.sock")
viper.SetDefault("DOCKER_API_VERSION", DockerAPIMinVersion) viper.SetDefault("DOCKER_API_VERSION", DockerAPIMinVersion)
viper.SetDefault("WATCHTOWER_POLL_INTERVAL", day) viper.SetDefault("WATCHTOWER_POLL_INTERVAL", defaultInterval)
viper.SetDefault("WATCHTOWER_TIMEOUT", time.Second*10) viper.SetDefault("WATCHTOWER_TIMEOUT", time.Second*10)
viper.SetDefault("WATCHTOWER_NOTIFICATIONS", []string{}) viper.SetDefault("WATCHTOWER_NOTIFICATIONS", []string{})
viper.SetDefault("WATCHTOWER_NOTIFICATIONS_LEVEL", "info") viper.SetDefault("WATCHTOWER_NOTIFICATIONS_LEVEL", "info")
@ -540,12 +541,23 @@ func ProcessFlagAliases(flags *pflag.FlagSet) {
setFlagIfDefault(flags, `notification-template`, tpl) setFlagIfDefault(flags, `notification-template`, tpl)
} }
if flags.Changed(`interval`) && flags.Changed(`schedule`) { scheduleChanged := flags.Changed(`schedule`)
intervalChanged := flags.Changed(`interval`)
// FIXME: snakeswap
// due to how viper is integrated by swapping the defaults for the flags, we need this hack:
if val, _ := flags.GetString(`schedule`); val != `` {
scheduleChanged = true
}
if val, _ := flags.GetInt(`interval`); val != defaultInterval {
intervalChanged = true
}
if intervalChanged && scheduleChanged {
log.Fatal(`Only schedule or interval can be defined, not both.`) log.Fatal(`Only schedule or interval can be defined, not both.`)
} }
// update schedule flag to match interval if it's set, or to the default if none of them are // update schedule flag to match interval if it's set, or to the default if none of them are
if flags.Changed(`interval`) || !flags.Changed(`schedule`) { if intervalChanged || !scheduleChanged {
interval, _ := flags.GetInt(`interval`) interval, _ := flags.GetInt(`interval`)
flags.Set(`schedule`, fmt.Sprintf(`@every %ds`, interval)) flags.Set(`schedule`, fmt.Sprintf(`@every %ds`, interval))
} }

@ -173,7 +173,7 @@ func TestProcessFlagAliasesSchedAndInterval(t *testing.T) {
RegisterSystemFlags(cmd) RegisterSystemFlags(cmd)
RegisterNotificationFlags(cmd) RegisterNotificationFlags(cmd)
require.NoError(t, cmd.ParseFlags([]string{`--schedule`, `@now`, `--interval`, `10`})) require.NoError(t, cmd.ParseFlags([]string{`--schedule`, `@hourly`, `--interval`, `10`}))
flags := cmd.Flags() flags := cmd.Flags()
assert.PanicsWithValue(t, `FATAL`, func() { assert.PanicsWithValue(t, `FATAL`, func() {
@ -181,6 +181,26 @@ func TestProcessFlagAliasesSchedAndInterval(t *testing.T) {
}) })
} }
func TestProcessFlagAliasesScheduleFromEnvironment(t *testing.T) {
cmd := new(cobra.Command)
err := os.Setenv("WATCHTOWER_SCHEDULE", `@hourly`)
require.NoError(t, err)
defer os.Unsetenv("WATCHTOWER_SCHEDULE")
SetDefaults()
RegisterDockerFlags(cmd)
RegisterSystemFlags(cmd)
RegisterNotificationFlags(cmd)
require.NoError(t, cmd.ParseFlags([]string{}))
flags := cmd.Flags()
ProcessFlagAliases(flags)
sched, _ := flags.GetString(`schedule`)
assert.Equal(t, `@hourly`, sched)
}
func TestProcessFlagAliasesInvalidPorcelaineVersion(t *testing.T) { func TestProcessFlagAliasesInvalidPorcelaineVersion(t *testing.T) {
logrus.StandardLogger().ExitFunc = func(_ int) { panic(`FATAL`) } logrus.StandardLogger().ExitFunc = func(_ int) { panic(`FATAL`) }
cmd := new(cobra.Command) cmd := new(cobra.Command)

Loading…
Cancel
Save