Skip to content

Commit

Permalink
#1199 - replace guava w/ cactoos
Browse files Browse the repository at this point in the history
  • Loading branch information
filfreire committed Feb 17, 2018
1 parent 4bbcf87 commit c0318c3
Show file tree
Hide file tree
Showing 47 changed files with 908 additions and 701 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>0.25.3</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
Expand Down Expand Up @@ -503,6 +508,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/com/rultor/agents/IndexesRequests.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
*/
package com.rultor.agents;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.rultor.spi.SuperAgent;
import com.rultor.spi.Talk;
import com.rultor.spi.Talks;
import java.io.IOException;
import org.cactoos.collection.Mapped;
import org.cactoos.list.SolidList;
import org.cactoos.scalar.MaxOf;
import org.cactoos.scalar.NumberOf;
import org.xembly.Directives;

/**
Expand Down Expand Up @@ -83,20 +84,18 @@ private int index(final Talks talks) throws IOException {
* @throws IOException if the content of the {@link Talk} object can't be
* read
*/
@SuppressWarnings("PMD.AvoidCatchingThrowable")
private int index(final Talk talk) throws IOException {
final Iterable<Integer> indexes = Iterables.transform(
final SolidList<Number> indexes = new SolidList<>(
new Mapped<>(
input -> new NumberOf(input),
talk.read()
.xpath("/talk/archive/log/@index|/talk/request/@index"),
new Function<String, Integer>() {
@Override
public Integer apply(final String input) {
return Integer.parseInt(input);
}
}
.xpath("/talk/archive/log/@index|/talk/request/@index")
)
);
final int index;
if (indexes.iterator().hasNext()) {
index = Ordering.natural().max(indexes);
index = new MaxOf(indexes).intValue();
} else {
index = 0;
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/rultor/agents/daemons/ArchivesDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
package com.rultor.agents.daemons;

import com.amazonaws.services.s3.model.ObjectMetadata;
import com.google.common.base.Joiner;
import com.jcabi.aspects.Immutable;
import com.jcabi.log.Logger;
import com.jcabi.s3.Bucket;
Expand All @@ -53,6 +52,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.input.NullInputStream;
import org.apache.commons.lang3.CharEncoding;
import org.cactoos.text.JoinedText;
import org.xembly.Directive;
import org.xembly.Directives;

Expand Down Expand Up @@ -92,7 +92,8 @@ public Iterable<Directive> process(final XML xml) throws IOException {
final File file = File.createTempFile("rultor", ".log");
final String dir = xml.xpath("/talk/daemon/dir/text()").get(0);
new Shell.Safe(shell).exec(
Joiner.on("; ").join(
new JoinedText(
"; ",
String.format("if [ -d %s ]", SSH.escape(dir)),
String.format("then cd %s", SSH.escape(dir)),
"else echo 'Build directory is absent, internal error'",
Expand All @@ -104,7 +105,7 @@ public Iterable<Directive> process(final XML xml) throws IOException {
"then cat stdout | iconv -f utf-8 -t utf-8 -c | LANG=en_US.UTF-8 col -bx",
"else echo 'Stdout not found, internal error'",
"fi"
),
).asString(),
new NullInputStream(0L),
new FileOutputStream(file),
Logger.stream(Level.WARNING, this)
Expand Down
80 changes: 42 additions & 38 deletions src/main/java/com/rultor/agents/daemons/EndsDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@
*/
package com.rultor.agents.daemons;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Tv;
import com.jcabi.log.Logger;
Expand All @@ -44,10 +38,17 @@
import com.rultor.agents.AbstractAgent;
import com.rultor.agents.shells.TalkShells;
import java.io.IOException;
import java.util.Collection;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.cactoos.Text;
import org.cactoos.collection.Mapped;
import org.cactoos.iterable.Filtered;
import org.cactoos.iterable.Skipped;
import org.cactoos.list.SolidList;
import org.cactoos.text.JoinedText;
import org.cactoos.text.SplitText;
import org.cactoos.text.TextOf;
import org.xembly.Directive;
import org.xembly.Directives;
import org.xembly.Xembler;
Expand Down Expand Up @@ -110,34 +111,35 @@ 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 Collection<String> lines = Lists.newArrayList(
Splitter.on(System.lineSeparator()).split(
EndsDaemon.stdout(shell, dir)
final SolidList<Text> lines = new SolidList<>(
new SplitText(
System.lineSeparator(),
new TextOf(
EndsDaemon.stdout(shell, dir)
)
)
);
final String highlights = Joiner.on("\n").join(
Iterables.transform(
Iterables.filter(
lines,
new Predicate<String>() {
@Override
public boolean apply(final String input) {
return input.startsWith(
EndsDaemon.HIGHLIGHTS_PREFIX
);
}
}
),
new Function<String, String>() {
@Override
public String apply(final String str) {
return StringUtils.removeStart(
str, EndsDaemon.HIGHLIGHTS_PREFIX
);
}
}
final SolidList<String> linesAsString = new SolidList<>(
new Mapped<>(
line -> line.asString(),
lines
)
);
final String highlights = new JoinedText(
"\n",
new Mapped<>(
s -> StringUtils.removeStart(
s.asString(),
EndsDaemon.HIGHLIGHTS_PREFIX
),
new Filtered<>(
input -> input.asString().startsWith(
EndsDaemon.HIGHLIGHTS_PREFIX
),
lines
)
)
).asString();
Logger.info(this, "daemon finished at %s, exit: %d", dir, exit);
return new Directives()
.xpath("/talk/daemon")
Expand All @@ -149,12 +151,13 @@ public String apply(final String str) {
.set(
Xembler.escape(
StringUtils.substring(
Joiner.on(System.lineSeparator()).join(
Iterables.skip(
lines,
Math.max(lines.size() - Tv.SIXTY, 0)
new JoinedText(
System.lineSeparator(),
new Skipped<>(
Math.max(lines.size() - Tv.SIXTY, 0),
linesAsString
)
),
).asString(),
-Tv.HUNDRED * Tv.THOUSAND
)
)
Expand Down Expand Up @@ -197,15 +200,16 @@ private static CharSequence stdout(final Shell shell, final String dir)
return new ShellCommand(
shell,
dir,
Joiner.on(';').join(
new JoinedText(
";",
"size=$(stat -c%s stdout)",
String.format("if [ $size -gt %d ]", max),
"then echo \"Output is too big ($size bytes)\"",
String.format("echo \"You see only the last %d bytes\"", max),
String.format("tail -c %d stdout", max),
"else cat stdout",
"fi"
)
).asString()
).exec();
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/rultor/agents/daemons/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
*/
package com.rultor.agents.daemons;

import com.google.common.base.Joiner;
import com.jcabi.aspects.Immutable;
import com.jcabi.log.Logger;
import com.jcabi.ssh.SSH;
Expand All @@ -40,6 +39,7 @@
import java.util.logging.Level;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.cactoos.text.JoinedText;

/**
* Script to run.
Expand Down Expand Up @@ -85,14 +85,15 @@ public int exec(final XML xml) throws IOException {
Logger.stream(Level.WARNING, this)
);
return new Shell.Empty(shell).exec(
Joiner.on(" && ").join(
new JoinedText(
" && ",
"set -o pipefail",
String.format("cd %s", SSH.escape(dir)),
String.format(
"/bin/bash %s >> stdout 2>&1",
SSH.escape(this.name)
)
)
).asString()
);
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/rultor/agents/daemons/ShellCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
*/
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;
import org.cactoos.text.JoinedText;

/**
* Command to run in a given shell and working directory.
Expand Down Expand Up @@ -84,10 +84,11 @@ final class ShellCommand {
*/
public String exec() throws IOException {
return new Shell.Plain(new Shell.Safe(this.shell)).exec(
Joiner.on(ShellCommand.SHELL_JOINER).join(
new JoinedText(
ShellCommand.SHELL_JOINER,
String.format("cd %s", SSH.escape(this.directory)),
this.command
)
).asString()
);
}

Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/rultor/agents/daemons/StartsDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
*/
package com.rultor.agents.daemons;

import com.google.common.base.Joiner;
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.RetryOnFailure;
import com.jcabi.log.Logger;
Expand All @@ -55,6 +54,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.NullInputStream;
import org.apache.commons.lang3.CharEncoding;
import org.cactoos.text.JoinedText;
import org.xembly.Directive;
import org.xembly.Directives;

Expand Down Expand Up @@ -127,7 +127,8 @@ public String run(final XML xml) throws IOException {
new Shell.Safe(shell).exec(
String.format("cd %s; cat > run.sh", SSH.escape(dir)),
IOUtils.toInputStream(
Joiner.on('\n').join(
new JoinedText(
"\n",
"#!/bin/bash",
"set -x",
"set -e",
Expand All @@ -148,20 +149,21 @@ public String run(final XML xml) throws IOException {
"uptime",
this.upload(shell, dir),
daemon.xpath("script/text()").get(0)
),
).asString(),
CharEncoding.UTF_8
),
Logger.stream(Level.INFO, this),
Logger.stream(Level.WARNING, this)
);
new Shell.Empty(new Shell.Safe(shell)).exec(
Joiner.on(" && ").join(
new JoinedText(
" && ",
String.format("cd %s", SSH.escape(dir)),
"chmod a+x run.sh",
"echo 'run.sh failed to start' > stdout",
// @checkstyle LineLength (1 line)
"( ( nohup ./run.sh </dev/null >stdout 2>&1; echo $? >status ) </dev/null >/dev/null & )"
)
).asString()
);
Logger.info(this, "daemon started at %s", dir);
return dir;
Expand Down Expand Up @@ -210,6 +212,7 @@ private String upload(final Shell shell, final String dir)
* @param dir Dir
* @throws IOException If fails
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private void gpg(final Shell shell, final String dir) throws IOException {
final Collection<XML> entries = this.profile.read().nodes(
"/p/entry[@key='decrypt']/entry"
Expand All @@ -218,11 +221,12 @@ private void gpg(final Shell shell, final String dir) throws IOException {
final String[] names = {"pubring.gpg", "secring.gpg"};
for (final String name : names) {
shell.exec(
Joiner.on(" && ").join(
new JoinedText(
" && ",
String.format("cd %s ", SSH.escape(dir)),
"mkdir -p .gpg",
String.format("cat > \".gpg/%s\"", name)
),
).asString(),
this.ring(name),
Logger.stream(Level.INFO, true),
Logger.stream(Level.WARNING, true)
Expand Down
Loading

0 comments on commit c0318c3

Please sign in to comment.