mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
pkg: move SetFlagsFromEnv to pkg package
This commit is contained in:
parent
b0617be7e3
commit
e30c1eeefd
23
main.go
23
main.go
@ -95,7 +95,7 @@ func main() {
|
|||||||
flag.Usage = pkg.UsageWithIgnoredFlagsFunc(flag.CommandLine, deprecated)
|
flag.Usage = pkg.UsageWithIgnoredFlagsFunc(flag.CommandLine, deprecated)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
SetFlagsFromEnv(flag.CommandLine)
|
pkg.SetFlagsFromEnv(flag.CommandLine)
|
||||||
|
|
||||||
if string(*proxyFlag) == proxyFlagValueOff {
|
if string(*proxyFlag) == proxyFlagValueOff {
|
||||||
startEtcd()
|
startEtcd()
|
||||||
@ -328,24 +328,3 @@ func (pf *ProxyFlag) Set(s string) error {
|
|||||||
func (pf *ProxyFlag) String() string {
|
func (pf *ProxyFlag) String() string {
|
||||||
return string(*pf)
|
return string(*pf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetFlagsFromEnv parses all registered flags in the given flagset,
|
|
||||||
// and if they are not already set it attempts to set their values from
|
|
||||||
// environment variables. Environment variables take the name of the flag but
|
|
||||||
// are UPPERCASE, have the prefix "ETCD_", and any dashes are replaced by
|
|
||||||
// underscores - for example: some-flag => ETCD_SOME_FLAG
|
|
||||||
func SetFlagsFromEnv(fs *flag.FlagSet) {
|
|
||||||
alreadySet := make(map[string]bool)
|
|
||||||
fs.Visit(func(f *flag.Flag) {
|
|
||||||
alreadySet[f.Name] = true
|
|
||||||
})
|
|
||||||
fs.VisitAll(func(f *flag.Flag) {
|
|
||||||
if !alreadySet[f.Name] {
|
|
||||||
key := "ETCD_" + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
|
|
||||||
val := os.Getenv(key)
|
|
||||||
if val != "" {
|
|
||||||
fs.Set(f.Name, val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
50
main_test.go
50
main_test.go
@ -1,52 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
import "flag"
|
"testing"
|
||||||
import "testing"
|
)
|
||||||
|
|
||||||
func TestSetFlagsFromEnv(t *testing.T) {
|
|
||||||
fs := flag.NewFlagSet("testing", flag.ExitOnError)
|
|
||||||
fs.String("a", "", "")
|
|
||||||
fs.String("b", "", "")
|
|
||||||
fs.String("c", "", "")
|
|
||||||
fs.Parse([]string{})
|
|
||||||
|
|
||||||
os.Clearenv()
|
|
||||||
// flags should be settable using env vars
|
|
||||||
os.Setenv("ETCD_A", "foo")
|
|
||||||
// and command-line flags
|
|
||||||
if err := fs.Set("b", "bar"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// command-line flags take precedence over env vars
|
|
||||||
os.Setenv("ETCD_C", "woof")
|
|
||||||
if err := fs.Set("c", "quack"); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// first verify that flags are as expected before reading the env
|
|
||||||
for f, want := range map[string]string{
|
|
||||||
"a": "",
|
|
||||||
"b": "bar",
|
|
||||||
"c": "quack",
|
|
||||||
} {
|
|
||||||
if got := fs.Lookup(f).Value.String(); got != want {
|
|
||||||
t.Fatalf("flag %q=%q, want %q", f, got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// now read the env and verify flags were updated as expected
|
|
||||||
SetFlagsFromEnv(fs)
|
|
||||||
for f, want := range map[string]string{
|
|
||||||
"a": "foo",
|
|
||||||
"b": "bar",
|
|
||||||
"c": "quack",
|
|
||||||
} {
|
|
||||||
if got := fs.Lookup(f).Value.String(); got != want {
|
|
||||||
t.Errorf("flag %q=%q, want %q", f, got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestProxyFlagSet(t *testing.T) {
|
func TestProxyFlagSet(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
22
pkg/flag.go
22
pkg/flag.go
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeprecatedFlag struct {
|
type DeprecatedFlag struct {
|
||||||
@ -42,3 +43,24 @@ func UsageWithIgnoredFlagsFunc(fs *flag.FlagSet, ignore []string) func() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetFlagsFromEnv parses all registered flags in the given flagset,
|
||||||
|
// and if they are not already set it attempts to set their values from
|
||||||
|
// environment variables. Environment variables take the name of the flag but
|
||||||
|
// are UPPERCASE, have the prefix "ETCD_", and any dashes are replaced by
|
||||||
|
// underscores - for example: some-flag => ETCD_SOME_FLAG
|
||||||
|
func SetFlagsFromEnv(fs *flag.FlagSet) {
|
||||||
|
alreadySet := make(map[string]bool)
|
||||||
|
fs.Visit(func(f *flag.Flag) {
|
||||||
|
alreadySet[f.Name] = true
|
||||||
|
})
|
||||||
|
fs.VisitAll(func(f *flag.Flag) {
|
||||||
|
if !alreadySet[f.Name] {
|
||||||
|
key := "ETCD_" + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
|
||||||
|
val := os.Getenv(key)
|
||||||
|
if val != "" {
|
||||||
|
fs.Set(f.Name, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
51
pkg/flag_test.go
Normal file
51
pkg/flag_test.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetFlagsFromEnv(t *testing.T) {
|
||||||
|
fs := flag.NewFlagSet("testing", flag.ExitOnError)
|
||||||
|
fs.String("a", "", "")
|
||||||
|
fs.String("b", "", "")
|
||||||
|
fs.String("c", "", "")
|
||||||
|
fs.Parse([]string{})
|
||||||
|
|
||||||
|
os.Clearenv()
|
||||||
|
// flags should be settable using env vars
|
||||||
|
os.Setenv("ETCD_A", "foo")
|
||||||
|
// and command-line flags
|
||||||
|
if err := fs.Set("b", "bar"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// command-line flags take precedence over env vars
|
||||||
|
os.Setenv("ETCD_C", "woof")
|
||||||
|
if err := fs.Set("c", "quack"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// first verify that flags are as expected before reading the env
|
||||||
|
for f, want := range map[string]string{
|
||||||
|
"a": "",
|
||||||
|
"b": "bar",
|
||||||
|
"c": "quack",
|
||||||
|
} {
|
||||||
|
if got := fs.Lookup(f).Value.String(); got != want {
|
||||||
|
t.Fatalf("flag %q=%q, want %q", f, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// now read the env and verify flags were updated as expected
|
||||||
|
SetFlagsFromEnv(fs)
|
||||||
|
for f, want := range map[string]string{
|
||||||
|
"a": "foo",
|
||||||
|
"b": "bar",
|
||||||
|
"c": "quack",
|
||||||
|
} {
|
||||||
|
if got := fs.Lookup(f).Value.String(); got != want {
|
||||||
|
t.Errorf("flag %q=%q, want %q", f, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
4
test
4
test
@ -15,9 +15,9 @@ COVER=${COVER:-"-cover"}
|
|||||||
source ./build
|
source ./build
|
||||||
|
|
||||||
# Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
|
# Hack: gofmt ./ will recursively check the .git directory. So use *.go for gofmt.
|
||||||
TESTABLE_AND_FORMATTABLE="client etcdserver etcdserver/etcdhttp etcdserver/etcdserverpb functional proxy raft snap store wait wal transport"
|
TESTABLE_AND_FORMATTABLE="client etcdserver etcdserver/etcdhttp etcdserver/etcdserverpb functional proxy raft snap store wait wal transport pkg"
|
||||||
TESTABLE="$TESTABLE_AND_FORMATTABLE ./"
|
TESTABLE="$TESTABLE_AND_FORMATTABLE ./"
|
||||||
FORMATTABLE="$TESTABLE_AND_FORMATTABLE *.go pkg"
|
FORMATTABLE="$TESTABLE_AND_FORMATTABLE *.go"
|
||||||
|
|
||||||
# user has not provided PKG override
|
# user has not provided PKG override
|
||||||
if [ -z "$PKG" ]; then
|
if [ -z "$PKG" ]; then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user