本文整理匯總了Golang中github.com/mongodb/mongo-tools/common/log.SetVerbosity函數的典型用法代碼示例。如果您正苦於以下問題:Golang SetVerbosity函數的具體用法?Golang SetVerbosity怎麽用?Golang SetVerbosity使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了SetVerbosity函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: simpleMongoDumpInstance
func simpleMongoDumpInstance() *MongoDump {
ssl := testutil.GetSSLOptions()
auth := testutil.GetAuthOptions()
namespace := &options.Namespace{
DB: testDB,
}
connection := &options.Connection{
Host: testServer,
Port: testPort,
}
toolOptions := &options.ToolOptions{
SSL: &ssl,
Namespace: namespace,
Connection: connection,
Auth: &auth,
HiddenOptions: &options.HiddenOptions{},
Verbosity: &options.Verbosity{},
}
outputOptions := &OutputOptions{}
inputOptions := &InputOptions{}
log.SetVerbosity(toolOptions.Verbosity)
return &MongoDump{
ToolOptions: toolOptions,
InputOptions: inputOptions,
OutputOptions: outputOptions,
ProgressManager: progress.NewProgressBarManager(log.Writer(0), progressBarWaitTime),
HandleSignals: HandleSignals,
}
}
示例2: simpleMongoDumpInstance
func simpleMongoDumpInstance() *MongoDump {
ssl := testutil.GetSSLOptions()
auth := testutil.GetAuthOptions()
namespace := &options.Namespace{
DB: testDB,
}
connection := &options.Connection{
Host: testServer,
Port: testPort,
}
toolOptions := &options.ToolOptions{
SSL: &ssl,
Namespace: namespace,
Connection: connection,
Auth: &auth,
Verbosity: &options.Verbosity{},
}
outputOptions := &OutputOptions{
NumParallelCollections: 1,
}
inputOptions := &InputOptions{}
log.SetVerbosity(toolOptions.Verbosity)
return &MongoDump{
ToolOptions: toolOptions,
InputOptions: inputOptions,
OutputOptions: outputOptions,
}
}
示例3: main
func main() {
go signals.Handle()
// initialize command-line opts
opts := options.New("mongofiles", mongofiles.Usage, options.EnabledOptions{Auth: true, Connection: true, Namespace: false})
storageOpts := &mongofiles.StorageOptions{}
opts.AddOptions(storageOpts)
args, err := opts.Parse()
if err != nil {
log.Logf(log.Always, "error parsing command line options: %v", err)
log.Logf(log.Always, "try 'mongofiles --help' for more information")
os.Exit(util.ExitBadOptions)
}
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
log.SetVerbosity(opts.Verbosity)
// add the specified database to the namespace options struct
opts.Namespace.DB = storageOpts.DB
// connect directly, unless a replica set name is explicitly specified
_, setName := util.ParseConnectionString(opts.Host)
opts.Direct = (setName == "")
opts.ReplicaSetName = setName
// create a session provider to connect to the db
provider, err := db.NewSessionProvider(*opts)
if err != nil {
log.Logf(log.Always, "error connecting to host: %v", err)
os.Exit(util.ExitError)
}
mf := mongofiles.MongoFiles{
ToolOptions: opts,
StorageOptions: storageOpts,
SessionProvider: provider,
}
if err := mf.ValidateCommand(args); err != nil {
log.Logf(log.Always, "%v", err)
log.Logf(log.Always, "try 'mongofiles --help' for more information")
os.Exit(util.ExitBadOptions)
}
output, err := mf.Run(true)
if err != nil {
log.Logf(log.Always, "Failed: %v", err)
os.Exit(util.ExitError)
}
fmt.Printf("%s", output)
}
示例4: main
func main() {
// initialize command-line opts
opts := options.New("mongodump", mongodump.Usage, options.EnabledOptions{true, true, true})
inputOpts := &mongodump.InputOptions{}
opts.AddOptions(inputOpts)
outputOpts := &mongodump.OutputOptions{}
opts.AddOptions(outputOpts)
args, err := opts.Parse()
if err != nil {
log.Logf(log.Always, "error parsing command line options: %v", err)
log.Logf(log.Always, "try 'mongodump --help' for more information")
os.Exit(util.ExitBadOptions)
}
if len(args) > 0 {
log.Logf(log.Always, "positional arguments not allowed: %v", args)
log.Logf(log.Always, "try 'mongodump --help' for more information")
os.Exit(util.ExitBadOptions)
}
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
// init logger
log.SetVerbosity(opts.Verbosity)
// connect directly, unless a replica set name is explicitly specified
_, setName := util.ParseConnectionString(opts.Host)
opts.Direct = (setName == "")
opts.ReplicaSetName = setName
dump := mongodump.MongoDump{
ToolOptions: opts,
OutputOptions: outputOpts,
InputOptions: inputOpts,
}
if err = dump.Init(); err != nil {
log.Logf(log.Always, "Failed: %v", err)
os.Exit(util.ExitError)
}
if err = dump.Dump(); err != nil {
log.Logf(log.Always, "Failed: %v", err)
if err == util.ErrTerminated {
os.Exit(util.ExitKill)
}
os.Exit(util.ExitError)
}
}
示例5: init
func init() {
// bump up the verbosity to make checking debug log output possible
log.SetVerbosity(&options.Verbosity{
Verbose: []bool{true, true, true, true},
})
}
示例6: main
func main() {
// initialize command-line opts
opts := options.New("mongotop", mongotop.Usage,
options.EnabledOptions{Auth: true, Connection: true, Namespace: false})
opts.UseReadOnlyHostDescription()
// add mongotop-specific options
outputOpts := &mongotop.Output{}
opts.AddOptions(outputOpts)
args, err := opts.Parse()
if err != nil {
log.Logvf(log.Always, "error parsing command line options: %v", err)
log.Logvf(log.Always, "try 'mongotop --help' for more information")
os.Exit(util.ExitBadOptions)
}
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
log.SetVerbosity(opts.Verbosity)
signals.Handle()
if len(args) > 1 {
log.Logvf(log.Always, "too many positional arguments")
log.Logvf(log.Always, "try 'mongotop --help' for more information")
os.Exit(util.ExitBadOptions)
}
sleeptime := 1 // default to 1 second sleep time
if len(args) > 0 {
sleeptime, err = strconv.Atoi(args[0])
if err != nil || sleeptime <= 0 {
log.Logvf(log.Always, "invalid sleep time: %v", args[0])
os.Exit(util.ExitBadOptions)
}
}
if outputOpts.RowCount < 0 {
log.Logvf(log.Always, "invalid value for --rowcount: %v", outputOpts.RowCount)
os.Exit(util.ExitBadOptions)
}
if opts.Auth.Username != "" && opts.Auth.Source == "" && !opts.Auth.RequiresExternalDB() {
log.Logvf(log.Always, "--authenticationDatabase is required when authenticating against a non $external database")
os.Exit(util.ExitBadOptions)
}
// connect directly, unless a replica set name is explicitly specified
_, setName := util.ParseConnectionString(opts.Host)
opts.Direct = (setName == "")
opts.ReplicaSetName = setName
// create a session provider to connect to the db
sessionProvider, err := db.NewSessionProvider(*opts)
if err != nil {
log.Logvf(log.Always, "error connecting to host: %v", err)
os.Exit(util.ExitError)
}
if setName == "" {
sessionProvider.SetReadPreference(mgo.PrimaryPreferred)
}
// fail fast if connecting to a mongos
isMongos, err := sessionProvider.IsMongos()
if err != nil {
log.Logvf(log.Always, "Failed: %v", err)
os.Exit(util.ExitError)
}
if isMongos {
log.Logvf(log.Always, "cannot run mongotop against a mongos")
os.Exit(util.ExitError)
}
// instantiate a mongotop instance
top := &mongotop.MongoTop{
Options: opts,
OutputOptions: outputOpts,
SessionProvider: sessionProvider,
Sleeptime: time.Duration(sleeptime) * time.Second,
}
// kick it off
if err := top.Run(); err != nil {
log.Logvf(log.Always, "Failed: %v", err)
os.Exit(util.ExitError)
}
}
示例7: main
func main() {
// initialize command-line opts
opts := options.New("mongoexport", mongoexport.Usage,
options.EnabledOptions{Auth: true, Connection: true, Namespace: true})
outputOpts := &mongoexport.OutputFormatOptions{}
opts.AddOptions(outputOpts)
inputOpts := &mongoexport.InputOptions{}
opts.AddOptions(inputOpts)
args, err := opts.Parse()
if err != nil {
log.Logvf(log.Always, "error parsing command line options: %v", err)
log.Logvf(log.Always, "try 'mongoexport --help' for more information")
os.Exit(util.ExitBadOptions)
}
if len(args) != 0 {
log.Logvf(log.Always, "too many positional arguments: %v", args)
log.Logvf(log.Always, "try 'mongoexport --help' for more information")
os.Exit(util.ExitBadOptions)
}
log.SetVerbosity(opts.Verbosity)
signals.Handle()
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
// connect directly, unless a replica set name is explicitly specified
_, setName := util.ParseConnectionString(opts.Host)
opts.Direct = (setName == "")
opts.ReplicaSetName = setName
provider, err := db.NewSessionProvider(*opts)
// temporarily allow secondary reads for the isMongos check
provider.SetReadPreference(mgo.Nearest)
isMongos, err := provider.IsMongos()
if err != nil {
log.Logvf(log.Always, "%v", err)
os.Exit(util.ExitError)
}
provider.SetFlags(db.DisableSocketTimeout)
if inputOpts.SlaveOk {
if inputOpts.ReadPreference != "" {
log.Logvf(log.Always, "--slaveOk can't be specified when --readPreference is specified")
os.Exit(util.ExitBadOptions)
}
log.Logvf(log.Always, "--slaveOk is deprecated and being internally rewritten as --readPreference=nearest")
inputOpts.ReadPreference = "nearest"
}
var mode mgo.Mode
if opts.ReplicaSetName != "" || isMongos {
mode = mgo.Primary
} else {
mode = mgo.Nearest
}
var tags bson.D
if inputOpts.ReadPreference != "" {
mode, tags, err = db.ParseReadPreference(inputOpts.ReadPreference)
if err != nil {
log.Logvf(log.Always, "error parsing --ReadPreference: %v", err)
os.Exit(util.ExitBadOptions)
}
if len(tags) > 0 {
provider.SetTags(tags)
}
}
// warn if we are trying to export from a secondary in a sharded cluster
if isMongos && mode != mgo.Primary {
log.Logvf(log.Always, db.WarningNonPrimaryMongosConnection)
}
provider.SetReadPreference(mode)
if err != nil {
log.Logvf(log.Always, "error connecting to host: %v", err)
os.Exit(util.ExitError)
}
exporter := mongoexport.MongoExport{
ToolOptions: *opts,
OutputOpts: outputOpts,
InputOpts: inputOpts,
SessionProvider: provider,
}
err = exporter.ValidateSettings()
if err != nil {
log.Logvf(log.Always, "error validating settings: %v", err)
//.........這裏部分代碼省略.........
示例8: main
func main() {
// initialize command-line opts
opts := options.New("mongorestore", mongorestore.Usage,
options.EnabledOptions{Auth: true, Connection: true})
nsOpts := &mongorestore.NSOptions{}
opts.AddOptions(nsOpts)
inputOpts := &mongorestore.InputOptions{}
opts.AddOptions(inputOpts)
outputOpts := &mongorestore.OutputOptions{}
opts.AddOptions(outputOpts)
extraArgs, err := opts.Parse()
if err != nil {
log.Logvf(log.Always, "error parsing command line options: %v", err)
log.Logvf(log.Always, "try 'mongorestore --help' for more information")
os.Exit(util.ExitBadOptions)
}
// Allow the db connector to fall back onto the current database when no
// auth database is given; the standard -d/-c options go into nsOpts now
opts.Namespace = &options.Namespace{DB: nsOpts.DB}
// print help or version info, if specified
if opts.PrintHelp(false) {
return
}
if opts.PrintVersion() {
return
}
log.SetVerbosity(opts.Verbosity)
targetDir, err := getTargetDirFromArgs(extraArgs, inputOpts.Directory)
if err != nil {
log.Logvf(log.Always, "%v", err)
log.Logvf(log.Always, "try 'mongorestore --help' for more information")
os.Exit(util.ExitBadOptions)
}
targetDir = util.ToUniversalPath(targetDir)
// connect directly, unless a replica set name is explicitly specified
_, setName := util.ParseConnectionString(opts.Host)
opts.Direct = (setName == "")
opts.ReplicaSetName = setName
provider, err := db.NewSessionProvider(*opts)
if err != nil {
log.Logvf(log.Always, "error connecting to host: %v", err)
os.Exit(util.ExitError)
}
provider.SetBypassDocumentValidation(outputOpts.BypassDocumentValidation)
// disable TCP timeouts for restore jobs
provider.SetFlags(db.DisableSocketTimeout)
restore := mongorestore.MongoRestore{
ToolOptions: opts,
OutputOptions: outputOpts,
InputOptions: inputOpts,
NSOptions: nsOpts,
TargetDirectory: targetDir,
SessionProvider: provider,
}
finishedChan := signals.HandleWithInterrupt(restore.HandleInterrupt)
defer close(finishedChan)
if err = restore.Restore(); err != nil {
log.Logvf(log.Always, "Failed: %v", err)
if err == util.ErrTerminated {
os.Exit(util.ExitKill)
}
os.Exit(util.ExitError)
}
}
示例9: main
func main() {
go signals.Handle()
// initialize command-line opts
opts := options.New(
"mongostat",
mongostat.Usage,
options.EnabledOptions{Connection: true, Auth: true, Namespace: false})
// add mongostat-specific options
statOpts := &mongostat.StatOptions{}
opts.AddOptions(statOpts)
args, err := opts.Parse()
if err != nil {
log.Logf(log.Always, "error parsing command line options: %v", err)
log.Logf(log.Always, "try 'mongostat --help' for more information")
os.Exit(util.ExitBadOptions)
}
log.SetVerbosity(opts.Verbosity)
sleepInterval := 1
if len(args) > 0 {
if len(args) != 1 {
log.Logf(log.Always, "too many positional arguments: %v", args)
log.Logf(log.Always, "try 'mongostat --help' for more information")
os.Exit(util.ExitBadOptions)
}
sleepInterval, err = strconv.Atoi(args[0])
if err != nil {
log.Logf(log.Always, "invalid sleep interval: %v", args[0])
os.Exit(util.ExitBadOptions)
}
if sleepInterval < 1 {
log.Logf(log.Always, "sleep interval must be at least 1 second")
os.Exit(util.ExitBadOptions)
}
}
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
if opts.Auth.Username != "" && opts.Auth.Source == "" && !opts.Auth.RequiresExternalDB() {
log.Logf(log.Always, "--authenticationDatabase is required when authenticating against a non $external database")
os.Exit(util.ExitBadOptions)
}
// we have to check this here, otherwise the user will be prompted
// for a password for each discovered node
if opts.Auth.ShouldAskForPassword() {
opts.Auth.Password = password.Prompt()
}
var formatter mongostat.LineFormatter
if statOpts.Json {
formatter = &mongostat.JSONLineFormatter{}
} else {
formatter = &mongostat.GridLineFormatter{
IncludeHeader: !statOpts.NoHeaders,
HeaderInterval: 10,
Writer: &text.GridWriter{ColumnPadding: 1},
}
}
seedHosts := util.CreateConnectionAddrs(opts.Host, opts.Port)
var cluster mongostat.ClusterMonitor
if statOpts.Discover || len(seedHosts) > 1 {
cluster = &mongostat.AsyncClusterMonitor{
ReportChan: make(chan mongostat.StatLine),
LastStatLines: map[string]*mongostat.StatLine{},
Formatter: formatter,
}
} else {
cluster = &mongostat.SyncClusterMonitor{
ReportChan: make(chan mongostat.StatLine),
Formatter: formatter,
}
}
var discoverChan chan string
if statOpts.Discover {
discoverChan = make(chan string, 128)
}
opts.Direct = true
_, setName := util.ParseConnectionString(opts.Host)
opts.ReplicaSetName = setName
stat := &mongostat.MongoStat{
Options: opts,
StatOptions: statOpts,
Nodes: map[string]*mongostat.NodeMonitor{},
Discovered: discoverChan,
SleepInterval: time.Duration(sleepInterval) * time.Second,
//.........這裏部分代碼省略.........
示例10: main
func main() {
go signals.Handle()
// initialize command-line opts
opts := options.New("bsondump", bsondump.Usage, options.EnabledOptions{})
bsonDumpOpts := &bsondump.BSONDumpOptions{}
opts.AddOptions(bsonDumpOpts)
args, err := opts.Parse()
if err != nil {
log.Logf(log.Always, "error parsing command line options: %v", err)
log.Logf(log.Always, "try 'bsondump --help' for more information")
os.Exit(util.ExitBadOptions)
}
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
log.SetVerbosity(opts.Verbosity)
// pull out the filename
if len(args) == 0 {
log.Logf(log.Always, "must provide a filename")
log.Logf(log.Always, "try 'bsondump --help' for more information")
os.Exit(util.ExitBadOptions)
} else if len(args) > 1 {
log.Logf(log.Always, "too many positional arguments: %v", args)
log.Logf(log.Always, "try 'bsondump --help' for more information")
os.Exit(util.ExitBadOptions)
}
dumper := bsondump.BSONDump{
ToolOptions: opts,
BSONDumpOptions: bsonDumpOpts,
FileName: args[0],
Out: os.Stdout,
}
log.Logf(log.DebugLow, "running bsondump with --objcheck: %v", bsonDumpOpts.ObjCheck)
if len(bsonDumpOpts.Type) != 0 && bsonDumpOpts.Type != "debug" && bsonDumpOpts.Type != "json" {
log.Logf(log.Always, "Unsupported output type '%v'. Must be either 'debug' or 'json'", bsonDumpOpts.Type)
os.Exit(util.ExitBadOptions)
}
err = dumper.Open()
if err != nil {
log.Logf(log.Always, "Failed: %v", err)
os.Exit(util.ExitError)
}
var numFound int
if bsonDumpOpts.Type == "debug" {
numFound, err = dumper.Debug()
} else {
numFound, err = dumper.JSON()
}
log.Logf(log.Always, "%v objects found", numFound)
if err != nil {
log.Log(log.Always, err.Error())
os.Exit(util.ExitError)
}
}
示例11: main
func main() {
go signals.Handle()
// initialize command line options
opts := options.New("mongooplog", mongooplog.Usage,
options.EnabledOptions{Auth: true, Connection: true, Namespace: false})
// add the mongooplog-specific options
sourceOpts := &mongooplog.SourceOptions{}
opts.AddOptions(sourceOpts)
log.Logf(log.Always, "warning: mongooplog is deprecated, and will be removed completely in a future release")
// parse the command line options
args, err := opts.Parse()
if err != nil {
log.Logf(log.Always, "error parsing command line options: %v", err)
log.Logf(log.Always, "try 'mongooplog --help' for more information")
os.Exit(util.ExitBadOptions)
}
if len(args) != 0 {
log.Logf(log.Always, "positional arguments not allowed: %v", args)
log.Logf(log.Always, "try 'mongooplog --help' for more information")
os.Exit(util.ExitBadOptions)
}
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
// init logger
log.SetVerbosity(opts.Verbosity)
// connect directly, unless a replica set name is explicitly specified
_, setName := util.ParseConnectionString(opts.Host)
opts.Direct = (setName == "")
opts.ReplicaSetName = setName
// validate the mongooplog options
if sourceOpts.From == "" {
log.Logf(log.Always, "command line error: need to specify --from")
os.Exit(util.ExitBadOptions)
}
// create a session provider for the destination server
sessionProviderTo, err := db.NewSessionProvider(*opts)
if err != nil {
log.Logf(log.Always, "error connecting to destination host: %v", err)
os.Exit(util.ExitError)
}
// create a session provider for the source server
opts.Connection.Host = sourceOpts.From
opts.Connection.Port = ""
sessionProviderFrom, err := db.NewSessionProvider(*opts)
if err != nil {
log.Logf(log.Always, "error connecting to source host: %v", err)
os.Exit(util.ExitError)
}
// initialize mongooplog
oplog := mongooplog.MongoOplog{
ToolOptions: opts,
SourceOptions: sourceOpts,
SessionProviderFrom: sessionProviderFrom,
SessionProviderTo: sessionProviderTo,
}
// kick it off
if err := oplog.Run(); err != nil {
log.Logf(log.Always, "error: %v", err)
os.Exit(util.ExitError)
}
}
示例12: main
func main() {
// initialize command-line opts
opts := options.New("mongorestore", mongorestore.Usage,
options.EnabledOptions{Auth: true, Connection: true, Namespace: true})
inputOpts := &mongorestore.InputOptions{}
opts.AddOptions(inputOpts)
outputOpts := &mongorestore.OutputOptions{}
opts.AddOptions(outputOpts)
extraArgs, err := opts.Parse()
if err != nil {
log.Logf(log.Always, "error parsing command line options: %v", err)
log.Logf(log.Always, "try 'mongorestore --help' for more information")
os.Exit(util.ExitBadOptions)
}
// print help or version info, if specified
if opts.PrintHelp(false) {
return
}
if opts.PrintVersion() {
return
}
log.SetVerbosity(opts.Verbosity)
targetDir, err := getTargetDirFromArgs(extraArgs, inputOpts.Directory)
if err != nil {
log.Logf(log.Always, "%v", err)
log.Logf(log.Always, "try 'mongorestore --help' for more information")
os.Exit(util.ExitBadOptions)
}
targetDir = util.ToUniversalPath(targetDir)
// connect directly, unless a replica set name is explicitly specified
_, setName := util.ParseConnectionString(opts.Host)
opts.Direct = (setName == "")
opts.ReplicaSetName = setName
provider, err := db.NewSessionProvider(*opts)
if err != nil {
log.Logf(log.Always, "error connecting to host: %v", err)
os.Exit(util.ExitError)
}
provider.SetBypassDocumentValidation(outputOpts.BypassDocumentValidation)
// disable TCP timeouts for restore jobs
provider.SetFlags(db.DisableSocketTimeout)
restore := mongorestore.MongoRestore{
ToolOptions: opts,
OutputOptions: outputOpts,
InputOptions: inputOpts,
TargetDirectory: targetDir,
SessionProvider: provider,
}
if err = restore.Restore(); err != nil {
log.Logf(log.Always, "Failed: %v", err)
if err == util.ErrTerminated {
os.Exit(util.ExitKill)
}
os.Exit(util.ExitError)
}
}
示例13: main
func main() {
go signals.Handle()
// initialize command-line opts
opts := options.New("mongoexport", mongoexport.Usage,
options.EnabledOptions{Auth: true, Connection: true, Namespace: true})
outputOpts := &mongoexport.OutputFormatOptions{}
opts.AddOptions(outputOpts)
inputOpts := &mongoexport.InputOptions{}
opts.AddOptions(inputOpts)
args, err := opts.Parse()
if err != nil {
log.Logf(log.Always, "error parsing command line options: %v", err)
log.Logf(log.Always, "try 'mongoexport --help' for more information")
os.Exit(util.ExitBadOptions)
}
if len(args) != 0 {
log.Logf(log.Always, "too many positional arguments: %v", args)
log.Logf(log.Always, "try 'mongoexport --help' for more information")
os.Exit(util.ExitBadOptions)
}
log.SetVerbosity(opts.Verbosity)
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
// connect directly, unless a replica set name is explicitly specified
_, setName := util.ParseConnectionString(opts.Host)
opts.Direct = (setName == "")
opts.ReplicaSetName = setName
provider, err := db.NewSessionProvider(*opts)
if err != nil {
log.Logf(log.Always, "error connecting to host: %v", err)
os.Exit(util.ExitError)
}
exporter := mongoexport.MongoExport{
ToolOptions: *opts,
OutputOpts: outputOpts,
InputOpts: inputOpts,
SessionProvider: provider,
}
err = exporter.ValidateSettings()
if err != nil {
log.Logf(log.Always, "error validating settings: %v", err)
log.Logf(log.Always, "try 'mongoexport --help' for more information")
os.Exit(util.ExitBadOptions)
}
writer, err := exporter.GetOutputWriter()
if err != nil {
log.Logf(log.Always, "error opening output stream: %v", err)
os.Exit(util.ExitError)
}
if writer == nil {
writer = os.Stdout
} else {
defer writer.Close()
}
numDocs, err := exporter.Export(writer)
if err != nil {
log.Logf(log.Always, "Failed: %v", err)
os.Exit(util.ExitError)
}
if numDocs == 1 {
log.Logf(log.Always, "exported %v record", numDocs)
} else {
log.Logf(log.Always, "exported %v records", numDocs)
}
}
示例14: main
func main() {
// initialize command-line opts
opts := options.New("mongodump", mongodump.Usage, options.EnabledOptions{true, true, true})
inputOpts := &mongodump.InputOptions{}
opts.AddOptions(inputOpts)
outputOpts := &mongodump.OutputOptions{}
opts.AddOptions(outputOpts)
args, err := opts.Parse()
if err != nil {
log.Logvf(log.Always, "error parsing command line options: %v", err)
log.Logvf(log.Always, "try 'mongodump --help' for more information")
os.Exit(util.ExitBadOptions)
}
if len(args) > 0 {
log.Logvf(log.Always, "positional arguments not allowed: %v", args)
log.Logvf(log.Always, "try 'mongodump --help' for more information")
os.Exit(util.ExitBadOptions)
}
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
// init logger
log.SetVerbosity(opts.Verbosity)
// connect directly, unless a replica set name is explicitly specified
_, setName := util.ParseConnectionString(opts.Host)
opts.Direct = (setName == "")
opts.ReplicaSetName = setName
// kick off the progress bar manager
progressManager := progress.NewBarWriter(log.Writer(0), progressBarWaitTime, progressBarLength, false)
progressManager.Start()
defer progressManager.Stop()
dump := mongodump.MongoDump{
ToolOptions: opts,
OutputOptions: outputOpts,
InputOptions: inputOpts,
ProgressManager: progressManager,
}
finishedChan := signals.HandleWithInterrupt(dump.HandleInterrupt)
defer close(finishedChan)
if err = dump.Init(); err != nil {
log.Logvf(log.Always, "Failed: %v", err)
os.Exit(util.ExitError)
}
if err = dump.Dump(); err != nil {
log.Logvf(log.Always, "Failed: %v", err)
if err == util.ErrTerminated {
os.Exit(util.ExitKill)
}
os.Exit(util.ExitError)
}
}
示例15: main
func main() {
go signals.Handle()
// initialize command-line opts
opts := options.New("bsondump", bsondump.Usage, options.EnabledOptions{})
bsonDumpOpts := &bsondump.BSONDumpOptions{}
opts.AddOptions(bsonDumpOpts)
args, err := opts.Parse()
if err != nil {
log.Logf(log.Always, "error parsing command line options: %v", err)
log.Logf(log.Always, "try 'bsondump --help' for more information")
os.Exit(util.ExitBadOptions)
}
// print help, if specified
if opts.PrintHelp(false) {
return
}
// print version, if specified
if opts.PrintVersion() {
return
}
log.SetVerbosity(opts.Verbosity)
if len(args) > 1 {
log.Logf(log.Always, "too many positional arguments: %v", args)
log.Logf(log.Always, "try 'bsondump --help' for more information")
os.Exit(util.ExitBadOptions)
}
// If the user specified a bson input file
if len(args) == 1 {
if bsonDumpOpts.BSONFileName != "" {
log.Logf(log.Always, "Cannot specify both a positional argument and --bsonFile")
os.Exit(util.ExitBadOptions)
}
bsonDumpOpts.BSONFileName = args[0]
}
dumper := bsondump.BSONDump{
ToolOptions: opts,
BSONDumpOptions: bsonDumpOpts,
}
reader, err := bsonDumpOpts.GetBSONReader()
if err != nil {
log.Logf(log.Always, "Getting BSON Reader Failed: %v", err)
os.Exit(util.ExitError)
}
dumper.BSONSource = db.NewBSONSource(reader)
defer dumper.BSONSource.Close()
writer, err := bsonDumpOpts.GetWriter()
if err != nil {
log.Logf(log.Always, "Getting Writer Failed: %v", err)
os.Exit(util.ExitError)
}
dumper.Out = writer
defer dumper.Out.Close()
log.Logf(log.DebugLow, "running bsondump with --objcheck: %v", bsonDumpOpts.ObjCheck)
if len(bsonDumpOpts.Type) != 0 && bsonDumpOpts.Type != "debug" && bsonDumpOpts.Type != "json" {
log.Logf(log.Always, "Unsupported output type '%v'. Must be either 'debug' or 'json'", bsonDumpOpts.Type)
os.Exit(util.ExitBadOptions)
}
var numFound int
if bsonDumpOpts.Type == "debug" {
numFound, err = dumper.Debug()
} else {
numFound, err = dumper.JSON()
}
log.Logf(log.Always, "%v objects found", numFound)
if err != nil {
log.Log(log.Always, err.Error())
os.Exit(util.ExitError)
}
}