File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ use anyhow:: Result ;
2+ use clap:: Parser ;
3+ use std:: process:: Command ;
4+
5+ #[ derive( Parser ) ]
6+ pub struct CloneArgs {
7+ /// Repository URL or path to clone
8+ repository : String ,
9+
10+ /// Directory to clone into (defaults to repository name)
11+ directory : Option < String > ,
12+
13+ /// Clone specific branch
14+ #[ arg( short, long) ]
15+ branch : Option < String > ,
16+ }
17+
18+ pub fn run ( args : CloneArgs ) -> Result < ( ) > {
19+ let repository = & args. repository ;
20+ let directory = & args. directory ;
21+ let branch = & args. branch ;
22+
23+ println ! ( ) ;
24+ println ! ( " ◇ cloning repository..." ) ;
25+
26+ let mut cmd = Command :: new ( "git" ) ;
27+ cmd. arg ( "clone" ) ;
28+
29+ if let Some ( b) = branch {
30+ cmd. arg ( "--branch" ) . arg ( b) ;
31+ }
32+
33+ cmd. arg ( repository) ;
34+ if let Some ( d) = directory {
35+ cmd. arg ( d) ;
36+ }
37+
38+ let status = cmd
39+ . output ( )
40+ . map_err ( |e| anyhow:: anyhow!( "Failed to run 'git clone': {}" , e) ) ?
41+ . status ;
42+
43+ if !status. success ( ) {
44+ anyhow:: bail!( "git clone failed" ) ;
45+ }
46+
47+ println ! ( " ◇ clone completed ✓" ) ;
48+
49+ let target_dir = if let Some ( d) = directory {
50+ d. to_string ( )
51+ } else {
52+ repository
53+ . split ( '/' )
54+ . last ( )
55+ . unwrap_or ( repository)
56+ . trim_end_matches ( ".git" )
57+ . to_string ( )
58+ } ;
59+
60+ std:: env:: set_current_dir ( & target_dir)
61+ . map_err ( |e| anyhow:: anyhow!( "Failed to enter directory '{}': {}" , target_dir, e) ) ?;
62+
63+ println ! ( ) ;
64+ println ! ( " ◇ running gitkit init..." ) ;
65+ println ! ( ) ;
66+
67+ crate :: init:: run ( ) ?;
68+
69+ Ok ( ( ) )
70+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ use anyhow::Result;
22use clap:: { Parser , Subcommand } ;
33
44mod attributes;
5+ mod clone;
56mod config;
67mod git;
78mod hooks;
@@ -24,6 +25,8 @@ struct Cli {
2425enum Command {
2526 /// Interactive wizard to configure your repo
2627 Init ,
28+ /// Clone repository and run init wizard
29+ Clone ( clone:: CloneArgs ) ,
2730 /// Manage git hooks
2831 Hooks {
2932 #[ command( subcommand) ]
@@ -50,6 +53,7 @@ fn main() -> Result<()> {
5053 let cli = Cli :: parse ( ) ;
5154 match cli. command {
5255 Command :: Init => init:: run ( ) ,
56+ Command :: Clone ( args) => clone:: run ( args) ,
5357 Command :: Hooks { action } => hooks:: run ( action) ,
5458 Command :: Ignore { action } => ignore:: run ( action) ,
5559 Command :: Attributes { action } => attributes:: run ( action) ,
You can’t perform that action at this time.
0 commit comments