@@ -81,6 +81,11 @@ type Options struct {
8181 DefaultTestAll bool
8282}
8383
84+ type envMatcher struct {
85+ re * regexp.Regexp
86+ action func (matches []string , value string )
87+ }
88+
8489func (o * Options ) addFlags (cmd * cobra.Command ) {
8590 var nfs cliflag.NamedFlagSets
8691
@@ -364,56 +369,81 @@ func (o *Options) assignSelfhosted(envs []string) {
364369 }
365370
366371 initOptions := func (name string ) {
372+ if name == "" {
373+ panic ("Not meant to be empty!" )
374+ }
367375 if o .Client .Selfhosted [name ] == nil {
368376 o .Client .Selfhosted [name ] = new (selfhosted.Options )
369377 }
370378 }
371379
372- regexActions := map [* regexp.Regexp ]func (matches []string , value string ){
373- selfhostedHostReg : func (matches []string , value string ) {
374- initOptions (matches [1 ])
375- o .Client .Selfhosted [matches [1 ]].Host = value
380+ // Go maps iterate in random order - Using a slice to consistency
381+ regexActions := []envMatcher {
382+ {
383+ re : selfhostedTokenPath ,
384+ action : func (matches []string , value string ) {
385+ initOptions (matches [1 ])
386+ o .Client .Selfhosted [matches [1 ]].TokenPath = value
387+ },
376388 },
377- selfhostedUsernameReg : func (matches []string , value string ) {
378- initOptions (matches [1 ])
379- o .Client .Selfhosted [matches [1 ]].Username = value
389+ {
390+ re : selfhostedTokenReg ,
391+ action : func (matches []string , value string ) {
392+ initOptions (matches [1 ])
393+ o .Client .Selfhosted [matches [1 ]].Bearer = value
394+ },
380395 },
381- selfhostedPasswordReg : func (matches []string , value string ) {
382- initOptions (matches [1 ])
383- o .Client .Selfhosted [matches [1 ]].Password = value
396+ // All your other patterns (host, username, password, insecure, capath...)
397+ {
398+ re : selfhostedHostReg ,
399+ action : func (matches []string , value string ) {
400+ initOptions (matches [1 ])
401+ o .Client .Selfhosted [matches [1 ]].Host = value
402+ },
384403 },
385- selfhostedTokenPath : func (matches []string , value string ) {
386- initOptions (matches [1 ])
387- o .Client .Selfhosted [matches [1 ]].TokenPath = value
404+ {
405+ re : selfhostedUsernameReg ,
406+ action : func (matches []string , value string ) {
407+ initOptions (matches [1 ])
408+ o .Client .Selfhosted [matches [1 ]].Username = value
409+ },
388410 },
389- selfhostedTokenReg : func (matches []string , value string ) {
390- initOptions (matches [1 ])
391- o .Client .Selfhosted [matches [1 ]].Bearer = value
411+ {
412+ re : selfhostedPasswordReg ,
413+ action : func (matches []string , value string ) {
414+ initOptions (matches [1 ])
415+ o .Client .Selfhosted [matches [1 ]].Password = value
416+ },
392417 },
393- selfhostedInsecureReg : func (matches []string , value string ) {
394- initOptions (matches [1 ])
395- if val , err := strconv .ParseBool (value ); err == nil {
396- o .Client .Selfhosted [matches [1 ]].Insecure = val
397- }
418+ {
419+ re : selfhostedInsecureReg ,
420+ action : func (matches []string , value string ) {
421+ initOptions (matches [1 ])
422+ if b , err := strconv .ParseBool (value ); err == nil {
423+ o .Client .Selfhosted [matches [1 ]].Insecure = b
424+ }
425+ },
398426 },
399- selfhostedCAPath : func (matches []string , value string ) {
400- initOptions (matches [1 ])
401- o .Client .Selfhosted [matches [1 ]].CAPath = value
427+ {
428+ re : selfhostedCAPath ,
429+ action : func (matches []string , value string ) {
430+ initOptions (matches [1 ])
431+ o .Client .Selfhosted [matches [1 ]].CAPath = value
432+ },
402433 },
403434 }
404435
405436 for _ , env := range envs {
406- pair := strings .SplitN (env , "=" , 2 )
407- if len (pair ) != 2 || len ( pair [1 ]) == 0 {
437+ parts := strings .SplitN (env , "=" , 2 )
438+ if len (parts ) != 2 || parts [1 ] == "" {
408439 continue
409440 }
441+ key := strings .ToUpper (parts [0 ])
442+ val := parts [1 ]
410443
411- key := strings .ToUpper (pair [0 ])
412- value := pair [1 ]
413-
414- for regex , action := range regexActions {
415- if matches := regex .FindStringSubmatch (key ); len (matches ) == 2 {
416- action (matches , value )
444+ for _ , p := range regexActions {
445+ if match := p .re .FindStringSubmatch (key ); len (match ) == 2 {
446+ p .action (match , val )
417447 break
418448 }
419449 }
0 commit comments