forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
39 lines (31 loc) · 889 Bytes
/
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
// 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 (
"flag"
"os"
"strings"
)
func parseFlags() {
parseFlagSetWithArgs(flag.CommandLine, os.Args)
}
func parseFlagSetWithArgs(flagSet *flag.FlagSet, args []string) {
flagsFromEnv(flagSet)
// Skip args[0] as flag.Parse() does.
flagSet.Parse(args[1:])
}
func flagsFromEnv(flagSet *flag.FlagSet) {
flagSet.VisitAll(func(f *flag.Flag) {
envName := flagEnvName(f.Name)
if value, found := os.LookupEnv(envName); found {
f.Value.Set(value)
}
})
}
const flagEnvPrefix = "EPR_"
func flagEnvName(name string) string {
name = strings.ToUpper(name)
name = strings.ReplaceAll(name, "-", "_")
return flagEnvPrefix + name
}