Skip to content

Commit

Permalink
Merge pull request #431 from Nukesor/group-json
Browse files Browse the repository at this point in the history
Group json
  • Loading branch information
Nukesor authored May 3, 2023
2 parents 22d3a8c + 0e4947b commit a7a0ff7
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 23 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.1.3] - unreleased
## [3.2.0] - unreleased

...
### Fix

- Fix broken bash autocompletion. Temporarily changes the name in the help texts to `pueue` and `pueued` [#426](https://github.com/Nukesor/pueue/issues/426)

### Added

- Add the `-j/--json` flag to `pueue group` to get a machine readable list of all current groups. [#430](https://github.com/Nukesor/pueue/issues/430)
- Add `pueued.plist` template to run pueue with launchd on MacOS. [#429](https://github.com/Nukesor/pueue/issues/429)

## [3.1.2] - 2023-02-26

Expand Down
6 changes: 5 additions & 1 deletion pueue/src/client/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ pub enum SubCommand {
/// Use this to add or remove groups.
/// By default, this will simply display all known groups.
Group {
/// Print the list of groups as json.
#[arg(short, long)]
json: bool,

#[command(subcommand)]
cmd: Option<GroupCommand>,
},
Expand Down Expand Up @@ -456,7 +460,7 @@ pub enum Shell {

#[derive(Parser, Debug)]
#[command(
name = "Pueue client",
name = "pueue",
about = "Interact with the Pueue daemon",
author,
version
Expand Down
5 changes: 3 additions & 2 deletions pueue/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl Client {
match subcommand {
SubCommand::Status { json, .. } => !json,
SubCommand::Log { json, .. } => !json,
SubCommand::Group { json, .. } => !json,
_ => true,
}
} else {
Expand Down Expand Up @@ -323,7 +324,7 @@ impl Client {
print_logs(task_logs, &self.subcommand, &self.style, &self.settings)
}
Message::GroupResponse(groups) => {
let group_text = format_groups(groups, &self.style);
let group_text = format_groups(groups, &self.subcommand, &self.style);
println!("{group_text}");
}
Message::Stream(text) => {
Expand Down Expand Up @@ -488,7 +489,7 @@ impl Client {
input: input.clone(),
}
.into(),
SubCommand::Group { cmd } => match cmd {
SubCommand::Group { cmd, .. } => match cmd {
Some(GroupCommand::Add { name, parallel }) => GroupMessage::Add {
name: name.to_owned(),
parallel_tasks: parallel.to_owned(),
Expand Down
17 changes: 16 additions & 1 deletion pueue/src/client/display/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@ use pueue_lib::{
state::{Group, GroupStatus},
};

use crate::client::cli::SubCommand;

use super::OutputStyle;

/// Print some info about the daemon's current groups.
/// This is used when calling `pueue group`.
pub fn format_groups(message: GroupResponseMessage, style: &OutputStyle) -> String {
pub fn format_groups(
message: GroupResponseMessage,
cli_command: &SubCommand,
style: &OutputStyle,
) -> String {
// Get commandline options to check whether we should return the groups as json.
let SubCommand::Group { json, .. } = cli_command else {
panic!("Got wrong Subcommand {cli_command:?} in format_groups. This shouldn't happen.")
};

if *json {
return serde_json::to_string(&message.groups).unwrap();
}

let mut text = String::new();
let mut group_iter = message.groups.iter().peekable();
while let Some((name, group)) = group_iter.next() {
Expand Down
19 changes: 9 additions & 10 deletions pueue/src/client/display/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,19 @@ pub fn print_logs(
) {
// Get actual commandline options.
// This is necessary to know how we should display/return the log information.
let (json, task_ids, lines, full) = match cli_command {
SubCommand::Log {
json,
task_ids,
lines,
full,
} => (*json, task_ids.clone(), *lines, *full),
_ => panic!("Got wrong Subcommand {cli_command:?} in print_log. This shouldn't happen"),
let SubCommand::Log {
json,
task_ids,
lines,
full,
} = cli_command else {
panic!("Got wrong Subcommand {cli_command:?} in print_log. This shouldn't happen");
};

let lines = determine_log_line_amount(full, &lines);
let lines = determine_log_line_amount(*full, lines);

// Return the server response in json representation.
if json {
if *json {
print_log_json(task_logs, settings, lines);
return;
}
Expand Down
1 change: 1 addition & 0 deletions pueue/src/client/query/filters.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(bindings_with_variant_name)]
use anyhow::{bail, Context, Result};
use chrono::{DateTime, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime};
use pest::iterators::Pair;
Expand Down
1 change: 1 addition & 0 deletions pueue/src/client/query/order_by.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(bindings_with_variant_name)]
use anyhow::Result;
use pest::iterators::Pair;

Expand Down
7 changes: 1 addition & 6 deletions pueue/src/daemon/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ use std::path::PathBuf;
use clap::{ArgAction, Parser, ValueHint};

#[derive(Parser, Debug)]
#[command(
name = "Pueue daemon",
about = "Start the Pueue daemon",
author,
version
)]
#[command(name = "pueued", about = "Start the Pueue daemon", author, version)]
pub struct CliArguments {
/// Verbose mode (-v, -vv, -vvv)
#[arg(short, long, action = ArgAction::Count)]
Expand Down
27 changes: 26 additions & 1 deletion pueue/tests/client/integration/group.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::BTreeMap;

use anyhow::{Context, Result};
use pueue_lib::network::message::*;
use pueue_lib::state::GroupStatus;
use pueue_lib::state::{Group, GroupStatus};

use crate::client::helper::*;

Expand Down Expand Up @@ -50,3 +52,26 @@ async fn colored() -> Result<()> {

Ok(())
}

/// Make sure that getting the list of groups as json works.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn json() -> Result<()> {
let daemon = daemon().await?;
let shared = &daemon.settings.shared;

// Get the group status output
let output = run_client_command(shared, &["group", "--json"])?;
let json = String::from_utf8_lossy(&output.stdout);
println!("{json}");

let state = get_state(shared).await?;
let deserialized_groups: BTreeMap<String, Group> =
serde_json::from_str(&json).context("Failed to deserialize json state")?;

assert_eq!(
deserialized_groups, state.groups,
"The serialized groups differ from the actual groups from the state."
);

Ok(())
}
23 changes: 23 additions & 0 deletions utils/pueued.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- This is the plist file for the pueue daemon on macos -->
<!-- Place pueued.plist in ~/Library/LaunchAgents -->
<!-- To enable the daemon navigate into the directory `cd ~/Library/LaunchAgents` and type `launchctl load pueued.plist` -->
<!-- To start the daemon type `launchctl start pueued` -->
<!-- If you want to check that the daemon is running type `launchctl list | grep pueued` -->
<!-- You have to change the program location, if pueue is not installed with homebrew -->

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>pueued</string>
<key>ProgramArguments</key>
<array>
<string>/opt/homebrew/bin/pueued</string>
<string>-vv</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

0 comments on commit a7a0ff7

Please sign in to comment.