Skip to content

Commit ab44cff

Browse files
committed
Finish import use case
1 parent a4ba908 commit ab44cff

File tree

14 files changed

+208
-33
lines changed

14 files changed

+208
-33
lines changed

src/main/java/org/hydev/mcpm/client/arguments/CommandsFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static List<CommandParser> baseParsers(boolean isMinecraft) {
5959
var updater = new UpdateInteractor(updateChecker, installer, tracker);
6060

6161
// Controllers
62-
var exportPluginsController = new ExportPluginsController(
62+
var exportPluginsController = new ExportController(
6363
new ExportInteractor(tracker), new LogExportPresenter());
6464
var listController = new ListController(new ListAllInteractor(tracker));
6565
var searchController = new SearchPackagesController(searcher, pager);

src/main/java/org/hydev/mcpm/client/arguments/parsers/ExportPluginsParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import net.sourceforge.argparse4j.inf.Namespace;
44
import net.sourceforge.argparse4j.inf.Subparser;
5-
import org.hydev.mcpm.client.commands.controllers.ExportPluginsController;
5+
import org.hydev.mcpm.client.commands.controllers.ExportController;
66
import org.hydev.mcpm.client.export.ExportPluginsInput;
77

88
import java.util.function.Consumer;
@@ -12,9 +12,9 @@
1212
*/
1313
public class ExportPluginsParser implements CommandParser
1414
{
15-
private final ExportPluginsController controller;
15+
private final ExportController controller;
1616

17-
public ExportPluginsParser(ExportPluginsController controller)
17+
public ExportPluginsParser(ExportController controller)
1818
{
1919
this.controller = controller;
2020
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.hydev.mcpm.client.arguments.parsers;
2+
3+
import net.sourceforge.argparse4j.inf.Namespace;
4+
import net.sourceforge.argparse4j.inf.Subparser;
5+
6+
import java.util.function.Consumer;
7+
8+
/**
9+
* Parser for the import use case
10+
*/
11+
public class ImportParser implements CommandParser {
12+
@Override
13+
public String name() {
14+
return "import";
15+
}
16+
17+
@Override
18+
public String description() {
19+
return "Import a plugins config from a previous export";
20+
}
21+
22+
@Override
23+
public void configure(Subparser parser) {
24+
25+
}
26+
27+
@Override
28+
public void run(Namespace details, Consumer<String> log) {
29+
30+
}
31+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @param boundary The export implementation
1313
* @param presenter The presenter to show the result
1414
*/
15-
public record ExportPluginsController(
15+
public record ExportController(
1616
ExportPluginsBoundary boundary,
1717
ExportPresenter presenter
1818
) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.hydev.mcpm.client.commands.controllers;
2+
3+
import org.hydev.mcpm.client.commands.presenters.ImportPresenter;
4+
import org.hydev.mcpm.client.export.ImportInput;
5+
import org.hydev.mcpm.client.export.ImportPluginsBoundary;
6+
7+
import java.util.function.Consumer;
8+
9+
/**
10+
* Controller for the import use case.
11+
*
12+
* @param boundary The boundary used to import
13+
* @param presenter The presenter to display the result
14+
*/
15+
public record ImportController(
16+
ImportPluginsBoundary boundary,
17+
ImportPresenter presenter
18+
) {
19+
20+
/**
21+
* Import the plugins.
22+
*
23+
* @param input Specification of where to import from
24+
* @param log The log to write to
25+
*/
26+
public void importPlugins(ImportInput input, Consumer<String> log) {
27+
var result = boundary.importPlugins(input);
28+
presenter.present(result, log);
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.hydev.mcpm.client.commands.presenters;
2+
3+
import org.hydev.mcpm.client.export.ImportResult;
4+
5+
import java.util.function.Consumer;
6+
7+
/**
8+
* Interface for displaying the results of an import
9+
*/
10+
public interface ImportPresenter {
11+
12+
/**
13+
* Present the result of the import
14+
*
15+
* @param result the result to present
16+
*/
17+
void present(ImportResult result, Consumer<String> log);
18+
}

src/main/java/org/hydev/mcpm/client/display/presenters/LogExportPresenter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
*/
1111
public class LogExportPresenter implements ExportPresenter {
1212

13-
private String getColor(ExportPluginsResult exportPluginsResult) {
14-
return switch (exportPluginsResult.state()) {
13+
private String getColor(ExportPluginsResult result) {
14+
return switch (result.state()) {
1515
case SUCCESS -> "&a";
1616
case FAILED_TO_FETCH_PLUGINS -> "&c";
1717
};
1818
}
1919

20-
private String getMessage(ExportPluginsResult exportPluginsResult) {
21-
return switch ((exportPluginsResult.state())) {
22-
case SUCCESS -> "Exported to " + exportPluginsResult.export();
20+
private String getMessage(ExportPluginsResult result) {
21+
return switch ((result.state())) {
22+
case SUCCESS -> "Exported to " + result.export();
2323
case FAILED_TO_FETCH_PLUGINS -> "Export failed to fetch plugins";
2424
};
2525
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.hydev.mcpm.client.display.presenters;
2+
3+
import org.hydev.mcpm.client.commands.presenters.ImportPresenter;
4+
import org.hydev.mcpm.client.export.ImportResult;
5+
6+
import java.util.function.Consumer;
7+
8+
/**
9+
* ImportPresenter that writes to a log
10+
*/
11+
public class LogImportPresenter implements ImportPresenter {
12+
13+
private String getColor(ImportResult result) {
14+
return switch (result.state()) {
15+
case SUCCESS -> "&a";
16+
case PARTIAL_SUCCESS -> "&6";
17+
case FAILURE -> "&c";
18+
case IMPORT_ERROR -> "&4";
19+
};
20+
}
21+
22+
private String getMessage(ImportResult result) {
23+
return switch (result.state()) {
24+
case SUCCESS -> "All plugins installed";
25+
case PARTIAL_SUCCESS -> "Some plugins failed to install:\n"
26+
+ String.join("\n", result.noninstalledPlugins);
27+
case FAILURE -> "All plugins failed to install";
28+
case IMPORT_ERROR -> "Import failed. " + result.error;
29+
};
30+
}
31+
32+
@Override
33+
public void present(ImportResult result, Consumer<String> log) {
34+
log.accept(getColor(result) + getMessage(result));
35+
}
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.hydev.mcpm.client.export;
2+
3+
/**
4+
* The input for the import use case.
5+
*
6+
* @param type The type of the import source (pastebin, file, etc)
7+
* @param input The location of the input
8+
*/
9+
public record ImportInput(
10+
String type,
11+
String input
12+
) {
13+
}

src/main/java/org/hydev/mcpm/client/export/ImportInteractor.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.core.type.TypeReference;
5-
import org.hydev.mcpm.client.commands.presenters.InstallResultPresenter;
5+
import org.hydev.mcpm.client.export.storage.StringStorage;
6+
import org.hydev.mcpm.client.export.storage.StringStorageFactory;
67
import org.hydev.mcpm.client.installer.InstallBoundary;
7-
import org.hydev.mcpm.client.installer.InstallResult;
88
import org.hydev.mcpm.client.installer.input.InstallInput;
99
import org.hydev.mcpm.client.search.SearchPackagesType;
1010
import org.hydev.mcpm.utils.Pair;
1111

12+
import java.io.IOException;
1213
import java.util.List;
1314

1415
import static org.hydev.mcpm.Constants.JACKSON;
1516

1617
/**
1718
* Default implementation of ImportPluginsBoundary
1819
*
19-
* @param install the install boundary
20+
* @param install the installer boundary
2021
*/
2122
public record ImportInteractor(InstallBoundary install) implements ImportPluginsBoundary {
2223
@Override
23-
public ImportResult importPlugins(String input) throws ImportException {
24+
public ImportResult importPlugins(ImportInput input) {
2425
try {
25-
var plugins = JACKSON.readValue(input, new TypeReference<List<ExportModel>>() { });
26+
StringStorage storage = StringStorageFactory.createStringStorage(input);
27+
String json = storage.load(input.input());
28+
var plugins = JACKSON.readValue(json, new TypeReference<List<ExportModel>>() { });
2629
// TODO: Install specific versions
2730
var results = plugins.stream()
2831
.map(p -> new Pair<>(p.name(),
@@ -34,7 +37,10 @@ public ImportResult importPlugins(String input) throws ImportException {
3437

3538
return new ImportResult(results);
3639
} catch (JsonProcessingException e) {
37-
throw new ImportException(e);
40+
return new ImportResult("Import source is invalid or corrupted.");
41+
} catch (IOException e) {
42+
return new ImportResult("Unable to read from import source." +
43+
" Check if you imported from the right source.");
3844
}
3945
}
4046
}

0 commit comments

Comments
 (0)