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 ac8709a commit d797c6c
Show file tree
Hide file tree
Showing 24 changed files with 364 additions and 121 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ rev_lines = "0.3"
rmp-serde = "1"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["indexmap"] }
shell-words = "1.1.0"
strum = { version = "0.26", features = ["derive"] }
sysinfo = "0.33"
tokio = { version = "1", features = ["full"] }
Expand Down
89 changes: 79 additions & 10 deletions docs/cli/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,56 @@
"config",
"add"
],
"usage": "config add",
"usage": "config add [--autostart] [--autostop] <ID> [ARGS]...",
"subcommands": {},
"args": [],
"flags": [],
"args": [
{
"name": "ID",
"usage": "<ID>",
"help": "ID of the daemon to add",
"help_first_line": "ID of the daemon to add",
"required": true,
"hide": false
},
{
"name": "ARGS",
"usage": "[ARGS]...",
"help": "Arguments to pass to the daemon",
"help_first_line": "Arguments to pass to the daemon",
"required": false,
"var": true,
"hide": false
}
],
"flags": [
{
"name": "autostart",
"usage": "--autostart",
"help": "Autostart the daemon when entering the directory",
"help_first_line": "Autostart the daemon when entering the directory",
"short": [],
"long": [
"autostart"
],
"hide": false,
"global": false
},
{
"name": "autostop",
"usage": "--autostop",
"help": "Autostop the daemon when leaving the directory",
"help_first_line": "Autostop the daemon when leaving the directory",
"short": [],
"long": [
"autostop"
],
"hide": false,
"global": false
}
],
"mounts": [],
"hide": false,
"help": "Add a new daemon to pitchfork.toml",
"help": "Add a new daemon to ./pitchfork.toml",
"name": "add",
"aliases": [
"a"
Expand All @@ -112,9 +155,18 @@
"config",
"remove"
],
"usage": "config remove",
"usage": "config remove <ID>",
"subcommands": {},
"args": [],
"args": [
{
"name": "ID",
"usage": "<ID>",
"help": "The ID of the daemon to remove",
"help_first_line": "The ID of the daemon to remove",
"required": true,
"hide": false
}
],
"flags": [],
"mounts": [],
"hide": false,
Expand Down Expand Up @@ -316,7 +368,7 @@
"full_cmd": [
"run"
],
"usage": "run [-f --force] <ID> [CMD]...",
"usage": "run [-f --force] <ID> [RUN]...",
"subcommands": {},
"args": [
{
Expand All @@ -328,8 +380,8 @@
"hide": false
},
{
"name": "CMD",
"usage": "[CMD]...",
"name": "RUN",
"usage": "[RUN]...",
"required": false,
"var": true,
"hide": false
Expand Down Expand Up @@ -376,7 +428,24 @@
"hide": false
}
],
"flags": [],
"flags": [
{
"name": "shell-pid",
"usage": "--shell-pid <SHELL_PID>",
"short": [],
"long": [
"shell-pid"
],
"hide": true,
"global": false,
"arg": {
"name": "SHELL_PID",
"usage": "<SHELL_PID>",
"required": true,
"hide": false
}
}
],
"mounts": [],
"hide": false,
"help": "Starts a daemon from a pitchfork.toml file",
Expand Down
4 changes: 2 additions & 2 deletions docs/cli/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ without a subcommand, lists all pitchfork.toml files from the current directory

## Subcommands

- [`pitchfork config add`](/cli/config/add.md)
- [`pitchfork config remove`](/cli/config/remove.md)
- [`pitchfork config add [--autostart] [--autostop] <ID> [ARGS]...`](/cli/config/add.md)
- [`pitchfork config remove <ID>`](/cli/config/remove.md)
24 changes: 22 additions & 2 deletions docs/cli/config/add.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
# `pitchfork config add`

- **Usage**: `pitchfork config add`
- **Usage**: `pitchfork config add [--autostart] [--autostop] <ID> [ARGS]...`
- **Aliases**: `a`

Add a new daemon to pitchfork.toml
Add a new daemon to ./pitchfork.toml

## Arguments

### `<ID>`

ID of the daemon to add

### `[ARGS]...`

Arguments to pass to the daemon

## Flags

### `--autostart`

Autostart the daemon when entering the directory

### `--autostop`

Autostop the daemon when leaving the directory
8 changes: 7 additions & 1 deletion docs/cli/config/remove.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# `pitchfork config remove`

- **Usage**: `pitchfork config remove`
- **Usage**: `pitchfork config remove <ID>`
- **Aliases**: `rm`

Remove a daemon from pitchfork.toml

## Arguments

### `<ID>`

The ID of the daemon to remove
6 changes: 3 additions & 3 deletions docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
- [`pitchfork activate <SHELL>`](/cli/activate.md)
- [`pitchfork clean`](/cli/clean.md)
- [`pitchfork config <SUBCOMMAND>`](/cli/config.md)
- [`pitchfork config add`](/cli/config/add.md)
- [`pitchfork config remove`](/cli/config/remove.md)
- [`pitchfork config add [--autostart] [--autostop] <ID> [ARGS]...`](/cli/config/add.md)
- [`pitchfork config remove <ID>`](/cli/config/remove.md)
- [`pitchfork completion <SHELL>`](/cli/completion.md)
- [`pitchfork disable <ID>`](/cli/disable.md)
- [`pitchfork enable <ID>`](/cli/enable.md)
- [`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 run [-f --force] <ID> [RUN]...`](/cli/run.md)
- [`pitchfork start [ID]...`](/cli/start.md)
- [`pitchfork status <ID>`](/cli/status.md)
- [`pitchfork stop <ID>`](/cli/stop.md)
Expand Down
4 changes: 2 additions & 2 deletions docs/cli/run.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `pitchfork run`

- **Usage**: `pitchfork run [-f --force] <ID> [CMD]...`
- **Usage**: `pitchfork run [-f --force] <ID> [RUN]...`
- **Aliases**: `r`

Runs a one-off daemon
Expand All @@ -11,7 +11,7 @@ Runs a one-off daemon

Name of the daemon to run

### `[CMD]...`
### `[RUN]...`

Runs a one-off daemon

Expand Down
12 changes: 10 additions & 2 deletions pitchfork.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ cmd "config" help="manage/edit pitchfork.toml files" {
long_help r"manage/edit pitchfork.toml files

without a subcommand, lists all pitchfork.toml files from the current directory"
cmd "add" help="Add a new daemon to pitchfork.toml" {
cmd "add" help="Add a new daemon to ./pitchfork.toml" {
alias "a"
flag "--autostart" help="Autostart the daemon when entering the directory"
flag "--autostop" help="Autostop the daemon when leaving the directory"
arg "<ID>" help="ID of the daemon to add"
arg "[ARGS]..." help="Arguments to pass to the daemon" var=true
}
cmd "remove" help="Remove a daemon from pitchfork.toml" {
alias "rm"
arg "<ID>" help="The ID of the daemon to remove"
}
}
cmd "completion" help="Generates shell completion scripts" {
Expand Down Expand Up @@ -55,10 +60,13 @@ cmd "run" help="Runs a one-off daemon" {
alias "r"
flag "-f --force"
arg "<ID>" help="Name of the daemon to run"
arg "[CMD]..." var=true
arg "[RUN]..." var=true
}
cmd "start" help="Starts a daemon from a pitchfork.toml file" {
alias "s"
flag "--shell-pid" hide=true {
arg "<SHELL_PID>"
}
arg "[ID]..." help="ID of the daemon(s) in pitchfork.toml to start" var=true
}
cmd "status" help="Display the status of a daemon" {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Activate {

impl Activate {
pub async fn run(&self) -> Result<()> {
let pitchfork = env::BIN_PATH.to_string_lossy().to_string();
let pitchfork = env::PITCHFORK_BIN.to_string_lossy().to_string();
let s = match self.shell.as_str() {
"bash" => format!(
r#"
Expand Down
26 changes: 24 additions & 2 deletions src/cli/cd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::Result;
use crate::pitchfork_toml::{PitchforkToml, PitchforkTomlAuto};
use crate::{env, Result};
use duct::cmd;
use itertools::Itertools;
use miette::IntoDiagnostic;

#[derive(Debug, clap::Args)]
#[clap(hide = true, verbatim_doc_comment)]
Expand All @@ -9,7 +13,25 @@ pub struct Cd {

impl Cd {
pub async fn run(&self) -> Result<()> {
dbg!(self);
let pt = PitchforkToml::all_merged();
let to_start = pt
.daemons
.into_iter()
.filter(|(_id, d)| d.auto.contains(&PitchforkTomlAuto::Start))
.map(|(id, _d)| id)
.collect_vec();
if to_start.is_empty() {
return Ok(());
}
let mut args = vec![
"start".into(),
"--shell-pid".into(),
self.shell_pid.to_string(),
];
for id in to_start {
args.push(id);
}
cmd(&*env::PITCHFORK_BIN, args).run().into_diagnostic()?;
Ok(())
}
}
35 changes: 33 additions & 2 deletions src/cli/config/add.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
use crate::pitchfork_toml::{PitchforkToml, PitchforkTomlAuto, PitchforkTomlDaemon};
use crate::Result;

/// Add a new daemon to pitchfork.toml
/// Add a new daemon to ./pitchfork.toml
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "a", verbatim_doc_comment)]
pub struct Add {}
pub struct Add {
/// ID of the daemon to add
pub id: String,
/// Arguments to pass to the daemon
#[clap(allow_hyphen_values = true, trailing_var_arg = true)]
pub args: Vec<String>,
/// Autostart the daemon when entering the directory
#[clap(long)]
pub autostart: bool,
/// Autostop the daemon when leaving the directory
#[clap(long)]
pub autostop: bool,
}

impl Add {
pub async fn run(&self) -> Result<()> {
let mut pt = PitchforkToml::read("pitchfork.toml").unwrap_or_default();
pt.path = pt.path.or(Some("pitchfork.toml".into()));
let mut auto = vec![];
if self.autostart {
auto.push(PitchforkTomlAuto::Start);
}
if self.autostop {
auto.push(PitchforkTomlAuto::Stop);
}
pt.daemons.insert(
self.id.clone(),
PitchforkTomlDaemon {
run: shell_words::join(&self.args),
auto,
},
);
pt.write()?;
println!("added {} to {}", self.id, pt.path.unwrap().display());
Ok(())
}
}
17 changes: 16 additions & 1 deletion src/cli/config/remove.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
use crate::pitchfork_toml::PitchforkToml;
use crate::Result;

/// Remove a daemon from pitchfork.toml
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "rm", verbatim_doc_comment)]
pub struct Remove {}
pub struct Remove {
/// The ID of the daemon to remove
id: String,
}

impl Remove {
pub async fn run(&self) -> Result<()> {
if let Some(path) = PitchforkToml::list_paths().first() {
let mut pt = PitchforkToml::read(path)?;
if pt.daemons.shift_remove(&self.id).is_some() {
pt.write()?;
println!("removed {} from {}", self.id, path.display());
} else {
warn!("{} not found in {}", self.id, path.display());
}
} else {
warn!("No pitchfork.toml files found");
}
Ok(())
}
}
Loading

0 comments on commit d797c6c

Please sign in to comment.