veyron/lib/flags: define all common flags and associated types here.
Change-Id: I4d1da7f3186ac156631c5c54771effec3668ea80
diff --git a/lib/flags/doc.go b/lib/flags/doc.go
index 8681ac4..ff98c9d 100644
--- a/lib/flags/doc.go
+++ b/lib/flags/doc.go
@@ -1,3 +1,36 @@
-// Package flags provides implementations of the flag.Value interface
-// that are commonly used.
+// Package flags provides flag definitions for commonly used flags and
+// and, where appropriate, implementations of the flag.Value interface
+// for those flags to ensure that only valid values of those flags
+// are supplied.
+// In general, this package will be used by veyron profiles and the
+// runtime implementations, but can also be used by any application
+// that wants access to the flags and environment variables it supports.
+//
+// TODO(cnicolaou): move reading of environment variables to here also,
+// flags will override the environment variable settings.
package flags
+
+import "flag"
+
+type Flags struct {
+ ListenProtocolFlag TCPProtocolFlag
+ ListenAddressFlag IPHostPortFlag
+ ListenProxyFlag string
+ NamespaceRootsFlag string // TODO(cnicolaou): provide flag.Value impl
+ CredentialsFlag string // TODO(cnicolaou): provide flag.Value impl
+}
+
+// New returns a new instance of flags.Flags. Calling Parse on the supplied
+// flagSet will populate the fields of the return flags.Flags with the appropriate
+// values.
+func New(fs *flag.FlagSet) *Flags {
+ t := &Flags{}
+ t.ListenProtocolFlag = TCPProtocolFlag{"tcp"}
+ t.ListenAddressFlag = IPHostPortFlag{Port: "0"}
+ fs.Var(&t.ListenProtocolFlag, "veyron.tcp.protocol", "protocol to listen with")
+ fs.Var(&t.ListenAddressFlag, "veyron.tcp.address", "address to listen on")
+ fs.StringVar(&t.ListenProxyFlag, "veyron.proxy", "", "object name of proxy service to use to export services across network boundaries")
+ fs.StringVar(&t.NamespaceRootsFlag, "veyron.namespace.roots", "", ": separated list of roots for the local namespace")
+ fs.StringVar(&t.CredentialsFlag, "veyron.credentials", "", "directory to use for storing security credentials")
+ return t
+}
diff --git a/lib/flags/flags_test.go b/lib/flags/flags_test.go
new file mode 100644
index 0000000..4030af6
--- /dev/null
+++ b/lib/flags/flags_test.go
@@ -0,0 +1,24 @@
+package flags_test
+
+import (
+ "flag"
+ "testing"
+
+ "veyron.io/veyron/veyron/lib/flags"
+)
+
+func TestFlags(t *testing.T) {
+ fs := flag.NewFlagSet("test", flag.ContinueOnError)
+ fl := flags.New(fs)
+
+ addr := "192.168.10.1:0"
+ roots := "ab:cd:ef"
+ args := []string{"--veyron.tcp.address=" + addr, "--veyron.namespace.roots=" + roots}
+ fs.Parse(args)
+ if got, want := fl.NamespaceRootsFlag, roots; got != want {
+ t.Errorf("got %q, want %q", got, want)
+ }
+ if got, want := fl.ListenAddressFlag.String(), addr; got != want {
+ t.Errorf("got %q, want %q", got, want)
+ }
+}
diff --git a/profiles/roaming/roaming.go b/profiles/roaming/roaming.go
index d9314bb..2f9381f 100644
--- a/profiles/roaming/roaming.go
+++ b/profiles/roaming/roaming.go
@@ -30,19 +30,14 @@
)
var (
- listenProtocolFlag = flags.TCPProtocolFlag{"tcp"}
- listenAddressFlag = flags.IPHostPortFlag{Port: "0"}
- listenProxyFlag string
-
+ commonFlags *flags.Flags
// ListenSpec is an initialized instance of ipc.ListenSpec that can
// be used with ipc.Listen.
ListenSpec ipc.ListenSpec
)
func init() {
- flag.Var(&listenProtocolFlag, "veyron.tcp.protocol", "protocol to listen with")
- flag.Var(&listenAddressFlag, "veyron.tcp.address", "address to listen on")
- flag.StringVar(&listenProxyFlag, "veyron.proxy", "", "object name of proxy service to use to export services across network boundaries")
+ commonFlags = flags.New(flag.CommandLine)
rt.RegisterProfile(New())
}
@@ -75,9 +70,9 @@
log := rt.Logger()
ListenSpec = ipc.ListenSpec{
- Protocol: listenProtocolFlag.Protocol,
- Address: listenAddressFlag.String(),
- Proxy: listenProxyFlag,
+ Protocol: commonFlags.ListenProtocolFlag.Protocol,
+ Address: commonFlags.ListenAddressFlag.String(),
+ Proxy: commonFlags.ListenProxyFlag,
}
// Our address is private, so we test for running on GCE and for its
diff --git a/profiles/static/static.go b/profiles/static/static.go
index 960f347..4997720 100644
--- a/profiles/static/static.go
+++ b/profiles/static/static.go
@@ -18,19 +18,14 @@
)
var (
- listenProtocolFlag = flags.TCPProtocolFlag{"tcp"}
- listenAddressFlag = flags.IPHostPortFlag{Port: "0"}
- listenProxyFlag string
-
+ commonFlags *flags.Flags
// ListenSpec is an initialized instance of ipc.ListenSpec that can
// be used with ipc.Listen.
ListenSpec ipc.ListenSpec
)
func init() {
- flag.Var(&listenProtocolFlag, "veyron.tcp.protocol", "protocol to listen with")
- flag.Var(&listenAddressFlag, "veyron.tcp.address", "address to listen on")
- flag.StringVar(&listenProxyFlag, "veyron.proxy", "", "object name of proxy service to use to export services across network boundaries")
+ commonFlags = flags.New(flag.CommandLine)
rt.RegisterProfile(New())
}
@@ -61,9 +56,9 @@
log := rt.Logger()
ListenSpec = ipc.ListenSpec{
- Protocol: listenProtocolFlag.Protocol,
- Address: listenAddressFlag.String(),
- Proxy: listenProxyFlag,
+ Protocol: commonFlags.ListenProtocolFlag.Protocol,
+ Address: commonFlags.ListenAddressFlag.String(),
+ Proxy: commonFlags.ListenProxyFlag,
}
// Our address is private, so we test for running on GCE and for its