use of com.fo0.robot.model.KeyValue in project Robot by fo0.
the class SSHPatternCheck method parsingSSHInputText.
@Test
public void parsingSSHInputText() {
String inputText = "$HOST(github.com) $PORT(22) $CMD(echo test)";
Pattern pattern = CONSTANTS_PATTERN.BASIC_PATTERN;
Matcher m = pattern.matcher(inputText);
List<KeyValue> list = new ArrayList<KeyValue>();
while (m.find()) {
list.add(KeyValue.builder().key(m.group(1)).value(m.group(2)).build());
}
KeyValue host = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.HOST)).findFirst().orElse(null);
KeyValue port = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PORT)).findFirst().orElse(null);
KeyValue cmd = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.CMD)).findFirst().orElse(null);
Assert.assertEquals(host.getValue(), "github.com");
Assert.assertEquals(port.getValue(), "22");
Assert.assertEquals(cmd.getValue(), "echo test");
}
use of com.fo0.robot.model.KeyValue in project Robot by fo0.
the class TypeDownloadChainTest method downloadTestParsing.
@Test
public void downloadTestParsing() {
ActionItem item = ActionItem.builder().type(EActionType.Download).value("$SRC(http://fo0.me/ip.php) $DST(ip.php)").description("description example").build();
List<KeyValue> list = item.parsedValue();
KeyValue url = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
Assert.assertEquals(CONSTANTS_PATTERN.SOURCE, url.getKey());
Assert.assertEquals("http://fo0.me/ip.php", url.getValue());
KeyValue path = list.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(null);
Assert.assertEquals(CONSTANTS_PATTERN.DESTINATION, path.getKey());
Assert.assertEquals("ip.php", path.getValue());
}
use of com.fo0.robot.model.KeyValue in project Robot by fo0.
the class ChainActionItem method targz.
public EChainResponse targz(List<KeyValue> list) throws Exception {
List<KeyValue> zipList = list;
KeyValue zipSrc = zipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
KeyValue zipDest = zipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(KeyValue.builder().build());
ctx.addToLog(type, "SRC: " + zipSrc.getValue());
ctx.addToLog(type, "DST: " + zipDest.getValue());
// old method
// ZipUtils.zip(zipSrc.getValue(), zipDest.getValue());
Archiver zipArchive = ArchiverFactory.createArchiver(ArchiveFormat.TAR, CompressionType.GZIP);
zipArchive.create(new File(zipDest.getValue()).getName(), new File(zipDest.getValue()).getParentFile(), new File(zipSrc.getValue()));
return EChainResponse.Continue;
}
use of com.fo0.robot.model.KeyValue in project Robot by fo0.
the class ChainActionItem method move.
public EChainResponse move(List<KeyValue> list) throws Exception {
List<KeyValue> zipList = list;
KeyValue src = zipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
KeyValue dst = zipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(KeyValue.builder().build());
KeyValue force = zipList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.FORCE)).findFirst().orElse(KeyValue.builder().value("false").build());
ctx.addToLog(type, "SRC: " + src.getValue());
ctx.addToLog(type, "DST: " + dst.getValue());
ctx.addToLog(type, "FORCE: " + force.getValue());
// checking first path
if (!Paths.get(src.getValue()).toAbsolutePath().toFile().exists()) {
ctx.addToLog(type, "Stopping Chain. Could not find src: " + Paths.get(src.getValue()).toAbsolutePath());
return EChainResponse.Break;
}
if (Paths.get(dst.getValue()).toAbsolutePath().toFile().exists()) {
if (StringUtils.equalsAnyIgnoreCase(force.getValue(), "true")) {
// force :: remove destination file
ctx.addToLog(type, "found destination file or folder. Force option has been found, deleting the destination: " + Paths.get(dst.getValue()).toAbsolutePath());
FileUtils.forceDelete(Paths.get(dst.getValue()).toAbsolutePath().toFile());
} else {
ctx.addToLog(type, "Stopping Chain. Destination File exists already and no force has been set." + Paths.get(dst.getValue()).toAbsolutePath());
return EChainResponse.Failed;
}
}
FileUtils.moveFile(Paths.get(src.getValue()).toAbsolutePath().toFile(), Paths.get(dst.getValue()).toAbsolutePath().toFile());
return EChainResponse.Continue;
}
use of com.fo0.robot.model.KeyValue in project Robot by fo0.
the class ChainActionItem method simple_commandline.
public EChainResponse simple_commandline(List<KeyValue> list) throws Exception {
List<KeyValue> cmdList = list;
// @formatter:off
KeyValue HOME = ActionUtils.parseAction(cmdList, CONSTANTS_PATTERN.HOME, System.getProperty("user.dir"));
KeyValue CMD = ActionUtils.parseAction(cmdList, CONSTANTS_PATTERN.CMD, cmdList.stream().map(KeyValue::getValue).findFirst().orElse(null));
// @formatter:on
ctx.addToLog(type, "HOME: " + HOME.getValue());
ctx.addToLog(type, "CMD: " + CMD.getValue());
Commander commander = new Commander(log -> {
ctx.addToLogPlain(type, log);
});
commander.execute(true, true, HOME.getValue(), CMD.getValue());
if (commander == null || commander.isError()) {
ctx.addToLog(type, "error at commander: " + CMD.getKey());
return EChainResponse.Failed;
}
return EChainResponse.Continue;
}
Aggregations