| 1 | mod cli; |
| 2 | mod config; |
| 3 | mod daemon; |
| 4 | mod process; |
| 5 | mod tui; |
| 6 | |
| 7 | use clap::{Parser, Subcommand}; |
| 8 | |
| 9 | #[derive(Parser)] |
| 10 | #[command( |
| 11 | name = "proses", |
| 12 | version = "0.1.0", |
| 13 | about = "Proses – Professional Secure Execution System", |
| 14 | long_about = None |
| 15 | )] |
| 16 | struct Cli { |
| 17 | #[command(subcommand)] |
| 18 | command: Commands, |
| 19 | } |
| 20 | |
| 21 | #[derive(Subcommand)] |
| 22 | enum Commands { |
| 23 | /// Start a new process (or replace an existing one with the same name) |
| 24 | #[command(visible_alias = "run")] |
| 25 | Start { |
| 26 | /// Shell command to execute (passed verbatim to sh -c) |
| 27 | command: String, |
| 28 | /// Human-readable name (defaults to the first token of the command) |
| 29 | #[arg(short, long)] |
| 30 | name: Option<String>, |
| 31 | /// Working directory (defaults to the current directory) |
| 32 | #[arg(long)] |
| 33 | cwd: Option<String>, |
| 34 | /// Extra environment variables in KEY=VALUE format |
| 35 | #[arg(long = "env", value_name = "KEY=VALUE")] |
| 36 | env: Vec<String>, |
| 37 | /// Max automatic restarts before marking errored (0 = unlimited) |
| 38 | #[arg(long, default_value_t = 10)] |
| 39 | max_restarts: u32, |
| 40 | }, |
| 41 | |
| 42 | /// Gracefully stop a process (it will not be auto-restarted) |
| 43 | Stop { |
| 44 | /// Process name or numeric ID |
| 45 | name_or_id: String, |
| 46 | }, |
| 47 | |
| 48 | /// Stop then immediately re-launch a process |
| 49 | Restart { |
| 50 | /// Process name or numeric ID |
| 51 | name_or_id: String, |
| 52 | }, |
| 53 | |
| 54 | /// Stop and permanently remove a process from the list |
| 55 | #[command(visible_alias = "rm", visible_alias = "del")] |
| 56 | Delete { |
| 57 | /// Process name or numeric ID |
| 58 | name_or_id: String, |
| 59 | }, |
| 60 | |
| 61 | /// Open the live process dashboard (interactive Ratatui TUI) |
| 62 | #[command(visible_alias = "ls", visible_alias = "ps", visible_alias = "status")] |
| 63 | List, |
| 64 | |
| 65 | /// Dump the last N lines of a process's log to stdout |
| 66 | Logs { |
| 67 | /// Process name or numeric ID |
| 68 | name_or_id: String, |
| 69 | /// Number of log lines to show |
| 70 | #[arg(short = 'n', long, default_value_t = 50)] |
| 71 | lines: usize, |
| 72 | }, |
| 73 | |
| 74 | /// Flush the in-memory store to ~/.proses/store.json |
| 75 | Save, |
| 76 | |
| 77 | /// Re-launch all processes that were running when last saved |
| 78 | Resurrect, |
| 79 | |
| 80 | /// Print detailed information about a process |
| 81 | #[command(visible_alias = "info")] |
| 82 | Show { |
| 83 | /// Process name or numeric ID |
| 84 | name_or_id: String, |
| 85 | }, |
| 86 | |
| 87 | /// Manage the background daemon |
| 88 | Daemon { |
| 89 | #[command(subcommand)] |
| 90 | command: DaemonCommands, |
| 91 | }, |
| 92 | } |
| 93 | |
| 94 | #[derive(Subcommand)] |
| 95 | enum DaemonCommands { |
| 96 | /// Start the background daemon |
| 97 | Start, |
| 98 | /// Stop the background daemon |
| 99 | Stop, |
| 100 | /// Show whether the daemon is running and its PID |
| 101 | Status, |
| 102 | } |
| 103 | |
| 104 | fn main() { |
| 105 | let cli = Cli::parse(); |
| 106 | |
| 107 | // Initialise ~/.proses/ paths before anything else. |
| 108 | config::init(); |
| 109 | |
| 110 | match cli.command { |
| 111 | // Daemon sub-commands manage the background process directly — no |
| 112 | // need to auto-start the daemon first. |
| 113 | Commands::Daemon { command } => match command { |
| 114 | DaemonCommands::Start => daemon::start_cmd(), |
| 115 | DaemonCommands::Stop => daemon::stop_cmd(), |
| 116 | DaemonCommands::Status => daemon::status_cmd(), |
| 117 | }, |
| 118 | |
| 119 | // All other commands talk to the daemon over IPC, so we must ensure |
| 120 | // it is running before proceeding. |
| 121 | other => { |
| 122 | daemon::ensure_running(); |
| 123 | dispatch(other); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | fn dispatch(command: Commands) { |
| 129 | match command { |
| 130 | Commands::Start { |
| 131 | command, |
| 132 | name, |
| 133 | cwd, |
| 134 | env, |
| 135 | max_restarts, |
| 136 | } => { |
| 137 | cli::start(command, name, cwd, env, max_restarts); |
| 138 | } |
| 139 | Commands::Stop { name_or_id } => cli::stop(name_or_id), |
| 140 | Commands::Restart { name_or_id } => cli::restart(name_or_id), |
| 141 | Commands::Delete { name_or_id } => cli::delete(name_or_id), |
| 142 | Commands::Save => cli::save(), |
| 143 | Commands::Resurrect => cli::resurrect(), |
| 144 | Commands::Show { name_or_id } => cli::show(name_or_id), |
| 145 | Commands::Logs { name_or_id, lines } => cli::logs(name_or_id, lines), |
| 146 | Commands::List => { |
| 147 | tui::run_dashboard().unwrap_or_else(|e| { |
| 148 | eprintln!(" ✗ TUI error: {e}"); |
| 149 | std::process::exit(1); |
| 150 | }); |
| 151 | } |
| 152 | Commands::Daemon { .. } => unreachable!(), |
| 153 | } |
| 154 | } |