Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 12, 2024
1 parent 327a76a commit ac8709a
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 66 deletions.
10 changes: 5 additions & 5 deletions docs/cli/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- [`pitchfork list [--hide-header]`](/cli/list.md)
- [`pitchfork logs [-n <N>] [-t --tail] [ID]...`](/cli/logs.md)
- [`pitchfork run [-f --force] <ID> [CMD]...`](/cli/run.md)
- [`pitchfork start [NAME]...`](/cli/start.md)
- [`pitchfork start [ID]...`](/cli/start.md)
- [`pitchfork status <ID>`](/cli/status.md)
- [`pitchfork stop <ID>`](/cli/stop.md)
- [`pitchfork supervisor <SUBCOMMAND>`](/cli/supervisor.md)
Expand Down
6 changes: 3 additions & 3 deletions docs/cli/start.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pitchfork.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/cli/clean.rs
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
4 changes: 2 additions & 2 deletions src/cli/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// ID of the daemon(s) in pitchfork.toml to start
id: Vec<String>,
}

impl Start {
Expand Down
16 changes: 14 additions & 2 deletions src/daemon.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
pub pid: Option<u32>,
pub status: DaemonStatus,
}

impl Display for Daemon {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.id)
}
}
23 changes: 23 additions & 0 deletions src/daemon_status.rs
Original file line number Diff line number Diff line change
@@ -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<i32>),
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(),
}
}
}
6 changes: 3 additions & 3 deletions src/ipc/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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),
Expand All @@ -34,7 +34,7 @@ pub enum IpcResponse {
Error(String),
DaemonAlreadyStopped,
DaemonAlreadyRunning,
DaemonStart { daemon: StateFileDaemon },
DaemonStart { daemon: Daemon },
DaemonFailed { error: String },
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate log;

mod cli;
mod daemon;
mod daemon_status;
mod env;
mod ipc;
mod logger;
Expand Down
9 changes: 6 additions & 3 deletions src/pitchfork_toml.rs
Original file line number Diff line number Diff line change
@@ -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<String, Daemon>,
pub daemons: IndexMap<String, PitchforkTomlDaemon>,
#[serde(skip)]
pub path: PathBuf,
}
Expand Down Expand Up @@ -44,3 +42,8 @@ impl PitchforkToml {
Ok(())
}
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct PitchforkTomlDaemon {
pub run: String,
}
43 changes: 3 additions & 40 deletions src/state_file.rs
Original file line number Diff line number Diff line change
@@ -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<String, StateFileDaemon>,
pub daemons: BTreeMap<String, Daemon>,
pub disabled: BTreeSet<String>,
#[serde(skip)]
pub(crate) path: PathBuf,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct StateFileDaemon {
pub id: String,
pub title: Option<String>,
pub pid: Option<u32>,
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<i32>),
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 {
Expand Down
12 changes: 7 additions & 5 deletions src/supervisor.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -306,7 +308,7 @@ impl Supervisor {
// TODO: cleanly stop ipc server
}

async fn active_daemons(&self) -> Vec<StateFileDaemon> {
async fn active_daemons(&self) -> Vec<Daemon> {
self.state_file
.lock()
.await
Expand All @@ -330,9 +332,9 @@ impl Supervisor {
id: &str,
pid: Option<u32>,
status: DaemonStatus,
) -> Result<StateFileDaemon> {
) -> Result<Daemon> {
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,
Expand All @@ -349,7 +351,7 @@ impl Supervisor {
Ok(daemon)
}

async fn get_daemon(&self, id: &str) -> Option<StateFileDaemon> {
async fn get_daemon(&self, id: &str) -> Option<Daemon> {
self.state_file.lock().await.daemons.get(id).cloned()
}
}

0 comments on commit ac8709a

Please sign in to comment.