mod git; mod parser; use clap::Parser; use parser::*; use rustyline::{error::ReadlineError, DefaultEditor}; use std::{io, ops::ControlFlow, process::Command}; use thiserror::Error; /// Shackle Shell - A replacement for git-shell with repo management commands built in. #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] struct Args { /// Run a single shell command and exit #[arg(short, long)] command: Option, } fn main() -> Result<(), ShackleError> { let args = Args::parse(); match args.command { Some(user_input) => { run_command(user_input)?; } None => { run_interactive_loop()?; } } Ok(()) } fn run_command(user_input: String) -> Result, ShackleError> { match user_input.parse::() { Err(parse_error) => { println!("{}", parse_error); } Ok(ShackleCommand::Whitespace) => {} Ok(ShackleCommand::Exit) => { return Ok(ControlFlow::Break(())); } Ok(ShackleCommand::GitInit(GitInitArgs { 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"); if upload_pack_args.strict { command.arg("strict"); } if upload_pack_args.no_strict { command.arg("no-strict"); } if let Some(timeout) = upload_pack_args.timeout { command.args(["timeout", &timeout.to_string()]); } if upload_pack_args.stateless_rpc { command.arg("stateless-rpc"); } if upload_pack_args.advertise_refs { command.arg("advertise-refs"); } // TODO: This should definitely be part of the arg parsing! command.arg(&upload_pack_args.directory.trim_matches('\'')); command.spawn()?.wait()?; } Ok(ShackleCommand::GitReceivePack(receive_pack_args)) => { let mut command = Command::new("git-receive-pack"); if receive_pack_args.http_backend_info_refs { command.arg("--http-backend-info-refs"); } // TODO: This should definitely be part of the arg parsing! command.arg(&receive_pack_args.directory.trim_matches('\'')); command.spawn()?.wait()?; } } Ok(ControlFlow::Continue(())) } fn run_interactive_loop() -> Result<(), ShackleError> { let mut rl = DefaultEditor::new()?; loop { let readline = rl.readline("> "); match readline { Ok(user_input) => { rl.add_history_entry(user_input.as_str())?; match run_command(user_input) { Ok(control_flow) => { if control_flow.is_break() { break; } } Err(e) => { println!("{:?}", e); } } } Err(ReadlineError::Interrupted) => { println!("Interrupted"); break; } Err(ReadlineError::Eof) => { break; } Err(err) => { println!("Error: {:?}", err); break; } } } Ok(()) } pub enum FlowControl {} #[derive(Error, Debug)] pub enum ShackleError { #[error(transparent)] IoError(#[from] io::Error), #[error(transparent)] GitError(#[from] git2::Error), #[error(transparent)] ReadlineError(#[from] ReadlineError), }