All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.carleton.gcrc.couch.command.dump.DumpMain Maven / Gradle / Ivy

There is a newer version: 2.2.7
Show newest version
package ca.carleton.gcrc.couch.command.dump;

import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import ca.carleton.gcrc.couch.app.DbDumpProcess;
import ca.carleton.gcrc.couch.client.CouchDb;
import ca.carleton.gcrc.couch.command.impl.DumpListener;

public class DumpMain {

	static public void main(String[] args) {
		DumpSettings dumpSettings = null;
		
		try {
			List arguments = new ArrayList(args.length);
			for(String arg : args){
				arguments.add(arg);
			}
			
			dumpSettings = new DumpSettings(DumpSettings.Type.DUMP);
			
			DumpMain app = new DumpMain();
			app.execute(dumpSettings, arguments);
			System.exit(0);
			
		} catch(Exception e) {
			
			PrintStream err = System.err;
			if( null != dumpSettings ) {
				err = dumpSettings.getErrStream();
			} 
			
			if( null != dumpSettings 
			 && dumpSettings.isDebug() ){
				e.printStackTrace(err);
				
			} else {
				err.print("Error: "+e.getMessage());
				err.println();
				
				int limit = 10;
				Throwable cause = e.getCause();
				while(null != cause && limit > 0) {
					err.print("Caused by: "+cause.getMessage());
					err.println();
					cause = cause.getCause();
					--limit;
				}
			}
			
			// Error
			System.exit(1);
		}
	}
	
	public void execute(DumpSettings dumpSettings, List args) throws Exception {
		
		// Turn arguments into a stack
		Stack argumentStack = new Stack();
		for(int i=args.size()-1; i>=0; --i){
			argumentStack.push( args.get(i) );
		}
		
		// Process global options
		dumpSettings.parseCommandLineArguments(argumentStack);
		
		// Check for help
		if( dumpSettings.isHelpRequested() ) {
			help( dumpSettings.getOutStream() );
			return;
		}
		
		// Ask the user about the missing options
		dumpSettings.acceptUserOptions();

		// Check that dump dir exists
		File dumpDir = dumpSettings.getDumpDir();
		if( null == dumpDir ) {
			throw new Exception("--dump-dir must be specified");
		}
		
		// Get CouchDb client
		CouchDb couchDb = dumpSettings.createCouchDb();
		
		// Create dump process
		DumpListener listener = new DumpListener( dumpSettings.getOutStream() );
		DbDumpProcess dumpProcess = new DbDumpProcess(couchDb, dumpDir);
		List docIds = dumpSettings.getDocIds();
		if( docIds.size() < 1 ) {
			dumpProcess.setAllDocs(true);
		} else {
			for(String docId : docIds) {
				dumpProcess.addDocId(docId);
			}
		}
		dumpProcess.setListener(listener);
		dumpProcess.dump();
	}
	
	private void help(PrintStream ps) {
		ps.println("couch-dump - Help");
		ps.println();
		ps.println("Command Syntax:");
		ps.println("  couch-dump [");
		ps.println("    Directory where all documents are dumped to.");
		ps.println();
		ps.println("--server ");
		ps.println("    URL to the CouchDb server.");
		ps.println();
		ps.println("--db ");
		ps.println("    Name of the database to dump or restore.");
		ps.println();
		ps.println("--user ");
		ps.println("    User name to be used during the dump or restore.");
		ps.println();
		ps.println("--password ");
		ps.println("    Password associated with the user.");
		ps.println();
		ps.println("--doc-id ");
		ps.println("    If this option is not specified, then all documents are selected.");
		ps.println("    for dump or restore. If this option is specified once or multiple");
		ps.println("    times, then only the requested documents are selected.");
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy