adding command looping and bypass command
This commit is contained in:
16
Makefile
Normal file
16
Makefile
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
LIBRARIES = -lreadline
|
||||||
|
RELEASE_ARGS = -DRELEASEBUILD
|
||||||
|
SOURCES = ./msh.c
|
||||||
|
OUTPUT_DIR = ./bin
|
||||||
|
OUTPUT_BIN = ${OUTPUT_DIR}/PROG
|
||||||
|
OUTPUT = -o ${OUTPUT_BIN}
|
||||||
|
|
||||||
|
build:
|
||||||
|
RUSTFLAGS='-C target-feature=+crt-static' cargo build
|
||||||
|
|
||||||
|
debug:
|
||||||
|
cargo build
|
||||||
|
|
||||||
|
install:
|
||||||
|
mv ${OUTPUT_BIN:PROG=mash} /usr/sbin/blah
|
||||||
|
|
||||||
95
main.rs
95
main.rs
@@ -1,7 +1,8 @@
|
|||||||
|
use std::io::Write;
|
||||||
use std::process::{Command, Output};
|
use std::process::{Command, Output};
|
||||||
use std::io::Result as IoResult;
|
use std::io::Result as IoResult;
|
||||||
use inquire::{Text, validator::{Validation}};
|
use inquire::{Text, validator::Validation};
|
||||||
use inquire::{error::InquireError, Select};
|
use inquire::Select;
|
||||||
use inquire::ui::{RenderConfig, Styled};
|
use inquire::ui::{RenderConfig, Styled};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
@@ -25,15 +26,22 @@ fn execute_command(command: &str) -> IoResult<String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn prompt(name:&str) -> String {
|
||||||
|
let mut line = String::new();
|
||||||
|
print!("{}", name);
|
||||||
|
std::io::stdout().flush().unwrap();
|
||||||
|
std::io::stdin().read_line(&mut line).expect("Error: Could not read a line");
|
||||||
|
|
||||||
|
return line.trim().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let prompt_gen_command = "echo -n \"[${USER}@${HOSTNAME} ${PWD##*/}]$\"";
|
let prompt_gen_command = "echo -n \"r-[${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();
|
let mut empty_render_config: RenderConfig = RenderConfig::default();
|
||||||
empty_render_config = empty_render_config.with_prompt_prefix(Styled::new(""));
|
empty_render_config = empty_render_config.with_prompt_prefix(Styled::new(""));
|
||||||
|
empty_render_config = empty_render_config.with_answered_prompt_prefix(Styled::new(""));
|
||||||
|
empty_render_config = empty_render_config.with_canceled_prompt_indicator(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",
|
||||||
@@ -47,36 +55,57 @@ fn main() {
|
|||||||
Ok(Validation::Invalid("Invalid change request (e.g: CHGXXXXXX)".into()))
|
Ok(Validation::Invalid("Invalid change request (e.g: CHGXXXXXX)".into()))
|
||||||
};
|
};
|
||||||
|
|
||||||
let validator = |input: &str| if input.chars().count() > 140 {
|
let mut bypass_change = false;
|
||||||
Ok(Validation::Invalid("You're only allowed 140 characters.".into()))
|
loop {
|
||||||
} else if input.chars().count() == 0 {
|
let prompt_command = match execute_command(&prompt_gen_command) {
|
||||||
Ok(Validation::Invalid("".into()))
|
Ok(output) => {
|
||||||
} else {
|
let prompt_output = prompt(&output);
|
||||||
Ok(Validation::Valid)
|
if prompt_output.chars().count() == 0 {
|
||||||
};
|
continue;
|
||||||
|
} else if prompt_output.starts_with("runme ") || prompt_output == "runme" {
|
||||||
let prompt_command = match Text::new(&prompt_gen)
|
bypass_change = true;
|
||||||
.with_validator(validator)
|
if prompt_output.len() > 6 {
|
||||||
.with_render_config(empty_render_config)
|
match execute_command(prompt_output.split_at(6).1) {
|
||||||
.prompt() {
|
Ok(output) => print!("{}", output),
|
||||||
Ok(input) => input,
|
Err(e) => eprintln!("Command failed: {}", e),
|
||||||
Err(_) => {
|
}
|
||||||
eprintln!("Failed to get system input.");
|
}
|
||||||
return;
|
continue;
|
||||||
}
|
} else {
|
||||||
|
prompt_output
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => return
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = Text::new("(e.g. CHGXXXXXX) Enter change request: ").with_validator(change_validator).prompt();
|
if !bypass_change {
|
||||||
|
let change_request = match Text::new("(e.g. CHGXXXXXX) Enter change request: ")
|
||||||
|
.with_validator(change_validator.clone())
|
||||||
|
.prompt() {
|
||||||
|
Ok(input) => input,
|
||||||
|
Err(_) => return,
|
||||||
|
};
|
||||||
|
println!("Change request validated: {}", change_request);
|
||||||
|
|
||||||
let ans: Result<&str, InquireError> = Select::new("Change Type?", options).prompt();
|
let ans = Select::new("Change Type?", options.clone())
|
||||||
|
.with_render_config(empty_render_config.clone())
|
||||||
|
.prompt();
|
||||||
|
|
||||||
match ans {
|
match ans {
|
||||||
Ok(_) => {
|
Ok(selected_option) => {
|
||||||
match execute_command(&prompt_command) {
|
println!("Selected option: {}", selected_option);
|
||||||
Ok(output) => print!("{}", output),
|
match execute_command(&prompt_command) {
|
||||||
Err(e) => eprintln!("Command failed: {}", e),
|
Ok(output) => print!("{}", output),
|
||||||
|
Err(e) => eprintln!("Command failed: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => println!("not running command..."),
|
||||||
}
|
}
|
||||||
},
|
} else {
|
||||||
Err(_) => println!("not running command..."),
|
match execute_command(&prompt_command) {
|
||||||
|
Ok(output) => print!("{}", output),
|
||||||
|
Err(e) => eprintln!("Command failed: {}", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user