From 2b827a0ab06fb715290a0450a3fff56d3e6f4ee6 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Mon, 20 Mar 2023 23:18:41 +0200 Subject: Put git repos into a user-specific dir --- src/git.rs | 20 ++++++++++++++------ src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 42 +----------------------------------------- src/user.rs | 7 +++++++ 4 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 src/lib.rs create mode 100644 src/user.rs (limited to 'src') diff --git a/src/git.rs b/src/git.rs index b3c41e9..753f53a 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,22 +1,30 @@ -use crate::parser::{GitReceivePackArgs, GitUploadPackArgs}; -use crate::ShackleError; - +use crate::{ + parser::{GitReceivePackArgs, GitUploadPackArgs}, + user::get_username, + ShackleError, +}; use git2::{Repository, RepositoryInitOptions}; use std::{path::PathBuf, process::Command}; -pub fn init(repo_name: &str) -> Result<(), ShackleError> { +pub struct GitInitResult { + pub path: PathBuf, +} + +pub fn init(repo_name: &str) -> Result { + let username = get_username().ok_or(ShackleError::UserReadError)?; let mut path = PathBuf::from("git"); + path.push(username); path.push(repo_name); path.set_extension("git"); Repository::init_opts( - path, + &path, &RepositoryInitOptions::new() .bare(true) .mkdir(true) .no_reinit(true), )?; - Ok(()) + Ok(GitInitResult { path }) } pub fn upload_pack(upload_pack_args: &GitUploadPackArgs) -> Result<(), ShackleError> { diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..b645918 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,43 @@ +pub mod git; +pub mod parser; +pub mod user; + +use parser::*; +use rustyline::error::ReadlineError; +use std::{io, ops::ControlFlow}; +use thiserror::Error; + +pub 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 })) => { + let init_result = git::init(&repo_name)?; + println!("Successfully created \"{}\"", init_result.path.display()); + } + Ok(ShackleCommand::GitUploadPack(upload_pack_args)) => { + git::upload_pack(&upload_pack_args)?; + } + Ok(ShackleCommand::GitReceivePack(receive_pack_args)) => { + git::receive_pack(&receive_pack_args)?; + } + } + Ok(ControlFlow::Continue(())) +} + +#[derive(Error, Debug)] +pub enum ShackleError { + #[error(transparent)] + IoError(#[from] io::Error), + #[error(transparent)] + GitError(#[from] git2::Error), + #[error(transparent)] + ReadlineError(#[from] ReadlineError), + #[error("Could not get the current user name")] + UserReadError, +} diff --git a/src/main.rs b/src/main.rs index 4b62aa1..4333866 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,6 @@ -mod git; -mod parser; - use clap::Parser; -use parser::*; use rustyline::{error::ReadlineError, DefaultEditor}; -use std::{io, ops::ControlFlow}; -use thiserror::Error; +use shackle::{run_command, ShackleError}; /// Shackle Shell - A replacement for git-shell with repo management commands built in. #[derive(Parser, Debug)] @@ -30,29 +25,6 @@ fn main() -> Result<(), ShackleError> { 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)) => { - git::upload_pack(&upload_pack_args)?; - } - Ok(ShackleCommand::GitReceivePack(receive_pack_args)) => { - git::receive_pack(&receive_pack_args)?; - } - } - Ok(ControlFlow::Continue(())) -} - fn run_interactive_loop() -> Result<(), ShackleError> { let mut rl = DefaultEditor::new()?; loop { @@ -86,15 +58,3 @@ fn run_interactive_loop() -> Result<(), ShackleError> { } 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), -} diff --git a/src/user.rs b/src/user.rs new file mode 100644 index 0000000..72aea8d --- /dev/null +++ b/src/user.rs @@ -0,0 +1,7 @@ +pub fn get_username() -> Option { + let uid = nix::unistd::getuid(); + nix::unistd::User::from_uid(uid) + .ok() + .flatten() + .map(|user| user.name) +} -- cgit v1.2.3