use std::path::PathBuf;
use getset::{Getters, WithSetters};
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;
use crate::domain::{
process::{AgentTool, Process, ProcessKind, RestartPolicy, StopPolicy},
value::{CommandLine, Description, PaneId, ProcessName},
};
#[derive(Clone, Debug, Serialize, Deserialize, Getters, WithSetters, TypedBuilder)]
#[set_with]
pub struct ProcessSpec {
#[getset(get = "pub", set_with = "pub")]
name: ProcessName,
#[getset(get = "pub", set_with = "pub")]
#[builder(default)]
command: Option<CommandLine>,
#[getset(get = "pub", set_with = "pub")]
#[builder(default)]
working_dir: Option<PathBuf>,
#[getset(get = "pub", set_with = "pub")]
#[builder(default)]
description: Option<Description>,
#[getset(get = "pub", set_with = "pub")]
#[builder(default)]
restart: Option<RestartPolicy>,
#[getset(get = "pub", set_with = "pub")]
#[builder(default)]
stop: Option<StopPolicy>,
#[getset(get = "pub", set_with = "pub")]
#[builder(default)]
autostart: Option<bool>,
}
impl ProcessSpec {
pub fn to_process(&self, id: PaneId, kind: ProcessKind) -> Process {
Process::builder()
.id(id)
.name(self.name.clone())
.kind(kind)
.agent_tool(
(kind == ProcessKind::Agent)
.then(|| AgentTool::from_command(self.command.as_ref())),
)
.command(self.command.clone())
.working_dir(self.working_dir.clone())
.description(self.description.clone())
.restart(self.restart_policy())
.stop(if kind == ProcessKind::Command {
self.stop.clone()
} else {
None
})
.autostart(self.should_autostart(kind))
.build()
}
pub fn restart_policy(&self) -> RestartPolicy {
self.restart.unwrap_or(RestartPolicy::Never)
}
pub fn should_autostart(&self, kind: ProcessKind) -> bool {
self.autostart.unwrap_or(kind != ProcessKind::Command)
}
pub fn effective_stop_policy(&self, kind: ProcessKind) -> Option<StopPolicy> {
if kind == ProcessKind::Command {
Some(self.stop.clone().unwrap_or_default())
} else {
None
}
}
}