summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-03-29 22:59:57 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-03-29 23:01:47 +0200
commit745cd8ab27480a3cf516a2684db2c4fd7cf2144c (patch)
treefeb950d52c3289b5112b81e3d3d6a79f0c94cb49
parent87da80d972c00358b46172ec9661e6a3a307a0a5 (diff)
Check user group when creating shared repos
-rw-r--r--readme.org3
-rw-r--r--src/git.rs13
-rw-r--r--src/lib.rs4
-rw-r--r--src/main.rs4
-rw-r--r--tests/cli.rs11
5 files changed, 26 insertions, 9 deletions
diff --git a/readme.org b/readme.org
index de840d5..31767bf 100644
--- a/readme.org
+++ b/readme.org
@@ -33,7 +33,8 @@ Pijul.
- [-] git init of shared repos
- [X] create the shared repo in the right place
- [X] use the right file permissions and config
- - [ ] don't allow this to be a group the user isn't in
+ - [X] don't allow this to be a group the user isn't in
+ - [ ] allow pull and push from shared repos
- [ ] listing of repos
- [ ] set repo descriptions
- [ ] set the main branch of a repo
diff --git a/src/git.rs b/src/git.rs
index c61bec1..dcdb71b 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -7,7 +7,7 @@ use std::{
path::{Path, PathBuf},
process::Command,
};
-use user_info::get_username;
+use user_info::{get_user_groups, get_username};
pub struct GitInitResult {
pub path: PathBuf,
@@ -18,8 +18,13 @@ fn personal_git_dir() -> Result<PathBuf, ShackleError> {
Ok(Path::new("git").join(username))
}
-fn group_git_dir(group: &str) -> PathBuf {
- Path::new("git").join(group)
+fn group_git_dir(group: &str) -> Result<PathBuf, ShackleError> {
+ let groups = get_user_groups();
+ if !groups.iter().any(|g| g == group) {
+ Err(ShackleError::InvalidGroup)
+ } else {
+ Ok(Path::new("git").join(group))
+ }
}
fn is_valid_personal_git_repo(path: &PathBuf) -> Result<bool, ShackleError> {
@@ -42,7 +47,7 @@ fn is_valid_personal_git_repo(path: &PathBuf) -> Result<bool, ShackleError> {
pub fn init(repo_name: &str, group: &Option<String>) -> Result<GitInitResult, ShackleError> {
fn init_group(repo_name: &str, group: &str) -> Result<GitInitResult, ShackleError> {
- let path = group_git_dir(group).join(repo_name).with_extension("git");
+ let path = group_git_dir(group)?.join(repo_name).with_extension("git");
Repository::init_opts(
&path,
diff --git a/src/lib.rs b/src/lib.rs
index c2eccbf..e96614f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -41,6 +41,6 @@ pub enum ShackleError {
UserReadError,
#[error("Path is not accessible")]
InvalidDirectory,
- #[error("`{0}`")]
- CustomError(String),
+ #[error("Unknown group")]
+ InvalidGroup,
}
diff --git a/src/main.rs b/src/main.rs
index d7a633f..acb6df2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -46,7 +46,7 @@ fn run_interactive_loop() -> Result<(), ShackleError> {
}
}
Err(e) => {
- println!("{:?}", e);
+ println!("{}", e);
}
}
}
@@ -58,7 +58,7 @@ fn run_interactive_loop() -> Result<(), ShackleError> {
break;
}
Err(err) => {
- println!("Error: {:?}", err);
+ println!("Error: {}", err);
break;
}
}
diff --git a/tests/cli.rs b/tests/cli.rs
index a4b36a9..4f8de2f 100644
--- a/tests/cli.rs
+++ b/tests/cli.rs
@@ -162,6 +162,17 @@ fn can_init_a_new_shared_git_repo() -> Result<()> {
}
#[test]
+fn does_not_init_shared_repo_if_the_user_isnt_in_the_group() -> Result<()> {
+ let mut c = spawn_interactive_process()?;
+ let group = "not-a-real-group";
+ let repo_name = "my-new-shared-repo";
+ c.p.send_line(&format!("git-init --group {} {}", group, repo_name))?;
+ c.p.exp_string("Unknown group")?;
+
+ Ok(())
+}
+
+#[test]
fn runs_a_single_command_and_exit_with_cli_flag() -> Result<()> {
let username = get_username().unwrap();
let repo_name = "another-new-repo";