@@ -2,7 +2,7 @@ use crate::repo;
2
2
use anyhow:: { anyhow, Result } ;
3
3
use git2:: { Cred , RemoteCallbacks } ;
4
4
use home;
5
- use log:: debug;
5
+ use log:: { debug, error } ;
6
6
use std:: collections:: HashMap ;
7
7
use std:: { env, fs, path:: Path , path:: PathBuf } ;
8
8
use walkdir:: WalkDir ;
@@ -47,20 +47,36 @@ pub fn sync(root: PathBuf, repos: &HashMap<String, repo::Repo>, _clean_only: &bo
47
47
}
48
48
49
49
// https://docs.rs/git2/latest/git2/build/struct.RepoBuilder.html
50
- // TODO: make the SSH params configurable.
51
50
fn clone_ssh ( url : & str , dst : & Path ) -> Result < ( ) > {
52
51
let mut callbacks = RemoteCallbacks :: new ( ) ;
53
- // TODO: fix this
54
- callbacks. credentials ( |_url, username_from_url, _allowed_types| {
52
+
53
+ callbacks. credentials ( |_url, username, _allowed_types| {
54
+ let mut ssh_privkey = PathBuf :: new ( ) ;
55
+ let mut ssh_privkey_pass = String :: from ( "" ) ;
56
+
57
+ // default
58
+ if let Some ( h) = home:: home_dir ( ) {
59
+ ssh_privkey = h. join ( ".ssh/id_rsa" ) ;
60
+ }
61
+
62
+ // default
63
+ if let Ok ( pw) = env:: var ( "SSH_PRIVKEY_PASS" ) {
64
+ ssh_privkey_pass = pw;
65
+ }
66
+
67
+ if !ssh_privkey. exists ( ) {
68
+ ssh_privkey = PathBuf :: from ( env:: var ( "SSH_PRIVKEY_PATH" ) . expect ( "$HOME/.ssh/id_rsa doesn't exists, you must specify an ssh private key path via SSH_PRIVKEY_PATH" ) ) ;
69
+ if !ssh_privkey. exists ( ) {
70
+ error ! ( "$SSH_PRIVKEY_PATH doesn't exists" ) ;
71
+ }
72
+ }
73
+
55
74
// https://libgit2.org/libgit2/#HEAD/group/credential/git_credential_ssh_key_from_agent
56
75
Cred :: ssh_key (
57
- username_from_url. unwrap ( ) ,
58
- Some ( Path :: new ( & format ! (
59
- "{}/.ssh/fastly_rsa.pub" ,
60
- env:: var( "HOME" ) . unwrap( )
61
- ) ) ) ,
62
- Path :: new ( & format ! ( "{}/.ssh/fastly_rsa" , env:: var( "HOME" ) . unwrap( ) ) ) ,
63
- Some ( env:: var ( "SSH_PASS" ) . unwrap ( ) . as_str ( ) ) ,
76
+ username. unwrap ( ) ,
77
+ None ,
78
+ & ssh_privkey. as_path ( ) ,
79
+ Some ( ssh_privkey_pass. as_str ( ) ) ,
64
80
)
65
81
} ) ;
66
82
0 commit comments