Skip to content

Commit

Permalink
change: Move reference client implementation into pueue_lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Nukesor committed Feb 13, 2025
1 parent c5abcca commit b190503
Show file tree
Hide file tree
Showing 32 changed files with 249 additions and 162 deletions.
2 changes: 1 addition & 1 deletion pueue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ handlebars.workspace = true
interim = { version = "0.1.2", features = ["chrono"] }
pest = "2.7"
pest_derive = "2.7"
pueue-lib = { version = "0.28.0", path = "../pueue_lib" }
pueue-lib = { version = "0.28.0", path = "../pueue_lib", features = ["client"] }
serde.workspace = true
serde_json.workspace = true
shell-escape = "0.1"
Expand Down
32 changes: 23 additions & 9 deletions pueue/src/bin/pueue.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::PathBuf;
use std::{
io::{stdout, IsTerminal},
path::PathBuf,
};

use clap::{CommandFactory, Parser};
use clap_complete::{generate, generate_to, shells};
Expand All @@ -7,20 +10,22 @@ use color_eyre::{
Result,
};
use pueue::client::{
cli::{CliArguments, Shell, SubCommand},
client::Client,
cli::{CliArguments, ColorChoice, Shell, SubCommand},
handle_command,
style::OutputStyle,
};
use pueue_lib::settings::Settings;
use pueue_lib::{client::Client, settings::Settings};

/// This is the main entry point of the client.
///
/// At first we do some basic setup:
/// The following happens in here:
/// - Parse the cli
/// - Initialize logging
/// - Read the config
///
/// Once all this is done, we init the [Client] struct and start the main loop via [Client::start].
/// - Default to `status` subcommand if no subcommand was specified
/// - Determine the current
/// - Initialize the [`Client`]
/// - Handle the command
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
// Parse commandline options.
Expand Down Expand Up @@ -62,6 +67,15 @@ async fn main() -> Result<()> {
query: Vec::new(),
});

// Determine whether we should color/style our output or not.
// The user can explicitly disable/enable this, otherwise we check whether we are on a TTY.
let style_enabled = match opt.color {
ColorChoice::Auto => stdout().is_terminal(),
ColorChoice::Always => true,
ColorChoice::Never => false,
};
let style = OutputStyle::new(&settings, style_enabled);

// Only show version incompatibility warnings if we aren't supposed to output json.
let show_version_warning = match subcommand {
SubCommand::Status { json, .. } => !json,
Expand All @@ -71,11 +85,11 @@ async fn main() -> Result<()> {
};

// Create client to talk with the daemon and connect.
let mut client = Client::new(settings, show_version_warning, &opt.color)
let mut client = Client::new(settings, show_version_warning)
.await
.context("Failed to initialize client.")?;

handle_command(&mut client, subcommand).await?;
handle_command(&mut client, &style, subcommand).await?;

Ok(())
}
Expand Down
8 changes: 5 additions & 3 deletions pueue/src/client/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ use std::{

use chrono::{DateTime, Local};
use pueue_lib::{
client::Client,
format::format_datetime,
network::message::{AddMessage, AddedTaskMessage},
Request, Response,
};

use super::{follow as follow_cmd, group_or_default, handle_response};
use crate::{client::client::Client, internal_prelude::*};
use crate::{client::style::OutputStyle, internal_prelude::*};

#[allow(clippy::too_many_arguments)]
pub async fn add_task(
client: &mut Client,
style: &OutputStyle,
mut command: Vec<String>,
working_directory: Option<PathBuf>,
escape: bool,
Expand Down Expand Up @@ -74,7 +76,7 @@ pub async fn add_task(
group_is_paused,
}) = response
else {
handle_response(&client.style, response)?;
handle_response(style, response)?;

Check warning on line 79 in pueue/src/client/commands/add.rs

View check run for this annotation

Codecov / codecov/patch

pueue/src/client/commands/add.rs#L79

Added line #L79 was not covered by tests
return Ok(());
};

Expand Down Expand Up @@ -102,7 +104,7 @@ pub async fn add_task(
}

if follow {
follow_cmd(client, Some(task_id), None).await?;
follow_cmd(client, style, Some(task_id), None).await?;
}

Ok(())
Expand Down
7 changes: 4 additions & 3 deletions pueue/src/client/commands/clean.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use pueue_lib::network::message::*;
use pueue_lib::{client::Client, network::message::*};

use super::handle_response;
use crate::{client::client::Client, internal_prelude::*};
use crate::{client::style::OutputStyle, internal_prelude::*};

/// Tell the daemon to clear finished tasks for a specific group or the whole daemon.
///
/// The `successful_only` determines whether finished tasks should be removed or not.
pub async fn clean(
client: &mut Client,
style: &OutputStyle,

Check warning on line 11 in pueue/src/client/commands/clean.rs

View check run for this annotation

Codecov / codecov/patch

pueue/src/client/commands/clean.rs#L11

Added line #L11 was not covered by tests
group: Option<String>,
successful_only: bool,
) -> Result<()> {
Expand All @@ -20,5 +21,5 @@ pub async fn clean(

let response = client.receive_response().await?;

handle_response(&client.style, response)
handle_response(style, response)

Check warning on line 24 in pueue/src/client/commands/clean.rs

View check run for this annotation

Codecov / codecov/patch

pueue/src/client/commands/clean.rs#L24

Added line #L24 was not covered by tests
}
12 changes: 7 additions & 5 deletions pueue/src/client/commands/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use std::{
path::{Path, PathBuf},
};

use pueue_lib::{error::Error, network::message::*, settings::Settings};
use pueue_lib::{client::Client, error::Error, network::message::*, settings::Settings};
use tempfile::tempdir;

use super::handle_response;
use crate::{client::client::Client, internal_prelude::*, process_helper::compile_shell_command};
use crate::{
client::style::OutputStyle, internal_prelude::*, process_helper::compile_shell_command,
};

/// This function handles the logic for editing tasks.
/// At first, we request the daemon to send us the task to edit.
Expand All @@ -19,7 +21,7 @@ use crate::{client::client::Client, internal_prelude::*, process_helper::compile
///
/// After receiving the task information, the user can then edit it in their editor.
/// Upon exiting the text editor, the line will then be read and sent to the server
pub async fn edit(client: &mut Client, task_ids: Vec<usize>) -> Result<()> {
pub async fn edit(client: &mut Client, style: &OutputStyle, task_ids: Vec<usize>) -> Result<()> {
// Request the data to edit from the server and issue a task-lock while doing so.
let init_message = Request::EditRequest(task_ids);
client.send_request(init_message).await?;
Expand All @@ -29,7 +31,7 @@ pub async fn edit(client: &mut Client, task_ids: Vec<usize>) -> Result<()> {
// In case we don't receive an EditResponse, something went wrong.
// Handle the response and return.
let Response::Edit(editable_tasks) = init_response else {
handle_response(&client.style, init_response)?;
handle_response(style, init_response)?;

Check warning on line 34 in pueue/src/client/commands/edit.rs

View check run for this annotation

Codecov / codecov/patch

pueue/src/client/commands/edit.rs#L34

Added line #L34 was not covered by tests
return Ok(());
};

Expand Down Expand Up @@ -65,7 +67,7 @@ pub async fn edit(client: &mut Client, task_ids: Vec<usize>) -> Result<()> {
client.send_request(Request::Edit(editable_tasks)).await?;

let response = client.receive_response().await?;
handle_response(&client.style, response)?;
handle_response(style, response)?;

Ok(())
}
Expand Down
7 changes: 4 additions & 3 deletions pueue/src/client/commands/enqueue.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use chrono::{DateTime, Local};
use pueue_lib::network::message::*;
use pueue_lib::{client::Client, network::message::*};

use super::{handle_response, selection_from_params};
use crate::{client::client::Client, internal_prelude::*};
use crate::{client::style::OutputStyle, internal_prelude::*};

/// Enqueue a stashed task or schedule it to be enqueued at a specific point in time.
pub async fn enqueue(
client: &mut Client,
style: &OutputStyle,
task_ids: Vec<usize>,
group: Option<String>,
all: bool,
Expand All @@ -21,5 +22,5 @@ pub async fn enqueue(

let response = client.receive_response().await?;

handle_response(&client.style, response)
handle_response(style, response)
}
8 changes: 4 additions & 4 deletions pueue/src/client/commands/env.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use pueue_lib::network::message::EnvMessage;
use pueue_lib::{client::Client, network::message::EnvMessage};

use super::handle_response;
use crate::{
client::{cli::EnvCommand, client::Client},
client::{cli::EnvCommand, style::OutputStyle},
internal_prelude::*,
};

/// Set or unset an environment variable on a task.
pub async fn env(client: &mut Client, cmd: EnvCommand) -> Result<()> {
pub async fn env(client: &mut Client, style: &OutputStyle, cmd: EnvCommand) -> Result<()> {
let request = match cmd {
EnvCommand::Set {
task_id,
Expand All @@ -25,5 +25,5 @@ pub async fn env(client: &mut Client, cmd: EnvCommand) -> Result<()> {

let response = client.receive_response().await?;

handle_response(&client.style, response)
handle_response(style, response)
}
9 changes: 6 additions & 3 deletions pueue/src/client/commands/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
};

use pueue_lib::{
client::Client,
log::{get_log_file_handle, get_log_path, seek_to_last_lines},
network::message::StreamRequestMessage,
Response,
Expand All @@ -12,9 +13,9 @@ use tokio::time::sleep;

use crate::{
client::{
client::Client,
commands::{get_state, get_task},
display_helper::print_error,
style::OutputStyle,
},
internal_prelude::*,
};
Expand All @@ -25,6 +26,7 @@ use crate::{
/// daemon in case they're somewhere inaccessible or on a remote machine.
pub async fn follow(
client: &mut Client,
style: &OutputStyle,
task_id: Option<usize>,
lines: Option<usize>,
) -> Result<()> {
Expand All @@ -36,7 +38,7 @@ pub async fn follow(
return Ok(());
}

remote_follow(client, task_id, lines).await
remote_follow(client, style, task_id, lines).await
}

/// Request the daemon to stream log files for some tasks.
Expand All @@ -45,6 +47,7 @@ pub async fn follow(
/// once the task finishes.
pub async fn remote_follow(
client: &mut Client,
style: &OutputStyle,
task_id: Option<usize>,
lines: Option<usize>,
) -> Result<()> {
Expand All @@ -64,7 +67,7 @@ pub async fn remote_follow(
}
Response::Close => break,
Response::Failure(text) => {
print_error(&client.style, &text);
print_error(style, &text);
std::process::exit(1);
}
_ => error!("Received unhandled response message: {response:?}"),
Expand Down
17 changes: 10 additions & 7 deletions pueue/src/client/commands/group.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use pueue_lib::network::message::*;
use pueue_lib::{client::Client, network::message::*};

use super::handle_response;
use crate::{
client::{
cli::GroupCommand, client::Client, display_helper::get_group_headline, style::OutputStyle,
},
client::{cli::GroupCommand, display_helper::get_group_headline, style::OutputStyle},
internal_prelude::*,
};

/// Add, remove a group or simply list all groups.
pub async fn group(client: &mut Client, cmd: Option<GroupCommand>, json: bool) -> Result<()> {
pub async fn group(
client: &mut Client,
style: &OutputStyle,
cmd: Option<GroupCommand>,
json: bool,
) -> Result<()> {
let request = match cmd {
Some(GroupCommand::Add { name, parallel }) => GroupMessage::Add {
name: name.to_owned(),
Expand All @@ -24,12 +27,12 @@ pub async fn group(client: &mut Client, cmd: Option<GroupCommand>, json: bool) -
let response = client.receive_response().await?;

if let Response::Group(groups) = response {
let group_text = format_groups(groups, &client.style, json);
let group_text = format_groups(groups, style, json);
println!("{group_text}");
return Ok(());
}

handle_response(&client.style, response)
handle_response(style, response)
}

/// Print some info about the daemon's current groups.
Expand Down
7 changes: 4 additions & 3 deletions pueue/src/client/commands/kill.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use pueue_lib::network::message::*;
use pueue_lib::{client::Client, network::message::*};

use super::{handle_response, handle_user_confirmation, selection_from_params};
use crate::{client::client::Client, internal_prelude::*};
use crate::{client::style::OutputStyle, internal_prelude::*};

/// Kill some running or paused task.
///
/// Can also be used to send a specific [`Signal`].
pub async fn kill(
client: &mut Client,
style: &OutputStyle,

Check warning on line 11 in pueue/src/client/commands/kill.rs

View check run for this annotation

Codecov / codecov/patch

pueue/src/client/commands/kill.rs#L11

Added line #L11 was not covered by tests
task_ids: Vec<usize>,
group: Option<String>,
all: bool,
Expand All @@ -26,5 +27,5 @@ pub async fn kill(

let response = client.receive_response().await?;

handle_response(&client.style, response)
handle_response(style, response)

Check warning on line 30 in pueue/src/client/commands/kill.rs

View check run for this annotation

Codecov / codecov/patch

pueue/src/client/commands/kill.rs#L30

Added line #L30 was not covered by tests
}
9 changes: 6 additions & 3 deletions pueue/src/client/commands/log/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use comfy_table::{Attribute as ComfyAttribute, Cell, CellAlignment, Table};
use crossterm::style::Color;
use pueue_lib::{
client::Client,
network::message::{TaskLogMessage, TaskSelection, *},
settings::Settings,
task::{Task, TaskResult, TaskStatus},
};

use super::{handle_response, selection_from_params, OutputStyle};
use crate::{client::client::Client, internal_prelude::*};
use crate::internal_prelude::*;

mod json;
mod local;
Expand All @@ -19,8 +20,10 @@ use remote::*;

/// Print the log output of finished tasks.
/// This may be selected tasks, all tasks of a group or **all** tasks.
#[allow(clippy::too_many_arguments)]
pub async fn print_logs(
client: &mut Client,
style: &OutputStyle,
task_ids: Vec<usize>,
group: Option<String>,
all: bool,
Expand All @@ -42,7 +45,7 @@ pub async fn print_logs(
let response = client.receive_response().await?;

let Response::Log(task_logs) = response else {
handle_response(&client.style, response)?;
handle_response(style, response)?;

Check warning on line 48 in pueue/src/client/commands/log/mod.rs

View check run for this annotation

Codecov / codecov/patch

pueue/src/client/commands/log/mod.rs#L48

Added line #L48 was not covered by tests
return Ok(());
};

Expand Down Expand Up @@ -72,7 +75,7 @@ pub async fn print_logs(
// Iterate over each task and print the respective log.
let mut task_iter = task_logs.iter().peekable();
while let Some((_, task_log)) = task_iter.next() {
print_log(task_log, &client.style, &client.settings, lines);
print_log(task_log, style, &client.settings, lines);

// Add a newline if there is another task that's going to be printed.
if let Some((_, task_log)) = task_iter.peek() {
Expand Down
Loading

0 comments on commit b190503

Please sign in to comment.