From 10fd7677ba0819d2435f328fc244d383ffb74b50 Mon Sep 17 00:00:00 2001 From: Jannis Morgenstern Date: Sun, 17 Sep 2023 23:18:50 +0200 Subject: [PATCH] fix: handle no slop error --- Cargo.lock | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++++ src/cli.rs | 12 ++++++++---- src/config.rs | 7 ++++--- src/main.rs | 13 ++++++++++++- 5 files changed, 78 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0625a8c..e777871 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -251,7 +251,9 @@ dependencies = [ "bitwarden", "clap", "clap-markdown", + "log", "serde", + "simple_logger", "tokio", "toml", "uuid", @@ -369,6 +371,17 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "colored" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2674ec482fbc38012cf31e6c42ba0177b431a0cb6f15fe40efa5aab1bda516f6" +dependencies = [ + "is-terminal", + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "const-oid" version = "0.9.5" @@ -757,6 +770,17 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6" +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys 0.48.0", +] + [[package]] name = "itoa" version = "1.0.9" @@ -1447,6 +1471,17 @@ dependencies = [ "rand_core", ] +[[package]] +name = "simple_logger" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2230cd5c29b815c9b699fb610b49a5ed65588f3509d9f0108be3a885da629333" +dependencies = [ + "colored", + "log", + "windows-sys 0.42.0", +] + [[package]] name = "slab" version = "0.4.9" @@ -1887,6 +1922,21 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + [[package]] name = "windows-sys" version = "0.45.0" diff --git a/Cargo.toml b/Cargo.toml index 6b454ba..1d0c6b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,11 @@ edition = "2021" bitwarden = { version = "0.3.0", features = ["secrets"] } clap = { version = "4.4.2", features = ["derive", "cargo", "env"] } clap-markdown = "0.1.3" +log = "0.4.20" serde = "1.0.188" +simple_logger = { version = "4.2.0", default-features = false, features = [ + "colors", +] } tokio = { version = "1.32.0", features = ["full"] } toml = "0.7.6" uuid = "1.4.1" diff --git a/src/cli.rs b/src/cli.rs index 2960ff7..7387b5b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -57,11 +57,15 @@ impl Cli { Cli { args } } - pub fn get_program(&self) -> (String, Vec) { + pub fn get_program(&self) -> Option<(String, Vec)> { let slop = &self.args.slop; - let program = &slop[0]; - let args = slop[1..].to_vec(); + match &slop.get(0) { + Some(program) => { + let args = slop[1..].to_vec(); - (program.to_owned(), args) + Some((program.to_string(), args)) + } + None => None, + } } } diff --git a/src/config.rs b/src/config.rs index f479091..32c3759 100644 --- a/src/config.rs +++ b/src/config.rs @@ -68,9 +68,10 @@ impl Config { .as_ref() .ok_or_else(|| Error::NoProfileInput)?; - let profile_name = cli_args.profile.clone().unwrap_or_else(|| { - get_profile_from_env(env_var_names).expect("no profile") - }); + let profile_name = cli_args + .profile + .clone() + .unwrap_or_else(|| get_profile_from_env(env_var_names).expect("no profile")); let profile = self .profiles diff --git a/src/main.rs b/src/main.rs index 0a811d6..7020cca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use simple_logger::SimpleLogger; use std::{ io::{self, Read, Write}, path::PathBuf, @@ -22,11 +23,21 @@ use crate::{bitwarden::BitwardenClient, cli::Cli}; #[tokio::main(flavor = "current_thread")] async fn main() { + SimpleLogger::new().init().unwrap(); + // generate docs // let markdown: String = clap_markdown::help_markdown::(); // println!("{}", markdown); let cli = Cli::new(); - let (program, program_args) = cli.get_program(); + let (program, program_args) = match cli.get_program() { + Some(t) => t, + None => { + log::error!("no slop provided"); + std::process::exit(1) + } + }; + + println!("{:?}", program); let config = Config::new(); let ConfigEvaluation {