Compare commits

..

2 Commits

Author SHA1 Message Date
aabf39f041 wip: convert to thiserror and anyhow 2023-11-26 11:34:26 +11:00
c03adb808a wip: implement error kind 2023-11-25 19:09:01 +11:00
6 changed files with 67 additions and 37 deletions

28
Cargo.lock generated
View File

@ -26,6 +26,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "anyhow"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]]
name = "autocfg"
version = "1.1.0"
@ -315,6 +321,26 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio"
version = "1.34.0"
@ -440,9 +466,11 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
name = "wingmate-rs"
version = "0.1.0"
dependencies = [
"anyhow",
"lazy_static",
"nix",
"regex",
"thiserror",
"tokio",
"tokio-util",
]

View File

@ -6,8 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.75"
lazy_static = "1.4.0"
nix = {version = "0.27.1", features = ["process", "signal", "fs"]}
regex = "1.10.2"
thiserror = "1.0.50"
tokio = { version = "1.34.0", features = ["full"] }
tokio-util = "0.7.10"

View File

@ -1,3 +1 @@
#!/bin/bash
exec sleep 1
you cannot run this file

View File

@ -42,7 +42,7 @@ pub struct Config {
impl Config {
pub fn find(search_path: Vec<String>) -> Result<Config, Box<dyn std_error::Error>> {
if search_path.is_empty() {
return Err(wingmate_error::InvalidConfigSearchPathError.into());
return Err(wingmate_error::WingmateInitError::InvalidConfigSearchPath.into());
}
let mut svc_commands: Vec<Command> = Vec::new();
@ -81,7 +81,7 @@ impl Config {
}
if svc_commands.is_empty() && cron.is_empty() {
return Err(wingmate_error::NoServiceOrCronFoundError.into());
return Err(wingmate_error::WingmateInitError::NoServiceOrCron.into());
}
let mut config = Config {

View File

@ -11,30 +11,37 @@ use nix::sys::signal::{kill, Signal};
use nix::errno::Errno;
use nix::unistd::Pid;
use crate::init::config;
use crate::init::error::NoShellAvailableError;
use crate::init::error::{NoShellAvailableError, WingmateInitError};
pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send + Sync>>>, cfg: &config::Config, cancel: CancellationToken)
-> Result<(), Box<dyn error::Error>> {
for svc_ in cfg.get_service_iter() {
let shell: String = cfg.get_shell().ok_or::<Box<dyn error::Error>>(NoShellAvailableError.into())?;
let mut shell: String = String::new();
if let config::Command::ShellPrefixed(_) = svc_ {
shell = cfg.get_shell().ok_or::<Box<dyn error::Error>>(NoShellAvailableError.into())?;
}
let svc = svc_.clone();
// if let config::Command::ShellPrefixed(_) = svc {
// shell = cfg.get_shell().ok_or::<Box<dyn error::Error>>(NoShellAvailableError.into())?;
// }
let cancel = cancel.clone();
ts.spawn(async move {
'autorestart: loop {
let mut child: Child;
let shell = shell.clone();
let svc = svc.clone();
match svc {
config::Command::Direct(c) => {
child = Command::new(c).spawn().expect("change me");
let exp_str = c.clone();
child = Command::new(c).spawn().map_err(|e| {
Box::new(WingmateInitError::SpawnError { source: e, message: exp_str })
})?;
},
config::Command::ShellPrefixed(s) => {
child = Command::new(shell).arg(s).spawn().expect("change me");
let shell = shell.clone();
let exp_str = s.clone();
let exp_shell = shell.clone();
child = Command::new(shell).arg(s).spawn().map_err(|e| {
Box::new(WingmateInitError::SpawnError { source: e, message: format!("{} {}", exp_shell, exp_str) })
})?;
}
}
@ -74,12 +81,12 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
},
}
}
println!("starter: task completed");
dbg!("starter: task completed");
Ok(())
});
}
println!("starter: spawning completed");
dbg!("starter: spawning completed");
Ok(())
}
@ -95,8 +102,7 @@ fn result_match(result: tokio_result<ExitStatus>) -> Result<(), Box<dyn error::E
}
}
//TODO: remove me! this is for debug + tracing purpose
println!("starter: sleep exited");
dbg!("starter: sleep exited");
Ok(())
}

View File

@ -1,29 +1,25 @@
use thiserror::Error;
use std::fmt;
use std::error;
#[derive(Debug, Clone)]
pub struct InvalidConfigSearchPathError;
impl fmt::Display for InvalidConfigSearchPathError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid config search path")
#[derive(Error,Debug)]
pub enum WingmateInitError {
#[error("invalid config search path")]
InvalidConfigSearchPath,
#[error("no service or cron found")]
NoServiceOrCron,
#[error("failed to spawn: {}", message)]
SpawnError {
#[source]
source: std::io::Error,
message: String,
}
}
impl error::Error for InvalidConfigSearchPathError {}
#[derive(Debug,Clone)]
pub struct NoServiceOrCronFoundError;
impl fmt::Display for NoServiceOrCronFoundError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "no service or cron found")
}
}
impl error::Error for NoServiceOrCronFoundError {}
#[derive(Debug,Clone)]
pub struct CronSyntaxError(pub String);