use of org.locationtech.geogig.api.porcelain.ConfigException 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();
}
}
}
use of org.locationtech.geogig.api.porcelain.ConfigException in project GeoGig by boundlessgeo.
the class GeogigCLI method unalias.
/**
* If the passed arguments contains an alias, it replaces it by the full command corresponding
* to that alias and returns anew set of arguments
*
* IF not, it returns the passed arguments
*
* @param args
* @return
*/
private String[] unalias(String[] args) {
final String aliasedCommand = args[0];
String configParam = "alias." + aliasedCommand;
boolean closeGeogig = false;
GeoGIG geogig = this.providedGeogig == null ? this.geogig : this.providedGeogig;
if (geogig == null) {
// in case the repo is not initialized yet
closeGeogig = true;
geogig = newGeoGIG(Hints.readOnly());
}
try {
Optional<String> unaliased = Optional.absent();
if (geogig.command(ResolveGeogigDir.class).call().isPresent()) {
unaliased = geogig.command(ConfigGet.class).setName(configParam).call();
}
if (!unaliased.isPresent()) {
unaliased = geogig.command(ConfigGet.class).setGlobal(true).setName(configParam).call();
}
if (!unaliased.isPresent()) {
return args;
}
Iterable<String> tokens = Splitter.on(" ").split(unaliased.get());
List<String> allArgs = Lists.newArrayList(tokens);
allArgs.addAll(Lists.newArrayList(Arrays.copyOfRange(args, 1, args.length)));
return allArgs.toArray(new String[0]);
} catch (ConfigException e) {
return args;
} finally {
if (closeGeogig) {
geogig.close();
}
}
}
use of org.locationtech.geogig.api.porcelain.ConfigException in project GeoGig by boundlessgeo.
the class CacheFactory method getConfig.
@SuppressWarnings("unchecked")
private <T> T getConfig(final String keyword, final T defaultValue) {
final String kw = configKeywordPrefix + "." + keyword;
ConfigDatabase configDatabase = configDb.get();
try {
Optional<? extends Object> value = configDatabase.get(kw, defaultValue.getClass());
if (value.isPresent()) {
LOGGER.trace("Got cache config property {} = {}", kw, value.get());
return (T) value.get();
}
} catch (ConfigException e) {
return defaultValue;
}
return defaultValue;
}
use of org.locationtech.geogig.api.porcelain.ConfigException in project GeoGig by boundlessgeo.
the class IniFileConfigDatabase method getGlobal.
public Optional<String> getGlobal(String key) {
try {
String[] parsed = parse(key);
Optional<String> result = global.get(parsed[0], parsed[1]);
if (result.isPresent() && result.get().length() > 0) {
return result;
} else {
return Optional.absent();
}
} catch (StringIndexOutOfBoundsException e) {
throw new ConfigException(e, StatusCode.SECTION_OR_KEY_INVALID);
} catch (IllegalArgumentException e) {
throw new ConfigException(e, null);
} catch (IOException e) {
throw new ConfigException(e, null);
}
}
Aggregations