Commit | Line | Data |
---|---|---|
d7c48e38 | 1 | use git2; |
3263e546 | 2 | use std::env; |
3263e546 | 3 | use std::error; |
d7c48e38 JC |
4 | use std::io; |
5 | use std::path; | |
3263e546 | 6 | |
5f1c95cc JC |
7 | fn check_ip() -> Result<(), Box<dyn error::Error>> { |
8 | let remote_ip = env::var("REMOTE_ADDR")?; | |
9 | let whitelist_ip = env::var("WHITELIST_IP")?; | |
10 | if remote_ip == whitelist_ip { | |
11 | Ok(()) | |
12 | } else { | |
13 | Err(Box::new(io::Error::new( | |
14 | io::ErrorKind::ConnectionRefused, | |
15 | format!("Blocked connection from {}", remote_ip), | |
16 | ))) | |
3263e546 JC |
17 | } |
18 | } | |
19 | ||
20 | fn main() -> Result<(), Box<dyn error::Error>> { | |
6802b5ed JC |
21 | check_ip()?; |
22 | let mut repo_name = String::new(); | |
23 | io::stdin().read_line(&mut repo_name)?; | |
24 | // remove trailing newline | |
25 | repo_name.pop(); | |
3263e546 | 26 | |
6802b5ed JC |
27 | let mut opts = git2::RepositoryInitOptions::new(); |
28 | git2::RepositoryInitOptions::bare(&mut opts, true) | |
29 | .mode(git2::RepositoryInitMode::SHARED_GROUP) | |
30 | .no_reinit(true) | |
31 | .template_path(path::Path::new("./templates")); | |
3263e546 | 32 | |
6802b5ed | 33 | let repo = git2::Repository::init_opts(format!("{}.git", repo_name), &opts)?; |
3263e546 | 34 | |
6802b5ed JC |
35 | let public_user = env::var("USER")?; |
36 | let public_name = env::var("PUBLIC")?; | |
37 | let public_path = env::var("PATH")?; | |
38 | repo.remote( | |
39 | "public", | |
40 | &format!( | |
41 | "{user}@{public}:{path}/{repo}.git", | |
42 | user = public_user, | |
43 | public = public_name, | |
44 | path = public_path, | |
45 | repo = repo_name | |
46 | ), | |
47 | )?; | |
48 | Ok(()) | |
3263e546 | 49 | } |