mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-11 22:10:09 +01:00
feat: shell eval method
This commit is contained in:
parent
9d90f7ce21
commit
a732a5f4c1
4 changed files with 56 additions and 4 deletions
10
config.md
10
config.md
|
|
@ -11,14 +11,18 @@ All available options are listed in the following example file:
|
||||||
```toml
|
```toml
|
||||||
# maximum time in milliseconds for getting previous output
|
# maximum time in milliseconds for getting previous output
|
||||||
timeout = 3000
|
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]
|
[package_manager]
|
||||||
# preferred package manager
|
# preferred package manager
|
||||||
package_manager = "pacman"
|
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:
|
# available options are:
|
||||||
# - System
|
# - System
|
||||||
# - Shell (nix and guix only)
|
# - Shell (nix and guix only)
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ pub struct Config {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub timeout: Timeout,
|
pub timeout: Timeout,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
pub eval_method: EvalMethod,
|
||||||
|
#[serde(default)]
|
||||||
pub package_manager: PackageManagerConfig,
|
pub package_manager: PackageManagerConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,6 +38,13 @@ pub enum InstallMethod {
|
||||||
Shell,
|
Shell,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Default, PartialEq)]
|
||||||
|
pub enum EvalMethod {
|
||||||
|
#[default]
|
||||||
|
Internal,
|
||||||
|
Shell,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_config() -> Config {
|
pub fn load_config() -> Config {
|
||||||
let path = config_path();
|
let path = config_path();
|
||||||
let exists = std::path::Path::new(&path).exists();
|
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) {
|
pub fn shell_evaluated_commands(shell: &str, command: &str, success: bool) {
|
||||||
let lines = command
|
let lines = command
|
||||||
.lines()
|
.lines()
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,12 @@ use colored::Colorize;
|
||||||
use inquire::*;
|
use inquire::*;
|
||||||
use ui::Color;
|
use ui::Color;
|
||||||
|
|
||||||
|
use crate::config;
|
||||||
use crate::data::Data;
|
use crate::data::Data;
|
||||||
use crate::rules::match_pattern;
|
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;
|
use crate::style::highlight_difference;
|
||||||
|
|
||||||
pub fn suggest_candidates(data: &mut Data) {
|
pub fn suggest_candidates(data: &mut Data) {
|
||||||
|
|
@ -143,6 +146,12 @@ pub fn confirm_suggestion(data: &Data) -> Result<(), String> {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
eprintln!("running command: {command}");
|
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 now = Instant::now();
|
||||||
let process = run_suggestion(data, command);
|
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> {
|
fn suggestion_err(data: &Data, command: &str) -> Result<(), String> {
|
||||||
let shell = &data.shell;
|
let shell = &data.shell;
|
||||||
let privilege = &data.privilege;
|
let privilege = &data.privilege;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue