Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 10, 2024
1 parent 80fe91a commit b569cd3
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 24 deletions.
99 changes: 82 additions & 17 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 @@ -17,6 +17,7 @@ path = "src/main.rs"
chrono = "0.4"
clap = { version = "4", features = ["derive"] }
clap_usage = "1"
cli-table = "0.4.9"
console = "0.15"
dirs = "5"
duct = "0.13"
Expand Down
17 changes: 15 additions & 2 deletions docs/cli/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,23 @@
"full_cmd": [
"list"
],
"usage": "list",
"usage": "list [--hide-header]",
"subcommands": {},
"args": [],
"flags": [],
"flags": [
{
"name": "hide-header",
"usage": "--hide-header",
"help": "Show header",
"help_first_line": "Show header",
"short": [],
"long": [
"hide-header"
],
"hide": false,
"global": false
}
],
"mounts": [],
"hide": false,
"help": "List all daemons",
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- [`pitchfork completion <SHELL>`](/cli/completion.md)
- [`pitchfork disable`](/cli/disable.md)
- [`pitchfork enable`](/cli/enable.md)
- [`pitchfork list`](/cli/list.md)
- [`pitchfork list [--hide-header]`](/cli/list.md)
- [`pitchfork logs [-n <N>] [-t --tail] [NAME]...`](/cli/logs.md)
- [`pitchfork run [-f --force] <NAME> [CMD]...`](/cli/run.md)
- [`pitchfork start [NAME]...`](/cli/start.md)
Expand Down
8 changes: 7 additions & 1 deletion docs/cli/list.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# `pitchfork list`

- **Usage**: `pitchfork list`
- **Usage**: `pitchfork list [--hide-header]`
- **Aliases**: `ls`

List all daemons

## Flags

### `--hide-header`

Show header
1 change: 1 addition & 0 deletions pitchfork.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cmd "enable" help="Allow a daemon to start" {
}
cmd "list" help="List all daemons" {
alias "ls"
flag "--hide-header" help="Show header"
}
cmd "logs" help="Displays logs for daemon(s)" {
alias "l"
Expand Down
31 changes: 29 additions & 2 deletions src/cli/list.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
use crate::Result;
use crate::state_file::StateFile;
use crate::{env, Result};
use cli_table::format::Separator;
use cli_table::{
format::{Border, Justify},
print_stdout, Cell, Table,
};

/// List all daemons
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "ls", verbatim_doc_comment)]
pub struct List {}
pub struct List {
/// Show header
#[clap(long)]
hide_header: bool,
}

impl List {
pub async fn run(&self) -> Result<()> {
let sf = StateFile::read(&*env::PITCHFORK_STATE_FILE)?;
let mut table = vec![];
for (name, daemon) in sf.daemons.iter() {
table.push(vec![
name.cell(),
daemon.pid.cell(),
daemon.status.style().cell().justify(Justify::Right),
]);
}
let mut table = table
.table()
.separator(Separator::builder().build())
.border(Border::builder().build());
if !self.hide_header || !console::user_attended() {
table = table.title(vec!["Name".cell(), "PID".cell(), "Status".cell()])
}
assert!(print_stdout(table).is_ok());
Ok(())
}
}
14 changes: 13 additions & 1 deletion src/state_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@ pub struct StateFileDaemon {
pub status: DaemonStatus,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, strum::Display)]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum DaemonStatus {
Failed(String),
Waiting,
Running,
}

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(),
}
}
}

impl StateFile {
pub fn new(path: PathBuf) -> Self {
Self {
Expand Down

0 comments on commit b569cd3

Please sign in to comment.