// Copyright 2011 The MOE Authors All Rights Reserved.
package com.google.devtools.moe.client.directives;
import com.google.common.collect.ImmutableList;
import com.google.devtools.moe.client.AppContext;
import com.google.devtools.moe.client.MoeOptions;
import com.google.devtools.moe.client.MoeProblem;
import com.google.devtools.moe.client.database.Db;
import com.google.devtools.moe.client.database.FileDb;
import com.google.devtools.moe.client.logic.BookkeepingLogic;
import com.google.devtools.moe.client.project.InvalidProject;
import com.google.devtools.moe.client.project.ProjectContext;
import com.google.devtools.moe.client.testing.DummyDb;
import org.kohsuke.args4j.Option;
import java.util.List;
/**
* Perform the necessary checks to update MOE's db.
*
*/
public class BookkeepingDirective implements Directive {
private final BookkeepingOptions options = new BookkeepingOptions();
public BookkeepingDirective() {}
@Override
public BookkeepingOptions getFlags() {
return options;
}
@Override
public int perform() {
ProjectContext context;
try {
context = AppContext.RUN.contextFactory.makeProjectContext(options.configFilename);
} catch (InvalidProject e) {
AppContext.RUN.ui.error(e, "Error creating project");
return 1;
}
Db db;
if (options.dbLocation.equals("dummy")) {
db = new DummyDb(true);
} else {
// TODO(user): also allow for url dbLocation types
try {
db = FileDb.makeDbFromFile(options.dbLocation);
} catch (MoeProblem e) {
AppContext.RUN.ui.error(e, "Error creating DB");
return 1;
}
}
List<String> names = ImmutableList.copyOf(context.migrationConfigs.keySet());
return BookkeepingLogic.bookkeep(names, db, options.dbLocation, context);
}
static class BookkeepingOptions extends MoeOptions {
@Option(name = "--config_file", required = true,
usage = "Location of MOE config file")
String configFilename = "";
@Option(name = "--db", required = true,
usage = "Location of MOE database")
String dbLocation = "";
}
}