veyron/lib/flags: have default flag values be accurate and to reflect the environment.
Arrange it so that the default values show by PrintDefaults reflect
the default name space root or the settings in the environment if
there are any.
Also, fix command line usage for the namespace tool.
Change-Id: I8ef687abf17fe4c590819f0ca59545046eed355c
diff --git a/lib/flags/flags.go b/lib/flags/flags.go
index 53a501d..0e664e4 100644
--- a/lib/flags/flags.go
+++ b/lib/flags/flags.go
@@ -24,6 +24,8 @@
Listen
)
+const defaultNamespaceRoot = "/proxy.envyor.com:8101"
+
// Flags represents the set of flag groups created by a call to
// CreateAndRegister.
type Flags struct {
@@ -32,6 +34,7 @@
}
type namespaceRootFlagVar struct {
+ isSet bool // is true when a flag have has been explicitly set.
roots []string
}
@@ -40,6 +43,11 @@
}
func (nsr *namespaceRootFlagVar) Set(v string) error {
+ if !nsr.isSet {
+ // override the default value and
+ nsr.isSet = true
+ nsr.roots = []string{}
+ }
nsr.roots = append(nsr.roots, v)
return nil
}
@@ -69,8 +77,14 @@
// group with the supplied flag.FlagSet.
func createAndRegisterRuntimeFlags(fs *flag.FlagSet) *RuntimeFlags {
f := &RuntimeFlags{}
+ roots, creds := readEnv()
+ if len(roots) == 0 {
+ f.namespaceRootsFlag.roots = []string{defaultNamespaceRoot}
+ } else {
+ f.namespaceRootsFlag.roots = roots
+ }
fs.Var(&f.namespaceRootsFlag, "veyron.namespace.root", "local namespace root; can be repeated to provided multiple roots")
- fs.StringVar(&f.Credentials, "veyron.credentials", "", "directory to use for storing security credentials")
+ fs.StringVar(&f.Credentials, "veyron.credentials", creds, "directory to use for storing security credentials")
return f
}
@@ -141,9 +155,9 @@
return f.FlagSet.Args()
}
-// legacyEnvInit provides support for the legacy NAMESPACE_ROOT? and
-// VEYRON_CREDENTIALS env vars.
-func (es *RuntimeFlags) legacyEnvInit() {
+// readEnv reads the legacy NAMESPACE_ROOT? and VEYRON_CREDENTIALS env vars.
+func readEnv() ([]string, string) {
+ roots := []string{}
for _, ev := range os.Environ() {
p := strings.SplitN(ev, "=", 2)
if len(p) != 2 {
@@ -151,36 +165,34 @@
}
k, v := p[0], p[1]
if strings.HasPrefix(k, "NAMESPACE_ROOT") && len(v) > 0 {
- es.NamespaceRoots = append(es.NamespaceRoots, v)
+ roots = append(roots, v)
}
}
- if creds := os.Getenv("VEYRON_CREDENTIALS"); creds != "" {
- es.Credentials = creds
- }
+ creds := os.Getenv("VEYRON_CREDENTIALS")
+ return roots, creds
}
-const defaultNamespaceRoot = "/proxy.envyor.com:8101"
-
// Parse parses the supplied args, as per flag.Parse
func (f *Flags) Parse(args []string) error {
- hasrt := f.groups[Runtime] != nil
- if hasrt {
- f.groups[Runtime].(*RuntimeFlags).legacyEnvInit()
- }
-
// TODO(cnicolaou): implement a single env var 'VANADIUM_OPTS'
// that can be used to specify any command line.
if err := f.FlagSet.Parse(args); err != nil {
return err
}
+ hasrt := f.groups[Runtime] != nil
if hasrt {
runtime := f.groups[Runtime].(*RuntimeFlags)
- if len(runtime.namespaceRootsFlag.roots) > 0 {
+ if runtime.namespaceRootsFlag.isSet {
+ // command line overrides the environment.
runtime.NamespaceRoots = runtime.namespaceRootsFlag.roots
- }
- if len(runtime.NamespaceRoots) == 0 {
- runtime.NamespaceRoots = []string{defaultNamespaceRoot}
+ } else {
+ // we have a default value for the command line, which
+ // is only used if the environment variables have not been
+ // supplied.
+ if len(runtime.NamespaceRoots) == 0 {
+ runtime.NamespaceRoots = runtime.namespaceRootsFlag.roots
+ }
}
}
return nil
diff --git a/tools/namespace/impl.go b/tools/namespace/impl.go
index 5b00ce6..759548d 100644
--- a/tools/namespace/impl.go
+++ b/tools/namespace/impl.go
@@ -202,9 +202,9 @@
Long: `
The namespace tool facilitates interaction with the Veyron namespace.
-The namespace roots are set from environment variables that have a name
+The namespace roots are set from the command line via veyron.namespace.root options or from environment variables that have a name
starting with NAMESPACE_ROOT, e.g. NAMESPACE_ROOT, NAMESPACE_ROOT_2,
-NAMESPACE_ROOT_GOOGLE, etc.
+NAMESPACE_ROOT_GOOGLE, etc. The command line options override the environment.
`,
Children: []*cmdline.Command{cmdGlob, cmdMount, cmdUnmount, cmdResolve, cmdResolveToMT, cmdUnresolve},
}