Add optional repository url to init to clone a remote

main
trivernis 10 months ago
parent 769fa3fa4a
commit dc4d260572
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: DFFFCC2C7A02DB45

710
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -16,7 +16,7 @@ clap = { version = "4.4.17", features = ["derive", "env"] }
dialoguer = "0.11.0" dialoguer = "0.11.0"
dirs = "5.0.1" dirs = "5.0.1"
figment = { version = "0.10.13", features = ["toml", "env"] } figment = { version = "0.10.13", features = ["toml", "env"] }
gix = { version = "0.57.1", default-features = false, features = ["basic", "index", "worktree-mutation", "revision"] } gix = { version = "0.57.1", default-features = false, features = ["basic", "index", "worktree-mutation", "revision", "blocking-network-client", "prodash", "blocking-http-transport-reqwest-rust-tls"] }
globset = { version = "0.4.14", features = ["serde", "serde1"] } globset = { version = "0.4.14", features = ["serde", "serde1"] }
handlebars = "5.0.0" handlebars = "5.0.0"
handlebars_switch = "0.6.0" handlebars_switch = "0.6.0"

@ -17,7 +17,7 @@ pub struct Args {
#[derive(Clone, Debug, Subcommand)] #[derive(Clone, Debug, Subcommand)]
pub enum Command { pub enum Command {
/// Initialize a silo repository /// Initialize a silo repository
Init, Init(InitArgs),
/// Applies the configuration stored in a silo repo /// Applies the configuration stored in a silo repo
Apply, Apply,
@ -28,6 +28,13 @@ pub enum Command {
Repo, Repo,
} }
#[derive(Clone, Debug, Parser)]
pub struct InitArgs {
/// Init using a remote repository
#[arg()]
pub remote: Option<String>,
}
fn default_repo() -> &'static str { fn default_repo() -> &'static str {
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref DEFAULT_REPO: String = dirs::data_dir() static ref DEFAULT_REPO: String = dirs::data_dir()

@ -1,7 +1,8 @@
use std::fs; use std::{fs, sync::atomic::AtomicBool};
use args::Args; use args::{Args, InitArgs};
use clap::Parser; use clap::Parser;
use gix::progress::Discard;
use miette::{Context, IntoDiagnostic, Result}; use miette::{Context, IntoDiagnostic, Result};
use repo::SiloRepo; use repo::SiloRepo;
@ -15,7 +16,7 @@ fn main() -> Result<()> {
init_logging(args.verbose); init_logging(args.verbose);
match &args.command { match &args.command {
args::Command::Init => init(&args)?, args::Command::Init(init_args) => init(&args, &init_args)?,
args::Command::Apply => apply(&args)?, args::Command::Apply => apply(&args)?,
args::Command::Context => { args::Command::Context => {
let repo = SiloRepo::open(&args.repo)?; let repo = SiloRepo::open(&args.repo)?;
@ -41,7 +42,9 @@ fn init_logging(verbose: bool) {
builder.filter_level(log::LevelFilter::Info) builder.filter_level(log::LevelFilter::Info)
}; };
builder builder
.filter_module("handlebars", log::LevelFilter::Off) .filter_module("handlebars", log::LevelFilter::Error)
.filter_module("rustls", log::LevelFilter::Error)
.filter_module("", log::LevelFilter::Error)
.init(); .init();
} }
@ -53,7 +56,15 @@ fn apply(args: &Args) -> Result<()> {
Ok(()) Ok(())
} }
fn init(args: &Args) -> Result<()> { fn init(args: &Args, init_args: &InitArgs) -> Result<()> {
if let Some(remote) = init_args.remote.as_ref() {
init_remote(args, init_args, remote)
} else {
init_local(args)
}
}
fn init_local(args: &Args) -> Result<()> {
if !args.repo.exists() { if !args.repo.exists() {
fs::create_dir_all(&args.repo) fs::create_dir_all(&args.repo)
.into_diagnostic() .into_diagnostic()
@ -70,3 +81,21 @@ fn init(args: &Args) -> Result<()> {
Ok(()) Ok(())
} }
fn init_remote(args: &Args, _init_args: &InitArgs, remote: &str) -> Result<()> {
log::info!("Cloning {remote} into {:?}", args.repo);
let interrupt = AtomicBool::new(false);
gix::prepare_clone(remote, &args.repo)
.into_diagnostic()
.context("clone repo")?
.fetch_then_checkout(Discard, &interrupt)
.into_diagnostic()
.context("fetch repo")?
.0
.main_worktree(Discard, &interrupt)
.into_diagnostic()
.context("checkout main")?
.0;
log::info!("Repo initialized at {:?}", args.repo);
Ok(())
}

Loading…
Cancel
Save