Search in sources :

Example 11 with ConfigException

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();
        }
    }
}
Also used : ConfigOp(org.locationtech.geogig.api.porcelain.ConfigOp) ConfigException(org.locationtech.geogig.api.porcelain.ConfigException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) ConfigAction(org.locationtech.geogig.api.porcelain.ConfigOp.ConfigAction) ConfigScope(org.locationtech.geogig.api.porcelain.ConfigOp.ConfigScope) Iterator(java.util.Iterator) Map(java.util.Map) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 12 with ConfigException

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();
        }
    }
}
Also used : ConfigGet(org.locationtech.geogig.api.porcelain.ConfigGet) ConfigException(org.locationtech.geogig.api.porcelain.ConfigException) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 13 with ConfigException

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;
}
Also used : ConfigDatabase(org.locationtech.geogig.storage.ConfigDatabase) ConfigException(org.locationtech.geogig.api.porcelain.ConfigException)

Example 14 with ConfigException

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);
    }
}
Also used : ConfigException(org.locationtech.geogig.api.porcelain.ConfigException) IOException(java.io.IOException)

Aggregations

ConfigException (org.locationtech.geogig.api.porcelain.ConfigException)14 ConfigDatabase (org.locationtech.geogig.storage.ConfigDatabase)6 ConfigOp (org.locationtech.geogig.api.porcelain.ConfigOp)5 Test (org.junit.Test)4 File (java.io.File)2 IOException (java.io.IOException)2 GeoGIG (org.locationtech.geogig.api.GeoGIG)2 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)2 MemoryUsage (java.lang.management.MemoryUsage)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Platform (org.locationtech.geogig.api.Platform)1 Remote (org.locationtech.geogig.api.Remote)1 ResolveGeogigDir (org.locationtech.geogig.api.plumbing.ResolveGeogigDir)1 ConfigGet (org.locationtech.geogig.api.porcelain.ConfigGet)1 ConfigAction (org.locationtech.geogig.api.porcelain.ConfigOp.ConfigAction)1 ConfigScope (org.locationtech.geogig.api.porcelain.ConfigOp.ConfigScope)1 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)1