summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-03-17 11:10:53 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-03-17 11:11:36 +0200
commitf5a00e9090b9d81936137c3fc676cfd0ad25430c (patch)
tree5384a422af6ec34685b31308caf2fd70f1d6114e /src
parentb541b141e66d5cf0ba4b190eda6422126951af6e (diff)
Use shlex to split shell arguments
Diffstat (limited to 'src')
-rw-r--r--src/main.rs4
-rw-r--r--src/parser.rs21
2 files changed, 21 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index 50f5127..bcbfe1c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -51,8 +51,8 @@ fn run_command(user_input: String) -> Result<ControlFlow<(), ()>, ShackleError>
return Ok(ControlFlow::Break(()));
}
Ok(ShackleCommand::GitInit(GitInitArgs { repo_name })) => {
- git::init(&repo_name)?; // TODO should report this error differently
- println!("Successfully created {}.git", repo_name);
+ git::init(&repo_name)?;
+ println!("Successfully created \"{}.git\"", repo_name);
}
Ok(ShackleCommand::GitUploadPack(upload_pack_args)) => {
let mut command = Command::new("git-upload-pack");
diff --git a/src/parser.rs b/src/parser.rs
index 59dc7d8..89e0d76 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -1,5 +1,6 @@
use clap::{Parser, Subcommand};
use std::str::FromStr;
+use thiserror::Error;
#[derive(Parser, Clone, Debug, PartialEq, Eq)]
#[command(name = "")]
@@ -44,8 +45,16 @@ pub struct GitReceivePackArgs {
pub directory: String,
}
+#[derive(Error, Debug)]
+pub enum ParserError {
+ #[error(transparent)]
+ ClapError(#[from] clap::error::Error),
+ #[error("`{0}`")]
+ LexerError(String),
+}
+
impl FromStr for ShackleCommand {
- type Err = clap::error::Error;
+ type Err = ParserError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let trimmed = s.trim();
@@ -54,7 +63,15 @@ impl FromStr for ShackleCommand {
} else if trimmed.len() == 0 {
Ok(ShackleCommand::Whitespace)
} else {
- ShackleCommand::try_parse_from([""].into_iter().chain(trimmed.split_whitespace()))
+ let lexed = shlex::split(trimmed);
+ match lexed {
+ None => Err(ParserError::LexerError("Incomplete input".to_string())),
+ Some(lexed) => {
+ let parsed =
+ ShackleCommand::try_parse_from(["".to_owned()].into_iter().chain(lexed))?;
+ Ok(parsed)
+ }
+ }
}
}
}