Skip to content

Commit

Permalink
#18 - Fixes executing commands with verbose parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-m committed Jun 14, 2016
1 parent 2023c59 commit 82096b9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;
import org.codehaus.plexus.util.cli.Commandline;
import org.codehaus.plexus.util.cli.DefaultConsumer;
import org.codehaus.plexus.util.cli.StreamConsumer;

/**
* Abstract git flow mojo.
Expand Down Expand Up @@ -621,23 +618,16 @@ private CommandResult executeCommand(final Commandline cmd,
cmd.clearArgs();
cmd.addArguments(args);

final StreamConsumer out;
if (verbose) {
out = new DefaultConsumer();
} else {
out = new CommandLineUtils.StringStreamConsumer();
}
final StringBufferStreamConsumer out = new StringBufferStreamConsumer(
verbose);

final CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();

// execute
final int exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);

String errorStr = err.getOutput();
String outStr = "";
if (out instanceof StringStreamConsumer) {
outStr = ((StringStreamConsumer) out).getOutput();
}
String outStr = out.getOutput();

if (failOnError && exitCode != SUCCESS_EXIT_CODE) {
// not all commands print errors to error stream
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2014-2016 Aleksandr Mashchenko.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.amashchenko.maven.plugin.gitflow;

import org.codehaus.plexus.util.cli.StreamConsumer;

public class StringBufferStreamConsumer implements StreamConsumer {
private static final String LS = System.getProperty("line.separator");

private final StringBuffer buffer;

private final boolean printOut;

public StringBufferStreamConsumer() {
this(false);
}

public StringBufferStreamConsumer(boolean printOut) {
this.buffer = new StringBuffer();
this.printOut = printOut;
}

@Override
public void consumeLine(String line) {
if (printOut) {
System.out.println(line);
}

buffer.append(line).append(LS);
}

public String getOutput() {
return buffer.toString();
}
}

0 comments on commit 82096b9

Please sign in to comment.