Skip to content

Commit

Permalink
chore: Enable loading actions from files or URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Apr 26, 2024
1 parent bcf8681 commit 1faf72f
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
package com.fortify.cli.common.action.cli.cmd;

import com.fortify.cli.common.action.cli.mixin.ActionSourceResolverMixin;
import com.fortify.cli.common.action.helper.ActionHelper;
import com.fortify.cli.common.action.helper.ActionHelper.ActionInvalidSignatureHandlers;
import com.fortify.cli.common.action.helper.ActionLoaderHelper;
import com.fortify.cli.common.action.helper.ActionLoaderHelper.ActionInvalidSignatureHandlers;
import com.fortify.cli.common.cli.cmd.AbstractRunnableCommand;

import picocli.CommandLine.Mixin;
Expand All @@ -27,7 +27,7 @@ public abstract class AbstractActionGetCommand extends AbstractRunnableCommand {
@Override
public final Integer call() {
initMixins();
System.out.println(ActionHelper.loadActionContents(actionSourceResolver.getActionSources(getType()), action, ActionInvalidSignatureHandlers.WARN));
System.out.println(ActionLoaderHelper.loadActionContents(actionSourceResolver.getActionSources(getType()), action, ActionInvalidSignatureHandlers.WARN));
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
package com.fortify.cli.common.action.cli.cmd;

import com.fortify.cli.common.action.cli.mixin.ActionSourceResolverMixin;
import com.fortify.cli.common.action.helper.ActionHelper;
import com.fortify.cli.common.action.helper.ActionHelper.ActionInvalidSignatureHandlers;
import com.fortify.cli.common.action.helper.ActionLoaderHelper;
import com.fortify.cli.common.action.helper.ActionLoaderHelper.ActionInvalidSignatureHandlers;
import com.fortify.cli.common.action.model.Action;
import com.fortify.cli.common.action.runner.ActionParameterHelper;
import com.fortify.cli.common.cli.cmd.AbstractRunnableCommand;
Expand All @@ -32,7 +32,7 @@ public abstract class AbstractActionHelpCommand extends AbstractRunnableCommand
@Override
public final Integer call() {
initMixins();
var actionDescriptor = ActionHelper.loadAction(actionSourceResolver.getActionSources(getType()), action, ActionInvalidSignatureHandlers.WARN);
var actionDescriptor = ActionLoaderHelper.loadAction(actionSourceResolver.getActionSources(getType()), action, ActionInvalidSignatureHandlers.WARN);
System.out.println(getActionHelp(actionDescriptor));
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.action.cli.mixin.ActionSourceResolverMixin;
import com.fortify.cli.common.action.helper.ActionHelper;
import com.fortify.cli.common.action.helper.ActionHelper.ActionInvalidSignatureHandlers;
import com.fortify.cli.common.action.helper.ActionLoaderHelper;
import com.fortify.cli.common.action.helper.ActionLoaderHelper.ActionInvalidSignatureHandlers;
import com.fortify.cli.common.json.JsonHelper;
import com.fortify.cli.common.output.cli.cmd.AbstractOutputCommand;
import com.fortify.cli.common.output.cli.cmd.IJsonNodeSupplier;
Expand All @@ -27,7 +27,7 @@ public abstract class AbstractActionListCommand extends AbstractOutputCommand im

@Override
public final JsonNode getJsonNode() {
return ActionHelper
return ActionLoaderHelper
.streamAsJson(actionSourceResolver.getActionSources(getType()),ActionInvalidSignatureHandlers.EVALUATE)
.collect(JsonHelper.arrayNodeCollector());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
package com.fortify.cli.common.action.cli.cmd;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.action.helper.ActionHelper;
import com.fortify.cli.common.action.helper.ActionImportHelper;
import com.fortify.cli.common.output.cli.cmd.AbstractOutputCommand;
import com.fortify.cli.common.output.cli.cmd.IJsonNodeSupplier;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;

public abstract class AbstractActionResetCommand extends AbstractOutputCommand implements IJsonNodeSupplier, IActionCommandResultSupplier {
@Override
public final JsonNode getJsonNode() {
return ActionHelper.reset(getType());
return ActionImportHelper.reset(getType());
}
@Override
public String getActionCommandResult() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.springframework.expression.spel.support.SimpleEvaluationContext;

import com.fortify.cli.common.action.cli.mixin.ActionSourceResolverMixin;
import com.fortify.cli.common.action.helper.ActionHelper;
import com.fortify.cli.common.action.helper.ActionLoaderHelper;
import com.fortify.cli.common.action.runner.ActionParameterHelper;
import com.fortify.cli.common.action.runner.ActionRunner;
import com.fortify.cli.common.cli.cmd.AbstractRunnableCommand;
Expand Down Expand Up @@ -52,7 +52,7 @@ public abstract class AbstractActionRunCommand extends AbstractRunnableCommand {
@Override @SneakyThrows
public final Integer call() {
initMixins();
var loadedAction = ActionHelper.loadAction(actionSourceResolver.getActionSources(getType()), action, this::confirmInvalidSignature);
var loadedAction = ActionLoaderHelper.loadAction(actionSourceResolver.getActionSources(getType()), action, this::confirmInvalidSignature);
Callable<Integer> delayedConsoleWriter = null;
try ( var progressWriter = progressWriterFactory.create() ) {
try ( var actionRunner = ActionRunner.builder()
Expand All @@ -69,7 +69,7 @@ public final Integer call() {
}

private void confirmInvalidSignature(SignedTextDescriptor descriptor) {
confirm.checkConfirmed("WARN: "+ActionHelper.getSignatureStatusMessage(descriptor.getSignatureStatus()));
confirm.checkConfirmed("WARN: "+ActionLoaderHelper.getSignatureStatusMessage(descriptor.getSignatureStatus()));
}

private Callable<Integer> run(ActionRunner actionRunner, IProgressWriterI18n progressWriter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.apache.commons.lang3.StringUtils;

import com.fortify.cli.common.action.helper.ActionHelper.ActionSource;
import com.fortify.cli.common.action.helper.ActionLoaderHelper.ActionSource;

import lombok.Getter;
import picocli.CommandLine.Option;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Copyright 2023 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors ("Open Text") are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*/
package com.fortify.cli.common.action.helper;

import java.nio.file.Files;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fortify.cli.common.action.helper.ActionLoaderHelper.ActionInvalidSignatureHandlers;
import com.fortify.cli.common.action.helper.ActionLoaderHelper.ActionSource;
import com.fortify.cli.common.action.model.Action.ActionProperties;
import com.fortify.cli.common.json.JsonHelper;
import com.fortify.cli.common.util.Break;

import lombok.SneakyThrows;

public class ActionImportHelper {
/*
@SneakyThrows
public static final ArrayNode importZip(String type, String source) {
var result = JsonHelper.getObjectMapper().createArrayNode();
IZipEntryProcessor<Boolean> processor = (zis, ze, isCustom)->importEntry(result, zis, getActionName(ze.getName()), type);
try {
var url = new URL(source);
try ( var unirest = createUnirestInstance(type, url) ) {
unirest.get(url.toString()).asObject(r->processZipEntries(r.getContent(), processor, true)).getBody();
}
} catch (MalformedURLException e ) {
try ( var is = Files.newInputStream(Path.of(source)) ) {
processZipEntries(is, processor, true);
}
}
return result;
}
*/

/*
@SneakyThrows
public static final ArrayNode importSingle(String type, String name, String source) {
var result = JsonHelper.getObjectMapper().createArrayNode();
var finalName = StringUtils.isBlank(name) ? getActionName(source) : name;
try {
var url = new URL(source);
try ( var unirest = createUnirestInstance(type, url) ) {
unirest.get(url.toString()).asObject(r->importEntry(result, r.getContent(), finalName, type)).getBody();
}
} catch (MalformedURLException e ) {
try ( var is = Files.newInputStream(Path.of(source)) ) {
importEntry(result, is, finalName, type);
}
}
return result;
}
*/
@SneakyThrows
public static final ArrayNode reset(String type) {
var zipPath = ActionLoaderHelper.customActionsZipPath(type);
var result = ActionLoaderHelper
.streamAsJson(ActionSource.importedActionSources(type), ActionInvalidSignatureHandlers.IGNORE)
.collect(JsonHelper.arrayNodeCollector());
Files.delete(zipPath);
return result;
}
/*
private static final boolean importEntry(ArrayNode importedEntries, InputStream is, String name, String type) {
try {
// Read input stream as string for further processing
var contents = FileUtils.readInputStreamAsString(is, StandardCharsets.US_ASCII);
// Read contents as JsonNode to update importedEntries array after import
var json = yamlObjectMapper.readValue(contents, ObjectNode.class);
// Verify that the template can be successfully parsed and post-processed
yamlObjectMapper.treeToValue(json, Action.class).postLoad(name, true);
// Import entry to zip-file
importEntry(name, type, contents);
// Add JSON to the imported entries array.
importedEntries.add(updateJson(name, true, json));
} catch ( Exception e ) {
LOG.warn("WARN: Skipping "+name+" due to errors, see debug log for details");
LOG.debug("WARN: Skipping "+name+" due to errors", e);
}
return true;
}
@SneakyThrows
private static void importEntry(String name, String type, String contents) {
Map<String, String> env = Collections.singletonMap("create", "true");
try (FileSystem zipfs = FileSystems.newFileSystem(getCustomActionsZipPath(type), env)) {
Path filePath = zipfs.getPath(getActionName(name)+".yaml");
Files.write(filePath, contents.getBytes(StandardCharsets.US_ASCII), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
}
*/
}
Loading

0 comments on commit 1faf72f

Please sign in to comment.