use clap::{Parser, Subcommand};
use std::path::PathBuf;
use rkit::commands;
use rkit::commands::ls::WalkerConfig;
use rkit::config;
use rkit::error::RkitResult;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
}
#[derive(Subcommand)]
enum Commands {
Clone {
url: String,
},
Ls {
#[arg(short, long)]
full: bool,
#[arg(long)]
max_depth: Option<usize>,
#[arg(long)]
follow_links: bool,
#[arg(long)]
same_file_system: bool,
#[arg(long)]
threads: Option<usize>,
#[arg(long)]
max_repos: Option<usize>,
#[arg(long)]
no_stop_at_git: bool,
},
View {
path: PathBuf,
},
}
fn main() -> RkitResult<()> {
let args = Cli::parse();
let log_level = match args.verbose {
0 => log::LevelFilter::Warn,
1 => log::LevelFilter::Info,
2 => log::LevelFilter::Debug,
_ => log::LevelFilter::Trace,
};
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(log_level.as_str()))
.format_timestamp(None)
.format_module_path(false)
.format_target(false)
.init();
let project_root = config::Config::load_or_create()?.expand_project_root()?;
match args.command {
Commands::Clone { url } => {
log::info!("Cloning repository: {}", url);
commands::clone::clone(&url, &project_root)
}
Commands::Ls {
full,
max_depth,
follow_links,
same_file_system,
threads,
max_repos,
no_stop_at_git,
} => {
let config = WalkerConfig {
max_depth,
follow_links,
same_file_system,
threads: threads.unwrap_or_else(|| {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
}),
max_repos,
stop_at_git: !no_stop_at_git,
};
commands::ls::list_repos(&project_root, full, Some(config))
}
Commands::View { path } => {
log::info!("Viewing repository: {}", path.display());
let repo_path = if path.is_absolute() {
path
} else {
project_root.join(path)
};
let config = config::Config::load_or_create()?;
commands::view::view_repo(&repo_path, config.rview.as_deref())
}
}
}