Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#934 Refactored EndsDaemon to use ShellCommand #1004

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions src/main/java/com/rultor/agents/daemons/EndsDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Tv;
import com.jcabi.log.Logger;
import com.jcabi.ssh.SSH;
import com.jcabi.ssh.Shell;
import com.jcabi.xml.XML;
import com.rultor.Time;
Expand All @@ -59,9 +58,6 @@
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 1.0
* @checkstyle ClassDataAbstractionCoupling (500 lines)
* @todo #567:30min/DEV Refactor this class to use less objects
* and remove checkstyle exception above.
*/
@Immutable
@ToString
Expand All @@ -73,11 +69,6 @@ public final class EndsDaemon extends AbstractAgent {
*/
public static final String HIGHLIGHTS_PREFIX = "RULTOR: ";

/**
* Join shell commands with this string.
*/
private static final String SHELL_JOINER = " && ";

/**
* Ctor.
*/
Expand Down Expand Up @@ -112,12 +103,11 @@ public Iterable<Directive> process(final XML xml) throws IOException {
private Iterable<Directive> end(final Shell shell,
final String dir) throws IOException {
final int exit = EndsDaemon.exit(shell, dir);
final String stdout = new Shell.Plain(new Shell.Safe(shell)).exec(
Joiner.on(EndsDaemon.SHELL_JOINER).join(
String.format("cd %s", SSH.escape(dir)),
"cat stdout"
)
);
final String stdout = new ShellCommand(
shell,
dir,
"cat stdout"
).exec();
final Collection<String> lines = Lists.newArrayList(
Splitter.on(System.lineSeparator()).split(stdout)
);
Expand Down Expand Up @@ -176,13 +166,11 @@ public String apply(final String str) {
*/
private static int exit(final Shell shell, final String dir)
throws IOException {
final String status = new Shell.Plain(new Shell.Safe(shell)).exec(
Joiner.on(EndsDaemon.SHELL_JOINER).join(
String.format("cd %s", SSH.escape(dir)),
"if [ ! -e status ]; then echo 127; exit; fi",
"cat status"
)
).trim().replaceAll("[^0-9]", "");
final String status = new ShellCommand(
shell,
dir,
"if [ ! -e status ]; then echo 127; exit; fi; cat status"
).exec().trim().replaceAll("[^0-9]", "");
final int exit;
if (status.isEmpty()) {
exit = 1;
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/com/rultor/agents/daemons/ShellCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright (c) 2009-2015, rultor.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rultor.agents.daemons;

import com.google.common.base.Joiner;
import com.jcabi.aspects.Immutable;
import com.jcabi.ssh.SSH;
import com.jcabi.ssh.Shell;
import java.io.IOException;

/**
* Command to run in a given shell and working directory.
*
* @author Armin Braun ([email protected])
* @version $Id$
* @since 1.62
*/
@Immutable
final class ShellCommand {

/**
* Join shell commands with this string.
*/
private static final String SHELL_JOINER = " && ";

/**
* Shell to use.
*/
private final transient Shell shell;

/**
* Shell command to run.
*/
private final transient String command;

/**
* Shell command to run.
*/
private final transient String directory;

/**
* Ctor.
* @param shll Shell
* @param dir String working directory
* @param cmd String command to run
*/
ShellCommand(final Shell shll, final String dir, final String cmd) {
this.shell = shll;
this.directory = dir;
this.command = cmd;
}

/**
* Executes the command.
* @return Stdout
* @throws IOException If fails
*/
public String exec() throws IOException {
return new Shell.Plain(new Shell.Safe(this.shell)).exec(
Joiner.on(ShellCommand.SHELL_JOINER).join(
String.format("cd %s", SSH.escape(this.directory)),
this.command
)
);
}

}