Skip to content

Commit 1ad4944

Browse files
author
David Collom
committed
Fix options parsing - ensuring that TOKEN_PATH and TOKEN_XXX aren't mixed
1 parent a193a71 commit 1ad4944

File tree

2 files changed

+77
-52
lines changed

2 files changed

+77
-52
lines changed

cmd/app/options.go

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,26 @@ var (
6464

6565
// Options is a struct to hold options for the version-checker.
6666
type Options struct {
67+
kubeConfigFlags *genericclioptions.ConfigFlags
68+
69+
Client client.Options
6770
MetricsServingAddress string
68-
DefaultTestAll bool
69-
CacheTimeout time.Duration
7071
LogLevel string
7172

72-
PprofBindAddress string
73+
PprofBindAddress string
74+
selfhosted selfhosted.Options
75+
76+
CacheTimeout time.Duration
77+
RequeueDuration time.Duration
7378
GracefulShutdownTimeout time.Duration
7479
CacheSyncPeriod time.Duration
7580

76-
kubeConfigFlags *genericclioptions.ConfigFlags
77-
selfhosted selfhosted.Options
81+
DefaultTestAll bool
82+
}
7883

79-
Client client.Options
84+
type envMatcher struct {
85+
re *regexp.Regexp
86+
action func(matches []string, value string)
8087
}
8188

8289
func (o *Options) addFlags(cmd *cobra.Command) {
@@ -124,6 +131,10 @@ func (o *Options) addAppFlags(fs *pflag.FlagSet) {
124131
"The time for an image version in the cache to be considered fresh. Images "+
125132
"will be rechecked after this interval.")
126133

134+
fs.DurationVarP(&o.RequeueDuration,
135+
"requeue-duration", "r", time.Hour,
136+
"The time a pod will be re-checked for new versions/tags")
137+
127138
fs.StringVarP(&o.LogLevel,
128139
"log-level", "v", "info",
129140
"Log level (debug, info, warn, error, fatal, panic).")
@@ -363,51 +374,73 @@ func (o *Options) assignSelfhosted(envs []string) {
363374
}
364375
}
365376

366-
regexActions := map[*regexp.Regexp]func(matches []string, value string){
367-
selfhostedHostReg: func(matches []string, value string) {
368-
initOptions(matches[1])
369-
o.Client.Selfhosted[matches[1]].Host = value
377+
// Go maps iterate in random order - Using a slice to consistency
378+
regexActions := []envMatcher{
379+
{
380+
re: selfhostedTokenPath,
381+
action: func(matches []string, value string) {
382+
initOptions(matches[1])
383+
o.Client.Selfhosted[matches[1]].TokenPath = value
384+
},
370385
},
371-
selfhostedUsernameReg: func(matches []string, value string) {
372-
initOptions(matches[1])
373-
o.Client.Selfhosted[matches[1]].Username = value
386+
{
387+
re: selfhostedTokenReg,
388+
action: func(matches []string, value string) {
389+
initOptions(matches[1])
390+
o.Client.Selfhosted[matches[1]].Bearer = value
391+
},
374392
},
375-
selfhostedPasswordReg: func(matches []string, value string) {
376-
initOptions(matches[1])
377-
o.Client.Selfhosted[matches[1]].Password = value
393+
// All your other patterns (host, username, password, insecure, capath...)
394+
{
395+
re: selfhostedHostReg,
396+
action: func(matches []string, value string) {
397+
initOptions(matches[1])
398+
o.Client.Selfhosted[matches[1]].Host = value
399+
},
378400
},
379-
selfhostedTokenPath: func(matches []string, value string) {
380-
initOptions(matches[1])
381-
o.Client.Selfhosted[matches[1]].TokenPath = value
401+
{
402+
re: selfhostedUsernameReg,
403+
action: func(matches []string, value string) {
404+
initOptions(matches[1])
405+
o.Client.Selfhosted[matches[1]].Username = value
406+
},
382407
},
383-
selfhostedTokenReg: func(matches []string, value string) {
384-
initOptions(matches[1])
385-
o.Client.Selfhosted[matches[1]].Bearer = value
408+
{
409+
re: selfhostedPasswordReg,
410+
action: func(matches []string, value string) {
411+
initOptions(matches[1])
412+
o.Client.Selfhosted[matches[1]].Password = value
413+
},
386414
},
387-
selfhostedInsecureReg: func(matches []string, value string) {
388-
initOptions(matches[1])
389-
if val, err := strconv.ParseBool(value); err == nil {
390-
o.Client.Selfhosted[matches[1]].Insecure = val
391-
}
415+
{
416+
re: selfhostedInsecureReg,
417+
action: func(matches []string, value string) {
418+
initOptions(matches[1])
419+
if b, err := strconv.ParseBool(value); err == nil {
420+
o.Client.Selfhosted[matches[1]].Insecure = b
421+
}
422+
},
392423
},
393-
selfhostedCAPath: func(matches []string, value string) {
394-
initOptions(matches[1])
395-
o.Client.Selfhosted[matches[1]].CAPath = value
424+
{
425+
re: selfhostedCAPath,
426+
action: func(matches []string, value string) {
427+
initOptions(matches[1])
428+
o.Client.Selfhosted[matches[1]].CAPath = value
429+
},
396430
},
397431
}
398432

399433
for _, env := range envs {
400-
pair := strings.SplitN(env, "=", 2)
401-
if len(pair) != 2 || len(pair[1]) == 0 {
434+
parts := strings.SplitN(env, "=", 2)
435+
if len(parts) != 2 || parts[1] == "" {
402436
continue
403437
}
438+
key := strings.ToUpper(parts[0])
439+
val := parts[1]
404440

405-
key := strings.ToUpper(pair[0])
406-
value := pair[1]
407-
408-
for regex, action := range regexActions {
409-
if matches := regex.FindStringSubmatch(key); len(matches) == 2 {
410-
action(matches, value)
441+
for _, p := range regexActions {
442+
if match := p.re.FindStringSubmatch(key); len(match) == 2 {
443+
p.action(match, val)
411444
break
412445
}
413446
}

cmd/app/options_test.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package app
22

33
import (
44
"os"
5-
"reflect"
65
"testing"
76

7+
"github.com/stretchr/testify/assert"
8+
89
"github.com/jetstack/version-checker/pkg/client"
910
"github.com/jetstack/version-checker/pkg/client/acr"
1011
"github.com/jetstack/version-checker/pkg/client/docker"
@@ -181,10 +182,7 @@ func TestComplete(t *testing.T) {
181182
o := new(Options)
182183
o.complete()
183184

184-
if !reflect.DeepEqual(o.Client, test.expOptions) {
185-
t.Errorf("unexpected client options, exp=%#+v got=%#+v",
186-
test.expOptions, o.Client)
187-
}
185+
assert.Exactly(t, test.expOptions, o.Client)
188186
})
189187
}
190188
}
@@ -201,7 +199,7 @@ func TestInvalidSelfhostedPanic(t *testing.T) {
201199
}
202200
for name, test := range tests {
203201
t.Run(name, func(t *testing.T) {
204-
defer func() { recover() }()
202+
defer func() { _ = recover() }()
205203

206204
o := new(Options)
207205
o.assignSelfhosted(test.envs)
@@ -236,10 +234,7 @@ func TestInvalidSelfhostedOpts(t *testing.T) {
236234

237235
valid := validSelfHostedOpts(&test.opts)
238236

239-
if !reflect.DeepEqual(test.valid, valid) {
240-
t.Errorf("unexpected selfhosted valid options, exp=%#+v got=%#+v",
241-
test.valid, valid)
242-
}
237+
assert.Equal(t, test.valid, valid)
243238
})
244239
}
245240
}
@@ -360,10 +355,7 @@ func TestAssignSelfhosted(t *testing.T) {
360355
o := new(Options)
361356
o.assignSelfhosted(test.envs)
362357

363-
if !reflect.DeepEqual(o.Client.Selfhosted, test.expOptions.Selfhosted) {
364-
t.Errorf("unexpected client selfhosted options, exp=%#+v got=%#+v",
365-
test.expOptions.Selfhosted, o.Client.Selfhosted)
366-
}
358+
assert.Exactly(t, test.expOptions.Selfhosted, o.Client.Selfhosted)
367359
})
368360
}
369361
}

0 commit comments

Comments
 (0)