From ac8709af9ffd54fb060ad9a06bda8074bf57f8a1 Mon Sep 17 00:00:00 2001 From: jdx <216188+jdx@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:44:35 -0600 Subject: [PATCH] wip --- docs/cli/commands.json | 10 +++++----- docs/cli/index.md | 2 +- docs/cli/start.md | 6 +++--- pitchfork.usage.kdl | 2 +- src/cli/clean.rs | 3 ++- src/cli/start.rs | 4 ++-- src/daemon.rs | 16 ++++++++++++++-- src/daemon_status.rs | 23 ++++++++++++++++++++++ src/ipc/mod.rs | 6 +++--- src/main.rs | 1 + src/pitchfork_toml.rs | 9 ++++++--- src/state_file.rs | 43 +++--------------------------------------- src/supervisor.rs | 12 +++++++----- 13 files changed, 71 insertions(+), 66 deletions(-) create mode 100644 src/daemon_status.rs diff --git a/docs/cli/commands.json b/docs/cli/commands.json index b6ac4cc..6d20525 100644 --- a/docs/cli/commands.json +++ b/docs/cli/commands.json @@ -363,14 +363,14 @@ "full_cmd": [ "start" ], - "usage": "start [NAME]...", + "usage": "start [ID]...", "subcommands": {}, "args": [ { - "name": "NAME", - "usage": "[NAME]...", - "help": "Name of the daemon(s) in pitchfork.toml to start", - "help_first_line": "Name of the daemon(s) in pitchfork.toml to start", + "name": "ID", + "usage": "[ID]...", + "help": "ID of the daemon(s) in pitchfork.toml to start", + "help_first_line": "ID of the daemon(s) in pitchfork.toml to start", "required": false, "var": true, "hide": false diff --git a/docs/cli/index.md b/docs/cli/index.md index af4db09..10db09a 100644 --- a/docs/cli/index.md +++ b/docs/cli/index.md @@ -17,7 +17,7 @@ - [`pitchfork list [--hide-header]`](/cli/list.md) - [`pitchfork logs [-n ] [-t --tail] [ID]...`](/cli/logs.md) - [`pitchfork run [-f --force] [CMD]...`](/cli/run.md) -- [`pitchfork start [NAME]...`](/cli/start.md) +- [`pitchfork start [ID]...`](/cli/start.md) - [`pitchfork status `](/cli/status.md) - [`pitchfork stop `](/cli/stop.md) - [`pitchfork supervisor `](/cli/supervisor.md) diff --git a/docs/cli/start.md b/docs/cli/start.md index 352d370..5609dc2 100644 --- a/docs/cli/start.md +++ b/docs/cli/start.md @@ -1,12 +1,12 @@ # `pitchfork start` -- **Usage**: `pitchfork start [NAME]...` +- **Usage**: `pitchfork start [ID]...` - **Aliases**: `s` Starts a daemon from a pitchfork.toml file ## Arguments -### `[NAME]...` +### `[ID]...` -Name of the daemon(s) in pitchfork.toml to start +ID of the daemon(s) in pitchfork.toml to start diff --git a/pitchfork.usage.kdl b/pitchfork.usage.kdl index 2b5bdeb..3cddd98 100644 --- a/pitchfork.usage.kdl +++ b/pitchfork.usage.kdl @@ -59,7 +59,7 @@ cmd "run" help="Runs a one-off daemon" { } cmd "start" help="Starts a daemon from a pitchfork.toml file" { alias "s" - arg "[NAME]..." help="Name of the daemon(s) in pitchfork.toml to start" var=true + arg "[ID]..." help="ID of the daemon(s) in pitchfork.toml to start" var=true } cmd "status" help="Display the status of a daemon" { alias "stat" diff --git a/src/cli/clean.rs b/src/cli/clean.rs index 953fa9f..a3918e6 100644 --- a/src/cli/clean.rs +++ b/src/cli/clean.rs @@ -1,4 +1,5 @@ -use crate::state_file::{DaemonStatus, StateFile}; +use crate::daemon_status::DaemonStatus; +use crate::state_file::StateFile; use crate::Result; /// Removes stopped/failed daemons from `pitchfork list` diff --git a/src/cli/start.rs b/src/cli/start.rs index 9936eb3..cc91505 100644 --- a/src/cli/start.rs +++ b/src/cli/start.rs @@ -6,8 +6,8 @@ use crate::Result; #[derive(Debug, clap::Args)] #[clap(visible_alias = "s", verbatim_doc_comment)] pub struct Start { - /// Name of the daemon(s) in pitchfork.toml to start - name: Vec, + /// ID of the daemon(s) in pitchfork.toml to start + id: Vec, } impl Start { diff --git a/src/daemon.rs b/src/daemon.rs index d15f79c..7b1711f 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -1,4 +1,16 @@ -#[derive(Debug, serde::Serialize, serde::Deserialize)] +use crate::daemon_status::DaemonStatus; +use std::fmt::Display; + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct Daemon { - pub run: String, + pub id: String, + pub title: Option, + pub pid: Option, + pub status: DaemonStatus, +} + +impl Display for Daemon { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.id) + } } diff --git a/src/daemon_status.rs b/src/daemon_status.rs new file mode 100644 index 0000000..efb37ed --- /dev/null +++ b/src/daemon_status.rs @@ -0,0 +1,23 @@ +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, strum::Display, strum::EnumIs)] +#[strum(serialize_all = "snake_case")] +#[serde(rename_all = "snake_case")] +pub enum DaemonStatus { + Failed(String), + Waiting, + Running, + Errored(Option), + Stopped, +} + +impl DaemonStatus { + pub fn style(&self) -> String { + let s = self.to_string(); + match self { + DaemonStatus::Failed(_) => console::style(s).red().to_string(), + DaemonStatus::Waiting => console::style(s).yellow().to_string(), + DaemonStatus::Running => console::style(s).green().to_string(), + DaemonStatus::Stopped => console::style(s).dim().to_string(), + DaemonStatus::Errored(_) => console::style(s).red().to_string(), + } + } +} diff --git a/src/ipc/mod.rs b/src/ipc/mod.rs index 9d9519d..5a5c508 100644 --- a/src/ipc/mod.rs +++ b/src/ipc/mod.rs @@ -1,5 +1,5 @@ +use crate::daemon::Daemon; use crate::env; -use crate::state_file::StateFileDaemon; use crate::Result; use interprocess::local_socket::{GenericFilePath, Name, ToFsName}; use miette::IntoDiagnostic; @@ -15,7 +15,7 @@ pub enum IpcMessage { Stop(String), DaemonAlreadyRunning(String), DaemonAlreadyStopped(String), - DaemonStart(StateFileDaemon), + DaemonStart(Daemon), DaemonStop { name: String }, DaemonFailed { name: String, error: String }, Response(String), @@ -34,7 +34,7 @@ pub enum IpcResponse { Error(String), DaemonAlreadyStopped, DaemonAlreadyRunning, - DaemonStart { daemon: StateFileDaemon }, + DaemonStart { daemon: Daemon }, DaemonFailed { error: String }, } diff --git a/src/main.rs b/src/main.rs index c4f818a..6e9ad2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ extern crate log; mod cli; mod daemon; +mod daemon_status; mod env; mod ipc; mod logger; diff --git a/src/pitchfork_toml.rs b/src/pitchfork_toml.rs index 8078a1f..eda3279 100644 --- a/src/pitchfork_toml.rs +++ b/src/pitchfork_toml.rs @@ -1,12 +1,10 @@ -use crate::daemon::Daemon; use crate::{env, Result}; use indexmap::IndexMap; use miette::IntoDiagnostic; use std::path::{Path, PathBuf}; - #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct PitchforkToml { - pub daemons: IndexMap, + pub daemons: IndexMap, #[serde(skip)] pub path: PathBuf, } @@ -44,3 +42,8 @@ impl PitchforkToml { Ok(()) } } + +#[derive(Debug, serde::Serialize, serde::Deserialize)] +pub struct PitchforkTomlDaemon { + pub run: String, +} diff --git a/src/state_file.rs b/src/state_file.rs index 4e14616..fac2cba 100644 --- a/src/state_file.rs +++ b/src/state_file.rs @@ -1,56 +1,19 @@ +use crate::daemon::Daemon; use crate::{env, Result}; use miette::IntoDiagnostic; use once_cell::sync::Lazy; use std::collections::{BTreeMap, BTreeSet}; -use std::fmt::{Debug, Display}; +use std::fmt::Debug; use std::path::{Path, PathBuf}; #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct StateFile { - pub daemons: BTreeMap, + pub daemons: BTreeMap, pub disabled: BTreeSet, #[serde(skip)] pub(crate) path: PathBuf, } -#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] -pub struct StateFileDaemon { - pub id: String, - pub title: Option, - pub pid: Option, - pub status: DaemonStatus, -} - -impl Display for StateFileDaemon { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.id) - } -} - -#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, strum::Display, strum::EnumIs)] -#[strum(serialize_all = "snake_case")] -#[serde(rename_all = "snake_case")] -pub enum DaemonStatus { - Failed(String), - Waiting, - Running, - Errored(Option), - Stopped, -} - -impl DaemonStatus { - pub fn style(&self) -> String { - let s = self.to_string(); - match self { - DaemonStatus::Failed(_) => console::style(s).red().to_string(), - DaemonStatus::Waiting => console::style(s).yellow().to_string(), - DaemonStatus::Running => console::style(s).green().to_string(), - DaemonStatus::Stopped => console::style(s).dim().to_string(), - DaemonStatus::Errored(_) => console::style(s).red().to_string(), - } - } -} - impl StateFile { pub fn new(path: PathBuf) -> Self { Self { diff --git a/src/supervisor.rs b/src/supervisor.rs index 44d2294..ae68484 100644 --- a/src/supervisor.rs +++ b/src/supervisor.rs @@ -1,7 +1,9 @@ +use crate::daemon::Daemon; +use crate::daemon_status::DaemonStatus; use crate::ipc::server::IpcServer; use crate::ipc::{IpcRequest, IpcResponse}; use crate::procs::PROCS; -use crate::state_file::{DaemonStatus, StateFile, StateFileDaemon}; +use crate::state_file::StateFile; use crate::watch_files::WatchFiles; use crate::{env, Result}; use itertools::Itertools; @@ -306,7 +308,7 @@ impl Supervisor { // TODO: cleanly stop ipc server } - async fn active_daemons(&self) -> Vec { + async fn active_daemons(&self) -> Vec { self.state_file .lock() .await @@ -330,9 +332,9 @@ impl Supervisor { id: &str, pid: Option, status: DaemonStatus, - ) -> Result { + ) -> Result { info!("upserting daemon: {id} pid: {pid:?} status: {status}"); - let daemon = StateFileDaemon { + let daemon = Daemon { id: id.to_string(), title: pid.and_then(|pid| PROCS.title(pid)), pid, @@ -349,7 +351,7 @@ impl Supervisor { Ok(daemon) } - async fn get_daemon(&self, id: &str) -> Option { + async fn get_daemon(&self, id: &str) -> Option { self.state_file.lock().await.daemons.get(id).cloned() } }