add dsx clone command

This commit is contained in:
2026-02-25 22:16:52 +00:00
parent 0d54b319f1
commit c02ecd58e8
3 changed files with 26 additions and 7 deletions
+9
View File
@@ -1,6 +1,7 @@
use std::{env, fs, path::PathBuf, process::Command}; use std::{env, fs, path::PathBuf, process::Command};
use dsx_common::builder::{self, BuildContext}; use dsx_common::builder::{self, BuildContext};
use reqwest::Url;
pub mod new; pub mod new;
pub mod repo; pub mod repo;
@@ -67,6 +68,14 @@ fn main() {
std::process::exit(1); std::process::exit(1);
} }
} }
"clone" => {
let url: Url = Url::parse(&args[2]).unwrap();
let name = url.path_segments().unwrap().next_back().unwrap();
let repo = env::current_dir().unwrap().join(name);
fs::create_dir_all(&repo).unwrap();
repo::clone(&repo, &url).expect("Failed to clone repository");
}
"push" => { "push" => {
if let Some(dir) = find_project_root() { if let Some(dir) = find_project_root() {
repo::push(&dir).expect("Failed to push repository"); repo::push(&dir).expect("Failed to push repository");
+14 -6
View File
@@ -3,8 +3,7 @@ use flate2::read::GzDecoder;
use flate2::{Compression, write::GzEncoder}; use flate2::{Compression, write::GzEncoder};
use reqwest::Url; use reqwest::Url;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::Read; use std::path::Path;
use std::path::{Path, PathBuf};
use tar::Builder; use tar::Builder;
pub fn push(repo_dir: &Path) -> Result<(), DsxError> { pub fn push(repo_dir: &Path) -> Result<(), DsxError> {
@@ -45,16 +44,25 @@ pub fn pull(repo_dir: &Path) -> Result<(), DsxError> {
let config_file = fs::read_to_string(repo_dir.join("Dsx.toml"))?; let config_file = fs::read_to_string(repo_dir.join("Dsx.toml"))?;
let config: DsxConfig = toml::from_str(&config_file).expect("Failed to parse config"); let config: DsxConfig = toml::from_str(&config_file).expect("Failed to parse config");
let mut repo_url = let repo_url =
Url::parse(&config.remote_url.expect( Url::parse(&config.remote_url.expect(
"Repository URL is not set in Dsx.toml, set it with the key 'remote'", "Repository URL is not set in Dsx.toml, set it with the key 'remote'",
)) ))
.unwrap(); .unwrap();
repo_url.path_segments_mut().unwrap().push("pull");
clone(repo_dir, &repo_url)
}
pub fn clone(repo_dir: &Path, url: &Url) -> Result<(), DsxError> {
let mut url = url.clone();
url.path_segments_mut().unwrap().push("pull");
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
let response = client.get(repo_url).send().map_err(|e| { let response = client.get(url).send().map_err(|e| {
DsxError::with_context("failed to stream from client", ErrorType::IoError) DsxError::with_context(
format!("failed to stream from client: {e}"),
ErrorType::IoError,
)
})?; })?;
if !response.status().is_success() { if !response.status().is_success() {
+3 -1
View File
@@ -45,7 +45,9 @@ fn language_colour() -> impl Function {
#[launch] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
dotenv().unwrap(); if dotenv().is_err() {
eprintln!("Failed to load .env file");
}
use routes::api; use routes::api;
use routes::pages; use routes::pages;