|
60 | 60 | selfhostedInsecureReg = regexp.MustCompile("^VERSION_CHECKER_SELFHOSTED_INSECURE_(.*)")
|
61 | 61 | )
|
62 | 62 |
|
63 |
| -// Options is a struct to hold options for the version-checker |
| 63 | +// Options is a struct to hold options for the version-checker. |
64 | 64 | type Options struct {
|
65 | 65 | MetricsServingAddress string
|
66 | 66 | DefaultTestAll bool
|
@@ -88,7 +88,7 @@ func (o *Options) addFlags(cmd *cobra.Command) {
|
88 | 88 | return nil
|
89 | 89 | })
|
90 | 90 |
|
91 |
| - cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { |
| 91 | + cmd.SetHelpFunc(func(cmd *cobra.Command, _ []string) { |
92 | 92 | fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine())
|
93 | 93 | cliflag.PrintSections(cmd.OutOrStdout(), nfs, 0)
|
94 | 94 | })
|
@@ -329,55 +329,53 @@ func (o *Options) assignSelfhosted(envs []string) {
|
329 | 329 | }
|
330 | 330 | }
|
331 | 331 |
|
332 |
| - for _, env := range envs { |
333 |
| - pair := strings.SplitN(env, "=", 2) |
334 |
| - if len(pair) != 2 || len(pair[1]) == 0 { |
335 |
| - continue |
336 |
| - } |
337 |
| - |
338 |
| - if matches := selfhostedHostReg.FindStringSubmatch(strings.ToUpper(pair[0])); len(matches) == 2 { |
| 332 | + regexActions := map[*regexp.Regexp]func(matches []string, value string){ |
| 333 | + selfhostedHostReg: func(matches []string, value string) { |
339 | 334 | initOptions(matches[1])
|
340 |
| - o.Client.Selfhosted[matches[1]].Host = pair[1] |
341 |
| - continue |
342 |
| - } |
343 |
| - |
344 |
| - if matches := selfhostedUsernameReg.FindStringSubmatch(strings.ToUpper(pair[0])); len(matches) == 2 { |
| 335 | + o.Client.Selfhosted[matches[1]].Host = value |
| 336 | + }, |
| 337 | + selfhostedUsernameReg: func(matches []string, value string) { |
345 | 338 | initOptions(matches[1])
|
346 |
| - o.Client.Selfhosted[matches[1]].Username = pair[1] |
347 |
| - continue |
348 |
| - } |
349 |
| - |
350 |
| - if matches := selfhostedPasswordReg.FindStringSubmatch(strings.ToUpper(pair[0])); len(matches) == 2 { |
| 339 | + o.Client.Selfhosted[matches[1]].Username = value |
| 340 | + }, |
| 341 | + selfhostedPasswordReg: func(matches []string, value string) { |
351 | 342 | initOptions(matches[1])
|
352 |
| - o.Client.Selfhosted[matches[1]].Password = pair[1] |
353 |
| - continue |
354 |
| - } |
355 |
| - |
356 |
| - if matches := selfhostedTokenPath.FindStringSubmatch(strings.ToUpper(pair[0])); len(matches) == 2 { |
| 343 | + o.Client.Selfhosted[matches[1]].Password = value |
| 344 | + }, |
| 345 | + selfhostedTokenPath: func(matches []string, value string) { |
357 | 346 | initOptions(matches[1])
|
358 |
| - o.Client.Selfhosted[matches[1]].TokenPath = pair[1] |
359 |
| - continue |
360 |
| - } |
361 |
| - |
362 |
| - if matches := selfhostedTokenReg.FindStringSubmatch(strings.ToUpper(pair[0])); len(matches) == 2 { |
| 347 | + o.Client.Selfhosted[matches[1]].TokenPath = value |
| 348 | + }, |
| 349 | + selfhostedTokenReg: func(matches []string, value string) { |
363 | 350 | initOptions(matches[1])
|
364 |
| - o.Client.Selfhosted[matches[1]].Bearer = pair[1] |
365 |
| - continue |
366 |
| - } |
367 |
| - |
368 |
| - if matches := selfhostedInsecureReg.FindStringSubmatch(strings.ToUpper(pair[0])); len(matches) == 2 { |
| 351 | + o.Client.Selfhosted[matches[1]].Bearer = value |
| 352 | + }, |
| 353 | + selfhostedInsecureReg: func(matches []string, value string) { |
369 | 354 | initOptions(matches[1])
|
370 |
| - val, err := strconv.ParseBool(pair[1]) |
371 |
| - if err == nil { |
| 355 | + if val, err := strconv.ParseBool(value); err == nil { |
372 | 356 | o.Client.Selfhosted[matches[1]].Insecure = val
|
373 | 357 | }
|
| 358 | + }, |
| 359 | + selfhostedCAPath: func(matches []string, value string) { |
| 360 | + initOptions(matches[1]) |
| 361 | + o.Client.Selfhosted[matches[1]].CAPath = value |
| 362 | + }, |
| 363 | + } |
| 364 | + |
| 365 | + for _, env := range envs { |
| 366 | + pair := strings.SplitN(env, "=", 2) |
| 367 | + if len(pair) != 2 || len(pair[1]) == 0 { |
374 | 368 | continue
|
375 | 369 | }
|
376 | 370 |
|
377 |
| - if matches := selfhostedCAPath.FindStringSubmatch(strings.ToUpper(pair[0])); len(matches) == 2 { |
378 |
| - initOptions(matches[1]) |
379 |
| - o.Client.Selfhosted[matches[1]].CAPath = pair[1] |
380 |
| - continue |
| 371 | + key := strings.ToUpper(pair[0]) |
| 372 | + value := pair[1] |
| 373 | + |
| 374 | + for regex, action := range regexActions { |
| 375 | + if matches := regex.FindStringSubmatch(key); len(matches) == 2 { |
| 376 | + action(matches, value) |
| 377 | + break |
| 378 | + } |
381 | 379 | }
|
382 | 380 | }
|
383 | 381 |
|
|
0 commit comments