mod git; mod parser; use clap::Parser; use parser::Command; use std::{io, io::Write, ops::ControlFlow, process}; 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 prompt() -> Result<(), ShackleError> { print!("> "); io::stdout().flush()?; Ok(()) } fn read_stdin() -> Result { let mut buffer = String::new(); io::stdin().read_line(&mut buffer)?; Ok(buffer) } 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(unknown_input) => { println!("Unknown input \"{}\"", unknown_input); } Ok(Command::Whitespace) => {} Ok(Command::Exit) => { return Ok(ControlFlow::Break(())); } Ok(Command::GitInit(repo_name)) => { git::init(&repo_name)?; // TODO should report this error differently println!("Successfully created {}.git", repo_name); } Ok(Command::GitUploadPack(git_dir)) => { process::Command::new("git") .args(["upload-pack", &git_dir]) .spawn()? .wait()?; } Ok(Command::GitReceivePack(git_dir)) => { process::Command::new("git") .args(["receive-pack", &git_dir]) .spawn()? .wait()?; } } Ok(ControlFlow::Continue(())) } fn run_interactive_loop() -> Result<(), ShackleError> { loop { prompt()?; let user_input = read_stdin()?; // TODO: should this report errors differently? Most of the errors are from user actions. let control_flow = run_command(user_input)?; if control_flow.is_break() { break; } } Ok(()) } pub enum FlowControl {} #[derive(Error, Debug)] pub enum ShackleError { #[error(transparent)] IoError(#[from] io::Error), #[error(transparent)] GitError(#[from] git2::Error), }