diff --git a/pom.xml b/pom.xml
index 3e16ee4..6d0049e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -89,7 +89,7 @@
false
anything
-
+ src/main/resources/icon.ico
./data
${maven.compiler.source}
diff --git a/src/main/java/ru/kirillius/mktbk/App.java b/src/main/java/ru/kirillius/mktbk/App.java
index f3d8461..e3d89d5 100644
--- a/src/main/java/ru/kirillius/mktbk/App.java
+++ b/src/main/java/ru/kirillius/mktbk/App.java
@@ -14,7 +14,7 @@ import java.util.logging.Level;
public class App {
private static final String LOG_CONTEXT = App.class.getSimpleName();
private List containers;
-
+ private Console console;
public static void main(String[] args) throws IOException, MikrotikApiException, InterruptedException {
SystemLogger.initializeLogging(Level.INFO, Collections.emptyList());
@@ -22,116 +22,110 @@ public class App {
new App();
}
- private String cin() {
- try {
- return reader.readLine();
- } catch (IOException e) {
- return "";
- }
- }
-
- private void pause() {
- System.out.println("Press any key to continue...");
- cin();
- }
-
- private BufferedReader reader;
-
private ContainerMetadata selectContainer() {
- System.out.println("Found containers:");
- var i = 0;
- for (var container : containers) {
- System.out.println(String.valueOf(++i) + ' ' + container);
- }
- do {
-
- var index = -1;
-
- if (containers.size() > 1) {
- do {
- index = -1;
- System.out.print("Select container you want to backup:");
- try {
- index = Integer.parseInt(cin()) - 1;
- } catch (NumberFormatException e) {
- SystemLogger.error("Invalid number", LOG_CONTEXT, e);
- }
- } while (index >= containers.size() || index < 0);
- }
-
- if (containers.isEmpty()) {
- return null;
- }
-
- var container = containers.get(index);
- System.out.print("Do you really want to backup container " + container + "? [y/n]");
-
- if (cin().equals("y")) {
- return container;
- }
- } while (true);
+// System.out.println("Found containers:");
+// var i = 0;
+// for (var container : containers) {
+// System.out.println(String.valueOf(++i) + ' ' + container);
+// }
+// do {
+//
+// var index = -1;
+//
+// if (containers.size() > 1) {
+// do {
+// index = -1;
+// System.out.print("Select container you want to backup:");
+// try {
+// index = Integer.parseInt(cin()) - 1;
+// } catch (NumberFormatException e) {
+// SystemLogger.error("Invalid number", LOG_CONTEXT, e);
+// }
+// } while (index >= containers.size() || index < 0);
+// }
+//
+// if (containers.isEmpty()) {
+// return null;
+// }
+//
+// var container = containers.get(index);
+// System.out.print("Do you really want to backup container " + container + "? [y/n]");
+//
+// if (cin().equals("y")) {
+// return container;
+// }
+// } while (true);
+ return null;
}
public App() throws IOException, MikrotikApiException, InterruptedException {
- try {
- reader = new BufferedReader(new InputStreamReader(System.in));
- auth();
- containers = deviceContext.getContainers();
-
- ContainerMetadata container;
- do {
- container = selectContainer();
- if (container == null) {
- return;
- }
-
- if (!container.isRunning()) {
- SystemLogger.warning("The selected container is not running. There is a limitation that requires the container to be running.", LOG_CONTEXT);
- SystemLogger.message("Starting container " + container, LOG_CONTEXT);
- deviceContext.getApiConnection().execute("/container/start .id=" + container.getId());
- try {
- Thread.sleep(10000L);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
-
- containers = deviceContext.getContainers();
- var id = container.getId();
- container = containers.stream().filter(containerMetadata -> containerMetadata.getId().equals(id)).findFirst().orElse(null);
- if (container == null || !container.isRunning()) {
- SystemLogger.error("Something went wrong. Unable to start container.", LOG_CONTEXT);
- pause();
- }
- }
- } while (container == null);
-
- SystemLogger.warning("/!\\ We are going to make a backup. You have to stop all services in this container to prevent data corruption.", LOG_CONTEXT);
- System.out.print("Type yes to continue or Ctrl+C to abort: ");
- while (!"yes".equals(cin())) ;
-
-
- var layerFile = new File(container.getGuid());
- var outputFile = new File("backup_of_" + container.getComment() + ".tar");
-
-
- var files = findFiles(container);
- downloadFiles(container, files, layerFile);
-
-
- SystemLogger.message("Building docker image " + outputFile.getName(), LOG_CONTEXT);
- var builder = new DockerImageBuilder(container);
- builder.build(layerFile, outputFile);
- SystemLogger.message("Backup is done ", LOG_CONTEXT);
- pause();
-
- } finally {
- if (reader != null) {
- reader.close();
- }
- if (deviceContext != null) {
- deviceContext.close();
- }
- }
+// try {
+// console = new Console();
+// auth();
+// var mode = console.select("What do you want to do?", List.of("Backup containers", "Restore backup", "Exit application"));
+// if(mode == 0){
+// selectWhatToBackup();
+// }else if(mode == 1){
+// selectWhatToRestore();
+// }
+//
+// containers = deviceContext.getContainers();
+//
+//
+// ContainerMetadata container;
+// do {
+// container = selectContainer();
+// if (container == null) {
+// return;
+// }
+//
+// if (!container.isRunning()) {
+// SystemLogger.warning("The selected container is not running. There is a limitation that requires the container to be running.", LOG_CONTEXT);
+// SystemLogger.message("Starting container " + container, LOG_CONTEXT);
+// deviceContext.getApiConnection().execute("/container/start .id=" + container.getId());
+// try {
+// Thread.sleep(10000L);
+// } catch (InterruptedException e) {
+// throw new RuntimeException(e);
+// }
+//
+// containers = deviceContext.getContainers();
+// var id = container.getId();
+// container = containers.stream().filter(containerMetadata -> containerMetadata.getId().equals(id)).findFirst().orElse(null);
+// if (container == null || !container.isRunning()) {
+// SystemLogger.error("Something went wrong. Unable to start container.", LOG_CONTEXT);
+// pause();
+// }
+// }
+// } while (container == null);
+//
+// SystemLogger.warning("/!\\ We are going to make a backup. You have to stop all services in this container to prevent data corruption.", LOG_CONTEXT);
+// System.out.print("Type yes to continue or Ctrl+C to abort: ");
+// while (!"yes".equals(cin())) ;
+//
+//
+// var layerFile = new File(container.getGuid());
+// var outputFile = new File("backup_of_" + container.getComment() + ".tar");
+//
+//
+// var files = findFiles(container);
+// downloadFiles(container, files, layerFile);
+//
+//
+// SystemLogger.message("Building docker image " + outputFile.getName(), LOG_CONTEXT);
+// var builder = new DockerImageBuilder(container);
+// builder.build(layerFile, outputFile);
+// SystemLogger.message("Backup is done ", LOG_CONTEXT);
+// pause();
+//
+// } finally {
+// if (reader != null) {
+// reader.close();
+// }
+// if (deviceContext != null) {
+// deviceContext.close();
+// }
+// }
// openSFTP();
@@ -177,7 +171,7 @@ public class App {
} catch (IOException e) {
status.clear();
SystemLogger.error("Unable to copy file " + file.getPath(), LOG_CONTEXT, e);
- pause();
+ console.pause();
}
}
tar.finish();
@@ -202,34 +196,34 @@ public class App {
private void auth() {
do {
- try {
-
- System.out.print("Enter remote host:");
- var host = cin();
- if (host.trim().isEmpty()) {
- throw new RuntimeException("Remote host is empty");
- }
-
- System.out.print("Enter username:");
- var username = cin();
- if (username.trim().isEmpty()) {
- throw new RuntimeException("Username is empty");
- }
- System.out.print("Enter password:");
- var password = cin();
- if (password.trim().isEmpty()) {
- throw new RuntimeException("Password is empty");
- }
-
- deviceContext = new DeviceContext(host, username, password);
- deviceContext.getApiConnection();
- } catch (Exception e) {
- SystemLogger.error("Unable to connect", LOG_CONTEXT, e);
- if (deviceContext != null) {
- deviceContext.close();
- }
- deviceContext = null;
- }
+// try {
+//
+// System.out.print("Enter remote host:");
+// var host = cin();
+// if (host.trim().isEmpty()) {
+// throw new RuntimeException("Remote host is empty");
+// }
+//
+// System.out.print("Enter username:");
+// var username = cin();
+// if (username.trim().isEmpty()) {
+// throw new RuntimeException("Username is empty");
+// }
+// System.out.print("Enter password:");
+// var password = cin();
+// if (password.trim().isEmpty()) {
+// throw new RuntimeException("Password is empty");
+// }
+//
+// deviceContext = new DeviceContext(host, username, password);
+// deviceContext.getApiConnection();
+// } catch (Exception e) {
+// SystemLogger.error("Unable to connect", LOG_CONTEXT, e);
+// if (deviceContext != null) {
+// deviceContext.close();
+// }
+// deviceContext = null;
+// }
} while (deviceContext == null);
SystemLogger.message("Connected", LOG_CONTEXT);
diff --git a/src/main/java/ru/kirillius/mktbk/Console.java b/src/main/java/ru/kirillius/mktbk/Console.java
new file mode 100644
index 0000000..79c54e9
--- /dev/null
+++ b/src/main/java/ru/kirillius/mktbk/Console.java
@@ -0,0 +1,94 @@
+package ru.kirillius.mktbk;
+
+import java.io.BufferedReader;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.List;
+
+public class Console implements Closeable {
+ private final BufferedReader reader;
+
+ public Console() {
+ reader = new BufferedReader(new InputStreamReader(System.in));
+ }
+
+ public void clear() {
+ var os = System.getProperty("os.name").toLowerCase();
+ if (os.contains("win")) {
+ try {
+ new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
+ } catch (InterruptedException | IOException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ System.out.print("\033[H\033[2J");
+ System.out.flush();
+ }
+ }
+
+ @Override
+ public void close() throws IOException {
+ reader.close();
+ }
+
+ public String read() {
+ try {
+ return reader.readLine();
+ } catch (IOException e) {
+ return "";
+ }
+ }
+
+ public boolean confirm(String question) {
+ do {
+ System.out.print(question + " [y/n]");
+ var result = read().trim();
+ if (result.equals("y")) {
+ return true;
+ } else if (result.equals("n")) {
+ return false;
+ }
+ } while (true);
+ }
+
+ public String prompt(String question, boolean canBeEmpty) {
+ do {
+ System.out.print(question + ": ");
+ var result = read().trim();
+ if (!canBeEmpty && result.trim().isEmpty()) {
+ continue;
+ }
+ return result;
+ } while (true);
+ }
+
+ public int select(String caption, List values) {
+ var index = -1;
+
+ if (values.size() > 1) {
+ do {
+ clear();
+ System.out.println(caption + ":");
+ var i = 0;
+ for (var row : values) {
+ System.out.println(String.valueOf(++i) + ' ' + row);
+ }
+ index = -1;
+
+ try {
+ index = Integer.parseInt(read()) - 1;
+ } catch (NumberFormatException ignored) {
+ }
+ } while (index >= values.size() || index < 0);
+ return (index);
+ } else {
+ return -1;
+ }
+ }
+
+ public void pause() {
+ System.out.println("Press enter to continue...");
+ read();
+ }
+}
diff --git a/src/main/resources/icon.ico b/src/main/resources/icon.ico
new file mode 100644
index 0000000..4b75886
Binary files /dev/null and b/src/main/resources/icon.ico differ