|
1 | | - |
| 1 | +use clap::{Parser, Subcommand}; |
| 2 | +use colored::*; |
| 3 | +use crate::{display_ascii, handle_run, handle_system, handle_unpack, handle_update, play_game, handle_immutable, handle_runtime, handle_install, handle_remove, handle_back, handle_clean, handle_snapshot, run_command_with_progress, RunCommands, SystemCommands, UnpackCommands}; |
| 4 | +use std::process::Command; |
| 5 | +#[derive(Parser)] |
| 6 | +#[command(name = "hacker", about = "A vibrant CLI tool for managing hacker tools, gaming, and system utilities", version = "1.1.0")] |
| 7 | +struct Cli { |
| 8 | + #[command(subcommand)] |
| 9 | + command: Commands, |
| 10 | +} |
| 11 | +#[derive(Subcommand)] |
| 12 | +enum Commands { |
| 13 | + /// Unpack various toolsets and applications |
| 14 | + Unpack { |
| 15 | + #[command(subcommand)] |
| 16 | + unpack_command: UnpackCommands, |
| 17 | + }, |
| 18 | + /// Display help information and list available commands |
| 19 | + Help, |
| 20 | + /// Display help UI |
| 21 | + HelpUi, |
| 22 | + /// Install a package using apt in snapshot |
| 23 | + Install { |
| 24 | + package: String, |
| 25 | + }, |
| 26 | + /// Remove a package using apt in snapshot |
| 27 | + Remove { |
| 28 | + package: String, |
| 29 | + }, |
| 30 | + /// Run flatpak install -y |
| 31 | + FlatpakInstall { |
| 32 | + package: String, |
| 33 | + }, |
| 34 | + /// Run flatpak remove -y |
| 35 | + FlatpakRemove { |
| 36 | + package: String, |
| 37 | + }, |
| 38 | + /// Run flatpak update -y |
| 39 | + FlatpakUpdate, |
| 40 | + /// System-related commands |
| 41 | + System { |
| 42 | + #[command(subcommand)] |
| 43 | + system_command: SystemCommands, |
| 44 | + }, |
| 45 | + /// Run specific HackerOS scripts and applications |
| 46 | + Run { |
| 47 | + #[command(subcommand)] |
| 48 | + run_command: RunCommands, |
| 49 | + }, |
| 50 | + /// Update the system |
| 51 | + Update, |
| 52 | + /// Play a simple terminal game |
| 53 | + Game, |
| 54 | + /// Information about Hacker programming language |
| 55 | + HackerLang, |
| 56 | + /// Display HackerOS ASCII art |
| 57 | + Ascii, |
| 58 | + /// Enter interactive Hacker shell mode |
| 59 | + Shell, |
| 60 | + /// Make system immutable |
| 61 | + Immutable, |
| 62 | + /// Run script in runtime mode (with safety checks) |
| 63 | + Runtime { |
| 64 | + script: String, |
| 65 | + }, |
| 66 | + /// Roll back to previous snapshot |
| 67 | + Back, |
| 68 | + /// Clean old snapshots |
| 69 | + Clean, |
| 70 | + /// Create a manual snapshot |
| 71 | + Snapshot, |
| 72 | +} |
| 73 | +fn main() { |
| 74 | + let cli = Cli::parse(); |
| 75 | + match cli.command { |
| 76 | + Commands::Unpack { unpack_command } => handle_unpack(unpack_command), |
| 77 | + Commands::Help => { |
| 78 | + let home = std::env::var("HOME").unwrap_or_default(); |
| 79 | + let help_bin = format!("{}/.hackeros/hacker-help", home); |
| 80 | + match Command::new(&help_bin).status() { |
| 81 | + Ok(status) => { |
| 82 | + if !status.success() { |
| 83 | + println!("{}", "Error running hacker-help. Ensure it's installed and executable in ~/.hackeros/".red().bold().on_black()); |
| 84 | + } |
| 85 | + } |
| 86 | + Err(e) => { |
| 87 | + println!("{}", format!("Failed to execute hacker-help: {}", e).red().bold().on_black()); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + Commands::HelpUi => { |
| 92 | + let home = std::env::var("HOME").unwrap_or_default(); |
| 93 | + let help_bin = format!("{}/.hackeros/hacker-help", home); |
| 94 | + match Command::new(&help_bin).status() { |
| 95 | + Ok(status) => { |
| 96 | + if !status.success() { |
| 97 | + println!("{}", "Error running hacker-help. Ensure it's installed and executable in ~/.hackeros/".red().bold().on_black()); |
| 98 | + } |
| 99 | + } |
| 100 | + Err(e) => { |
| 101 | + println!("{}", format!("Failed to execute hacker-help: {}", e).red().bold().on_black()); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + Commands::Install { package } => handle_install(&package), |
| 106 | + Commands::Remove { package } => handle_remove(&package), |
| 107 | + Commands::FlatpakInstall { package } => run_command_with_progress("flatpak", vec!["install", "-y", "flathub", &package], "Running flatpak install"), |
| 108 | + Commands::FlatpakRemove { package } => run_command_with_progress("flatpak", vec!["remove", "-y", &package], "Running flatpak remove"), |
| 109 | + Commands::FlatpakUpdate => run_command_with_progress("flatpak", vec!["update", "-y"], "Running flatpak update"), |
| 110 | + Commands::System { system_command } => handle_system(system_command), |
| 111 | + Commands::Run { run_command } => handle_run(run_command), |
| 112 | + Commands::Update => handle_update(), |
| 113 | + Commands::Game => play_game(), |
| 114 | + Commands::HackerLang => { |
| 115 | + println!("{}", "========== Hacker Programming Language ==========".magenta().bold().on_black()); |
| 116 | + println!("{}", "To use the hacker programming language for files/scripts with .hacker extension,".cyan().bold().on_black()); |
| 117 | + println!("{}", "use the command 'hackerc' to compile or run them.".cyan().bold().on_black()); |
| 118 | + println!("{}", "Note: This is for advanced users. Ensure hackerc is installed separately.".yellow().bold().on_black()); |
| 119 | + println!("{}", "========== End of Info ==========".magenta().bold().on_black()); |
| 120 | + } |
| 121 | + Commands::Ascii => display_ascii(), |
| 122 | + Commands::Shell => { |
| 123 | + let home = std::env::var("HOME").unwrap_or_default(); |
| 124 | + let shell_bin = format!("{}/.hackeros/hacker-shell", home); |
| 125 | + match Command::new(&shell_bin).status() { |
| 126 | + Ok(status) => { |
| 127 | + if !status.success() { |
| 128 | + println!("{}", "Error running hacker-shell. Ensure it's installed and executable in ~/.hackeros/".red().bold().on_black()); |
| 129 | + } |
| 130 | + } |
| 131 | + Err(e) => { |
| 132 | + println!("{}", format!("Failed to execute hacker-shell: {}", e).red().bold().on_black()); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + Commands::Immutable => handle_immutable(), |
| 137 | + Commands::Runtime { script } => handle_runtime(&script), |
| 138 | + Commands::Back => handle_back(), |
| 139 | + Commands::Clean => handle_clean(), |
| 140 | + Commands::Snapshot => handle_snapshot(), |
| 141 | + } |
| 142 | +} |
0 commit comments