Skip to content

Commit 41e6bbf

Browse files
committed
feat: add gitkit clone command with auto-init
1 parent 49d5174 commit 41e6bbf

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/clone.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::Result;
22
use clap::{Parser, Subcommand};
33

44
mod attributes;
5+
mod clone;
56
mod config;
67
mod git;
78
mod hooks;
@@ -24,6 +25,8 @@ struct Cli {
2425
enum 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),

0 commit comments

Comments
 (0)