mirror of
https://codeberg.org/iff/pay-respects.git
synced 2025-10-05 16:12:54 +02:00
feat: shell eval method
This commit is contained in:
10
config.md
10
config.md
@@ -11,14 +11,18 @@ All available options are listed in the following example file:
|
||||
```toml
|
||||
# maximum time in milliseconds for getting previous output
|
||||
timeout = 3000
|
||||
# your preferred command for privileges
|
||||
sudo = "run0"
|
||||
|
||||
# how suggestions are evaluated after being confirmed
|
||||
# options can be:
|
||||
# - Internal: commands are evaluated inside `pay-respects`
|
||||
# - Shell: current working shell is responsible for execution
|
||||
eval_method = "Internal"
|
||||
|
||||
[package_manager]
|
||||
# preferred package manager
|
||||
package_manager = "pacman"
|
||||
|
||||
# preferred installation method, can be limited with the package manager
|
||||
# preferred installation method, can be limited by the package manager
|
||||
# available options are:
|
||||
# - System
|
||||
# - Shell (nix and guix only)
|
||||
|
@@ -7,6 +7,8 @@ pub struct Config {
|
||||
#[serde(default)]
|
||||
pub timeout: Timeout,
|
||||
#[serde(default)]
|
||||
pub eval_method: EvalMethod,
|
||||
#[serde(default)]
|
||||
pub package_manager: PackageManagerConfig,
|
||||
}
|
||||
|
||||
@@ -36,6 +38,13 @@ pub enum InstallMethod {
|
||||
Shell,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default, PartialEq)]
|
||||
pub enum EvalMethod {
|
||||
#[default]
|
||||
Internal,
|
||||
Shell,
|
||||
}
|
||||
|
||||
pub fn load_config() -> Config {
|
||||
let path = config_path();
|
||||
let exists = std::path::Path::new(&path).exists();
|
||||
|
@@ -408,6 +408,19 @@ pub fn shell_syntax(shell: &str, command: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_privilege(shell: &str, privilege: &str, command: &str) -> String {
|
||||
if command.contains("&&") || command.contains("||") || command.contains('>') {
|
||||
format!(
|
||||
"{} {} -c \"{}\"",
|
||||
privilege,
|
||||
shell,
|
||||
command.replace("\"", "\\\"")
|
||||
)
|
||||
} else {
|
||||
format!("{} {}", privilege, command)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shell_evaluated_commands(shell: &str, command: &str, success: bool) {
|
||||
let lines = command
|
||||
.lines()
|
||||
|
@@ -7,9 +7,12 @@ use colored::Colorize;
|
||||
use inquire::*;
|
||||
use ui::Color;
|
||||
|
||||
use crate::config;
|
||||
use crate::data::Data;
|
||||
use crate::rules::match_pattern;
|
||||
use crate::shell::{add_candidates_no_dup, module_output, shell_evaluated_commands, shell_syntax};
|
||||
use crate::shell::{
|
||||
add_candidates_no_dup, add_privilege, module_output, shell_evaluated_commands, shell_syntax,
|
||||
};
|
||||
use crate::style::highlight_difference;
|
||||
|
||||
pub fn suggest_candidates(data: &mut Data) {
|
||||
@@ -143,6 +146,12 @@ pub fn confirm_suggestion(data: &Data) -> Result<(), String> {
|
||||
#[cfg(debug_assertions)]
|
||||
eprintln!("running command: {command}");
|
||||
|
||||
let eval_method = &data.config.eval_method;
|
||||
if eval_method == &config::EvalMethod::Shell {
|
||||
shell_suggestion(data, command);
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let now = Instant::now();
|
||||
let process = run_suggestion(data, command);
|
||||
|
||||
@@ -185,6 +194,23 @@ pub fn run_suggestion(data: &Data, command: &str) -> std::process::ExitStatus {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shell_suggestion(data: &Data, command: &str) {
|
||||
let shell = &data.shell;
|
||||
let privilege = &data.privilege;
|
||||
let command = if let Some(env) = &data.env {
|
||||
format!("{env} {command}")
|
||||
} else {
|
||||
command.to_string()
|
||||
};
|
||||
|
||||
let command = if let Some(privilege) = privilege {
|
||||
add_privilege(shell, privilege, &command)
|
||||
} else {
|
||||
command
|
||||
};
|
||||
println!("{}", command);
|
||||
}
|
||||
|
||||
fn suggestion_err(data: &Data, command: &str) -> Result<(), String> {
|
||||
let shell = &data.shell;
|
||||
let privilege = &data.privilege;
|
||||
|
Reference in New Issue
Block a user