-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See #602 & ActionCommand/TestCommand/Test2Command in com.fortify.cli.…
…fod.action.cli.cmd package
- Loading branch information
Showing
4 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/action/cli/cmd/ActionCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* 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.fod.action.cli.cmd; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
|
||
import com.fortify.cli.common.action.model.Action; | ||
import com.fortify.cli.common.action.model.Action.ActionMetadata; | ||
import com.fortify.cli.common.action.model.ActionParameter; | ||
import com.fortify.cli.common.action.runner.ActionParameterHelper; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import picocli.CommandLine; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Model.CommandSpec; | ||
import picocli.CommandLine.Model.OptionSpec; | ||
import picocli.CommandLine.Spec; | ||
|
||
@Command | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class ActionCommand implements Callable<Integer> { | ||
private final Action action; | ||
@Spec | ||
private CommandSpec spec; | ||
|
||
private static final CommandSpec asCommandSpec( | ||
CommandSpec runCmd, // Represents applicable 'action run' command like 'fcli fod action run' | ||
Action action // Action instance to be run | ||
) { | ||
CommandSpec root = CommandSpec.create().resourceBundleBaseName("com.fortify.cli.common.i18n.FortifyCLIMessages"); | ||
CommandSpec newRunCmd = replicateRunCmd(root, runCmd); | ||
CommandSpec actionCmd = CommandSpec.forAnnotatedObject(new ActionCommand(action)); | ||
addUsage(actionCmd, action); | ||
addOptions(actionCmd, action); | ||
newRunCmd.addSubcommand(action.getMetadata().getName(), actionCmd); | ||
return actionCmd; | ||
} | ||
|
||
private static void addUsage(CommandSpec actionCmd, Action action) { | ||
actionCmd.usageMessage().header(action.getUsage().getHeader()); | ||
actionCmd.usageMessage().description(action.getUsage().getDescription()); | ||
|
||
} | ||
|
||
private static void addOptions(CommandSpec actionCmd, Action action) { | ||
for ( var o : ActionParameterHelper.getOptionDescriptors(action) ) { | ||
var optionSpec = OptionSpec.builder(o.getName(), o.getAliases().toArray(String[]::new)) | ||
.arity("1") // TODO arity 0..1 for boolean options, ... | ||
.description(o.getDescription()) | ||
.build(); | ||
actionCmd.addOption(optionSpec); | ||
} | ||
} | ||
|
||
private static CommandSpec replicateRunCmd(CommandSpec root, CommandSpec runCmd) { | ||
// TODO Iterate over runCmd and parents to replicate command structure | ||
var result = CommandSpec.create().name("run"); | ||
result.parent(CommandSpec.create().name("action")); | ||
return result; | ||
} | ||
|
||
|
||
public static final CommandLine asCommandLine( | ||
CommandSpec runCmd, // Represents applicable 'action run' command like 'fcli fod action run' | ||
Action action // Action instance to be run | ||
) { | ||
CommandLine cl = new CommandLine(asCommandSpec(runCmd, action)); | ||
//cl.setDefaultValueProvider(FortifyCLIDefaultValueProvider.getInstance()); | ||
return cl; | ||
} | ||
|
||
public Integer call() { | ||
System.out.println(spec); | ||
spec.options().forEach(o->System.out.println(String.format("%s: %s", o.longestName(), o.getValue()))); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/action/cli/cmd/Test2Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* 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.fod.action.cli.cmd; | ||
|
||
import com.fortify.cli.common.action.cli.mixin.ActionResolverMixin; | ||
import com.fortify.cli.common.action.cli.mixin.ActionValidationMixin; | ||
import com.fortify.cli.common.cli.cmd.AbstractRunnableCommand; | ||
import com.fortify.cli.common.cli.mixin.CommandHelperMixin; | ||
|
||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Mixin; | ||
|
||
@Command(name = "test2") | ||
public class Test2Command extends AbstractRunnableCommand { | ||
@Mixin private ActionResolverMixin.RequiredParameter actionResolver; | ||
@Mixin private CommandHelperMixin commandHelper; | ||
@Mixin private ActionValidationMixin actionValidationMixin; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
var validationHandler = actionValidationMixin.getActionValidationHandler(); | ||
var actionLoadResult = actionResolver.load("FoD", validationHandler); | ||
ActionCommand | ||
.asCommandLine(commandHelper.getCommandSpec(), actionLoadResult.getAction()) | ||
.usage(System.out); | ||
return 0; | ||
} | ||
|
||
|
||
} |
41 changes: 41 additions & 0 deletions
41
fcli-core/fcli-fod/src/main/java/com/fortify/cli/fod/action/cli/cmd/TestCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* 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.fod.action.cli.cmd; | ||
|
||
import com.fortify.cli.common.action.cli.mixin.ActionResolverMixin; | ||
import com.fortify.cli.common.action.cli.mixin.ActionValidationMixin; | ||
import com.fortify.cli.common.cli.cmd.AbstractRunnableCommand; | ||
import com.fortify.cli.common.cli.mixin.CommandHelperMixin; | ||
|
||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Mixin; | ||
import picocli.CommandLine.Unmatched; | ||
|
||
@Command(name = "test") | ||
public class TestCommand extends AbstractRunnableCommand { | ||
@Mixin private ActionResolverMixin.RequiredParameter actionResolver; | ||
@Mixin private CommandHelperMixin commandHelper; | ||
@Mixin private ActionValidationMixin actionValidationMixin; | ||
@Unmatched private String[] actionArgs = new String[] {}; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
var validationHandler = actionValidationMixin.getActionValidationHandler(); | ||
var actionLoadResult = actionResolver.load("FoD", validationHandler); | ||
return ActionCommand | ||
.asCommandLine(commandHelper.getCommandSpec(), actionLoadResult.getAction()) | ||
.execute(actionArgs); | ||
} | ||
|
||
|
||
} |