2: Add --exclude option and --help
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
package com.bartek.esa;
|
package com.bartek.esa;
|
||||||
|
|
||||||
import com.bartek.esa.cli.model.CliArgsOptions;
|
import com.bartek.esa.cli.model.CliArgsOptions;
|
||||||
import com.bartek.esa.dispatcher.dispatcher.MethodDispatcher;
|
|
||||||
import com.bartek.esa.dispatcher.model.DispatcherActions;
|
|
||||||
import com.bartek.esa.cli.parser.CliArgsParser;
|
import com.bartek.esa.cli.parser.CliArgsParser;
|
||||||
import com.bartek.esa.di.DaggerDependencyInjector;
|
import com.bartek.esa.di.DaggerDependencyInjector;
|
||||||
|
import com.bartek.esa.dispatcher.dispatcher.MethodDispatcher;
|
||||||
|
import com.bartek.esa.dispatcher.model.DispatcherActions;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,15 @@ package com.bartek.esa.cli.model;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
public class CliArgsOptions {
|
public class CliArgsOptions {
|
||||||
private String sourceAnalysisDirectory;
|
private String sourceAnalysisDirectory;
|
||||||
private String apkAuditFile;
|
private String apkAuditFile;
|
||||||
|
private List<String> excludes;
|
||||||
|
|
||||||
public boolean isSourceAnalysis() {
|
public boolean isSourceAnalysis() {
|
||||||
return sourceAnalysisDirectory != null;
|
return sourceAnalysisDirectory != null;
|
||||||
@@ -16,4 +20,12 @@ public class CliArgsOptions {
|
|||||||
public boolean isApkAudit() {
|
public boolean isApkAudit() {
|
||||||
return apkAuditFile != null;
|
return apkAuditFile != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static CliArgsOptions empty() {
|
||||||
|
return CliArgsOptions.builder()
|
||||||
|
.sourceAnalysisDirectory(null)
|
||||||
|
.apkAuditFile(null)
|
||||||
|
.excludes(Collections.emptyList())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,39 +1,66 @@
|
|||||||
package com.bartek.esa.cli.parser;
|
package com.bartek.esa.cli.parser;
|
||||||
|
|
||||||
import com.bartek.esa.cli.model.CliArgsOptions;
|
import com.bartek.esa.cli.model.CliArgsOptions;
|
||||||
import com.bartek.esa.error.EsaException;
|
|
||||||
import io.vavr.control.Try;
|
|
||||||
import org.apache.commons.cli.*;
|
import org.apache.commons.cli.*;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static java.util.Arrays.asList;
|
||||||
|
import static java.util.Collections.emptyList;
|
||||||
|
|
||||||
public class CliArgsParser {
|
public class CliArgsParser {
|
||||||
private static final String SRC_OPT = "src";
|
private static final String SRC_OPT = "src";
|
||||||
private static final String APK_OPT = "apk";
|
private static final String APK_OPT = "apk";
|
||||||
|
private static final String EXCLUDE_OPT = "exclude";
|
||||||
|
private static final String HELP_OPT = "help";
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public CliArgsParser() {}
|
public CliArgsParser() {}
|
||||||
|
|
||||||
public CliArgsOptions parse(String[] args) {
|
public CliArgsOptions parse(String[] args) {
|
||||||
CommandLine command = Try.of(() -> new DefaultParser().parse(prepareOptions(), args))
|
try {
|
||||||
.getOrElseThrow(EsaException::new);
|
CommandLine command = new DefaultParser().parse(prepareOptions(), args);
|
||||||
|
|
||||||
return CliArgsOptions.builder()
|
if (command.hasOption(HELP_OPT)) {
|
||||||
.sourceAnalysisDirectory(command.hasOption(SRC_OPT) ? command.getOptionValue(SRC_OPT) : null)
|
printHelp();
|
||||||
.apkAuditFile(command.hasOption(APK_OPT) ? command.getOptionValue(APK_OPT) : null)
|
return CliArgsOptions.empty();
|
||||||
.build();
|
}
|
||||||
|
|
||||||
|
boolean isNeitherAuditNorAnalysis = !command.hasOption(SRC_OPT) && !command.hasOption(APK_OPT);
|
||||||
|
if (isNeitherAuditNorAnalysis) {
|
||||||
|
printHelp();
|
||||||
|
return CliArgsOptions.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
return CliArgsOptions.builder()
|
||||||
|
.sourceAnalysisDirectory(command.hasOption(SRC_OPT) ? command.getOptionValue(SRC_OPT) : null)
|
||||||
|
.apkAuditFile(command.hasOption(APK_OPT) ? command.getOptionValue(APK_OPT) : null)
|
||||||
|
.excludes(command.hasOption(EXCLUDE_OPT) ? asList(command.getOptionValues(EXCLUDE_OPT)) : emptyList())
|
||||||
|
.build();
|
||||||
|
} catch (ParseException e) {
|
||||||
|
printHelp();
|
||||||
|
return CliArgsOptions.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void printHelp() {
|
||||||
|
HelpFormatter formatter = new HelpFormatter();
|
||||||
|
formatter.printHelp("esa", prepareOptions(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Options prepareOptions() {
|
private Options prepareOptions() {
|
||||||
Options options = new Options();
|
Options options = new Options();
|
||||||
options.addOption(source());
|
options.addOption(source());
|
||||||
options.addOption(apk());
|
options.addOption(apk());
|
||||||
|
options.addOption(exclude());
|
||||||
|
options.addOption(help());
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Option source() {
|
private Option source() {
|
||||||
return Option.builder()
|
return Option.builder()
|
||||||
.longOpt(SRC_OPT)
|
.longOpt(SRC_OPT)
|
||||||
|
.argName("SOURCE_PATH")
|
||||||
.hasArg()
|
.hasArg()
|
||||||
.desc("perform analysis for Java code and XML resources")
|
.desc("perform analysis for Java code and XML resources")
|
||||||
.build();
|
.build();
|
||||||
@@ -42,8 +69,26 @@ public class CliArgsParser {
|
|||||||
private Option apk() {
|
private Option apk() {
|
||||||
return Option.builder()
|
return Option.builder()
|
||||||
.longOpt(APK_OPT)
|
.longOpt(APK_OPT)
|
||||||
|
.argName("APK_PATH")
|
||||||
.hasArg()
|
.hasArg()
|
||||||
.desc("perform audit for compiled APK file")
|
.desc("perform audit for compiled APK file")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Option exclude() {
|
||||||
|
return Option.builder()
|
||||||
|
.longOpt(EXCLUDE_OPT)
|
||||||
|
.argName("CODES")
|
||||||
|
.numberOfArgs(Option.UNLIMITED_VALUES)
|
||||||
|
.desc("exclude particular security checks from audit/analysis")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Option help() {
|
||||||
|
return Option.builder()
|
||||||
|
.longOpt(HELP_OPT)
|
||||||
|
.argName("h")
|
||||||
|
.desc("print this help")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user