func main() { // initialize command-line opts 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", "0.0.1", usageStr) inputOpts := &options.InputOptions{} opts.AddOptions(inputOpts) ingestOpts := &options.IngestOptions{} opts.AddOptions(ingestOpts) _, err := opts.Parse() if err != nil { util.Panicf("error parsing command line options: %v", err) } // print help, if specified if opts.PrintHelp() { return } // print version, if specified if opts.PrintVersion() { return } // create a session provider to connect to the db sessionProvider, err := db.InitSessionProvider(*opts) if err != nil { util.Panicf("error initializing database session: %v", err) } importer := mongoimport.MongoImport{ ToolOptions: opts, InputOptions: inputOpts, IngestOptions: ingestOpts, SessionProvider: sessionProvider, } if err = importer.ValidateSettings(); err != nil { util.PrintfTimeStamped("Error validating settings: %v\n", err) os.Exit(1) } numDocs, err := importer.ImportDocuments() if !opts.Quiet { message := fmt.Sprintf("imported 1 object\n") if numDocs != 1 { message = fmt.Sprintf("imported %v objects\n", numDocs) } util.PrintfTimeStamped(message) } if err != nil { fmt.Fprintf(os.Stderr, "Error importing documents: %v\n", err) os.Exit(1) } }
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) } }