Search in sources :

Example 1 with ConfigAction

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();
        }
    }
}
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)

Aggregations

Iterator (java.util.Iterator)1 Map (java.util.Map)1 GeoGIG (org.locationtech.geogig.api.GeoGIG)1 ConfigException (org.locationtech.geogig.api.porcelain.ConfigException)1 ConfigOp (org.locationtech.geogig.api.porcelain.ConfigOp)1 ConfigAction (org.locationtech.geogig.api.porcelain.ConfigOp.ConfigAction)1 ConfigScope (org.locationtech.geogig.api.porcelain.ConfigOp.ConfigScope)1 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)1 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)1