本文整理匯總了Golang中github.com/mongodb/mongo-tools/common/util.ParseConnectionString函數的典型用法代碼示例。如果您正苦於以下問題:Golang ParseConnectionString函數的具體用法?Golang ParseConnectionString怎麽用?Golang ParseConnectionString使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ParseConnectionString函數的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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)
}
示例2: 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)
}
}
示例3: 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)
}
}
示例4: 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)
}
}
示例5: 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)
//.........這裏部分代碼省略.........
示例6: 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)
}
}
示例7: 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)
}
}
示例8: 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)
}
}
示例9: 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)
}
}
示例10: main
//.........這裏部分代碼省略.........
if statOpts.Json {
factory = stat_consumer.FormatterConstructors["json"]
} else if statOpts.Interactive {
factory = stat_consumer.FormatterConstructors["interactive"]
} else {
factory = stat_consumer.FormatterConstructors[""]
}
formatter := factory(statOpts.RowCount, !statOpts.NoHeaders)
cliFlags := 0
if statOpts.Columns == "" {
cliFlags = line.FlagAlways
if statOpts.Discover {
cliFlags |= line.FlagDiscover
cliFlags |= line.FlagHosts
}
if statOpts.All {
cliFlags |= line.FlagAll
}
if strings.Contains(opts.Host, ",") {
cliFlags |= line.FlagHosts
}
}
var customHeaders []string
if statOpts.Columns != "" {
customHeaders = optionCustomHeaders(statOpts.Columns)
} else if statOpts.AppendColumns != "" {
customHeaders = optionCustomHeaders(statOpts.AppendColumns)
}
var keyNames map[string]string
if statOpts.Deprecated {
keyNames = line.DeprecatedKeyMap()
} else if statOpts.Columns == "" {
keyNames = line.DefaultKeyMap()
} else {
keyNames = optionKeyNames(statOpts.Columns)
}
if statOpts.AppendColumns != "" {
addKN := optionKeyNames(statOpts.AppendColumns)
for k, v := range addKN {
keyNames[k] = v
}
}
readerConfig := &status.ReaderConfig{
HumanReadable: statOpts.HumanReadable == "true",
}
if statOpts.Json {
readerConfig.TimeFormat = "15:04:05"
}
consumer := stat_consumer.NewStatConsumer(cliFlags, customHeaders,
keyNames, readerConfig, formatter, os.Stdout)
seedHosts := util.CreateConnectionAddrs(opts.Host, opts.Port)
var cluster mongostat.ClusterMonitor
if statOpts.Discover || len(seedHosts) > 1 {
cluster = &mongostat.AsyncClusterMonitor{
ReportChan: make(chan *status.ServerStatus),
ErrorChan: make(chan *status.NodeError),
LastStatLines: map[string]*line.StatLine{},
Consumer: consumer,
}
} else {
cluster = &mongostat.SyncClusterMonitor{
ReportChan: make(chan *status.ServerStatus),
ErrorChan: make(chan *status.NodeError),
Consumer: consumer,
}
}
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,
Cluster: cluster,
}
for _, v := range seedHosts {
stat.AddNewNode(v)
}
// kick it off
err = stat.Run()
if err != nil {
log.Logvf(log.Always, "Failed: %v", err)
os.Exit(util.ExitError)
}
}
示例11: 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,
//.........這裏部分代碼省略.........
示例12: main
func main() {
go signals.Handle()
// initialize command-line opts
opts := options.New("mongoimport", mongoimport.Usage,
options.EnabledOptions{Auth: true, Connection: true, Namespace: true})
inputOpts := &mongoimport.InputOptions{}
opts.AddOptions(inputOpts)
ingestOpts := &mongoimport.IngestOptions{}
opts.AddOptions(ingestOpts)
args, err := opts.Parse()
if err != nil {
log.Logf(log.Always, "error parsing command line options: %v", err)
log.Logf(log.Always, "try 'mongoimport --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
// create a session provider to connect to the db
sessionProvider, err := db.NewSessionProvider(*opts)
if err != nil {
log.Logf(log.Always, "error connecting to host: %v", err)
os.Exit(util.ExitError)
}
m := mongoimport.MongoImport{
ToolOptions: opts,
InputOptions: inputOpts,
IngestOptions: ingestOpts,
SessionProvider: sessionProvider,
}
if err = m.ValidateSettings(args); err != nil {
log.Logf(log.Always, "error validating settings: %v", err)
log.Logf(log.Always, "try 'mongoimport --help' for more information")
os.Exit(util.ExitError)
}
numDocs, err := m.ImportDocuments()
if !opts.Quiet {
if err != nil {
log.Logf(log.Always, "Failed: %v", err)
}
message := fmt.Sprintf("imported 1 document")
if numDocs != 1 {
message = fmt.Sprintf("imported %v documents", numDocs)
}
log.Logf(log.Always, message)
}
if err != nil {
os.Exit(util.ExitError)
}
}