ca.carleton.gcrc.couch.command.dump.DumpMain Maven / Gradle / Ivy
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 [
© 2015 - 2025 Weber Informatics LLC | Privacy Policy