├── .gitignore ├── pom.xml └── src └── main └── java └── org └── example ├── Main.java ├── menus ├── AptMenu.java ├── MainMenu.java └── Menu.java └── tools ├── APT.java └── Terminal.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | !**/src/main/**/target/ 4 | !**/src/test/**/target/ 5 | 6 | ### IntelliJ IDEA ### 7 | .idea/modules.xml 8 | .idea/jarRepositories.xml 9 | .idea/compiler.xml 10 | .idea/libraries/ 11 | *.iws 12 | *.iml 13 | *.ipr 14 | 15 | ### Eclipse ### 16 | .apt_generated 17 | .classpath 18 | .factorypath 19 | .project 20 | .settings 21 | .springBeans 22 | .sts4-cache 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | build/ 31 | !**/src/main/**/build/ 32 | !**/src/test/**/build/ 33 | 34 | ### VS Code ### 35 | .vscode/ 36 | 37 | ### Mac OS ### 38 | .DS_Store 39 | /.idea/ 40 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | co.kahveci 8 | Linux-Terminal-Automation 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 23 13 | 23 14 | UTF-8 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/java/org/example/Main.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | import org.example.menus.MainMenu; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | new MainMenu().show(); 8 | } 9 | } -------------------------------------------------------------------------------- /src/main/java/org/example/menus/AptMenu.java: -------------------------------------------------------------------------------- 1 | package org.example.menus; 2 | 3 | import org.example.tools.APT; 4 | 5 | public class AptMenu extends Menu{ 6 | public void show() { 7 | int aptChoice; 8 | 9 | while (true) { 10 | System.out.println("\n>> apt operations:"); 11 | System.out.println("\t[1] Update"); 12 | System.out.println("\t[2] Other"); 13 | System.out.println("\t[0] Return to main menu"); 14 | 15 | aptChoice = scanner.nextInt(); 16 | scanner.nextLine(); // consume the newline character 17 | 18 | switch (aptChoice) { 19 | case 1: 20 | APT.update(); 21 | break; 22 | case 2: 23 | System.out.println("Other APT operation not yet implemented."); 24 | break; 25 | case 0: 26 | return; 27 | default: 28 | System.out.println("Invalid selection. Please try again."); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/example/menus/MainMenu.java: -------------------------------------------------------------------------------- 1 | package org.example.menus; 2 | 3 | public class MainMenu extends Menu{ 4 | public void show() { 5 | int mainChoice; 6 | 7 | while (true) { 8 | System.out.println("\n> main menu"); 9 | System.out.println("\t[1] apt"); 10 | System.out.println("\t[2] other operations"); 11 | System.out.println("\t[0] close terminal and exit program"); 12 | 13 | mainChoice = scanner.nextInt(); 14 | scanner.nextLine(); // consume the newline character 15 | 16 | switch (mainChoice) { 17 | case 1: 18 | new AptMenu().show(); 19 | break; 20 | case 2: 21 | System.out.println("Other operations not yet implemented."); 22 | break; 23 | case 0: 24 | System.out.println("Exiting program..."); 25 | return; 26 | default: 27 | System.out.println("Invalid selection. Please try again."); 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/example/menus/Menu.java: -------------------------------------------------------------------------------- 1 | package org.example.menus; 2 | 3 | import java.util.Scanner; 4 | 5 | public abstract class Menu { 6 | static Scanner scanner = new Scanner(System.in); 7 | public abstract void show(); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/org/example/tools/APT.java: -------------------------------------------------------------------------------- 1 | package org.example.tools; 2 | 3 | import static org.example.tools.Terminal.run; 4 | 5 | public class APT { 6 | public static void update() { 7 | // String line; 8 | 9 | String[] commands = { 10 | "gnome-terminal", "--wait", "--", 11 | "bash", "-c", 12 | "sudo apt update && sudo apt upgrade -y && sudo apt autopurge -y && sudo apt clean; exec bash" 13 | }; 14 | 15 | run(commands); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/example/tools/Terminal.java: -------------------------------------------------------------------------------- 1 | package org.example.tools; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.InputStreamReader; 5 | 6 | public class Terminal { 7 | public static void run(String[] commands) { 8 | String line; 9 | try { 10 | ProcessBuilder processBuilder = new ProcessBuilder(commands); 11 | Process process = processBuilder.start(); 12 | 13 | BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 14 | while ((line = reader.readLine()) != null) { 15 | System.out.println(line); 16 | } 17 | process.waitFor(); 18 | } catch (Exception e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | } 23 | --------------------------------------------------------------------------------