mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-29 03:35:10 +03:00
Add leo add impl with correct imports folder
This commit is contained in:
parent
0dd3e0b3d3
commit
6b8f3d23fa
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1042,6 +1042,7 @@ dependencies = [
|
||||
"snarkos-utilities",
|
||||
"thiserror",
|
||||
"toml",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -43,6 +43,7 @@ serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = { version = "1.0" }
|
||||
toml = { version = "0.5" }
|
||||
thiserror = { version = "1.0" }
|
||||
zip = { version = "0.5" }
|
||||
|
||||
[dev-dependencies]
|
||||
rusty-hook = { version = "0.11.2" }
|
||||
|
@ -11,8 +11,9 @@ use crate::{
|
||||
credentials::*,
|
||||
errors::{AddError::*, CLIError::AddError},
|
||||
};
|
||||
use leo_package::{imports::{ImportsDirectory, IMPORTS_DIRECTORY_NAME}, root::Manifest};
|
||||
|
||||
use std::{collections::HashMap, env::current_dir, fs::File, io::Write};
|
||||
use std::{convert::TryFrom, collections::HashMap, env::current_dir, fs::{File, create_dir_all}, io::{Read, Write}, path::Path};
|
||||
|
||||
pub const ADD_URL: &str = "api/package/fetch";
|
||||
|
||||
@ -49,6 +50,7 @@ impl CLI for AddCommand {
|
||||
const SUBCOMMANDS: &'static [SubCommandType] = &[];
|
||||
|
||||
fn parse(arguments: &clap::ArgMatches) -> Result<Self::Options, crate::errors::CLIError> {
|
||||
// TODO update to new package manager API without an author field
|
||||
if arguments.is_present("author") && arguments.is_present("package_name") {
|
||||
return Ok((
|
||||
arguments.value_of("author").map(|s| s.to_string()),
|
||||
@ -56,14 +58,18 @@ impl CLI for AddCommand {
|
||||
arguments.value_of("version").map(|s| s.to_string()),
|
||||
));
|
||||
} else {
|
||||
return Err(AddError(MissingAuthorOrPackageName));
|
||||
return Ok((None, None, None));
|
||||
}
|
||||
}
|
||||
|
||||
fn output(options: Self::Options) -> Result<Self::Output, crate::errors::CLIError> {
|
||||
let token = read_token()?;
|
||||
|
||||
let (mut result, package_name) = match options {
|
||||
let path = current_dir()?;
|
||||
// Enforce that the current directory is a leo package
|
||||
Manifest::try_from(&path)?;
|
||||
|
||||
let (response, package_name) = match options {
|
||||
(Some(author), Some(package_name), version) => {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let url = format!("{}{}", PACKAGE_MANAGER_URL, ADD_URL);
|
||||
@ -77,7 +83,7 @@ impl CLI for AddCommand {
|
||||
}
|
||||
|
||||
match client.post(&url).bearer_auth(token).json(&json).send() {
|
||||
Ok(result) => (result, package_name),
|
||||
Ok(response) => (response, package_name),
|
||||
//Cannot connect to the server
|
||||
Err(_error) => {
|
||||
return Err(AddError(ConnectionUnavailable(
|
||||
@ -90,16 +96,36 @@ impl CLI for AddCommand {
|
||||
};
|
||||
|
||||
let mut path = current_dir()?;
|
||||
path.push(format!("{}.zip", package_name));
|
||||
ImportsDirectory::create(&path)?;
|
||||
path.push(IMPORTS_DIRECTORY_NAME);
|
||||
path.push(package_name);
|
||||
create_dir_all(&path)?;
|
||||
|
||||
// TODO (raychu86) Display package download progress
|
||||
let mut buffer: Vec<u8> = vec![];
|
||||
result.copy_to(&mut buffer).unwrap();
|
||||
let bytes = response.bytes()?;
|
||||
let reader = std::io::Cursor::new(bytes);
|
||||
|
||||
let mut file = File::create(path)?;
|
||||
file.write_all(&buffer)?;
|
||||
let mut zip_arhive = zip::ZipArchive::new(reader).unwrap();
|
||||
|
||||
log::info!("Successfully added package");
|
||||
for i in 0..zip_arhive.len() {
|
||||
let file = zip_arhive.by_index(i).unwrap();
|
||||
let file_name = file.name();
|
||||
|
||||
let mut file_path = path.clone();
|
||||
file_path.push(file_name);
|
||||
|
||||
if file_name.ends_with("/") {
|
||||
create_dir_all(file_path)?;
|
||||
} else {
|
||||
if let Some(parent_directory) = path.parent() {
|
||||
create_dir_all(parent_directory)?;
|
||||
}
|
||||
|
||||
|
||||
File::create(file_path)?.write_all(&file.bytes().map(|e| e.unwrap()).collect::<Vec<u8>>())?;
|
||||
}
|
||||
}
|
||||
|
||||
log::info!("Successfully added a package");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,9 @@ pub enum CLIError {
|
||||
#[error("{}", _0)]
|
||||
InitError(InitError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ImportsDirectoryError(ImportsDirectoryError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
InputDirectoryError(InputsDirectoryError),
|
||||
|
||||
@ -115,6 +118,13 @@ impl From<InitError> for CLIError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ImportsDirectoryError> for CLIError {
|
||||
fn from(error: ImportsDirectoryError) -> Self {
|
||||
log::error!("{}\n", error);
|
||||
CLIError::ImportsDirectoryError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<InputsDirectoryError> for CLIError {
|
||||
fn from(error: InputsDirectoryError) -> Self {
|
||||
log::error!("{}\n", error);
|
||||
@ -234,6 +244,13 @@ impl From<leo_input::errors::InputParserError> for CLIError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for CLIError {
|
||||
fn from(error:reqwest::Error) -> Self {
|
||||
log::error!("{}\n", error);
|
||||
CLIError::Crate("rewquest", format!("{}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<snarkos_errors::algorithms::snark::SNARKError> for CLIError {
|
||||
fn from(error: snarkos_errors::algorithms::snark::SNARKError) -> Self {
|
||||
log::error!("{}\n", error);
|
||||
|
@ -13,6 +13,9 @@ pub use self::login::*;
|
||||
pub mod new;
|
||||
pub use self::new::*;
|
||||
|
||||
pub mod publish;
|
||||
pub use self::publish::*;
|
||||
|
||||
pub mod run;
|
||||
pub use self::run::*;
|
||||
|
||||
|
7
leo/errors/commands/publish.rs
Normal file
7
leo/errors/commands/publish.rs
Normal file
@ -0,0 +1,7 @@
|
||||
use std::ffi::OsString;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum PublishError {
|
||||
#[error("missing package author")]
|
||||
MissingPackageAuthor,
|
||||
}
|
Loading…
Reference in New Issue
Block a user