Skip to content

Commit 26efdbe

Browse files
authored
Fix options parsing - ensuring that TOKEN_PATH and TOKEN_XXX aren't mixed (#367)
1 parent c5d890e commit 26efdbe

File tree

2 files changed

+74
-52
lines changed

2 files changed

+74
-52
lines changed

cmd/app/options.go

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,25 @@ 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
7377
GracefulShutdownTimeout time.Duration
7478
CacheSyncPeriod time.Duration
7579

76-
kubeConfigFlags *genericclioptions.ConfigFlags
77-
selfhosted selfhosted.Options
80+
DefaultTestAll bool
81+
}
7882

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

8288
func (o *Options) addFlags(cmd *cobra.Command) {
@@ -363,59 +369,83 @@ func (o *Options) assignSelfhosted(envs []string) {
363369
}
364370
}
365371

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
372+
// Go maps iterate in random order - Using a slice to consistency
373+
regexActions := []envMatcher{
374+
{
375+
re: selfhostedTokenPath,
376+
action: func(matches []string, value string) {
377+
initOptions(matches[1])
378+
o.Client.Selfhosted[matches[1]].TokenPath = value
379+
},
370380
},
371-
selfhostedUsernameReg: func(matches []string, value string) {
372-
initOptions(matches[1])
373-
o.Client.Selfhosted[matches[1]].Username = value
381+
{
382+
re: selfhostedTokenReg,
383+
action: func(matches []string, value string) {
384+
initOptions(matches[1])
385+
o.Client.Selfhosted[matches[1]].Bearer = value
386+
},
374387
},
375-
selfhostedPasswordReg: func(matches []string, value string) {
376-
initOptions(matches[1])
377-
o.Client.Selfhosted[matches[1]].Password = value
388+
// All your other patterns (host, username, password, insecure, capath...)
389+
{
390+
re: selfhostedHostReg,
391+
action: func(matches []string, value string) {
392+
initOptions(matches[1])
393+
o.Client.Selfhosted[matches[1]].Host = value
394+
},
378395
},
379-
selfhostedTokenPath: func(matches []string, value string) {
380-
initOptions(matches[1])
381-
o.Client.Selfhosted[matches[1]].TokenPath = value
396+
{
397+
re: selfhostedUsernameReg,
398+
action: func(matches []string, value string) {
399+
initOptions(matches[1])
400+
o.Client.Selfhosted[matches[1]].Username = value
401+
},
382402
},
383-
selfhostedTokenReg: func(matches []string, value string) {
384-
initOptions(matches[1])
385-
o.Client.Selfhosted[matches[1]].Bearer = value
403+
{
404+
re: selfhostedPasswordReg,
405+
action: func(matches []string, value string) {
406+
initOptions(matches[1])
407+
o.Client.Selfhosted[matches[1]].Password = value
408+
},
386409
},
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-
}
410+
{
411+
re: selfhostedInsecureReg,
412+
action: func(matches []string, value string) {
413+
initOptions(matches[1])
414+
if b, err := strconv.ParseBool(value); err == nil {
415+
o.Client.Selfhosted[matches[1]].Insecure = b
416+
}
417+
},
392418
},
393-
selfhostedCAPath: func(matches []string, value string) {
394-
initOptions(matches[1])
395-
o.Client.Selfhosted[matches[1]].CAPath = value
419+
{
420+
re: selfhostedCAPath,
421+
action: func(matches []string, value string) {
422+
initOptions(matches[1])
423+
o.Client.Selfhosted[matches[1]].CAPath = value
424+
},
396425
},
397426
}
398427

399428
for _, env := range envs {
400-
pair := strings.SplitN(env, "=", 2)
401-
if len(pair) != 2 || len(pair[1]) == 0 {
429+
parts := strings.SplitN(env, "=", 2)
430+
if len(parts) != 2 || parts[1] == "" {
402431
continue
403432
}
433+
key := strings.ToUpper(parts[0])
434+
val := parts[1]
404435

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)
436+
for _, p := range regexActions {
437+
if match := p.re.FindStringSubmatch(key); len(match) == 2 {
438+
p.action(match, val)
411439
break
412440
}
413441
}
414442
}
415443

444+
// If we have some selfhosted flags, lets set them here...
416445
if len(o.selfhosted.Host) > 0 {
417446
o.Client.Selfhosted[o.selfhosted.Host] = &o.selfhosted
418447
}
448+
419449
if !validSelfHostedOpts(o) {
420450
panic(fmt.Errorf("invalid self hosted configuration"))
421451
}

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)