Skip to content

Commit

Permalink
chore: Partial data-extract implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Feb 27, 2024
1 parent da6fc5c commit fa36294
Show file tree
Hide file tree
Showing 101 changed files with 4,885 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright 2021, 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.cli.cmd;

import com.fortify.cli.common.action.helper.ActionHelper;
import com.fortify.cli.common.cli.cmd.AbstractRunnableCommand;

import picocli.CommandLine.Parameters;

public abstract class AbstractActionGetCommand extends AbstractRunnableCommand implements Runnable {
@Parameters(arity="1", descriptionKey="fcli.action.run.action") private String action;

@Override
public final void run() {
initMixins();
System.out.println(ActionHelper.loadContents(getType(), action));
}

protected abstract String getType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright 2021, 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.cli.cmd;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fortify.cli.common.action.helper.ActionHelper;
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;

public abstract class AbstractActionListCommand extends AbstractOutputCommand implements IJsonNodeSupplier {
@Override
public final JsonNode getJsonNode() {
return ActionHelper.list(getType())
.map(JsonHelper.getObjectMapper()::valueToTree)
.map(ObjectNode.class::cast)
.collect(JsonHelper.arrayNodeCollector());
}
@Override
public final boolean isSingular() {
return false;
}
protected abstract String getType();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*******************************************************************************
* Copyright 2021, 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.cli.cmd;

import java.util.List;

import org.springframework.expression.spel.support.SimpleEvaluationContext;

import com.fortify.cli.common.action.helper.ActionDescriptor;
import com.fortify.cli.common.action.helper.ActionHelper;
import com.fortify.cli.common.action.helper.ActionRunner;
import com.fortify.cli.common.cli.cmd.AbstractRunnableCommand;
import com.fortify.cli.common.cli.mixin.CommonOptionMixins.OptionParametersMixin;
import com.fortify.cli.common.cli.mixin.CommonOptionMixins.OptionParametersMixin.OptionParameterHelper;
import com.fortify.cli.common.progress.cli.mixin.ProgressWriterFactoryMixin;
import com.fortify.cli.common.progress.helper.IProgressWriterI18n;
import com.fortify.cli.common.util.DisableTest;
import com.fortify.cli.common.util.DisableTest.TestType;

import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

public abstract class AbstractActionRunCommand extends AbstractRunnableCommand implements Runnable {
@Parameters(arity="1", descriptionKey="fcli.action.run.action") private String action;
@Option(names="--action-help", required=false, arity = "0", descriptionKey="fcli.action.run.action-help") private boolean isActionHelpRequested;
@DisableTest({TestType.MULTI_OPT_SPLIT, TestType.MULTI_OPT_PLURAL_NAME, TestType.OPT_LONG_NAME})
@Option(names="--<action-parameter>", paramLabel="<value>", descriptionKey="fcli.action.run.action-parameter")
private List<String> dummyForSynopsis;
@Mixin private OptionParametersMixin actionParameters;
@Mixin private ProgressWriterFactoryMixin progressWriterFactory;

@Override
public final void run() {
initMixins();
Runnable delayedConsoleWriter = null;
try ( var progressWriter = progressWriterFactory.create() ) {
progressWriter.writeProgress("Loading action %s", action);
var actionDescriptor = ActionHelper.load(getType(), action);
try ( var actionRunner = ActionRunner.builder()
.action(actionDescriptor)
.inputParameters(actionParameters.getOptions())
.progressWriter(progressWriter).build() )
{
delayedConsoleWriter = run(actionDescriptor, actionRunner, progressWriter);
}
}
delayedConsoleWriter.run();
}

private Runnable run(ActionDescriptor actionDescriptor, ActionRunner actionRunner, IProgressWriterI18n progressWriter) {
actionRunner.getSpelEvaluator().configure(context->configure(actionRunner, context));
OptionParameterHelper helper = actionParameters.getHelper(actionRunner::configureOptionParameterHelper);
if ( isActionHelpRequested ) {
String actionHelp = getActionHelp(actionDescriptor, helper);
return ()->{System.out.println(actionHelp);};
} else {
helper.validate();
progressWriter.writeProgress("Executing action %s", action);
return actionRunner.execute();
}
}

private final String getActionHelp(ActionDescriptor action, OptionParameterHelper helper) {
var usage = action.getUsage();
return String.format(
"\nAction: %s\n"+
"\n%s\n"+
"\n%s\n"+
"\nAction options:\n"+
"%s",
action.getName(), usage.getHeader(), usage.getDescription(), helper.getSupportedOptionsTable());
}

protected abstract String getType();
protected abstract void configure(ActionRunner actionRunner, SimpleEvaluationContext context);
}
Loading

0 comments on commit fa36294

Please sign in to comment.