2: Create basic options and method dispatcher
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.bartek.esa;
|
||||
|
||||
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.di.DaggerDependencyInjector;
|
||||
|
||||
@@ -7,14 +10,23 @@ import javax.inject.Inject;
|
||||
|
||||
public class EsaMain {
|
||||
private final CliArgsParser cliArgsParser;
|
||||
private final MethodDispatcher methodDispatcher;
|
||||
|
||||
@Inject
|
||||
EsaMain(CliArgsParser cliArgsParser) {
|
||||
EsaMain(CliArgsParser cliArgsParser, MethodDispatcher methodDispatcher) {
|
||||
this.cliArgsParser = cliArgsParser;
|
||||
this.methodDispatcher = methodDispatcher;
|
||||
}
|
||||
|
||||
private void run(String[] args) {
|
||||
// cliArgsParser.parse(...)
|
||||
DispatcherActions dispatcherActions = DispatcherActions.builder()
|
||||
.sourceAnalysis(source -> {})
|
||||
.apkAudit(source -> {})
|
||||
.build();
|
||||
|
||||
CliArgsOptions options = cliArgsParser.parse(args);
|
||||
|
||||
methodDispatcher.dispatchMethod(options, dispatcherActions);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.bartek.esa.cli.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class CliArgsActions {
|
||||
}
|
||||
19
src/main/java/com/bartek/esa/cli/model/CliArgsOptions.java
Normal file
19
src/main/java/com/bartek/esa/cli/model/CliArgsOptions.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.bartek.esa.cli.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class CliArgsOptions {
|
||||
private String sourceAnalysisDirectory;
|
||||
private String apkAuditFile;
|
||||
|
||||
public boolean isSourceAnalysis() {
|
||||
return sourceAnalysisDirectory != null;
|
||||
}
|
||||
|
||||
public boolean isApkAudit() {
|
||||
return apkAuditFile != null;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,49 @@
|
||||
package com.bartek.esa.cli.parser;
|
||||
|
||||
import com.bartek.esa.cli.model.CliArgsActions;
|
||||
import com.bartek.esa.cli.model.CliArgsOptions;
|
||||
import com.bartek.esa.error.EsaException;
|
||||
import io.vavr.control.Try;
|
||||
import org.apache.commons.cli.*;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class CliArgsParser {
|
||||
private static final String SRC_OPT = "src";
|
||||
private static final String APK_OPT = "apk";
|
||||
|
||||
@Inject
|
||||
public CliArgsParser() {}
|
||||
|
||||
public void parse(String[] args, CliArgsActions actions) {
|
||||
public CliArgsOptions parse(String[] args) {
|
||||
CommandLine command = Try.of(() -> new DefaultParser().parse(prepareOptions(), args))
|
||||
.getOrElseThrow(EsaException::new);
|
||||
|
||||
return CliArgsOptions.builder()
|
||||
.sourceAnalysisDirectory(command.hasOption(SRC_OPT) ? command.getOptionValue(SRC_OPT) : null)
|
||||
.apkAuditFile(command.hasOption(APK_OPT) ? command.getOptionValue(APK_OPT) : null)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Options prepareOptions() {
|
||||
Options options = new Options();
|
||||
options.addOption(source());
|
||||
options.addOption(apk());
|
||||
return options;
|
||||
}
|
||||
|
||||
private Option source() {
|
||||
return Option.builder()
|
||||
.longOpt(SRC_OPT)
|
||||
.hasArg()
|
||||
.desc("perform analysis for Java code and XML resources")
|
||||
.build();
|
||||
}
|
||||
|
||||
private Option apk() {
|
||||
return Option.builder()
|
||||
.longOpt(APK_OPT)
|
||||
.hasArg()
|
||||
.desc("perform audit for compiled APK file")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@ package com.bartek.esa.di;
|
||||
|
||||
import com.bartek.esa.EsaMain;
|
||||
import com.bartek.esa.cli.di.CliModule;
|
||||
import com.bartek.esa.dispatcher.di.DispatcherModule;
|
||||
import dagger.Component;
|
||||
|
||||
@Component(modules = {
|
||||
CliModule.class
|
||||
CliModule.class,
|
||||
DispatcherModule.class
|
||||
})
|
||||
public interface DependencyInjector {
|
||||
EsaMain esa();
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.bartek.esa.dispatcher.di;
|
||||
|
||||
import com.bartek.esa.dispatcher.dispatcher.MethodDispatcher;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module
|
||||
public class DispatcherModule {
|
||||
|
||||
@Provides
|
||||
MethodDispatcher methodDispatcher() {
|
||||
return new MethodDispatcher();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.bartek.esa.dispatcher.dispatcher;
|
||||
|
||||
import com.bartek.esa.cli.model.CliArgsOptions;
|
||||
import com.bartek.esa.dispatcher.model.DispatcherActions;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class MethodDispatcher {
|
||||
|
||||
@Inject
|
||||
public MethodDispatcher() {
|
||||
|
||||
}
|
||||
|
||||
public void dispatchMethod(CliArgsOptions options, DispatcherActions actions) {
|
||||
if(options.isSourceAnalysis()) {
|
||||
actions.getSourceAnalysis().accept(options.getSourceAnalysisDirectory());
|
||||
return;
|
||||
}
|
||||
|
||||
if(options.isApkAudit()) {
|
||||
actions.getApkAudit().accept(options.getApkAuditFile());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.bartek.esa.dispatcher.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class DispatcherActions {
|
||||
private Consumer<String> sourceAnalysis;
|
||||
private Consumer<String> apkAudit;
|
||||
}
|
||||
Reference in New Issue
Block a user