all sorts of things
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
qvalentin 2022-05-16 21:09:45 +02:00
parent d1fdad7cf9
commit 3ef31c9c3c
Signed by: qvalentin
GPG key ID: C979FA1EAFCABF1C
23 changed files with 228 additions and 27 deletions

View file

@ -28,7 +28,7 @@ abstract public class Subcommand {
private void commandExits(String command) {
if (commands.get(command) == null) {
throw new CliError("Subcommand does not exist! " + getUsage());
throw new CliError("Subcommand " + command + " does not exist! " + getUsage());
}
}
}

View file

@ -12,6 +12,7 @@ public class LinkCommands extends Subcommand {
public LinkCommands(LinkCliAdapter linkCliAdapter) {
this.linkCliAdapter = linkCliAdapter;
commands.put("add", this::addLink);
commands.put("get", this::getAll);
}
@Override
@ -26,10 +27,13 @@ public class LinkCommands extends Subcommand {
}
private String addLink(String[] args) {
linkCliAdapter.addLink(args[1],
Arrays.stream(Arrays.copyOfRange(args, 3, args.length)).collect(Collectors.toSet()),
args[2]);
return "Added the new Link";
}
private String getAll(String[] args) {
return String.join(System.lineSeparator(), linkCliAdapter.getLinks());
}
}

View file

@ -9,7 +9,6 @@ public class TagCommands extends Subcommand {
final private CustomTagsCliAdapter customTagsCliAdapter;
final private HashMap<String, Function<String[], String>> commands = new HashMap<>();
public TagCommands(CustomTagsCliAdapter customTagsCliAdapter) {
this.customTagsCliAdapter = customTagsCliAdapter;
@ -24,7 +23,7 @@ public class TagCommands extends Subcommand {
@Override
public String getUsage() {
return "Usage: " + System.lineSeparator()+
getSubcommand() + "add tagName tagRegexExpression";
getSubcommand() + " add tagName tagRegexExpression";
}
private String addCustomTag(String[] args) {

View file

@ -0,0 +1,96 @@
package main;
import category.CategoryRepository;
import category.CategoryUseCase;
import category.RandomCategoryIdGenerator;
import cli.CliError;
import cli.Subcommand;
import cli.category.CategoryCliAdapter;
import cli.category.CategoryCommands;
import cli.link.LinkCliAdapter;
import cli.link.LinkCommands;
import cli.tag.CustomTagsCliAdapter;
import cli.tag.TagCommands;
import link.LinkRepository;
import link.LinkUseCase;
import link.RandomLinkIdGenerator;
import persistence.GenericCSVDAO;
import persistence.category.CSVCategoryPersistenceAdapter;
import persistence.category.CategoryEntity;
import persistence.customTags.CSVCustomTagMatcherPersistenceAdapter;
import persistence.customTags.CustomTagMatcherEntity;
import persistence.link.CSVLinkPersistenceAdapter;
import persistence.link.LinkEntity;
import tag.CustomTagsUseCase;
import tag.TagMatcher;
import tag.TagMatcherRepository;
import tag.TaggingUseCase;
import tag.matcherImplementations.GitHubTagMatcher;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;
public class CommandHandler {
private final HashMap<String, Subcommand> subcommands = new HashMap<>();
public CommandHandler() {
this.addSubcommands();
}
public void executeCommand(String[] args) {
if(args.length == 0){
//TODO: print usage
}
var command = subcommands.get(args[0]);
if (command == null) {
throw new CliError("Unknown Command");
}
var output = command.executeSubcommand(Arrays.copyOfRange(args, 1, args.length));
System.out.println(output);
}
private void addSubcommands() {
CategoryRepository categoryRepository =
new CategoryRepository(new CSVCategoryPersistenceAdapter(new GenericCSVDAO<>(new File("category.csv"),
CategoryEntity::new)));
CategoryCliAdapter categoryCliAdapter = new CategoryCliAdapter(new CategoryUseCase(categoryRepository,
new RandomCategoryIdGenerator(
categoryRepository)));
var categoryCommands = new CategoryCommands(categoryCliAdapter);
subcommands.put(categoryCommands.getSubcommand(), categoryCommands);
LinkRepository linkRepository =
new LinkRepository(new CSVLinkPersistenceAdapter(new GenericCSVDAO<>(new File("links.csv"),
LinkEntity::new)));
Set<TagMatcher> staticTagMatchers = Set.of(new GitHubTagMatcher());
TagMatcherRepository tagMatcherRepository =
new TagMatcherRepository(new CSVCustomTagMatcherPersistenceAdapter(new GenericCSVDAO<>(
new File("customtags.csv"),
CustomTagMatcherEntity::new)), staticTagMatchers);
TaggingUseCase taggingUseCase =
new TaggingUseCase(tagMatcherRepository, new RandomLinkIdGenerator(linkRepository));
LinkCliAdapter linkCliAdapter =
new LinkCliAdapter(new LinkUseCase(linkRepository,
categoryRepository,
taggingUseCase,
new RandomLinkIdGenerator(linkRepository)));
var linkCommands = new LinkCommands(linkCliAdapter);
subcommands.put(linkCommands.getSubcommand(), linkCommands);
TagCommands tagCommands =
new TagCommands(new CustomTagsCliAdapter(new CustomTagsUseCase(tagMatcherRepository)));
subcommands.put(tagCommands.getSubcommand(), tagCommands);
}
}

View file

@ -10,12 +10,11 @@ import java.io.File;
public class Main {
public static void main(String[] args) {
TagName test = new TagName("ds");
var csvCategoryPersistenceAdapter =
new CSVCategoryPersistenceAdapter(new GenericCSVDAO<CategoryEntity>(new File("dsa"),
CategoryEntity::new));
System.out.println(test);
CommandHandler commandHandler = new CommandHandler();
commandHandler.executeCommand(args);
}
}

View file

@ -21,6 +21,17 @@ public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
public GenericCSVDAO(File file, Function<String[], T> constructor) {
this.file = file;
this.constructor = constructor;
createFileIfMissing();
}
private void createFileIfMissing() {
if (file.exists()) return;
try {
file.createNewFile();
}
catch (IOException e) {
throw new PersistenceError("Could not create the file " + file.getAbsolutePath());
}
}
public Set<T> getALl() {
@ -31,7 +42,7 @@ public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
}
}
catch (IOException e) {
e.printStackTrace();
throw new PersistenceError("Something went wrong while reading from file " + file.getAbsolutePath());
}
return lines
@ -55,7 +66,7 @@ public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
bufferedWriter.newLine();
}
catch (IOException e) {
e.printStackTrace();
throw new PersistenceError("Something went wrong while writing to file " + file.getAbsolutePath());
}
}
@ -65,7 +76,7 @@ public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
bufferedWriter.write("");
}
catch (IOException e) {
e.printStackTrace();
throw new PersistenceError("Something went wrong while writing to file " + file.getAbsolutePath());
}
}
}

View file

@ -0,0 +1,8 @@
package persistence;
public class PersistenceError extends RuntimeException {
public PersistenceError(String message) {
super(message);
}
}