Console ui refactor

This commit is contained in:
kirill.labutin 2025-02-06 01:29:58 +03:00
parent 5c3bc9060c
commit c69f90c7de
4 changed files with 225 additions and 137 deletions

View File

@ -89,7 +89,7 @@
<addDependencies>false</addDependencies>
<preCp>anything</preCp>
</classPath>
<!-- <icon>src/main/resources/icon.ico</icon>-->
<icon>src/main/resources/icon.ico</icon>
<jre>
<path>./data</path>
<minVersion>${maven.compiler.source}</minVersion>

View File

@ -14,7 +14,7 @@ import java.util.logging.Level;
public class App {
private static final String LOG_CONTEXT = App.class.getSimpleName();
private List<ContainerMetadata> 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);

View File

@ -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<String> 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();
}
}

BIN
src/main/resources/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB