add dsx clone command
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::{env, fs, path::PathBuf, process::Command};
|
||||
|
||||
use dsx_common::builder::{self, BuildContext};
|
||||
use reqwest::Url;
|
||||
|
||||
pub mod new;
|
||||
pub mod repo;
|
||||
@@ -67,6 +68,14 @@ fn main() {
|
||||
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" => {
|
||||
if let Some(dir) = find_project_root() {
|
||||
repo::push(&dir).expect("Failed to push repository");
|
||||
|
||||
+14
-6
@@ -3,8 +3,7 @@ use flate2::read::GzDecoder;
|
||||
use flate2::{Compression, write::GzEncoder};
|
||||
use reqwest::Url;
|
||||
use std::fs::{self, File};
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::Path;
|
||||
use tar::Builder;
|
||||
|
||||
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: DsxConfig = toml::from_str(&config_file).expect("Failed to parse config");
|
||||
|
||||
let mut repo_url =
|
||||
let repo_url =
|
||||
Url::parse(&config.remote_url.expect(
|
||||
"Repository URL is not set in Dsx.toml, set it with the key 'remote'",
|
||||
))
|
||||
.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 response = client.get(repo_url).send().map_err(|e| {
|
||||
DsxError::with_context("failed to stream from client", ErrorType::IoError)
|
||||
let response = client.get(url).send().map_err(|e| {
|
||||
DsxError::with_context(
|
||||
format!("failed to stream from client: {e}"),
|
||||
ErrorType::IoError,
|
||||
)
|
||||
})?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
|
||||
@@ -45,7 +45,9 @@ fn language_colour() -> impl Function {
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
dotenv().unwrap();
|
||||
if dotenv().is_err() {
|
||||
eprintln!("Failed to load .env file");
|
||||
}
|
||||
|
||||
use routes::api;
|
||||
use routes::pages;
|
||||
|
||||
Reference in New Issue
Block a user