Makes -l as an alternative to <snippet>. Also changes <name> to optionnal and mandatory
This commit is contained in:
parent
3c7f672a55
commit
9efba43112
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "snp"
|
||||
authors = ["Dandaedre"]
|
||||
version = "1.0.1"
|
||||
version = "1.0.2"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="coverage: 99%">
|
||||
<title>coverage: 99%</title>
|
||||
xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="coverage: 95%">
|
||||
<title>coverage: 95%</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
|
||||
<stop offset="1" stop-opacity=".1"/>
|
||||
|
|
@ -16,8 +16,8 @@
|
|||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110">
|
||||
<text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text>
|
||||
<text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text>
|
||||
<text aria-hidden="true" x="775" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">99%</text>
|
||||
<text x="775" y="140" transform="scale(.1)" fill="#fff" textLength="250">99%</text>
|
||||
<text aria-hidden="true" x="775" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">95%</text>
|
||||
<text x="775" y="140" transform="scale(.1)" fill="#fff" textLength="250">95%</text>
|
||||
</g>
|
||||
<script xmlns=""/>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
|
|
@ -94,6 +94,7 @@ mod test {
|
|||
};
|
||||
}
|
||||
|
||||
// #[ignore]
|
||||
#[test]
|
||||
fn test_populate_actions() {
|
||||
// false_cli!("snp", "name", "-p"), Actions::Locate
|
||||
|
|
@ -136,11 +137,8 @@ mod test {
|
|||
false_cli!("snp", "name", "-c", "-r", "new_name", "-f"),
|
||||
vec![Actions::Create]
|
||||
),
|
||||
(false_cli!("snp", "name", "-l"), vec![Actions::List]),
|
||||
(
|
||||
false_cli!("snp", "name", "-l", "-c", "-e", "-E"),
|
||||
vec![Actions::List]
|
||||
),
|
||||
(false_cli!("snp", "-l"), vec![Actions::List]),
|
||||
// (false_cli!("snp", "-l"), vec![Actions::List]),
|
||||
);
|
||||
for (cli, res) in group.iter() {
|
||||
println!("Testing {:?}..actions expected {:?}", cli, res);
|
||||
|
|
@ -149,8 +147,10 @@ mod test {
|
|||
assert!(res.iter().zip(fnd).all(|(a, b)| *a == b));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_execute_parsed_cli() {
|
||||
// println!("Executing test for cli");
|
||||
let list_cli = vec![
|
||||
false_cli!("snp", "parsed_cli", "-e", "-c"),
|
||||
false_cli!("snp", "parsed_cli", "-c", "-f"),
|
||||
|
|
@ -160,7 +160,7 @@ mod test {
|
|||
false_cli!("snp", "parsed_cli", "-c"),
|
||||
false_cli!("snp", "parsed_cli", "-r", "new_name", "-f"),
|
||||
false_cli!("snp", "new_name", "-d"),
|
||||
false_cli!("snp", "new_name", "-l"),
|
||||
false_cli!("snp", "-l"),
|
||||
];
|
||||
|
||||
let bad_cli = vec![
|
||||
|
|
|
|||
39
src/main.rs
39
src/main.rs
|
|
@ -20,43 +20,49 @@ mod wrapper_functions;
|
|||
#[command(author, version, about, verbatim_doc_comment)]
|
||||
pub struct Cli {
|
||||
/// The Snippet name (and therefore his path)
|
||||
#[arg()]
|
||||
snippet: String,
|
||||
#[arg(group = "mandatory", group = "mutex")]
|
||||
snippet: Option<String>,
|
||||
|
||||
/// Try to create a Snippet with the given name and default arguments
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
#[arg(short, long, default_value_t = false, requires = "mandatory")]
|
||||
create: bool,
|
||||
|
||||
/// Find the path of the snippet with the given name
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
#[arg(short, long, default_value_t = false, requires = "mandatory")]
|
||||
path: bool,
|
||||
|
||||
/// Launch the editor and edit the Snippet content
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
#[arg(short, long, default_value_t = false, requires = "mandatory")]
|
||||
edit: bool,
|
||||
|
||||
/// Launch the editor and edit the whole snippet
|
||||
#[arg(short = 'E', long = "Edit", default_value_t = false)]
|
||||
#[arg(
|
||||
short = 'E',
|
||||
long = "Edit",
|
||||
default_value_t = false,
|
||||
requires = "mandatory"
|
||||
)]
|
||||
edit_all: bool,
|
||||
|
||||
/// The list of arguments to give to the snippet
|
||||
/// if the -c flag is used, it's used to create the default arguments
|
||||
#[arg(requires = "mandatory")]
|
||||
args: Vec<String>,
|
||||
|
||||
/// Force the creation/moving/renaming of a Snippet even if it mean overwriting an existing one
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
#[arg(short, long, default_value_t = false, requires = "mandatory")]
|
||||
force: bool,
|
||||
|
||||
/// Delete a snippet, takes precedence over everything
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
#[arg(short, long, default_value_t = false, requires = "mandatory")]
|
||||
delete: bool,
|
||||
|
||||
/// Try to rename the Snippet (Use -f if the destination name already exist)
|
||||
#[arg(short, long)]
|
||||
#[arg(short, long, requires = "mandatory")]
|
||||
rename: Option<String>,
|
||||
|
||||
/// List all snippets in the snippet folder
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
#[arg(short, long, default_value_t = false, group = "mutex")]
|
||||
list: bool,
|
||||
}
|
||||
|
||||
|
|
@ -65,3 +71,16 @@ fn main() -> Result<(), errors::SnippetError> {
|
|||
let cli = Cli::parse();
|
||||
actions::execute_parsed_cli(cli)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use clap::CommandFactory;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_cli_main() {
|
||||
let cmd = Cli::command_for_update();
|
||||
cmd.debug_assert();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ use crate::{dynamic_formatter, Cli};
|
|||
///
|
||||
/// Will error on snippet not existing
|
||||
pub fn locate_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
let status = check_dir_and_open(&cli.snippet)?;
|
||||
let status = check_dir_and_open(cli.snippet.as_ref().unwrap())?;
|
||||
match status {
|
||||
FileStatus::Opened((p, _)) => {
|
||||
println!("{:?}", p.as_os_str());
|
||||
|
|
@ -31,8 +31,12 @@ pub fn locate_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
/// Create a new snippet with the given cli args
|
||||
pub fn create_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
// Create a snippet with the given data
|
||||
let snp = Snippet::new(cli.snippet.clone(), cli.args.clone(), String::from(""));
|
||||
let status = check_dir_and_open(&cli.snippet)?;
|
||||
let snp = Snippet::new(
|
||||
cli.snippet.as_ref().unwrap().clone(),
|
||||
cli.args.clone(),
|
||||
String::from(""),
|
||||
);
|
||||
let status = check_dir_and_open(cli.snippet.as_ref().unwrap())?;
|
||||
let (f, p) = match status {
|
||||
FileStatus::Opened((p, _)) => {
|
||||
if cli.force {
|
||||
|
|
@ -54,10 +58,10 @@ pub fn create_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
pub fn rename_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
// Check wether the snippet exitsts
|
||||
let new_name = cli.rename.as_ref().unwrap();
|
||||
if new_name == &cli.snippet {
|
||||
if new_name == cli.snippet.as_ref().unwrap() {
|
||||
return Ok(()); // The noop is a success ?
|
||||
}
|
||||
let (f, oldpath) = match check_dir_and_open(&cli.snippet)? {
|
||||
let (f, oldpath) = match check_dir_and_open(cli.snippet.as_ref().unwrap())? {
|
||||
FileStatus::Opened((p, f)) => (f, p),
|
||||
FileStatus::NotFound(e) => return Err(SnippetError::NotFound(e)),
|
||||
};
|
||||
|
|
@ -83,7 +87,8 @@ pub fn rename_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
// Save snippet
|
||||
println!(
|
||||
"The snippet {} was sucessfuly renamed as {}",
|
||||
cli.snippet, new_name
|
||||
cli.snippet.as_ref().unwrap(),
|
||||
new_name
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -91,13 +96,17 @@ pub fn rename_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
/// Open and load snippet and edit it
|
||||
pub fn edit_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
// Try loading, if not existant check if create or error
|
||||
let status = check_dir_and_open(&cli.snippet)?;
|
||||
let status = check_dir_and_open(cli.snippet.as_ref().unwrap())?;
|
||||
let (mut snp, _, p) = match status {
|
||||
FileStatus::Opened((p, f)) => (Snippet::from_file(&f)?, f, p),
|
||||
FileStatus::NotFound(p) => {
|
||||
if cli.create {
|
||||
(
|
||||
Snippet::new(cli.snippet.clone(), cli.args.clone(), String::from("")),
|
||||
Snippet::new(
|
||||
cli.snippet.as_ref().unwrap().clone(),
|
||||
cli.args.clone(),
|
||||
String::from(""),
|
||||
),
|
||||
File::create(&p)?,
|
||||
p,
|
||||
)
|
||||
|
|
@ -124,13 +133,16 @@ pub fn edit_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
/// a creation or an override
|
||||
pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
// Try loading the snippet
|
||||
let status = check_dir_and_open(&cli.snippet);
|
||||
let status = check_dir_and_open(cli.snippet.as_ref().unwrap());
|
||||
let (buf, mut origin_p) = match status? {
|
||||
FileStatus::Opened((p, f)) => (read_to_string(f)?, p),
|
||||
FileStatus::NotFound(p) => {
|
||||
if cli.create {
|
||||
File::create(&p)?;
|
||||
(Snippet::empty(cli.snippet.clone(), cli.args.clone()), p)
|
||||
(
|
||||
Snippet::empty(cli.snippet.clone().unwrap(), cli.args.clone()),
|
||||
p,
|
||||
)
|
||||
} else {
|
||||
return Err(SnippetError::NotFoundNoRetryError(p));
|
||||
}
|
||||
|
|
@ -143,7 +155,7 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
// Try reparsing, on error no modifications will be made
|
||||
match Snippet::from_string(&buf) {
|
||||
Ok(snp) => {
|
||||
if cli.snippet == snp.name {
|
||||
if cli.snippet.as_ref().unwrap() == &snp.name {
|
||||
// No renaming has occured, the file can safely be overriten
|
||||
snp.write_over(&origin_p)?;
|
||||
println!(
|
||||
|
|
@ -213,7 +225,7 @@ pub fn list_snippets(_cli: &Cli) -> Result<(), SnippetError> {
|
|||
/// Print the snippet to stdout
|
||||
pub fn print_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
// Try to load the snippet <name>
|
||||
let (_, f) = match check_dir_and_open(&cli.snippet)? {
|
||||
let (_, f) = match check_dir_and_open(cli.snippet.as_ref().unwrap())? {
|
||||
FileStatus::Opened((p, f)) => (p, f),
|
||||
FileStatus::NotFound(p) => return Err(SnippetError::NotFound(p)),
|
||||
};
|
||||
|
|
@ -233,7 +245,7 @@ pub fn print_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
|||
/// Delete a given snippet, errors if the snippet doesn't exists
|
||||
pub fn delete_snippet(cli: &Cli) -> Result<(), SnippetError> {
|
||||
let mut path = config_folder()?;
|
||||
path.push(format!("{}.yaml", cli.snippet));
|
||||
path.push(format!("{}.yaml", cli.snippet.as_ref().unwrap()));
|
||||
std::fs::remove_file(path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -308,6 +320,7 @@ pub mod test {
|
|||
assert!(delete_snippet(false_cli!("snp", "-d", "delete_snippet")).is_ok());
|
||||
}
|
||||
|
||||
// #[ignore]
|
||||
#[test]
|
||||
fn test_locate() {
|
||||
// Try to locate an unexisting snippet
|
||||
|
|
@ -315,14 +328,14 @@ pub mod test {
|
|||
_ = std::fs::remove_file(&theorical_path);
|
||||
|
||||
// Locating the snippet should fail
|
||||
assert!(locate_snippet(false_cli!("-l", "test_locate")).is_err());
|
||||
assert!(locate_snippet(false_cli!("snp", "-f", "test_locate")).is_err());
|
||||
|
||||
let mut f = File::create(&theorical_path).expect("Couldn't create file");
|
||||
write!(f, "N").expect("Couldn't write on the new file");
|
||||
f.sync_data().expect("Can't sync file");
|
||||
|
||||
// Locating the snippet should now suceed
|
||||
assert!(locate_snippet(false_cli!("-l", "test_locate")).is_ok());
|
||||
assert!(locate_snippet(false_cli!("-f", "test_locate")).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -334,57 +347,59 @@ pub mod test {
|
|||
assert!(print_snippet(false_cli!("snp", "test_print")).is_err());
|
||||
|
||||
// Create snippet
|
||||
assert!(create_snippet(false_cli!("-c", "test_print")).is_ok());
|
||||
assert!(create_snippet(false_cli!("snp", "-c", "test_print")).is_ok());
|
||||
assert!(print_snippet(false_cli!("snp", "test_print")).is_ok());
|
||||
// assert!(print_snippet(false_cli!("-l", "test_print")).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create() {
|
||||
_ = std::fs::remove_file(snippet_path("create_snippet").unwrap());
|
||||
// Test to create empty
|
||||
assert!(create_snippet(false_cli!("-c", "create_snippet")).is_ok());
|
||||
assert!(create_snippet(false_cli!("snp", "-c", "create_snippet")).is_ok());
|
||||
|
||||
// Test to create again
|
||||
assert!(create_snippet(false_cli!("-c", "create_snippet")).is_err());
|
||||
assert!(create_snippet(false_cli!("snp", "-c", "create_snippet")).is_err());
|
||||
|
||||
// Test to create with force
|
||||
assert!(create_snippet(false_cli!("-c", "-f", "create_snippet")).is_ok());
|
||||
assert!(create_snippet(false_cli!("snp", "-c", "-f", "create_snippet")).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edit() {
|
||||
_ = std::fs::remove_file(snippet_path("edit_content").unwrap());
|
||||
// Open unnexitant
|
||||
assert!(edit_snippet(false_cli!("-e", "edit_content")).is_err());
|
||||
assert!(edit_snippet(false_cli!("snp", "-e", "edit_content")).is_err());
|
||||
|
||||
// Force creation
|
||||
assert!(edit_snippet(false_cli!("-e", "-c", "edit_content")).is_ok());
|
||||
assert!(edit_snippet(false_cli!("snp", "-e", "-c", "edit_content")).is_ok());
|
||||
|
||||
// Edit when created
|
||||
|
||||
println!("{:?}", edit_snippet(false_cli!("-e", "edit_content")));
|
||||
assert!(edit_snippet(false_cli!("-e", "edit_content")).is_ok());
|
||||
println!(
|
||||
"{:?}",
|
||||
edit_snippet(false_cli!("snp", "-e", "edit_content"))
|
||||
);
|
||||
assert!(edit_snippet(false_cli!("snp", "-e", "edit_content")).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_edit_whole() {
|
||||
_ = std::fs::remove_file(snippet_path("edit_whole").unwrap());
|
||||
// Open unnexitant
|
||||
assert!(edit_whole_snippet(false_cli!("-E", "edit_whole")).is_err());
|
||||
assert!(edit_whole_snippet(false_cli!("snp", "-E", "edit_whole")).is_err());
|
||||
|
||||
// Force creation
|
||||
assert!(edit_whole_snippet(false_cli!("-E", "-c", "edit_whole")).is_ok());
|
||||
assert!(edit_whole_snippet(false_cli!("snp", "-E", "-c", "edit_whole")).is_ok());
|
||||
|
||||
// Edit when created
|
||||
assert!(edit_whole_snippet(false_cli!("-E", "edit_whole")).is_ok());
|
||||
assert!(edit_whole_snippet(false_cli!("snp", "-E", "edit_whole")).is_ok());
|
||||
|
||||
// Create a badly formatted file
|
||||
if let FileStatus::Opened((p, _)) = check_dir_and_open("edit_whole").unwrap() {
|
||||
let mut f = File::create(p).unwrap();
|
||||
write!(f, "badly formatter yaml").unwrap();
|
||||
|
||||
assert!(edit_whole_snippet(false_cli!("-E", "edit_whole")).is_err());
|
||||
assert!(edit_whole_snippet(false_cli!("snp", "-E", "edit_whole")).is_err());
|
||||
assert!(matches!(
|
||||
check_dir_and_open("edit_whole.bad").unwrap(),
|
||||
FileStatus::Opened(_)
|
||||
|
|
@ -400,7 +415,7 @@ pub mod test {
|
|||
_ = std::fs::remove_file(&oldpath);
|
||||
_ = std::fs::remove_file(&newpath);
|
||||
_ = std::fs::remove_file(filledpath);
|
||||
assert!(create_snippet(false_cli!("-c", "rename_snippet", "-f")).is_ok());
|
||||
assert!(create_snippet(false_cli!("snp", "-c", "rename_snippet", "-f")).is_ok());
|
||||
assert!(
|
||||
rename_snippet(false_cli!("snp", "-r", "renamed_snippet", "rename_snippet")).is_ok()
|
||||
);
|
||||
|
|
@ -410,8 +425,8 @@ pub mod test {
|
|||
|
||||
assert!(rename_snippet(false_cli!("snp", "-r", "non_ex", "non_ex")).is_ok());
|
||||
assert!(rename_snippet(false_cli!("snp", "-r", "non_ex", "renamed_snippet")).is_err());
|
||||
assert!(create_snippet(false_cli!("-c", "filled_destination", "-f")).is_ok());
|
||||
assert!(create_snippet(false_cli!("-c", "rename_snippet", "-f")).is_ok());
|
||||
assert!(create_snippet(false_cli!("snp", "-c", "filled_destination", "-f")).is_ok());
|
||||
assert!(create_snippet(false_cli!("snp", "-c", "rename_snippet", "-f")).is_ok());
|
||||
assert!(matches!(
|
||||
rename_snippet(false_cli!(
|
||||
"snp",
|
||||
|
|
@ -449,8 +464,8 @@ pub mod test {
|
|||
println!("Trying to resave without destination {:?}", res);
|
||||
assert!(res.is_ok());
|
||||
|
||||
assert!(create_snippet(false_cli!("-c", "new_snippet")).is_err());
|
||||
assert!(create_snippet(false_cli!("-c", "resave_snippet")).is_err());
|
||||
assert!(create_snippet(false_cli!("snp", "-c", "new_snippet")).is_err());
|
||||
assert!(create_snippet(false_cli!("snp", "-c", "resave_snippet")).is_err());
|
||||
|
||||
let res = edit_whole_snippet(false_cli!("snp", "resave_snippet", "-f"));
|
||||
println!("Sucessfully erasing {:?}", res);
|
||||
|
|
|
|||
Reference in New Issue