Finish FVM package
This commit is contained in:
parent
f2bf17fde9
commit
3c390e5396
13 changed files with 178 additions and 28 deletions
26
src/common/fvm/git.rs
Normal file
26
src/common/fvm/git.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
use crate::common::utils::git;
|
||||
|
||||
fn get_prefix_from_arch() -> Option<String> {
|
||||
if std::env::consts::ARCH == "x86_64" {
|
||||
Some(format!("x64"))
|
||||
} else if std::env::consts::ARCH == "x86" {
|
||||
Some(format!("ia32"))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_file_url() -> Result<String, String> {
|
||||
let git_response = git::get_git_latest_release("leoafarias/fvm")
|
||||
.ok_or(format!("Failed to get git release"))?;
|
||||
|
||||
let arch_prefix = get_prefix_from_arch().ok_or(format!("Arch not supported"))?;
|
||||
|
||||
for asset in git_response.assets {
|
||||
if asset.name.contains(std::env::consts::OS) && asset.name.contains(arch_prefix.as_str()) {
|
||||
return Ok(asset.browser_download_url);
|
||||
}
|
||||
}
|
||||
|
||||
Err(format!("OS not supported"))
|
||||
}
|
27
src/common/fvm/mod.rs
Normal file
27
src/common/fvm/mod.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
mod git;
|
||||
|
||||
use crate::common::utils::downloader;
|
||||
use crate::common::utils::extractor;
|
||||
use crate::common::utils::installer;
|
||||
|
||||
// use std::process::Command;
|
||||
|
||||
pub fn install() -> Result<(), String> {
|
||||
println!("Installing FVM");
|
||||
|
||||
let url = git::get_file_url()?;
|
||||
|
||||
let filename = url.split('/').last()
|
||||
.ok_or(format!("Failed to download file"))?;
|
||||
|
||||
let file = downloader::download_file(&url, &filename)
|
||||
.ok_or(format!("Failed to download file"))?;
|
||||
|
||||
extractor::extract_file(&file, "fvm")?;
|
||||
|
||||
installer::install("fvm/fvm", installer::InstallType::Command).ok_or(format!("Failed to install"))?;
|
||||
|
||||
println!("FVM Installed");
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
pub static TEMP_FOLDER: &str = "temp";
|
||||
pub static INSTALL_FOLDER: &str = ".autoconfig";
|
||||
|
||||
pub mod utils;
|
||||
pub mod utils;
|
||||
pub mod fvm;
|
|
@ -6,13 +6,7 @@ use std::{fs::{
|
|||
|
||||
pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> {
|
||||
let file = File::open(&path).ok()?;
|
||||
let mut archive = match zip::ZipArchive::new(file) {
|
||||
Ok(archive) => archive,
|
||||
Err(err) => {
|
||||
println!("[ERROR][EXTRACTOR] {}", err);
|
||||
return None
|
||||
}
|
||||
};
|
||||
let mut archive = zip::ZipArchive::new(file).ok()?;
|
||||
|
||||
let path = path::Path::new(common::TEMP_FOLDER).join(outdir);
|
||||
|
||||
|
@ -22,15 +16,8 @@ pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> {
|
|||
let output_path = path.join(file_path);
|
||||
|
||||
if (&*file.name()).ends_with('/') {
|
||||
println!("File {} extracted to \"{}\"", i, output_path.display());
|
||||
create_dir_all(&output_path).ok()?;
|
||||
} else {
|
||||
println!(
|
||||
"File {} extracted to \"{}\" ({} bytes)",
|
||||
i,
|
||||
output_path.display(),
|
||||
file.size()
|
||||
);
|
||||
if let Some(p) = output_path.parent() {
|
||||
if !p.exists() {
|
||||
create_dir_all(&p).ok()?;
|
||||
|
|
|
@ -9,6 +9,9 @@ pub struct Asset {
|
|||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct GitResponse {
|
||||
pub tag_name: String,
|
||||
pub prerelease: bool,
|
||||
pub draft: bool,
|
||||
pub assets: Vec<Asset>
|
||||
}
|
||||
|
||||
|
|
41
src/common/utils/installer.rs
Normal file
41
src/common/utils/installer.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
use std::path::{PathBuf, Path};
|
||||
use std::{io, fs};
|
||||
|
||||
pub enum InstallType {
|
||||
Command,
|
||||
Config
|
||||
}
|
||||
|
||||
pub fn get_install_dir() -> Result<PathBuf, String> {
|
||||
let home_dir = dirs::home_dir()
|
||||
.ok_or(format!("Failed to get home_directory"))?;
|
||||
|
||||
Ok(home_dir.join(super::super::INSTALL_FOLDER))
|
||||
}
|
||||
|
||||
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
|
||||
fs::create_dir_all(&dst)?;
|
||||
for entry in fs::read_dir(src)? {
|
||||
let entry = entry?;
|
||||
let ty = entry.file_type()?;
|
||||
if ty.is_dir() {
|
||||
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
|
||||
} else {
|
||||
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn install(source_path: &str, install_type: InstallType) -> Option <()> {
|
||||
let source_folder = std::path::Path::new(super::super::TEMP_FOLDER).join(source_path);
|
||||
|
||||
let subfolder = match install_type {
|
||||
InstallType::Command => "bin",
|
||||
InstallType::Config => "configs"
|
||||
};
|
||||
|
||||
let install_folder = get_install_dir().ok()?.join(subfolder);
|
||||
copy_dir_all(source_folder, install_folder).ok()?;
|
||||
Some(())
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
pub mod downloader;
|
||||
pub mod extractor;
|
||||
pub mod git;
|
||||
pub mod git;
|
||||
pub mod installer;
|
Reference in a new issue