1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#![feature(backtrace)] use std::path::PathBuf; use crate::cli_format::CliFormatImpl; use crate::command_handler::CommandHandler; use crate::command_parser::CommandParser; use crate::config::Config; use crate::console_output::ConsoleOutput; use crate::default_error::DefaultError; use crate::git::GitImpl; use crate::repository::{Repository, RepositoryImpl}; use crate::shell::{shell_command, ShellImpl}; pub mod config; pub mod console_output; pub mod default_error; pub mod logger; #[doc(hidden)] pub mod test_env; mod banners; mod cli_format; mod command_handler; mod command_parser; mod env; mod git; mod note; mod repository; mod search_match; mod shell; mod usage; pub const PKG_NAME: &str = env!("CARGO_PKG_NAME"); pub const PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); pub const PKG_AUTHORS: &str = env!("CARGO_PKG_AUTHORS"); pub const PKG_DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION"); pub fn parse_and_apply_command(args: Vec<String>, config: &Config) -> Result<ConsoleOutput, DefaultError> { check_prerequisites()?; let command = CommandParser::new().parse_arguments(args)?; let shell = ShellImpl::new(config); let git = GitImpl::new(&shell); let format = CliFormatImpl::new(); let repository = RepositoryImpl::new(config, &shell, &git); let handler = CommandHandler::new(&repository, &format); repository.init()?; handler.apply_command(command) } fn check_prerequisites() -> Result<(), DefaultError> { assert_exists("sh", "sh must be installed and in path variable")?; assert_exists("git", "Git must be installed and in path variable")?; assert_exists("$EDITOR", "$EDITOR variable must be set in your shell") } fn assert_exists(command: &str, message: &str) -> Result<(), DefaultError> { let cmd = shell_command(format!("which {}", command).as_str(), &PathBuf::from("/")); match cmd { Ok(o) if o.status == 0 => Ok(()), Ok(o) if o.status != 0 => Err(DefaultError::new(message.to_string())), _ => Err(DefaultError::new(message.to_string())), } } #[cfg(test)] mod tests { use super::*; #[test] pub fn assert_exists_should_work() { assert_exists("ls", "error-message").unwrap(); } #[test] pub fn assert_exists_should_fail() { let res = assert_exists("non-existing-command", "error-message").unwrap_err(); assert_eq!(res.message, "error-message"); } }