Skip to content

Commit

Permalink
chore: Option & positional parameter refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Oct 16, 2023
1 parent 0e9d4c3 commit c99be89
Show file tree
Hide file tree
Showing 23 changed files with 60 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ private void checkOptionDescription(Results results, CommandSpec cmdSpec, Option
var descriptionArray = optionSpec.description();
if ( descriptionArray.length==0 || Stream.of(descriptionArray).allMatch(StringUtils::isBlank) ) {
String descriptionKey = StringUtils.isBlank(optionSpec.descriptionKey()) ? "<default>" : optionSpec.descriptionKey();
results.add(TestType.OPT_EMPTY_DESCRIPTION, Level.ERROR, cmdSpec, optionSpec, String.format("%s: Option has an empty description (descriptionKey=%s)", getBundleName(cmdSpec), descriptionKey));
String cmd = cmdSpec.qualifiedName();
results.add(TestType.OPT_EMPTY_DESCRIPTION, Level.ERROR, cmdSpec, optionSpec, String.format("%s: Option has an empty description (%s, descriptionKey=%s)", getBundleName(cmdSpec), cmd, descriptionKey));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@
public class CommonOptionMixins {
private CommonOptionMixins() {}

public static class OptionalOutputFile {
@Option(names = {"-f", "--output-file"})
@Getter private File outputFile;
public static class OptionalFile {
@Option(names = {"-f", "--file"})
@Getter private File file;
}

public static class RequiredFile {
@Option(names = {"-f", "--file"}, required=true)
@Getter private File file;
}

public static class RequireConfirmation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ log-level = Set logging level. Note that DEBUG and TRACE levels may result in se
being written to the log file. Allowed values: ${COMPLETION-CANDIDATES}.
log-file = File where logging data will be written. Defaults to fcli.log in current directory \
if --log-level is specified.
output-file = Output file

# Generic, non command-specific output and query options
arggroup.output.heading = Output options:%n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.nio.file.Path;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.cli.util.EnvSuffix;
import com.fortify.cli.common.cli.mixin.CommonOptionMixins;
import com.fortify.cli.common.http.ssl.truststore.helper.TrustStoreConfigDescriptor;
import com.fortify.cli.common.http.ssl.truststore.helper.TrustStoreConfigHelper;
import com.fortify.cli.common.output.cli.cmd.AbstractOutputCommand;
Expand All @@ -30,24 +30,22 @@
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

@Command(name=OutputHelperMixins.Set.CMD_NAME)
public class TrustStoreSetCommand extends AbstractOutputCommand implements IJsonNodeSupplier, IActionCommandResultSupplier, IRecordTransformer {
@Mixin @Getter private OutputHelperMixins.Set outputHelper;

@EnvSuffix("PATH") @Parameters(index = "0", arity = "1", descriptionKey = "fcli.config.truststore.set.trustStorePath")
private Path trustStorePath;
@Mixin CommonOptionMixins.RequiredFile fileMixin;

@Option(names = {"-p", "--truststore-password"})
@Option(names = {"-p", "--password"})
private String trustStorePassword;

@Option(names = {"-t", "--truststore-type"}, defaultValue = "jks")
@Option(names = {"-t", "--type"}, defaultValue = "jks")
private String trustStoreType;

@Override
public JsonNode getJsonNode() {
Path absolutePath = trustStorePath.toAbsolutePath();
Path absolutePath = fileMixin.getFile().toPath().toAbsolutePath();
if ( !Files.exists(absolutePath) ) {
throw new IllegalArgumentException("Trust store cannot be found: "+absolutePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ fcli.config.truststore.usage.header = Manage fcli trust store configuration.
fcli.config.truststore.clear.usage.header = Clear SSL trust store configuration to use default trust store.
fcli.config.truststore.get.usage.header = Get current SSL trust store configuration.
fcli.config.truststore.set.usage.header = Configure SSL trust store.
fcli.config.truststore.set.trustStorePath = Path to custom SSL trust store.
fcli.config.truststore.set.truststore-password = SSL trust store password.
fcli.config.truststore.set.truststore-type = SSL trust store type (jks, pkcs12).
fcli.config.truststore.set.file = Path to custom SSL trust store file.
fcli.config.truststore.set.password = Optional SSL trust store password.
fcli.config.truststore.set.type = SSL trust store type (jks, pkcs12).

#################################################################################################################
# The following section lists human-readable header names used by table and tree output formats;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
public class SCDastScanDownloadCommand extends AbstractSCDastOutputCommand implements IJsonNodeSupplier, IActionCommandResultSupplier {
@Getter @Mixin private OutputHelperMixins.Download outputHelper;
@Mixin private SCDastScanResolverMixin.PositionalParameter scanResolver;
@Mixin private CommonOptionMixins.OptionalOutputFile optionalDestination;
@Mixin private CommonOptionMixins.OptionalFile optionalDestination;
@Option(names = {"-t", "--type"}, required=true, converter = DownloadTypeConverter.class, completionCandidates = DownloadTypeIterable.class)
@Getter private DownloadType type;

@Override
public JsonNode getJsonNode() {
var unirest = getUnirestInstance();
SCDastScanDescriptor descriptor = scanResolver.getScanDescriptor(unirest);
File downloadPath = optionalDestination.getOutputFile();
File downloadPath = optionalDestination.getFile();
if ( downloadPath==null ) {
String identifier = StringUtils.isBlank(descriptor.getName())
? String.format("scan-%s", descriptor.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fortify.cli.common.cli.util.EnvSuffix;
import com.fortify.cli.common.output.cli.cmd.IJsonNodeSupplier;
import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
Expand All @@ -32,12 +31,11 @@
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

@Command(name = OutputHelperMixins.Start.CMD_NAME)
public final class SCDastScanStartCommand extends AbstractSCDastOutputCommand implements IJsonNodeSupplier, IActionCommandResultSupplier {
@Getter @Mixin private OutputHelperMixins.Start outputHelper;
@EnvSuffix("NAME") @Parameters(index = "0", arity = "1", paramLabel = "name") private String scanName;
@Option(names = {"--name", "-n"}, required=true) private String scanName;
@Mixin private SCDastScanSettingsResolverMixin.RequiredOption scanSettingsResolver;
@Mixin private SCDastScanPolicyResolverMixin.OptionalOption scanPolicyResolver;
private final ObjectMapper objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ scan = Single scan id.
fcli.sc-dast.scan.usage.header = Manage ScanCentral DAST scans and results.
fcli.sc-dast.scan.download.usage.header = Download scan fpr, logs, results, settings or site tree.
fcli.sc-dast.scan.download.type = Artifact type to download: ${COMPLETION-CANDIDATES}.
fcli.sc-dast.scan.download.file = Optional output file name.
fcli.sc-dast.scan.get.usage.header = Get the details for a single scan.
fcli.sc-dast.scan.list.usage.header = List and query scans.
# TODO Describe date/time format for these options
fcli.sc-dast.scan.list.started-after = List scans started after the specified date.
fcli.sc-dast.scan.list.started-before = List scans started before the specified date.
fcli.sc-dast.scan.start.usage.header = Start a new scan.
fcli.sc-dast.scan.start.name[0] = The name for this scan.
fcli.sc-dast.scan.start.name = The name for this scan.
fcli.sc-dast.scan.start.overrides-file = File containing override values for the scan.
fcli.sc-dast.scan.start.mode = Overrides the scan mode. Accepted values are: ${COMPLETION-CANDIDATES}.
fcli.sc-dast.scan.start.login-macro = Overrides the scan login macro binary file id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class SSCAppVersionDownloadStateCommand extends AbstractSSCOutputCommand
public JsonNode getJsonNode() {
var unirest = getUnirestInstance();
SSCAppVersionDescriptor av = parentResolver.getAppVersionDescriptor(unirest);
File destination = downloadOptions.getDestination().getOutputFile();
File destination = downloadOptions.getDestination().getFile();
if ( destination==null ) {
destination = new File(String.format("./%s_%s.fpr", av.getApplicationName(), av.getVersionName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@Command(name = OutputHelperMixins.Set.CMD_NAME)
public class SSCAppVersionAttributeSetCommand extends AbstractSSCJsonNodeOutputCommand {
@Getter @Mixin private OutputHelperMixins.Set outputHelper;
@Mixin private SSCAppVersionAttributeUpdateMixin.RequiredPositionalParameter attrUpdateMixin;
@Mixin private SSCAppVersionAttributeUpdateMixin.RequiredAttrOption attrUpdateMixin;
@Mixin private SSCAppVersionResolverMixin.RequiredOption parentResolver;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@

import java.util.Map;

import com.fortify.cli.common.cli.util.EnvSuffix;

import lombok.Getter;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

public class SSCAppVersionAttributeUpdateMixin {
private static final String PARAM_LABEL = "[CATEGORY:]ATTR=VALUE[,VALUE...]";
Expand All @@ -32,10 +29,8 @@ public static class OptionalAttrOption extends AbstractSSCAppVersionAttributeUpd
@Getter private Map<String,String> attributes;
}

public static class RequiredPositionalParameter extends AbstractSSCAppVersionAttributeUpdateMixin {
@EnvSuffix("ATTRS") @Parameters(index = "0..*", arity = "1..*", paramLabel = PARAM_LABEL, descriptionKey = "fcli.ssc.appversion-attribute.update.param")
public static class RequiredAttrOption extends AbstractSSCAppVersionAttributeUpdateMixin {
@Option(names = {"--attrs", "--attributes"}, required = true, split = ",", paramLabel = PARAM_LABEL, descriptionKey = "fcli.ssc.appversion-attribute.update.option")
@Getter private Map<String,String> attributes;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class SSCArtifactDownloadCommand extends AbstractSSCArtifactOutputCommand
public JsonNode getJsonNode() {
var unirest = getUnirestInstance();
SSCArtifactDescriptor descriptor = artifactResolver.getArtifactDescriptor(unirest);
File destination = downloadOptions.getDestination().getOutputFile();
File destination = downloadOptions.getDestination().getFile();
if ( destination==null ) {
destination = new File(String.format("./artifact_%s.fpr", descriptor.getId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@

import java.io.File;

import com.fortify.cli.common.cli.util.EnvSuffix;
import com.fortify.cli.common.cli.mixin.CommonOptionMixins;
import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;

import lombok.Getter;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

@Command(name = OutputHelperMixins.Upload.CMD_NAME)
public class SSCArtifactUploadCommand extends AbstractSSCArtifactUploadCommand {
@Getter @Mixin private OutputHelperMixins.Upload outputHelper;
@EnvSuffix("FILE") @Getter @Parameters(arity="1", descriptionKey = "fcli.ssc.artifact.upload.file")
private File file;
@Mixin private CommonOptionMixins.RequiredFile fileMixin;

@Option(names = {"-e", "--engine-type"})
@Getter private String engineType;
Expand All @@ -36,4 +34,9 @@ public class SSCArtifactUploadCommand extends AbstractSSCArtifactUploadCommand {
public boolean isSingular() {
return true;
}

@Override
protected File getFile() {
return fileMixin.getFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

@Getter
public class SSCArtifactDownloadOptions {
@Mixin private CommonOptionMixins.OptionalOutputFile destination;
@Mixin private CommonOptionMixins.OptionalFile destination;
@Option(names = "--no-include-sources", negatable = true, descriptionKey = "download.no-include-sources") private boolean includeSources = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
*******************************************************************************/
package com.fortify.cli.ssc.issue_template.cli.cmd;

import java.io.File;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fortify.cli.common.cli.util.EnvSuffix;
import com.fortify.cli.common.cli.mixin.CommonOptionMixins;
import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.ssc._common.output.cli.cmd.AbstractSSCJsonNodeOutputCommand;
Expand All @@ -28,15 +26,12 @@
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

@Command(name = OutputHelperMixins.Create.CMD_NAME)
public class SSCIssueTemplateCreateCommand extends AbstractSSCJsonNodeOutputCommand implements IActionCommandResultSupplier {
@Getter @Mixin private OutputHelperMixins.Create outputHelper;
@EnvSuffix("ISSUE_TEMPLATE") @Parameters(index = "0", arity = "1", descriptionKey = "fcli.ssc.issue-template.name")
private String issueTemplateName;
@Option(names={"--issue-template-file","-f"}, required = true)
private String fileName;
@Option(names={"--name","-n"}, required = true) private String issueTemplateName;
@Mixin private CommonOptionMixins.RequiredFile fileMixin;
@Option(names={"--description","-d"}, required = false, defaultValue = "")
private String description;
@Option(names={"--set-as-default"})
Expand All @@ -49,7 +44,7 @@ public JsonNode getJsonNode(UnirestInstance unirest) {
.queryString("description", description)
.queryString("confirmIgnoreCustomTagUpdates", "true")
.multiPartContent()
.field("file", new File(fileName))
.field("file", fileMixin.getFile())
.asObject(JsonNode.class).getBody();
if ( setAsDefault ) {
ObjectNode data = (ObjectNode)body.get("data").deepCopy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@Command(name = OutputHelperMixins.Download.CMD_NAME)
public class SSCIssueTemplateDownloadCommand extends AbstractSSCJsonNodeOutputCommand implements IActionCommandResultSupplier {
@Getter @Mixin private OutputHelperMixins.Download outputHelper;
@Mixin private CommonOptionMixins.OptionalOutputFile outputFileMixin;
@Mixin private CommonOptionMixins.OptionalFile fileMixin;

@CommandLine.Mixin
private SSCIssueTemplateResolverMixin.PositionalParameterSingle issueTemplateResolver;
Expand All @@ -42,7 +42,7 @@ public class SSCIssueTemplateDownloadCommand extends AbstractSSCJsonNodeOutputCo
public JsonNode getJsonNode(UnirestInstance unirest) {
SSCIssueTemplateDescriptor descriptor = issueTemplateResolver.getIssueTemplateDescriptor(unirest);
String issueTemplateId = descriptor.getId();
File destination = outputFileMixin.getOutputFile();
File destination = fileMixin.getFile();
if (destination==null ) {
destination = new File(String.format("./%s", descriptor.getOriginalFileName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
public class SSCReportTemplateDownloadCommand extends AbstractSSCJsonNodeOutputCommand implements IActionCommandResultSupplier {
@Getter @Mixin private OutputHelperMixins.Download outputHelper;
@Mixin private SSCReportTemplateResolverMixin.PositionalParameterSingle reportTemplateResolver;
@Mixin private CommonOptionMixins.OptionalOutputFile outputFileMixin;
@Mixin private CommonOptionMixins.OptionalFile fileMixin;

@Override
public JsonNode getJsonNode(UnirestInstance unirest) {
SSCReportTemplateDescriptor descriptor = reportTemplateResolver.getReportTemplateDescriptor(unirest);
var destination = outputFileMixin.getOutputFile();
var destination = fileMixin.getFile();
if ( destination==null ) {
destination = new File(String.format("./%s", descriptor.getFileName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class PositionalParameterSingle extends AbstractSSCReportTemplateR
@Getter private String reportTemplateNameOrId;
}
public static class RequiredOption extends AbstractSSCReportTemplateResolverMixin {
@EnvSuffix("REPORT_TEMPLATE") @Option(names="--report-template", required=true, descriptionKey = "reportTemplateNameOrId")
@Option(names="--report-template", required=true, descriptionKey = "reportTemplateNameOrId")
@Getter private String reportTemplateNameOrId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class SSCRoleCreateCommand extends AbstractSSCBaseRequestOutputCommand im
*/

@Getter
@EnvSuffix("NAME")@Parameters(index = "0", descriptionKey = "fcli.ssc.role.name")
@EnvSuffix("NAME") @Parameters(index = "0", descriptionKey = "fcli.ssc.role.name")
private String name;

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
*******************************************************************************/
package com.fortify.cli.ssc.seed_bundle.cli.cmd;

import java.io.File;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fortify.cli.common.cli.util.EnvSuffix;
import com.fortify.cli.common.cli.mixin.CommonOptionMixins;
import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.ssc._common.output.cli.cmd.AbstractSSCJsonNodeOutputCommand;
Expand All @@ -26,23 +24,21 @@
import lombok.Getter;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Parameters;

@Command(name = OutputHelperMixins.Upload.CMD_NAME)
public class SSCSeedBundleUploadCommand extends AbstractSSCJsonNodeOutputCommand implements IActionCommandResultSupplier {
@Getter @Mixin private OutputHelperMixins.Upload outputHelper;
@EnvSuffix("FILE") @Parameters(index = "0", arity = "1", descriptionKey = "fcli.ssc.seed-bundle.upload.seedBundle")
private File seedBundle;
@Mixin private CommonOptionMixins.RequiredFile fileMixin;

@Override
public JsonNode getJsonNode(UnirestInstance unirest) {
unirest.post(SSCUrls.SEED_BUNDLES)
.multiPartContent()
.field("file", seedBundle)
.field("file", fileMixin.getFile())
.asObject(JsonNode.class).getBody();
return new ObjectMapper().createObjectNode()
.put("type", "SeedBundle")
.put("file", seedBundle.getAbsolutePath());
.put("file", fileMixin.getFile().getAbsolutePath());
}

@Override
Expand Down
Loading

0 comments on commit c99be89

Please sign in to comment.