summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormike o'brien <mpobrien005@gmail.com>2014-11-13 21:45:17 -0500
committermike o'brien <mpobrien005@gmail.com>2014-11-14 15:27:35 -0500
commiteb70dc3eb2c6683280cd0dc773c87229774cb6b7 (patch)
tree1bba40e305994ff1580c90fc19ab0db27f9d3bbf
parent0af49b09589ae692be9cc3c8989a2402922e3e24 (diff)
downloadmongo-eb70dc3eb2c6683280cd0dc773c87229774cb6b7.tar.gz
TOOLS-310 allow tools to disable groups of options when not needed
Former-commit-id: abcec264a8b7c4632709ce38c4e3923e47801d64
-rw-r--r--bsondump/main/bsondump.go2
-rw-r--r--bsondump/options/options.go2
-rw-r--r--common/options/options.go151
-rw-r--r--common/options/options_gssapi.go2
-rw-r--r--common/options/options_ssl.go2
-rw-r--r--mongodump/main/mongodump.go2
-rw-r--r--mongoexport/main/mongoexport.go2
-rw-r--r--mongofiles/main/mongofiles.go4
-rw-r--r--mongoimport/main/mongoimport.go2
-rw-r--r--mongooplog/main/mongooplog.go2
-rw-r--r--mongorestore/main/mongorestore.go2
-rw-r--r--mongostat/main/mongostat.go5
-rw-r--r--mongotop/main/mongotop.go4
13 files changed, 70 insertions, 112 deletions
diff --git a/bsondump/main/bsondump.go b/bsondump/main/bsondump.go
index 6fba023af35..d31d13c08ce 100644
--- a/bsondump/main/bsondump.go
+++ b/bsondump/main/bsondump.go
@@ -11,7 +11,7 @@ import (
func main() {
// initialize command-line opts
- opts := commonopts.New("bsondump", "<file>")
+ opts := commonopts.New("bsondump", "<file>", commonopts.EnabledOptions{})
bsonDumpOpts := &options.BSONDumpOptions{}
opts.AddOptions(bsonDumpOpts)
diff --git a/bsondump/options/options.go b/bsondump/options/options.go
index e06d6c3bd26..1cadf24cbdc 100644
--- a/bsondump/options/options.go
+++ b/bsondump/options/options.go
@@ -1,7 +1,7 @@
package options
type BSONDumpOptions struct {
- Type string `long:"type" default:"json" description:"type of output: json, debug"`
+ Type string `long:"type" default:"json" description:"type of output: debug, json"`
ObjCheck bool `long:"objcheck" description:"validate bson during processing"`
NoObjCheck bool `long:"noobjcheck" description:"don't validate bson during processing"`
}
diff --git a/common/options/options.go b/common/options/options.go
index 7ea0e435894..ae06180f203 100644
--- a/common/options/options.go
+++ b/common/options/options.go
@@ -31,17 +31,13 @@ type ToolOptions struct {
*Connection
*SSL
*Auth
- *Namespace
*Kerberos
+ *Namespace
//Force direct connection to the server and disable the
//drivers automatic repl set discovery logic.
Direct bool
- // Extra tool-specific options that can be specified by calling
- // AddOptions
- Extra []ExtraOptions
-
// for caching the parser
parser *flags.Parser
}
@@ -103,15 +99,17 @@ type Kerberos struct {
type OptionRegistrationFunction func(self *ToolOptions) error
-var OptionRegistrationFunctions []OptionRegistrationFunction
+var ConnectionOptFunctions []OptionRegistrationFunction
-func init() {
- OptionRegistrationFunctions = append(OptionRegistrationFunctions, registerCommonOptions, registerExtraOptions)
+type EnabledOptions struct {
+ Auth bool
+ Connection bool
+ Namespace bool
}
// Ask for a new instance of tool options
-func New(appName, usageStr string) *ToolOptions {
- return &ToolOptions{
+func New(appName, usageStr string, enabled EnabledOptions) *ToolOptions {
+ opts := &ToolOptions{
AppName: appName,
VersionStr: VersionStr,
UsageStr: usageStr,
@@ -123,7 +121,42 @@ func New(appName, usageStr string) *ToolOptions {
Auth: &Auth{},
Namespace: &Namespace{},
Kerberos: &Kerberos{},
+ parser: flags.NewNamedParser(appName, flags.None),
+ }
+
+ opts.parser.Usage = usageStr
+
+ if _, err := opts.parser.AddGroup("general options", "", opts.General); err != nil {
+ panic(fmt.Errorf("couldn't register general options: %v", err))
+ }
+ if _, err := opts.parser.AddGroup("verbosity options", "", opts.Verbosity); err != nil {
+ panic(fmt.Errorf("couldn't register verbosity options: %v", err))
+ }
+
+ if enabled.Connection {
+ if _, err := opts.parser.AddGroup("connection options", "", opts.Connection); err != nil {
+ panic(fmt.Errorf("couldn't register connection options: %v", err))
+ }
+
+ //Register options that were enabled at compile time with build tags (ssl, sasl)
+ for _, optionRegistrationFunction := range ConnectionOptFunctions {
+ if err := optionRegistrationFunction(opts); err != nil {
+ panic(fmt.Errorf("couldn't register command-line options: %v", err))
+ }
+ }
+ }
+
+ if enabled.Auth {
+ if _, err := opts.parser.AddGroup("authentication options", "", opts.Auth); err != nil {
+ panic(fmt.Errorf("couldn't register auth options"))
+ }
+ }
+ if enabled.Namespace {
+ if _, err := opts.parser.AddGroup("namespace options", "", opts.Namespace); err != nil {
+ panic(fmt.Errorf("couldn't register namespace options"))
+ }
}
+ return opts
}
// Print the usage message for the tool to stdout. Returns whether or not the
@@ -162,104 +195,24 @@ func (self *ToolOptions) GetAuthenticationDatabase() string {
return ""
}
-// Add extra command line options into the tool options
-func (self *ToolOptions) AddOptions(opts ExtraOptions) {
- self.Extra = append(self.Extra, opts)
+// AddOptions registers an additional options group to this instance
+func (self *ToolOptions) AddOptions(opts ExtraOptions) error {
+ _, err := self.parser.AddGroup(opts.Name()+" options", "", opts)
+ if err != nil {
+ return fmt.Errorf("error setting command line options for"+
+ " %v: %v", opts.Name(), err)
+ }
+ return nil
}
// Parse the command line args. Returns any extra args not accounted for by
// parsing, as well as an error if the parsing returns an error.
func (self *ToolOptions) Parse() ([]string, error) {
-
- // init a parser for the flags
- self.parser = flags.NewNamedParser(self.AppName, flags.None)
- self.parser.Usage = self.UsageStr
-
- for _, optionRegistrationFunction := range OptionRegistrationFunctions {
- if err := optionRegistrationFunction(self); err != nil {
- return nil, err
- }
- }
-
- // parse
return self.parser.Parse()
}
-func registerExtraOptions(self *ToolOptions) error {
-
- // register all of the extra options
- for _, eo := range self.Extra {
- _, err := self.parser.AddGroup(eo.Name()+" options", "", eo)
- if err != nil {
- return fmt.Errorf("error setting command line options for"+
- " %v: %v", eo.Name(), err)
- }
- }
- return nil
-
-}
-
-func registerCommonOptions(self *ToolOptions) error {
-
- // register self to receive the flags
- if _, err := self.parser.AddGroup("general options", "", self.General); err != nil {
- return err
- }
- if _, err := self.parser.AddGroup("verbosity options", "", self.Verbosity); err != nil {
- return err
- }
- if _, err := self.parser.AddGroup("connection options", "", self.Connection); err != nil {
- return err
- }
- if _, err := self.parser.AddGroup("authentication options", "", self.Auth); err != nil {
- return err
- }
- if _, err := self.parser.AddGroup("namespace options", "", self.Namespace); err != nil {
- return err
- }
- return nil
-}
-
-////////////
-
-// Run the post-parse logic
-func (self *ToolOptions) PostParse() error {
- // build the filter string and options based on the db and collection
- // specified, if any
-
- /*
- if self.DB != "" {
- self.FilterNS = self.DB + "."
- if self.Collection != "" {
- self.FilterBoth = true
- self.FilterNS += self.Collection
- }
- } else if self.Collection != "" {
- self.FilterOnlyColl = true
- self.FilterNS = "." + self.Collection
- }
-
- // post-parse the extra params
- if self.ExtraOptions != nil {
- if err := self.ExtraOptions.PostParse(); err != nil {
- return err
- }
- }
- */
-
- return nil
-}
-
-// Run the validation logic
+//Validate() runs validation checks that are global to all tools.
func (self *ToolOptions) Validate() error {
- /*
- if self.ExtraOptions != nil {
- if err := self.ExtraOptions.Validate(); err != nil {
- return err
- }
- }
- */
-
switch {
case self.DBPath != "" && self.Host != "":
return fmt.Errorf("--dbpath is not allowed when --host is specified")
diff --git a/common/options/options_gssapi.go b/common/options/options_gssapi.go
index 31c1ccd49fb..f10aa3a2b3b 100644
--- a/common/options/options_gssapi.go
+++ b/common/options/options_gssapi.go
@@ -3,7 +3,7 @@
package options
func init() {
- OptionRegistrationFunctions = append(OptionRegistrationFunctions, registerGSSAPIOptions)
+ ConnectionOptFunctions = append(ConnectionOptFunctions, registerGSSAPIOptions)
}
func registerGSSAPIOptions(self *ToolOptions) error {
diff --git a/common/options/options_ssl.go b/common/options/options_ssl.go
index c252de65e6a..008f4d25f25 100644
--- a/common/options/options_ssl.go
+++ b/common/options/options_ssl.go
@@ -3,7 +3,7 @@
package options
func init() {
- OptionRegistrationFunctions = append(OptionRegistrationFunctions, registerSSLOptions)
+ ConnectionOptFunctions = append(ConnectionOptFunctions, registerSSLOptions)
}
func registerSSLOptions(self *ToolOptions) error {
diff --git a/mongodump/main/mongodump.go b/mongodump/main/mongodump.go
index 1381dc42cde..52dde4516dc 100644
--- a/mongodump/main/mongodump.go
+++ b/mongodump/main/mongodump.go
@@ -11,7 +11,7 @@ import (
func main() {
// initialize command-line opts
- opts := commonopts.New("mongodump", "<options>")
+ opts := commonopts.New("mongodump", "<options>", commonopts.EnabledOptions{true, true, true})
inputOpts := &options.InputOptions{}
opts.AddOptions(inputOpts)
diff --git a/mongoexport/main/mongoexport.go b/mongoexport/main/mongoexport.go
index 37c24dbf2d1..8b85fd54449 100644
--- a/mongoexport/main/mongoexport.go
+++ b/mongoexport/main/mongoexport.go
@@ -12,7 +12,7 @@ import (
func main() {
// initialize command-line opts
- opts := commonopts.New("mongoexport", "<options>")
+ opts := commonopts.New("mongoexport", "<options>", commonopts.EnabledOptions{Auth: true, Connection: true, Namespace: true})
outputOpts := &options.OutputFormatOptions{}
opts.AddOptions(outputOpts)
diff --git a/mongofiles/main/mongofiles.go b/mongofiles/main/mongofiles.go
index 82b61fca2e4..6026386a473 100644
--- a/mongofiles/main/mongofiles.go
+++ b/mongofiles/main/mongofiles.go
@@ -4,7 +4,7 @@ import (
"fmt"
"github.com/mongodb/mongo-tools/common/db"
"github.com/mongodb/mongo-tools/common/log"
- commonOpts "github.com/mongodb/mongo-tools/common/options"
+ commonopts "github.com/mongodb/mongo-tools/common/options"
"github.com/mongodb/mongo-tools/mongofiles"
"github.com/mongodb/mongo-tools/mongofiles/options"
"os"
@@ -27,7 +27,7 @@ const (
func main() {
// initialize command-line opts
- opts := commonOpts.New("mongofiles", Usage)
+ opts := commonopts.New("mongofiles", Usage, commonopts.EnabledOptions{Auth: true, Connection: true, Namespace: true})
storageOpts := &options.StorageOptions{}
opts.AddOptions(storageOpts)
diff --git a/mongoimport/main/mongoimport.go b/mongoimport/main/mongoimport.go
index 57d7eb0d928..3beb2dc7066 100644
--- a/mongoimport/main/mongoimport.go
+++ b/mongoimport/main/mongoimport.go
@@ -15,7 +15,7 @@ func main() {
usageStr := " --host myhost --db my_cms --collection docs < mydocfile." +
"json \n\nImport CSV, TSV or JSON data into MongoDB.\n\nWhen importing " +
"JSON documents, each document must be a separate line of the input file."
- opts := commonopts.New("mongoimport", usageStr)
+ opts := commonopts.New("mongoimport", usageStr, commonopts.EnabledOptions{Auth: true, Connection: true, Namespace: true})
inputOpts := &options.InputOptions{}
opts.AddOptions(inputOpts)
diff --git a/mongooplog/main/mongooplog.go b/mongooplog/main/mongooplog.go
index 1b2dcf8c0ea..33631b2b9de 100644
--- a/mongooplog/main/mongooplog.go
+++ b/mongooplog/main/mongooplog.go
@@ -13,7 +13,7 @@ import (
func main() {
// initialize command line options
- opts := commonopts.New("mongooplog", "<options>")
+ opts := commonopts.New("mongooplog", "<options>", commonopts.EnabledOptions{Auth: true, Connection: true, Namespace: false})
// add the mongooplog-specific options
sourceOpts := &options.SourceOptions{}
diff --git a/mongorestore/main/mongorestore.go b/mongorestore/main/mongorestore.go
index ca7404ad66b..851269af842 100644
--- a/mongorestore/main/mongorestore.go
+++ b/mongorestore/main/mongorestore.go
@@ -14,7 +14,7 @@ import (
func main() {
// initialize command-line opts
- opts := commonopts.New("mongorestore", "<options>")
+ opts := commonopts.New("mongorestore", "<options>", commonopts.EnabledOptions{Auth: true, Connection: true, Namespace: true})
inputOpts := &options.InputOptions{}
opts.AddOptions(inputOpts)
outputOpts := &options.OutputOptions{}
diff --git a/mongostat/main/mongostat.go b/mongostat/main/mongostat.go
index 3cc04aab37f..1cf6af55a54 100644
--- a/mongostat/main/mongostat.go
+++ b/mongostat/main/mongostat.go
@@ -14,7 +14,10 @@ import (
func main() {
// initialize command-line opts
- opts := commonopts.New("mongostat", "<options>")
+ opts := commonopts.New(
+ "mongostat",
+ "[options] <polling interval in seconds>",
+ commonopts.EnabledOptions{Connection: true, Auth: true, Namespace: false})
// add mongotop-specific options
statOpts := &options.StatOptions{}
diff --git a/mongotop/main/mongotop.go b/mongotop/main/mongotop.go
index fb035cb66c8..34b0728b1ee 100644
--- a/mongotop/main/mongotop.go
+++ b/mongotop/main/mongotop.go
@@ -20,7 +20,9 @@ const (
func main() {
// initialize command-line opts
- opts := commonopts.New("mongotop", "<options> <sleeptime>")
+ opts := commonopts.New("mongotop",
+ "<options> <sleeptime>",
+ commonopts.EnabledOptions{Auth: true, Connection: true, Namespace: false})
// add mongotop-specific options
outputOpts := &options.Output{}