add cli spawning
This commit is contained in:
parent
80bf7ba82d
commit
a1b79e742f
4 changed files with 200 additions and 30 deletions
47
src/main.rs
47
src/main.rs
|
|
@ -1,20 +1,26 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2025 April Faye John <april.john@denic.de> & Contributors
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 April Faye John <april.john@denic.de> & Contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
mod gui;
|
||||
mod bininfo;
|
||||
mod gui;
|
||||
mod pman;
|
||||
|
||||
use clap::{Subcommand, Parser};
|
||||
use pman::init_process_manager;
|
||||
use bunt::println;
|
||||
use clap::{Parser, Subcommand};
|
||||
use log::info;
|
||||
use pman::{init_process_manager, ProcessCommand, ProcessManager};
|
||||
use shadow_rs::shadow;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use tokio::{signal, sync::mpsc::Sender};
|
||||
|
||||
shadow!(build);
|
||||
|
||||
static PMAN_SENDER: OnceLock<Sender<ProcessCommand>> = OnceLock::new();
|
||||
|
||||
/// Simple program to greet a person
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
|
|
@ -39,30 +45,26 @@ enum Commands {
|
|||
remote: String,
|
||||
},
|
||||
#[command(about = "List compile time backed info to audit binary")]
|
||||
Shadow {
|
||||
outfile: Option<String>,
|
||||
},
|
||||
Shadow { outfile: Option<String> },
|
||||
#[command(about = "Show test gui")]
|
||||
Gui,
|
||||
Devtest,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
let args = Args::parse();
|
||||
|
||||
init_process_manager();
|
||||
let _ = init_process_manager();
|
||||
|
||||
match args.command {
|
||||
|
||||
Commands::Clone { remote } => {
|
||||
|
||||
}
|
||||
Commands::Clone { remote } => {}
|
||||
|
||||
Commands::Shadow { outfile } => {
|
||||
|
||||
bininfo::print_info();
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
Commands::Gui => {
|
||||
|
|
@ -70,7 +72,20 @@ fn main() {
|
|||
if let Err(e) = res {
|
||||
println!("{}", e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Commands::Devtest => {
|
||||
tokio::select! {
|
||||
_ = signal::ctrl_c() => {
|
||||
info!("Ctrl-c received");
|
||||
let sender = PMAN_SENDER.get().unwrap();
|
||||
let _ = sender.send(ProcessCommand::Shutdown).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//handling anything here for gui wont work
|
||||
println!("exit");
|
||||
}
|
||||
|
|
|
|||
76
src/pman.rs
76
src/pman.rs
|
|
@ -1,16 +1,36 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2025 April Faye John <april.john@denic.de> & Contributors
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 April Faye John <april.john@denic.de> & Contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
use clap::Command;
|
||||
use crate::PMAN_SENDER;
|
||||
use anyhow::{Ok, Result};
|
||||
use tokio::process::{Child, Command};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use log::{debug, info};
|
||||
use tokio::sync::mpsc;use tokio::sync::mpsc::Receiver;
|
||||
|
||||
pub fn init_process_manager() {
|
||||
let ProcessManager = ProcessManager::default();
|
||||
pub fn init_process_manager() -> Result<()> {
|
||||
let (tx, mut rx) = mpsc::channel::<ProcessCommand>(32);
|
||||
let mut process_manager = ProcessManager::default();
|
||||
let _ = PMAN_SENDER.set(tx);
|
||||
debug!("[pman-main] Spawning pman task");
|
||||
|
||||
tokio::spawn(async move {
|
||||
info!("[pman-task] Hello from Pman");
|
||||
while process_manager.lifecycle(&mut rx).await.is_ok() {
|
||||
let result = process_manager.alive_check().await;
|
||||
if let Err(e) = result {
|
||||
println!("{}", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ProcessManager {
|
||||
processes: Vec<Command>
|
||||
processes: Vec<Child>,
|
||||
}
|
||||
|
||||
impl Default for ProcessManager {
|
||||
|
|
@ -21,23 +41,51 @@ impl Default for ProcessManager {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ProcessCommand {
|
||||
SpawnShellCmd { cmd: String },
|
||||
Shutdown,
|
||||
}
|
||||
|
||||
impl ProcessManager {
|
||||
fn spawn_shell_command(self, command: String) {
|
||||
fn spawn_shell_command(&mut self, command: String) {
|
||||
if cfg!(target_os = "windows") {
|
||||
return self.spawn_cmd_command(command);
|
||||
} else if cfg!(target_os = "macos") {
|
||||
} else if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
|
||||
// TODO: reasearch if ios also has zsh
|
||||
return self.spawn_zsh_command(command);
|
||||
} else {
|
||||
return self.spawn_bash_command(command);
|
||||
}
|
||||
}
|
||||
fn spawn_bash_command(self, command: String) {
|
||||
|
||||
fn spawn_bash_command(&mut self, command: String) {
|
||||
let cmd = Command::new("bash")
|
||||
.arg("-c")
|
||||
.arg(command)
|
||||
.spawn().unwrap();
|
||||
self.processes.push(cmd);
|
||||
}
|
||||
fn spawn_cmd_command(self, command: String) {
|
||||
|
||||
fn spawn_cmd_command(&mut self, command: String) {}
|
||||
fn spawn_zsh_command(&mut self, command: String) {}
|
||||
pub async fn lifecycle(&mut self, rx: &mut Receiver<ProcessCommand>) -> Result<()> {
|
||||
let opt = rx.recv().await;
|
||||
if let Some(message) = opt {
|
||||
match message {
|
||||
ProcessCommand::SpawnShellCmd { cmd } => {}
|
||||
ProcessCommand::Shutdown => {
|
||||
info!("Close command");
|
||||
anyhow::bail!("Close command");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
anyhow::bail!("Channel closed");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn spawn_zsh_command(self, command: String) {
|
||||
|
||||
async fn alive_check(&mut self) -> Result<()> {
|
||||
for process in &mut self.processes {
|
||||
process.stdout
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue