Skip to content

Commit

Permalink
add: Use new shared.alias_file config
Browse files Browse the repository at this point in the history
  • Loading branch information
Nukesor committed Aug 16, 2022
1 parent 63e4bda commit 339f333
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Added

- Show a hint when calling `pueue log` if the task output has been truncated. [#318](https://github.com/Nukesor/pueue/issues/318)
- Add `Settings.shared.alias_file`, which allows to set the location of the `pueue_aliases.yml` file.

### Fixed

Expand Down
4 changes: 4 additions & 0 deletions daemon/network/message_handler/add.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crossbeam_channel::Sender;

use pueue_lib::aliasing::insert_alias;
use pueue_lib::network::message::*;
use pueue_lib::state::{GroupStatus, SharedState};
use pueue_lib::task::{Task, TaskStatus};
Expand Down Expand Up @@ -52,6 +53,9 @@ pub fn add_task(
message.dependencies,
message.label,
);
// Insert the client alias if we applicable.
task.command = insert_alias(settings, task.original_command.clone());

// Sort and deduplicate dependency id.
task.dependencies.sort_unstable();
task.dependencies.dedup();
Expand Down
2 changes: 1 addition & 1 deletion daemon/network/message_handler/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn edit(message: EditMessage, state: &SharedState, settings: &Settings) -> M

task.status = task.prev_status.clone();
task.original_command = message.command.clone();
task.command = insert_alias(message.command.clone());
task.command = insert_alias(settings, message.command.clone());
task.path = message.path.clone();
ok_or_return_failure_message!(save_state(&state, settings));

Expand Down
7 changes: 4 additions & 3 deletions daemon/network/message_handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crossbeam_channel::Sender;
use pueue_lib::settings::Settings;
use std::fmt::Display;

use crossbeam_channel::Sender;

use pueue_lib::network::message::*;
use pueue_lib::settings::Settings;
use pueue_lib::state::SharedState;

use crate::network::response_helper::*;
Expand Down Expand Up @@ -45,7 +46,7 @@ pub fn handle_message(
Message::Pause(message) => pause::pause(message, sender, state),
Message::Remove(task_ids) => remove::remove(task_ids, state, settings),
Message::Reset(message) => reset(message, sender),
Message::Restart(message) => restart::restart_multiple(message, sender, state),
Message::Restart(message) => restart::restart_multiple(message, sender, state, settings),
Message::Send(message) => send::send(message, sender, state),
Message::Start(message) => start::start(message, sender, state),
Message::Stash(task_ids) => stash::stash(task_ids, state),
Expand Down
13 changes: 10 additions & 3 deletions daemon/network/message_handler/restart.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crossbeam_channel::Sender;
use pueue_lib::settings::Settings;
use std::sync::MutexGuard;

use pueue_lib::aliasing::insert_alias;
Expand All @@ -16,6 +17,7 @@ pub fn restart_multiple(
message: RestartMessage,
sender: &Sender<Message>,
state: &SharedState,
settings: &Settings,
) -> Message {
let task_ids: Vec<usize> = message.tasks.iter().map(|task| task.task_id).collect();
let mut state = state.lock().unwrap();
Expand All @@ -31,7 +33,7 @@ pub fn restart_multiple(

// Actually restart all tasks
for task in message.tasks.iter() {
restart(&mut state, task, message.stashed);
restart(&mut state, task, message.stashed, settings);
}

// Tell the task manager to start the task immediately if requested.
Expand All @@ -52,7 +54,12 @@ pub fn restart_multiple(
///
/// The "not in-place" restart functionality is actually just a copy the finished task + create a
/// new task, which is completely handled on the client-side.
fn restart(state: &mut MutexGuard<State>, to_restart: &TasksToRestart, stashed: bool) {
fn restart(
state: &mut MutexGuard<State>,
to_restart: &TasksToRestart,
stashed: bool,
settings: &Settings,
) {
// Check if we actually know this task.
let task = if let Some(task) = state.tasks.get_mut(&to_restart.task_id) {
task
Expand All @@ -74,7 +81,7 @@ fn restart(state: &mut MutexGuard<State>, to_restart: &TasksToRestart, stashed:

// Update command and path.
task.original_command = to_restart.command.clone();
task.command = insert_alias(to_restart.command.clone());
task.command = insert_alias(settings, to_restart.command.clone());
task.path = to_restart.path.clone();

// Reset all variables of any previous run.
Expand Down
37 changes: 14 additions & 23 deletions lib/src/aliasing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@ use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;

use log::{info, warn};
use log::info;

use crate::{error::Error, settings::configuration_directories};
use crate::error::Error;
use crate::settings::Settings;

/// Return the contents of the alias file, if it exists and can be parsed. \
/// The file has to be located in `pueue_directory` and named `pueue_aliases.yml`.
pub fn get_aliases() -> Result<HashMap<String, String>, Error> {
pub fn get_aliases(settings: &Settings) -> Result<HashMap<String, String>, Error> {
// Go through all config directories and check for a alias file.
let mut alias_file_path = None;
for directory in configuration_directories() {
let path = directory.join("pueue_aliases.yml");
if path.exists() {
alias_file_path = Some(path);
}
}
let path = settings.shared.alias_file();

// Return early if we cannot find the file
let path = match alias_file_path {
None => {
info!("Didn't find pueue alias file.");
return Ok(HashMap::new());
}
Some(alias_file_path) => alias_file_path,
if !path.exists() {
info!("Didn't find pueue alias file at {path:?}.");
return Ok(HashMap::new());
};

// Read the file content
Expand All @@ -42,24 +34,23 @@ pub fn get_aliases() -> Result<HashMap<String, String>, Error> {

/// Check if there exists an alias for a given command.
/// Only the first word will be replaced.
pub fn insert_alias(command: String) -> String {
pub fn insert_alias(settings: &Settings, command: String) -> String {
// Get the first word of the command.
let first = match command.split_whitespace().next() {
Some(first) => first,
None => return command,
};

let aliases = match get_aliases() {
let aliases = match get_aliases(settings) {
Err(err) => {
warn!("Failed to open aliases file: {err}");
info!("Couldn't read aliases file: {err}");
return command;
}
Ok(aliases) => aliases,
};

for (original, alias) in aliases.iter() {
if original == first {
return command.replacen(original, alias, 1);
}
if let Some(alias) = aliases.get(first) {
return command.replacen(first, alias, 1);
}

command
Expand Down
8 changes: 3 additions & 5 deletions lib/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use chrono::prelude::*;
use serde_derive::{Deserialize, Serialize};
use strum_macros::Display;

use crate::{aliasing::insert_alias, state::PUEUE_DEFAULT_GROUP};
use crate::state::PUEUE_DEFAULT_GROUP;

/// This enum represents the status of the internal task handling of Pueue.
/// They basically represent the internal task life-cycle.
Expand Down Expand Up @@ -77,12 +77,10 @@ impl Task {
dependencies: Vec<usize>,
label: Option<String>,
) -> Task {
let command = insert_alias(original_command.clone());

Task {
id: 0,
original_command,
command,
original_command: original_command.clone(),
command: original_command,
path,
envs,
group,
Expand Down

0 comments on commit 339f333

Please sign in to comment.