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> <addDependencies>false</addDependencies>
<preCp>anything</preCp> <preCp>anything</preCp>
</classPath> </classPath>
<!-- <icon>src/main/resources/icon.ico</icon>--> <icon>src/main/resources/icon.ico</icon>
<jre> <jre>
<path>./data</path> <path>./data</path>
<minVersion>${maven.compiler.source}</minVersion> <minVersion>${maven.compiler.source}</minVersion>

View File

@ -14,7 +14,7 @@ import java.util.logging.Level;
public class App { public class App {
private static final String LOG_CONTEXT = App.class.getSimpleName(); private static final String LOG_CONTEXT = App.class.getSimpleName();
private List<ContainerMetadata> containers; private List<ContainerMetadata> containers;
private Console console;
public static void main(String[] args) throws IOException, MikrotikApiException, InterruptedException { public static void main(String[] args) throws IOException, MikrotikApiException, InterruptedException {
SystemLogger.initializeLogging(Level.INFO, Collections.emptyList()); SystemLogger.initializeLogging(Level.INFO, Collections.emptyList());
@ -22,116 +22,110 @@ public class App {
new 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() { private ContainerMetadata selectContainer() {
System.out.println("Found containers:"); // System.out.println("Found containers:");
var i = 0; // var i = 0;
for (var container : containers) { // for (var container : containers) {
System.out.println(String.valueOf(++i) + ' ' + container); // System.out.println(String.valueOf(++i) + ' ' + container);
} // }
do { // do {
//
var index = -1; // var index = -1;
//
if (containers.size() > 1) { // if (containers.size() > 1) {
do { // do {
index = -1; // index = -1;
System.out.print("Select container you want to backup:"); // System.out.print("Select container you want to backup:");
try { // try {
index = Integer.parseInt(cin()) - 1; // index = Integer.parseInt(cin()) - 1;
} catch (NumberFormatException e) { // } catch (NumberFormatException e) {
SystemLogger.error("Invalid number", LOG_CONTEXT, e); // SystemLogger.error("Invalid number", LOG_CONTEXT, e);
} // }
} while (index >= containers.size() || index < 0); // } while (index >= containers.size() || index < 0);
} // }
//
if (containers.isEmpty()) { // if (containers.isEmpty()) {
return null; // return null;
} // }
//
var container = containers.get(index); // var container = containers.get(index);
System.out.print("Do you really want to backup container " + container + "? [y/n]"); // System.out.print("Do you really want to backup container " + container + "? [y/n]");
//
if (cin().equals("y")) { // if (cin().equals("y")) {
return container; // return container;
} // }
} while (true); // } while (true);
return null;
} }
public App() throws IOException, MikrotikApiException, InterruptedException { public App() throws IOException, MikrotikApiException, InterruptedException {
try { // try {
reader = new BufferedReader(new InputStreamReader(System.in)); // console = new Console();
auth(); // auth();
containers = deviceContext.getContainers(); // var mode = console.select("What do you want to do?", List.of("Backup containers", "Restore backup", "Exit application"));
// if(mode == 0){
ContainerMetadata container; // selectWhatToBackup();
do { // }else if(mode == 1){
container = selectContainer(); // selectWhatToRestore();
if (container == null) { // }
return; //
} // containers = deviceContext.getContainers();
//
if (!container.isRunning()) { //
SystemLogger.warning("The selected container is not running. There is a limitation that requires the container to be running.", LOG_CONTEXT); // ContainerMetadata container;
SystemLogger.message("Starting container " + container, LOG_CONTEXT); // do {
deviceContext.getApiConnection().execute("/container/start .id=" + container.getId()); // container = selectContainer();
try { // if (container == null) {
Thread.sleep(10000L); // return;
} catch (InterruptedException e) { // }
throw new RuntimeException(e); //
} // if (!container.isRunning()) {
// SystemLogger.warning("The selected container is not running. There is a limitation that requires the container to be running.", LOG_CONTEXT);
containers = deviceContext.getContainers(); // SystemLogger.message("Starting container " + container, LOG_CONTEXT);
var id = container.getId(); // deviceContext.getApiConnection().execute("/container/start .id=" + container.getId());
container = containers.stream().filter(containerMetadata -> containerMetadata.getId().equals(id)).findFirst().orElse(null); // try {
if (container == null || !container.isRunning()) { // Thread.sleep(10000L);
SystemLogger.error("Something went wrong. Unable to start container.", LOG_CONTEXT); // } catch (InterruptedException e) {
pause(); // throw new RuntimeException(e);
} // }
} //
} while (container == null); // containers = deviceContext.getContainers();
// var id = container.getId();
SystemLogger.warning("/!\\ We are going to make a backup. You have to stop all services in this container to prevent data corruption.", LOG_CONTEXT); // container = containers.stream().filter(containerMetadata -> containerMetadata.getId().equals(id)).findFirst().orElse(null);
System.out.print("Type yes to continue or Ctrl+C to abort: "); // if (container == null || !container.isRunning()) {
while (!"yes".equals(cin())) ; // SystemLogger.error("Something went wrong. Unable to start container.", LOG_CONTEXT);
// pause();
// }
var layerFile = new File(container.getGuid()); // }
var outputFile = new File("backup_of_" + container.getComment() + ".tar"); // } 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);
var files = findFiles(container); // System.out.print("Type yes to continue or Ctrl+C to abort: ");
downloadFiles(container, files, layerFile); // while (!"yes".equals(cin())) ;
//
//
SystemLogger.message("Building docker image " + outputFile.getName(), LOG_CONTEXT); // var layerFile = new File(container.getGuid());
var builder = new DockerImageBuilder(container); // var outputFile = new File("backup_of_" + container.getComment() + ".tar");
builder.build(layerFile, outputFile); //
SystemLogger.message("Backup is done ", LOG_CONTEXT); //
pause(); // var files = findFiles(container);
// downloadFiles(container, files, layerFile);
} finally { //
if (reader != null) { //
reader.close(); // SystemLogger.message("Building docker image " + outputFile.getName(), LOG_CONTEXT);
} // var builder = new DockerImageBuilder(container);
if (deviceContext != null) { // builder.build(layerFile, outputFile);
deviceContext.close(); // SystemLogger.message("Backup is done ", LOG_CONTEXT);
} // pause();
} //
// } finally {
// if (reader != null) {
// reader.close();
// }
// if (deviceContext != null) {
// deviceContext.close();
// }
// }
// openSFTP(); // openSFTP();
@ -177,7 +171,7 @@ public class App {
} catch (IOException e) { } catch (IOException e) {
status.clear(); status.clear();
SystemLogger.error("Unable to copy file " + file.getPath(), LOG_CONTEXT, e); SystemLogger.error("Unable to copy file " + file.getPath(), LOG_CONTEXT, e);
pause(); console.pause();
} }
} }
tar.finish(); tar.finish();
@ -202,34 +196,34 @@ public class App {
private void auth() { private void auth() {
do { do {
try { // try {
//
System.out.print("Enter remote host:"); // System.out.print("Enter remote host:");
var host = cin(); // var host = cin();
if (host.trim().isEmpty()) { // if (host.trim().isEmpty()) {
throw new RuntimeException("Remote host is empty"); // throw new RuntimeException("Remote host is empty");
} // }
//
System.out.print("Enter username:"); // System.out.print("Enter username:");
var username = cin(); // var username = cin();
if (username.trim().isEmpty()) { // if (username.trim().isEmpty()) {
throw new RuntimeException("Username is empty"); // throw new RuntimeException("Username is empty");
} // }
System.out.print("Enter password:"); // System.out.print("Enter password:");
var password = cin(); // var password = cin();
if (password.trim().isEmpty()) { // if (password.trim().isEmpty()) {
throw new RuntimeException("Password is empty"); // throw new RuntimeException("Password is empty");
} // }
//
deviceContext = new DeviceContext(host, username, password); // deviceContext = new DeviceContext(host, username, password);
deviceContext.getApiConnection(); // deviceContext.getApiConnection();
} catch (Exception e) { // } catch (Exception e) {
SystemLogger.error("Unable to connect", LOG_CONTEXT, e); // SystemLogger.error("Unable to connect", LOG_CONTEXT, e);
if (deviceContext != null) { // if (deviceContext != null) {
deviceContext.close(); // deviceContext.close();
} // }
deviceContext = null; // deviceContext = null;
} // }
} while (deviceContext == null); } while (deviceContext == null);
SystemLogger.message("Connected", LOG_CONTEXT); 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