2020-03-04 21:01:23 +00:00
|
|
|
use crate::display;
|
|
|
|
use crate::filesystem;
|
|
|
|
use crate::option::Config;
|
|
|
|
|
|
|
|
use regex::Regex;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs;
|
|
|
|
use std::io::Write;
|
|
|
|
|
2020-03-14 08:09:09 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-03-04 21:01:23 +00:00
|
|
|
pub struct SuggestionOpts {
|
|
|
|
pub header_lines: u8,
|
|
|
|
pub column: Option<u8>,
|
2020-03-11 14:30:25 +00:00
|
|
|
pub delimiter: Option<String>,
|
2020-03-14 12:33:07 +00:00
|
|
|
pub suggestion_type: SuggestionType,
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 12:33:07 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2020-03-14 07:59:31 +00:00
|
|
|
pub enum SuggestionType {
|
|
|
|
Disabled,
|
|
|
|
SingleSelection,
|
|
|
|
MultipleSelections,
|
|
|
|
SingleRecommendation,
|
|
|
|
SnippetSelection,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Suggestion = (String, Option<SuggestionOpts>);
|
2020-03-04 21:01:23 +00:00
|
|
|
|
|
|
|
fn gen_snippet(snippet: &str, line: &str) -> String {
|
|
|
|
if snippet.is_empty() {
|
|
|
|
line.to_string()
|
|
|
|
} else {
|
|
|
|
format!("{}{}", &snippet[..snippet.len() - 2], line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 22:04:47 +00:00
|
|
|
fn remove_quote(txt: &str) -> String {
|
|
|
|
txt.replace('"', "").replace('\'', "")
|
|
|
|
}
|
|
|
|
|
2020-03-04 21:01:23 +00:00
|
|
|
fn parse_opts(text: &str) -> SuggestionOpts {
|
|
|
|
let mut header_lines: u8 = 0;
|
|
|
|
let mut column: Option<u8> = None;
|
|
|
|
let mut multi = false;
|
2020-03-14 08:09:09 +00:00
|
|
|
let mut allow_extra = false;
|
2020-03-11 14:30:25 +00:00
|
|
|
let mut delimiter: Option<String> = None;
|
2020-03-04 21:01:23 +00:00
|
|
|
|
|
|
|
let mut parts = text.split(' ');
|
|
|
|
|
|
|
|
while let Some(p) = parts.next() {
|
|
|
|
match p {
|
|
|
|
"--multi" => multi = true,
|
2020-03-14 08:09:09 +00:00
|
|
|
"--allow-extra" => allow_extra = true,
|
2020-03-11 14:30:25 +00:00
|
|
|
"--header" | "--headers" | "--header-lines" => {
|
2020-03-12 22:04:47 +00:00
|
|
|
header_lines = remove_quote(parts.next().unwrap()).parse::<u8>().unwrap()
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
2020-03-12 22:04:47 +00:00
|
|
|
"--column" => column = Some(remove_quote(parts.next().unwrap()).parse::<u8>().unwrap()),
|
|
|
|
"--delimiter" => delimiter = Some(remove_quote(parts.next().unwrap()).to_string()),
|
2020-03-04 21:01:23 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 08:09:09 +00:00
|
|
|
let result = SuggestionOpts {
|
2020-03-04 21:01:23 +00:00
|
|
|
header_lines,
|
|
|
|
column,
|
2020-03-11 14:30:25 +00:00
|
|
|
delimiter,
|
2020-03-14 08:09:09 +00:00
|
|
|
suggestion_type: match (multi, allow_extra) {
|
2020-03-14 12:33:07 +00:00
|
|
|
(true, _) => SuggestionType::MultipleSelections, // multi wins over allow-extra
|
|
|
|
(false, true) => SuggestionType::SingleRecommendation,
|
|
|
|
(false, false) => SuggestionType::SingleSelection,
|
2020-03-14 08:09:09 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
result
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_variable_line(line: &str) -> (&str, &str, Option<SuggestionOpts>) {
|
|
|
|
let re = Regex::new(r"^\$\s*([^:]+):(.*)").unwrap();
|
|
|
|
let caps = re.captures(line).unwrap();
|
|
|
|
let variable = caps.get(1).unwrap().as_str().trim();
|
|
|
|
let mut command_plus_opts = caps.get(2).unwrap().as_str().split("---");
|
2020-03-14 12:33:07 +00:00
|
|
|
let command: &str = command_plus_opts.next().unwrap();
|
|
|
|
let command_option_string: Option<&str> = command_plus_opts.next();
|
2020-03-14 08:09:09 +00:00
|
|
|
let command_options = command_option_string.map(parse_opts);
|
|
|
|
(variable, command, command_options)
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_file(
|
|
|
|
path: &str,
|
2020-03-14 07:59:31 +00:00
|
|
|
variables: &mut HashMap<String, Suggestion>,
|
2020-03-04 21:01:23 +00:00
|
|
|
stdin: &mut std::process::ChildStdin,
|
|
|
|
) {
|
|
|
|
let mut tags = String::from("");
|
|
|
|
let mut comment = String::from("");
|
|
|
|
let mut snippet = String::from("");
|
|
|
|
|
2020-03-07 21:03:51 +00:00
|
|
|
let (tag_width, comment_width) = *display::WIDTHS;
|
2020-03-04 21:01:23 +00:00
|
|
|
|
|
|
|
if let Ok(lines) = filesystem::read_lines(path) {
|
|
|
|
for l in lines {
|
|
|
|
let line = l.unwrap();
|
|
|
|
|
|
|
|
// tag
|
|
|
|
if line.starts_with('%') {
|
|
|
|
tags = String::from(&line[2..]);
|
|
|
|
}
|
2020-03-09 18:02:54 +00:00
|
|
|
// metacomment
|
|
|
|
else if line.starts_with(';') {
|
|
|
|
}
|
2020-03-04 21:01:23 +00:00
|
|
|
// comment
|
|
|
|
else if line.starts_with('#') {
|
|
|
|
comment = String::from(&line[2..]);
|
|
|
|
}
|
|
|
|
// variable
|
|
|
|
else if line.starts_with('$') {
|
2020-03-14 08:09:09 +00:00
|
|
|
let (variable, command, opts) = parse_variable_line(&line);
|
2020-03-04 21:01:23 +00:00
|
|
|
variables.insert(
|
|
|
|
format!("{};{}", tags, variable),
|
|
|
|
(String::from(command), opts),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// snippet with line break
|
|
|
|
else if line.ends_with('\\') {
|
|
|
|
snippet = if !snippet.is_empty() {
|
|
|
|
format!("{}{}", &snippet[..snippet.len() - 2], line)
|
|
|
|
} else {
|
|
|
|
line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// blank
|
|
|
|
else if line.is_empty() {
|
|
|
|
}
|
|
|
|
// snippet
|
|
|
|
else {
|
|
|
|
let full_snippet = gen_snippet(&snippet, &line);
|
2020-03-07 21:03:51 +00:00
|
|
|
match stdin.write_all(
|
2020-03-04 21:01:23 +00:00
|
|
|
display::format_line(
|
|
|
|
&tags[..],
|
|
|
|
&comment[..],
|
|
|
|
&full_snippet[..],
|
|
|
|
tag_width,
|
|
|
|
comment_width,
|
|
|
|
)
|
|
|
|
.as_bytes(),
|
|
|
|
) {
|
|
|
|
Ok(_) => snippet = String::from(""),
|
|
|
|
Err(_) => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 12:33:07 +00:00
|
|
|
pub fn read_all(
|
|
|
|
config: &Config,
|
|
|
|
stdin: &mut std::process::ChildStdin,
|
|
|
|
) -> HashMap<String, Suggestion> {
|
2020-03-14 07:59:31 +00:00
|
|
|
let mut variables: HashMap<String, Suggestion> = HashMap::new();
|
2020-03-04 21:01:23 +00:00
|
|
|
|
2020-03-12 22:04:47 +00:00
|
|
|
let mut fallback: String = String::from("");
|
|
|
|
let folders_str = config.path.as_ref().unwrap_or_else(|| {
|
2020-03-13 02:32:28 +00:00
|
|
|
if let Some(f) = filesystem::cheat_pathbuf() {
|
|
|
|
fallback = filesystem::pathbuf_to_string(f);
|
|
|
|
}
|
2020-03-12 22:04:47 +00:00
|
|
|
&fallback
|
|
|
|
});
|
2020-03-04 21:01:23 +00:00
|
|
|
let folders = folders_str.split(':');
|
|
|
|
|
|
|
|
for folder in folders {
|
|
|
|
if let Ok(paths) = fs::read_dir(folder) {
|
|
|
|
for path in paths {
|
2020-03-12 16:03:04 +00:00
|
|
|
let path_os_str = path.unwrap().path().into_os_string();
|
|
|
|
let path_str = path_os_str.to_str().unwrap();
|
|
|
|
if path_str.ends_with(".cheat") {
|
|
|
|
read_file(path_str, &mut variables, stdin);
|
|
|
|
}
|
2020-03-04 21:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
variables
|
|
|
|
}
|
2020-03-14 08:09:09 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_variable_line() {
|
2020-03-14 12:33:07 +00:00
|
|
|
let (variable, command, command_options) =
|
|
|
|
parse_variable_line("$ user : echo -e \"$(whoami)\\nroot\" --- --allow-extra");
|
2020-03-14 08:09:09 +00:00
|
|
|
assert_eq!(command, " echo -e \"$(whoami)\\nroot\" ");
|
|
|
|
assert_eq!(variable, "user");
|
2020-03-14 12:33:07 +00:00
|
|
|
assert_eq!(
|
|
|
|
command_options,
|
|
|
|
Some(SuggestionOpts {
|
|
|
|
header_lines: 0,
|
|
|
|
column: None,
|
|
|
|
delimiter: None,
|
|
|
|
suggestion_type: SuggestionType::SingleRecommendation
|
|
|
|
})
|
|
|
|
);
|
2020-03-14 08:09:09 +00:00
|
|
|
}
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_file() {
|
|
|
|
let path = "tests/cheats/ssh.cheat";
|
|
|
|
let mut variables: HashMap<String, Suggestion> = HashMap::new();
|
2020-03-14 12:33:07 +00:00
|
|
|
let mut child = Command::new("cat").stdin(Stdio::piped()).spawn().unwrap();
|
2020-03-14 08:09:09 +00:00
|
|
|
let child_stdin = child.stdin.as_mut().unwrap();
|
2020-03-14 12:33:07 +00:00
|
|
|
read_file(path, &mut variables, child_stdin);
|
|
|
|
let mut result: HashMap<String, (String, std::option::Option<_>)> = HashMap::new();
|
|
|
|
result.insert(
|
|
|
|
"ssh;user".to_string(),
|
|
|
|
(
|
|
|
|
" echo -e \"$(whoami)\\nroot\" ".to_string(),
|
|
|
|
Some(SuggestionOpts {
|
|
|
|
header_lines: 0,
|
|
|
|
column: None,
|
|
|
|
delimiter: None,
|
|
|
|
suggestion_type: SuggestionType::SingleRecommendation,
|
|
|
|
}),
|
|
|
|
),
|
2020-03-14 08:09:09 +00:00
|
|
|
);
|
|
|
|
assert_eq!(variables, result);
|
|
|
|
}
|
|
|
|
}
|