Search in sources :

Example 51 with ConsoleReader

use of jline.console.ConsoleReader in project apex-core by apache.

the class ApexCli method run.

public void run() throws IOException {
    ConsoleReader reader = new ConsoleReader();
    reader.setExpandEvents(false);
    reader.setBellEnabled(false);
    try {
        processSourceFile(StramClientUtils.getConfigDir() + "/clirc_system", reader);
    } catch (Exception ex) {
    // ignore
    }
    try {
        processSourceFile(StramClientUtils.getUserDTDirectory() + "/clirc", reader);
    } catch (Exception ex) {
    // ignore
    }
    if (consolePresent) {
        printWelcomeMessage();
        setupCompleter(reader);
        setupHistory(reader);
    //reader.setHandleUserInterrupt(true);
    } else {
        reader.setEchoCharacter((char) 0);
    }
    setupAgents();
    String line;
    PrintWriter out = new PrintWriter(System.out);
    int i = 0;
    while (true) {
        if (commandsToExecute != null) {
            if (i >= commandsToExecute.length) {
                break;
            }
            line = commandsToExecute[i++];
        } else {
            line = readLine(reader);
            if (line == null) {
                break;
            }
        }
        processLine(line, reader, true);
        out.flush();
    }
    if (topLevelHistory != null) {
        try {
            topLevelHistory.flush();
        } catch (IOException ex) {
            LOG.warn("Cannot flush command history", ex);
        }
    }
    if (changingLogicalPlanHistory != null) {
        try {
            changingLogicalPlanHistory.flush();
        } catch (IOException ex) {
            LOG.warn("Cannot flush command history", ex);
        }
    }
    if (consolePresent) {
        System.out.println("exit");
    }
}
Also used : ConsoleReader(jline.console.ConsoleReader) IOException(java.io.IOException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.commons.cli.ParseException) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) PrintWriter(java.io.PrintWriter)

Example 52 with ConsoleReader

use of jline.console.ConsoleReader in project GeoGig by boundlessgeo.

the class Apply method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(patchFiles.size() < 2, "Only one single patch file accepted");
    checkParameter(!patchFiles.isEmpty(), "No patch file specified");
    ConsoleReader console = cli.getConsole();
    GeoGIG geogig = cli.getGeogig();
    File patchFile = new File(patchFiles.get(0));
    checkParameter(patchFile.exists(), "Patch file cannot be found");
    FileInputStream stream;
    try {
        stream = new FileInputStream(patchFile);
    } catch (FileNotFoundException e1) {
        throw new CommandFailedException("Can't open patch file " + patchFile, e1);
    }
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        Closeables.closeQuietly(reader);
        Closeables.closeQuietly(stream);
        throw new CommandFailedException("Error reading patch file " + patchFile, e);
    }
    Patch patch = PatchSerializer.read(reader);
    Closeables.closeQuietly(reader);
    Closeables.closeQuietly(stream);
    if (reverse) {
        patch = patch.reversed();
    }
    if (summary) {
        console.println(patch.toString());
    } else if (check) {
        VerifyPatchResults verify = cli.getGeogig().command(VerifyPatchOp.class).setPatch(patch).call();
        Patch toReject = verify.getToReject();
        Patch toApply = verify.getToApply();
        if (toReject.isEmpty()) {
            console.println("Patch can be applied.");
        } else {
            console.println("Error: Patch cannot be applied\n");
            console.println("Applicable entries:\n");
            console.println(toApply.toString());
            console.println("\nConflicting entries:\n");
            console.println(toReject.toString());
        }
    } else {
        try {
            Patch rejected = geogig.command(ApplyPatchOp.class).setPatch(patch).setApplyPartial(reject).call();
            if (reject) {
                if (rejected.isEmpty()) {
                    console.println("Patch applied succesfully");
                } else {
                    int accepted = patch.count() - rejected.count();
                    StringBuilder sb = new StringBuilder();
                    File file = new File(patchFile.getAbsolutePath() + ".rej");
                    sb.append("Patch applied only partially.\n");
                    sb.append(Integer.toString(accepted) + " changes were applied.\n");
                    sb.append(Integer.toString(rejected.count()) + " changes were rejected.\n");
                    BufferedWriter writer = Files.newWriter(file, Charsets.UTF_8);
                    PatchSerializer.write(writer, patch);
                    writer.flush();
                    writer.close();
                    sb.append("Patch file with rejected changes created at " + file.getAbsolutePath() + "\n");
                    throw new CommandFailedException(sb.toString());
                }
            } else {
                console.println("Patch applied succesfully");
            }
        } catch (CannotApplyPatchException e) {
            throw new CommandFailedException(e);
        }
    }
}
Also used : ConsoleReader(jline.console.ConsoleReader) InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VerifyPatchResults(org.locationtech.geogig.api.plumbing.diff.VerifyPatchResults) FileInputStream(java.io.FileInputStream) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) BufferedWriter(java.io.BufferedWriter) BufferedReader(java.io.BufferedReader) CannotApplyPatchException(org.locationtech.geogig.api.porcelain.CannotApplyPatchException) File(java.io.File) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) VerifyPatchOp(org.locationtech.geogig.api.plumbing.diff.VerifyPatchOp) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 53 with ConsoleReader

use of jline.console.ConsoleReader in project GeoGig by boundlessgeo.

the class Branch method runInternal.

@Override
public void runInternal(final GeogigCLI cli) throws IOException {
    final GeoGIG geogig = cli.getGeogig();
    final ConsoleReader console = cli.getConsole();
    if (delete) {
        checkParameter(!branchName.isEmpty(), "no name specified for deletion");
        for (String br : branchName) {
            Optional<? extends Ref> deletedBranch;
            deletedBranch = geogig.command(BranchDeleteOp.class).setName(br).call();
            checkParameter(deletedBranch.isPresent(), "No branch called '%s'.", br);
            console.println(String.format("Deleted branch '%s'.", br));
        }
        return;
    }
    checkParameter(branchName.size() < 3, "too many arguments: %s", branchName);
    if (rename) {
        checkParameter(!branchName.isEmpty(), "You must specify a branch to rename.");
        if (branchName.size() == 1) {
            Optional<Ref> headRef = geogig.command(RefParse.class).setName(Ref.HEAD).call();
            geogig.command(BranchRenameOp.class).setNewName(branchName.get(0)).setForce(force).call();
            if (headRef.isPresent()) {
                SymRef ref = (SymRef) headRef.get();
                console.println("renamed branch '" + ref.getTarget().substring(Ref.HEADS_PREFIX.length()) + "' to '" + branchName.get(0) + "'");
            }
        } else {
            geogig.command(BranchRenameOp.class).setOldName(branchName.get(0)).setNewName(branchName.get(1)).setForce(force).call();
            console.println("renamed branch '" + branchName.get(0) + "' to '" + branchName.get(1) + "'");
        }
        return;
    }
    if (branchName.isEmpty()) {
        listBranches(cli);
        return;
    }
    final String branch = branchName.get(0);
    final String origin = branchName.size() > 1 ? branchName.get(1) : Ref.HEAD;
    Ref newBranch = geogig.command(BranchCreateOp.class).setName(branch).setForce(force).setOrphan(orphan).setAutoCheckout(checkout).setSource(origin).call();
    console.println("Created branch " + newBranch.getName());
}
Also used : Ref(org.locationtech.geogig.api.Ref) SymRef(org.locationtech.geogig.api.SymRef) BranchRenameOp(org.locationtech.geogig.api.porcelain.BranchRenameOp) SymRef(org.locationtech.geogig.api.SymRef) ConsoleReader(jline.console.ConsoleReader) BranchDeleteOp(org.locationtech.geogig.api.porcelain.BranchDeleteOp) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 54 with ConsoleReader

use of jline.console.ConsoleReader in project GeoGig by boundlessgeo.

the class Checkout method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    final GeoGIG geogig = cli.getGeogig();
    checkParameter(branchOrStartPoint.size() != 0 || !paths.isEmpty(), "no branch or paths specified");
    checkParameter(branchOrStartPoint.size() < 2, "too many arguments");
    try {
        final ConsoleReader console = cli.getConsole();
        String branchOrCommit = (branchOrStartPoint.size() > 0 ? branchOrStartPoint.get(0) : null);
        CheckoutResult result = geogig.command(CheckoutOp.class).setForce(force).setSource(branchOrCommit).addPaths(paths).setOurs(ours).setTheirs(theirs).call();
        switch(result.getResult()) {
            case CHECKOUT_LOCAL_BRANCH:
                console.println("Switched to branch '" + result.getNewRef().localName() + "'");
                break;
            case CHECKOUT_REMOTE_BRANCH:
                console.println("Branch '" + result.getNewRef().localName() + "' was set up to track remote branch '" + result.getNewRef().localName() + "' from " + result.getRemoteName() + ".");
                console.println("Switched to a new branch '" + result.getNewRef().localName() + "'");
                break;
            case UPDATE_OBJECTS:
                console.println("Objects in the working tree were updated to the specifed version.");
                break;
            case DETACHED_HEAD:
                console.println("You are in 'detached HEAD' state. HEAD is now at " + result.getOid().toString().substring(0, 7) + "...");
                break;
            default:
                break;
        }
    } catch (CheckoutException e) {
        switch(e.statusCode) {
            case LOCAL_CHANGES_NOT_COMMITTED:
                throw new CommandFailedException("Working tree and index are not clean. To overwrite local changes, use the --force option", e);
            case UNMERGED_PATHS:
                throw new CommandFailedException(e.getMessage(), e);
        }
    }
}
Also used : ConsoleReader(jline.console.ConsoleReader) CheckoutResult(org.locationtech.geogig.api.porcelain.CheckoutResult) GeoGIG(org.locationtech.geogig.api.GeoGIG) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) CheckoutException(org.locationtech.geogig.api.porcelain.CheckoutException)

Example 55 with ConsoleReader

use of jline.console.ConsoleReader in project GeoGig by boundlessgeo.

the class Insert method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    ConsoleReader console = cli.getConsole();
    geogig = cli.getGeogig();
    Iterable<String> lines = null;
    if (filepath != null) {
        File file = new File(filepath);
        checkParameter(file.exists(), "Insert file cannot be found");
        lines = Files.readLines(file, Charsets.UTF_8);
    } else {
        String featuresText = Joiner.on("\n").join(inputs);
        lines = Splitter.on("\n").split(featuresText);
    }
    Map<String, List<Feature>> features = readFeatures(lines);
    long count = 0;
    for (String key : features.keySet()) {
        List<Feature> treeFeatures = features.get(key);
        geogig.getRepository().workingTree().insert(key, treeFeatures.iterator(), cli.getProgressListener(), null, treeFeatures.size());
        count += treeFeatures.size();
    }
    console.print(Long.toString(count) + " features successfully inserted.");
}
Also used : ConsoleReader(jline.console.ConsoleReader) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Feature(org.opengis.feature.Feature)

Aggregations

ConsoleReader (jline.console.ConsoleReader)97 UnsupportedTerminal (jline.UnsupportedTerminal)44 GeogigCLI (org.locationtech.geogig.cli.GeogigCLI)42 GeoGIG (org.locationtech.geogig.api.GeoGIG)22 IOException (java.io.IOException)19 Before (org.junit.Before)19 Test (org.junit.Test)17 File (java.io.File)12 MockitoException (org.mockito.exceptions.base.MockitoException)12 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)11 ObjectId (org.locationtech.geogig.api.ObjectId)9 TestPlatform (org.locationtech.geogig.api.TestPlatform)8 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)8 Ref (org.locationtech.geogig.api.Ref)7 CLITestContextBuilder (org.locationtech.geogig.cli.test.functional.general.CLITestContextBuilder)7 Ansi (org.fusesource.jansi.Ansi)6 NodeRef (org.locationtech.geogig.api.NodeRef)6 RevCommit (org.locationtech.geogig.api.RevCommit)6 FileInputStream (java.io.FileInputStream)4 RevObject (org.locationtech.geogig.api.RevObject)4