forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
82 lines (70 loc) · 1.77 KB
/
flags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"crypto/tls"
"errors"
"flag"
"fmt"
"os"
"strings"
)
var supportedTLSVersions map[string]uint16 = map[string]uint16{
"1.0": tls.VersionTLS10,
"1.1": tls.VersionTLS11,
"1.2": tls.VersionTLS12,
"1.3": tls.VersionTLS13,
}
type tlsVersionValue uint16
func (t tlsVersionValue) String() string {
switch t {
case tls.VersionTLS10:
return "1.0"
case tls.VersionTLS11:
return "1.1"
case tls.VersionTLS12:
return "1.2"
case tls.VersionTLS13:
return "1.3"
default:
return ""
}
}
func (t *tlsVersionValue) Set(s string) error {
if _, ok := supportedTLSVersions[s]; !ok {
return fmt.Errorf("unsupported TLS version: %s", s)
}
*t = tlsVersionValue(supportedTLSVersions[s])
return nil
}
func parseFlags() error {
return parseFlagSetWithArgs(flag.CommandLine, os.Args)
}
func parseFlagSetWithArgs(flagSet *flag.FlagSet, args []string) error {
err := flagsFromEnv(flagSet)
if err != nil {
return err
}
// Skip args[0] as flag.Parse() does.
flagSet.Parse(args[1:])
return nil
}
func flagsFromEnv(flagSet *flag.FlagSet) error {
var flagErrors error
flagSet.VisitAll(func(f *flag.Flag) {
envName := flagEnvName(f.Name)
if value, found := os.LookupEnv(envName); found {
if err := f.Value.Set(value); err != nil {
flagErrors = errors.Join(flagErrors, fmt.Errorf("failed to set -%s: %v", f.Name, err))
}
}
})
return flagErrors
}
const flagEnvPrefix = "EPR_"
func flagEnvName(name string) string {
name = strings.ToUpper(name)
name = strings.ReplaceAll(name, "-", "_")
return flagEnvPrefix + name
}