forked from Spencer/chg-shell
adding command exec
This commit is contained in:
50
main.rs
50
main.rs
@@ -1,8 +1,40 @@
|
|||||||
|
use std::process::{Command, Output};
|
||||||
|
use std::io::Result as IoResult;
|
||||||
use inquire::{Text, validator::{Validation}};
|
use inquire::{Text, validator::{Validation}};
|
||||||
use inquire::{error::InquireError, Select};
|
use inquire::{error::InquireError, Select};
|
||||||
|
use inquire::ui::{RenderConfig, Styled};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
fn execute_command(command: &str) -> IoResult<String> {
|
||||||
|
// Use Command to run the bash shell with the -c option to pass the command string
|
||||||
|
let output: Output = Command::new("bash")
|
||||||
|
.arg("-c")
|
||||||
|
.arg(command)
|
||||||
|
.output()?;
|
||||||
|
|
||||||
|
if output.status.success() {
|
||||||
|
// Convert the stdout of the command to a String
|
||||||
|
Ok(String::from_utf8_lossy(&output.stdout).to_string())
|
||||||
|
} else {
|
||||||
|
// Handle errors by converting stderr to a String and returning an Err variant
|
||||||
|
let error_message = String::from_utf8_lossy(&output.stderr);
|
||||||
|
Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Other,
|
||||||
|
format!("Command failed with error: {}", error_message),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let prompt_gen_command = "echo -n \"[${USER}@${HOSTNAME} ${PWD##*/}]$\"";
|
||||||
|
let prompt_gen = match execute_command(&prompt_gen_command) {
|
||||||
|
Ok(output) => output,
|
||||||
|
Err(_) => return
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut empty_render_config: RenderConfig = RenderConfig::default();
|
||||||
|
empty_render_config = empty_render_config.with_prompt_prefix(Styled::new(""));
|
||||||
|
|
||||||
let re = Regex::new(r"CHG[0-9]{6}$").unwrap();
|
let re = Regex::new(r"CHG[0-9]{6}$").unwrap();
|
||||||
let options: Vec<&str> = vec!["File change", "Package Update", "Package Removal",
|
let options: Vec<&str> = vec!["File change", "Package Update", "Package Removal",
|
||||||
"File Removal", "Misc. Command"];
|
"File Removal", "Misc. Command"];
|
||||||
@@ -23,16 +55,28 @@ fn main() {
|
|||||||
Ok(Validation::Valid)
|
Ok(Validation::Valid)
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = Text::new("system prompt $ ")
|
let prompt_command = match Text::new(&prompt_gen)
|
||||||
.with_validator(validator)
|
.with_validator(validator)
|
||||||
.prompt();
|
.with_render_config(empty_render_config)
|
||||||
|
.prompt() {
|
||||||
|
Ok(input) => input,
|
||||||
|
Err(_) => {
|
||||||
|
eprintln!("Failed to get system input.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let _ = Text::new("(e.g. CHGXXXXXX) Enter change request: ").with_validator(change_validator).prompt();
|
let _ = Text::new("(e.g. CHGXXXXXX) Enter change request: ").with_validator(change_validator).prompt();
|
||||||
|
|
||||||
let ans: Result<&str, InquireError> = Select::new("Change Type?", options).prompt();
|
let ans: Result<&str, InquireError> = Select::new("Change Type?", options).prompt();
|
||||||
|
|
||||||
match ans {
|
match ans {
|
||||||
Ok(_) => println!("running command..."),
|
Ok(_) => {
|
||||||
|
match execute_command(&prompt_command) {
|
||||||
|
Ok(output) => print!("{}", output),
|
||||||
|
Err(e) => eprintln!("Command failed: {}", e),
|
||||||
|
}
|
||||||
|
},
|
||||||
Err(_) => println!("not running command..."),
|
Err(_) => println!("not running command..."),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user