use of org.locationtech.geogig.api.porcelain.ConfigOp.ConfigAction in project GeoGig by boundlessgeo.
the class Config method runInternal.
/**
* Executes the config command using the provided options.
*/
@Override
public void runInternal(GeogigCLI cli) throws IOException {
GeoGIG geogig = cli.getGeogig();
boolean closeIt = geogig == null;
if (closeIt) {
// we're not in a repository, need a geogig anyways to run the global commands
geogig = cli.newGeoGIG(Hints.readOnly());
}
try {
String name = null;
String value = null;
if (nameValuePair != null && !nameValuePair.isEmpty()) {
name = nameValuePair.get(0);
value = buildValueString();
}
ConfigAction action = resolveConfigAction();
if (action == ConfigAction.CONFIG_NO_ACTION) {
printUsage(cli);
throw new CommandFailedException();
}
if (global && local) {
printUsage(cli);
throw new CommandFailedException();
}
ConfigScope scope = ConfigScope.DEFAULT;
if (global) {
scope = ConfigScope.GLOBAL;
} else if (local) {
scope = ConfigScope.LOCAL;
}
final Optional<Map<String, String>> commandResult = geogig.command(ConfigOp.class).setScope(scope).setAction(action).setName(name).setValue(value).call();
if (commandResult.isPresent()) {
switch(action) {
case CONFIG_GET:
{
cli.getConsole().println(commandResult.get().get(name));
break;
}
case CONFIG_LIST:
{
Iterator<Map.Entry<String, String>> it = commandResult.get().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
cli.getConsole().println(pairs.getKey() + "=" + pairs.getValue());
}
break;
}
default:
break;
}
}
} catch (ConfigException e) {
// since we don't have regex support yet.
switch(e.statusCode) {
case INVALID_LOCATION:
// TODO: This could probably be more descriptive.
throw new CommandFailedException("The config location is invalid", e);
case CANNOT_WRITE:
throw new CommandFailedException("Cannot write to the config", e);
case SECTION_OR_NAME_NOT_PROVIDED:
throw new InvalidParameterException("No section or name was provided", e);
case SECTION_OR_KEY_INVALID:
throw new InvalidParameterException("The section or key is invalid", e);
case OPTION_DOES_NOT_EXIST:
throw new InvalidParameterException("Tried to unset an option that does not exist", e);
case MULTIPLE_OPTIONS_MATCH:
throw new InvalidParameterException("Tried to unset/set an option for which multiple lines match", e);
case INVALID_REGEXP:
throw new InvalidParameterException("Tried to use an invalid regexp", e);
case USERHOME_NOT_SET:
throw new InvalidParameterException("Used --global option without $HOME being properly set", e);
case TOO_MANY_ACTIONS:
throw new InvalidParameterException("Tried to use more than one action at a time", e);
case MISSING_SECTION:
throw new InvalidParameterException("Could not find a section with the name provided", e);
case TOO_MANY_ARGS:
throw new InvalidParameterException("Too many arguments provided.", e);
}
} finally {
if (closeIt) {
geogig.close();
}
}
}
Aggregations