1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 09:04:12 +02:00

init arg parsing

This commit is contained in:
Robin Appelman 2021-03-12 19:32:55 +01:00
commit d426b537b2
4 changed files with 175 additions and 1 deletions

52
Cargo.lock generated
View file

@ -15,6 +15,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
dependencies = [
"memchr",
]
[[package]]
name = "anyhow"
version = "1.0.38"
@ -374,6 +383,7 @@ dependencies = [
"color-eyre",
"maplit",
"min-id",
"parse-display",
"tokio",
]
@ -647,6 +657,31 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2386b4ebe91c2f7f51082d4cefa145d030e33a1842a96b12e4885cc3c01f7a55"
[[package]]
name = "parse-display"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7271152b3c46c07c729698e7a5248e2744466b3446d222c97a0b1315925a97b1"
dependencies = [
"once_cell",
"parse-display-derive",
"regex",
]
[[package]]
name = "parse-display-derive"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6a9f3e41b237b77c99c09686481c235964ff5878229412b226c451f3e809f4f"
dependencies = [
"once_cell",
"proc-macro2",
"quote",
"regex",
"regex-syntax",
"syn",
]
[[package]]
name = "percent-encoding"
version = "2.1.0"
@ -781,6 +816,23 @@ dependencies = [
"redox_syscall",
]
[[package]]
name = "regex"
version = "1.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54fd1046a3107eb58f42de31d656fee6853e5d276c455fd943742dce89fc3dd3"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548"
[[package]]
name = "ring"
version = "0.16.20"

View file

@ -10,4 +10,5 @@ color-eyre = "0.5"
min-id = "0.1"
maplit = "1"
camino = "1"
tokio = { version = "1" , features = ["fs", "macros"] }
tokio = { version = "1" , features = ["fs", "macros"] }
parse-display = "0.4"

120
src/args.rs Normal file
View file

@ -0,0 +1,120 @@
use color_eyre::{eyre::WrapErr, Report, Result};
use std::env::Args;
use std::str::FromStr;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct HazeArgs {
id: Option<String>,
command: Command,
options: Vec<String>,
}
impl HazeArgs {
pub fn parse<I, S>(mut args: I) -> Result<HazeArgs>
where
S: AsRef<str> + ToString,
I: Iterator<Item = S>,
{
let _bin = args.next().unwrap();
let (id, command) = match args.next() {
Some(sub_or_id) => {
if let Ok(command) = sub_or_id.as_ref().parse() {
(None, command)
} else {
if let Some(sub) = args.next() {
(Some(sub_or_id.to_string()), sub.as_ref().parse()?)
} else {
(Some(sub_or_id.to_string()), Command::List)
}
}
}
None => (None, Command::List),
};
let options = args.map(|s| s.to_string()).collect();
Ok(HazeArgs {
id,
command,
options,
})
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Command {
List,
Start,
Stop,
Test,
Exec,
Occ,
Db,
}
impl FromStr for Command {
type Err = Report;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"list" => Ok(Command::List),
"start" => Ok(Command::Start),
"stop" => Ok(Command::Stop),
"test" => Ok(Command::Test),
"exec" => Ok(Command::Exec),
"occ" => Ok(Command::Occ),
"db" => Ok(Command::Db),
_ => Err(Report::msg(format!("Unknown command: {}", s))),
}
}
}
#[test]
fn test_arg_parse() {
assert_eq!(
HazeArgs::parse(vec!["haze"].into_iter()).unwrap(),
HazeArgs {
id: None,
command: Command::List,
options: Vec::new(),
}
);
assert_eq!(
HazeArgs::parse(vec!["haze", "test"].into_iter()).unwrap(),
HazeArgs {
id: None,
command: Command::Test,
options: Vec::new(),
}
);
assert_eq!(
HazeArgs::parse(vec!["haze", "asdasd"].into_iter()).unwrap(),
HazeArgs {
id: Some("asdasd".to_string()),
command: Command::List,
options: Vec::new(),
}
);
assert_eq!(
HazeArgs::parse(vec!["haze", "asdasd", "db"].into_iter()).unwrap(),
HazeArgs {
id: Some("asdasd".to_string()),
command: Command::Db,
options: Vec::new(),
}
);
assert_eq!(
HazeArgs::parse(vec!["haze", "exec", "foo", "bar"].into_iter()).unwrap(),
HazeArgs {
id: None,
command: Command::Exec,
options: vec!["foo".to_string(), "bar".to_string()],
}
);
assert_eq!(
HazeArgs::parse(vec!["haze", "asdasd", "exec", "foo", "bar"].into_iter()).unwrap(),
HazeArgs {
id: Some("asdasd".to_string()),
command: Command::Exec,
options: vec!["foo".to_string(), "bar".to_string()],
}
);
}

View file

@ -3,6 +3,7 @@ use crate::config::HazeConfig;
use bollard::Docker;
use color_eyre::{eyre::WrapErr, Result};
mod args;
mod cloud;
mod config;