Search in sources :

Example 1 with GfshHistory

use of org.apache.geode.management.internal.cli.shell.jline.GfshHistory in project geode by apache.

the class ShellCommands method history.

@CliCommand(value = CliStrings.HISTORY, help = CliStrings.HISTORY__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GFSH })
public Result history(@CliOption(key = { CliStrings.HISTORY__FILE }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.HISTORY__FILE__HELP) String saveHistoryTo, @CliOption(key = { CliStrings.HISTORY__CLEAR }, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.HISTORY__CLEAR__HELP) Boolean clearHistory) {
    // process clear history
    if (clearHistory) {
        return executeClearHistory();
    } else {
        // Process file option
        Gfsh gfsh = Gfsh.getCurrentInstance();
        ErrorResultData errorResultData = null;
        StringBuilder contents = new StringBuilder();
        Writer output = null;
        int historySize = gfsh.getHistorySize();
        String historySizeString = String.valueOf(historySize);
        int historySizeWordLength = historySizeString.length();
        GfshHistory gfshHistory = gfsh.getGfshHistory();
        Iterator<?> it = gfshHistory.entries();
        boolean flagForLineNumbers = !(saveHistoryTo != null && saveHistoryTo.length() > 0);
        long lineNumber = 0;
        while (it.hasNext()) {
            String line = it.next().toString();
            if (line.isEmpty() == false) {
                if (flagForLineNumbers) {
                    lineNumber++;
                    contents.append(String.format("%" + historySizeWordLength + "s  ", lineNumber));
                }
                contents.append(line);
                contents.append(GfshParser.LINE_SEPARATOR);
            }
        }
        try {
            // write to a user file
            if (saveHistoryTo != null && saveHistoryTo.length() > 0) {
                File saveHistoryToFile = new File(saveHistoryTo);
                output = new BufferedWriter(new FileWriter(saveHistoryToFile));
                if (!saveHistoryToFile.exists()) {
                    errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.HISTORY__MSG__FILE_DOES_NOT_EXISTS);
                    return ResultBuilder.buildResult(errorResultData);
                }
                if (!saveHistoryToFile.isFile()) {
                    errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.HISTORY__MSG__FILE_SHOULD_NOT_BE_DIRECTORY);
                    return ResultBuilder.buildResult(errorResultData);
                }
                if (!saveHistoryToFile.canWrite()) {
                    errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.HISTORY__MSG__FILE_CANNOT_BE_WRITTEN);
                    return ResultBuilder.buildResult(errorResultData);
                }
                output.write(contents.toString());
            }
        } catch (IOException ex) {
            return ResultBuilder.createInfoResult("File error " + ex.getMessage() + " for file " + saveHistoryTo);
        } finally {
            try {
                if (output != null) {
                    output.close();
                }
            } catch (IOException e) {
                errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine("exception in closing file");
                return ResultBuilder.buildResult(errorResultData);
            }
        }
        if (saveHistoryTo != null && saveHistoryTo.length() > 0) {
            // since written to file no need to display the content
            return ResultBuilder.createInfoResult("Wrote successfully to file " + saveHistoryTo);
        } else {
            return ResultBuilder.createInfoResult(contents.toString());
        }
    }
}
Also used : GfshHistory(org.apache.geode.management.internal.cli.shell.jline.GfshHistory) FileWriter(java.io.FileWriter) IOException(java.io.IOException) ConnectionEndpoint(org.apache.geode.management.internal.cli.util.ConnectionEndpoint) ConverterHint(org.apache.geode.management.cli.ConverterHint) BufferedWriter(java.io.BufferedWriter) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) File(java.io.File) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Aggregations

BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 CliMetaData (org.apache.geode.management.cli.CliMetaData)1 ConverterHint (org.apache.geode.management.cli.ConverterHint)1 ErrorResultData (org.apache.geode.management.internal.cli.result.ErrorResultData)1 Gfsh (org.apache.geode.management.internal.cli.shell.Gfsh)1 GfshHistory (org.apache.geode.management.internal.cli.shell.jline.GfshHistory)1 ConnectionEndpoint (org.apache.geode.management.internal.cli.util.ConnectionEndpoint)1 CliCommand (org.springframework.shell.core.annotation.CliCommand)1