Refactor + Remove structopt
This commit is contained in:
parent
988c78b615
commit
c6e652c12b
24 changed files with 163 additions and 338 deletions
|
@ -1,5 +0,0 @@
|
|||
pub static TEMP_FOLDER: &str = "temp";
|
||||
pub static INSTALL_FOLDER: &str = ".autoconfig";
|
||||
|
||||
pub mod utils;
|
||||
pub mod packages;
|
|
@ -1,26 +0,0 @@
|
|||
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"))
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
mod git;
|
||||
|
||||
use crate::common::utils::downloader;
|
||||
use crate::common::utils::extractor;
|
||||
use crate::common::utils::installer;
|
||||
|
||||
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 +0,0 @@
|
|||
pub mod fvm;
|
|
@ -1,5 +0,0 @@
|
|||
pub mod downloader;
|
||||
pub mod extractor;
|
||||
pub mod git;
|
||||
pub mod installer;
|
||||
pub mod configure;
|
70
src/installer/git/mod.rs
Normal file
70
src/installer/git/mod.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
use std::env::consts;
|
||||
|
||||
use super::utils::{configure, downloader, extractor, git, file_utils};
|
||||
|
||||
pub struct GitFileIdentifier {
|
||||
pub os_name: String,
|
||||
pub arch: String,
|
||||
pub os_identifier: String,
|
||||
pub arch_identifier: String,
|
||||
}
|
||||
|
||||
/// Git configuration structure
|
||||
pub struct GitConfig {
|
||||
/// base url from git url (ex: fvm url is https://github.com/leoafarias/fvm so the package name is leoafarias/fvm)
|
||||
pub package: String,
|
||||
/// Must exist in Release of git repo (default to latest)
|
||||
pub version: String,
|
||||
pub git_identifiers: Vec<GitFileIdentifier>,
|
||||
pub archive_subfolder: String,
|
||||
pub install_type: file_utils::InstallType,
|
||||
}
|
||||
|
||||
fn _get_file_url(package: &str, version: &str, git_identifiers: Vec<GitFileIdentifier>) -> Result<String, String> {
|
||||
let git_response = git::get_git_release_by_version(package, version)
|
||||
.ok_or(format!("Failed to get git release"))?;
|
||||
|
||||
let git_identifier = git_identifiers.into_iter()
|
||||
.find(|id| {
|
||||
id.arch == consts::ARCH && (id.os_name == consts::OS || id.os_name == consts::FAMILY)
|
||||
})
|
||||
.ok_or(format!("CPU ARCH or OS not supported"))?;
|
||||
|
||||
for asset in git_response.assets {
|
||||
if asset.name.contains(git_identifier.arch_identifier.as_str()) && asset.name.contains(git_identifier.os_identifier.as_str()) {
|
||||
return Ok(asset.browser_download_url);
|
||||
}
|
||||
}
|
||||
|
||||
Err(format!("{} not support current OS or CPU ARCH", package))
|
||||
}
|
||||
|
||||
fn _install(config: GitConfig) -> Result<(), String> {
|
||||
let url = _get_file_url(config.package.as_str(), config.version.as_str(), config.git_identifiers)?;
|
||||
|
||||
let filename = url.split('/').last()
|
||||
.ok_or(format!("Failed to get name of file"))?;
|
||||
|
||||
let file = downloader::download_file(&url, &filename)
|
||||
.ok_or(format!("Failed to download file"))?;
|
||||
|
||||
extractor::extract_file(&file, config.package.as_str())?;
|
||||
|
||||
let package_temp_folder = std::path::Path::new(config.package.as_str());
|
||||
let extractor_package_temp_folder = package_temp_folder.join(config.archive_subfolder.as_str());
|
||||
let extractor_package_temp_folder_str = extractor_package_temp_folder
|
||||
.as_os_str()
|
||||
.to_str()
|
||||
.ok_or(format!("Failed to get extracted folder"))?;
|
||||
|
||||
file_utils::install(extractor_package_temp_folder_str, config.install_type).ok_or(format!("Failed to install"))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn process(config: GitConfig, config_mode: configure::ConfigMode) -> Result<(), String> {
|
||||
match config_mode {
|
||||
configure::ConfigMode::INSTALL => _install(config),
|
||||
configure::ConfigMode::UNINSTALL => Err(format!("not yet implemented")),
|
||||
}
|
||||
}
|
17
src/installer/mod.rs
Normal file
17
src/installer/mod.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
pub mod git;
|
||||
pub mod utils;
|
||||
|
||||
use utils::configure::ConfigMode;
|
||||
|
||||
use self::git::GitConfig;
|
||||
|
||||
|
||||
pub enum Installer {
|
||||
GIT(GitConfig),
|
||||
}
|
||||
|
||||
pub fn process(installer: Installer, config_mode: ConfigMode) -> Result<(), String> {
|
||||
match installer {
|
||||
Installer::GIT(config) => git::process(config, config_mode),
|
||||
}
|
||||
}
|
|
@ -2,14 +2,14 @@ use std::path::Path;
|
|||
use std::fs::{create_dir_all, remove_dir_all};
|
||||
|
||||
use super::ConfigMode;
|
||||
use super::super::installer;
|
||||
use super::super::file_utils;
|
||||
|
||||
fn folder_exist(folder: &str) -> bool {
|
||||
Path::new(folder).is_dir()
|
||||
}
|
||||
|
||||
pub fn configure_folder(mode: &ConfigMode) -> Option<()> {
|
||||
let binary_folder_path = installer::get_install_dir(installer::InstallType::Root).ok()?;
|
||||
let binary_folder_path = file_utils::get_install_dir(file_utils::InstallType::Root).ok()?;
|
||||
let binary_folder_str = binary_folder_path.to_str()?;
|
||||
|
||||
match mode {
|
|
@ -1,7 +1,7 @@
|
|||
use winreg::RegKey;
|
||||
use winreg::enums::*;
|
||||
use crate::common::utils::installer;
|
||||
use super::ConfigMode;
|
||||
use super::super::super::file_utils;
|
||||
|
||||
fn env_exist(path_env: &str, binary_folder: &str) -> bool {
|
||||
for env in path_env.split(';') {
|
||||
|
@ -17,7 +17,7 @@ pub fn configure_env(mode: &ConfigMode) -> Option<()> {
|
|||
let hklm = RegKey::predef(HKEY_CURRENT_USER);
|
||||
let environment = hklm.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE).ok()?;
|
||||
let mut reg_value : String = environment.get_value("PATH").ok()?;
|
||||
let binary_folder_path = installer::get_install_dir(installer::InstallType::Command).ok()?;
|
||||
let binary_folder_path = file_utils::get_install_dir(file_utils::InstallType::Command).ok()?;
|
||||
let binary_folder_str = binary_folder_path.to_str()?;
|
||||
|
||||
match mode {
|
|
@ -1,14 +1,14 @@
|
|||
use std::{fs, path::PathBuf};
|
||||
|
||||
fn create_download_folder() -> Option<()> {
|
||||
fs::create_dir_all(super::super::TEMP_FOLDER).ok()
|
||||
fs::create_dir_all(super::TEMP_FOLDER).ok()
|
||||
}
|
||||
|
||||
pub fn download_file(url : &String, filename: &str) -> Option<PathBuf> {
|
||||
create_download_folder()?;
|
||||
let mut response = reqwest::blocking::get(url).ok()?;
|
||||
|
||||
let fname = std::path::Path::new(super::super::TEMP_FOLDER).join(filename);
|
||||
let fname = std::path::Path::new(super::TEMP_FOLDER).join(filename);
|
||||
|
||||
let mut dest = fs::File::create(fname.clone()).ok()?;
|
||||
response.copy_to(&mut dest).ok()?;
|
|
@ -1,14 +1,13 @@
|
|||
use std::{fs::File, path::{Path, PathBuf}};
|
||||
use flate2::read::GzDecoder;
|
||||
use tar::Archive;
|
||||
use crate::common;
|
||||
|
||||
|
||||
pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> {
|
||||
let file = File::open(path).ok()?;
|
||||
let tar = GzDecoder::new(file);
|
||||
let mut archive = Archive::new(tar);
|
||||
let path = Path::new(common::TEMP_FOLDER).join(outdir);
|
||||
let path = Path::new(super::super::TEMP_FOLDER).join(outdir);
|
||||
archive.unpack(path).ok()?;
|
||||
|
||||
Some(())
|
|
@ -1,4 +1,3 @@
|
|||
use crate::common;
|
||||
use std::{fs::{
|
||||
File,
|
||||
create_dir_all
|
||||
|
@ -8,7 +7,7 @@ pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> {
|
|||
let file = File::open(&path).ok()?;
|
||||
let mut archive = zip::ZipArchive::new(file).ok()?;
|
||||
|
||||
let path = path::Path::new(common::TEMP_FOLDER).join(outdir);
|
||||
let path = path::Path::new(super::super::TEMP_FOLDER).join(outdir);
|
||||
|
||||
for i in 0..archive.len() {
|
||||
let mut file = archive.by_index(i).ok()?;
|
|
@ -17,7 +17,7 @@ pub fn get_install_dir(install_type: InstallType) -> Result<PathBuf, String> {
|
|||
InstallType::Root => ""
|
||||
};
|
||||
|
||||
Ok(home_dir.join(super::super::INSTALL_FOLDER).join(subfolder))
|
||||
Ok(home_dir.join(super::INSTALL_FOLDER).join(subfolder))
|
||||
}
|
||||
|
||||
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
|
||||
|
@ -35,7 +35,7 @@ fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()>
|
|||
}
|
||||
|
||||
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 source_folder = std::path::Path::new(super::TEMP_FOLDER).join(source_path);
|
||||
|
||||
let install_folder = get_install_dir(install_type).ok()?;
|
||||
copy_dir_all(source_folder, install_folder).ok()?;
|
|
@ -36,7 +36,3 @@ pub fn get_git_release_by_version(repo: &str, version: &str) -> Option<GitRespon
|
|||
|
||||
Some(git_response)
|
||||
}
|
||||
|
||||
pub fn get_git_latest_release(repo: &str) -> Option<GitResponse> {
|
||||
Some(get_git_release_by_version(repo, "latest")?)
|
||||
}
|
8
src/installer/utils/mod.rs
Normal file
8
src/installer/utils/mod.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
pub(self) static TEMP_FOLDER: &str = "temp";
|
||||
pub(self) static INSTALL_FOLDER: &str = ".autoconfig";
|
||||
|
||||
pub mod downloader;
|
||||
pub mod extractor;
|
||||
pub mod git;
|
||||
pub mod file_utils;
|
||||
pub mod configure;
|
|
@ -1,35 +0,0 @@
|
|||
|
||||
use structopt::{
|
||||
StructOpt,
|
||||
clap::{
|
||||
arg_enum
|
||||
}
|
||||
};
|
||||
|
||||
arg_enum! {
|
||||
#[derive(Debug)]
|
||||
enum Tool {
|
||||
Fvm
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "autoconfig", about = "Auto install local config.")]
|
||||
struct Opt {
|
||||
#[structopt(short = "i", long = "install", help = "Available values : fvm (Flutter version manager)")]
|
||||
tools: Vec<Tool>,
|
||||
}
|
||||
|
||||
|
||||
pub fn start() {
|
||||
let opt = Opt::from_args();
|
||||
for tool in &opt.tools {
|
||||
let result = match tool {
|
||||
Tool::Fvm => super::common::packages::fvm::install()
|
||||
};
|
||||
|
||||
if let Err(err) = result {
|
||||
eprintln!("[ERROR] {}", err);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
|
||||
use structopt::{
|
||||
StructOpt,
|
||||
clap::{
|
||||
arg_enum
|
||||
}
|
||||
};
|
||||
|
||||
arg_enum! {
|
||||
#[derive(Debug)]
|
||||
enum Tool {
|
||||
Fvm
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "autoconfig", about = "Auto install local config.")]
|
||||
struct Opt {
|
||||
#[structopt(short = "i", long = "install", help = "Available values : fvm (Flutter version manager)")]
|
||||
tools: Vec<Tool>,
|
||||
}
|
||||
|
||||
|
||||
pub fn start() {
|
||||
let opt = Opt::from_args();
|
||||
for tool in &opt.tools {
|
||||
let result = match tool {
|
||||
Tool::Fvm => super::common::packages::fvm::install()
|
||||
};
|
||||
|
||||
if let Err(err) = result {
|
||||
eprintln!("[ERROR] {}", err);
|
||||
}
|
||||
}
|
||||
}
|
80
src/main.rs
80
src/main.rs
|
@ -1,31 +1,67 @@
|
|||
#[cfg(target_os = "windows")]
|
||||
mod windows;
|
||||
use crate::installer::{
|
||||
Installer,
|
||||
utils::{
|
||||
file_utils::InstallType,
|
||||
configure::{
|
||||
configure,
|
||||
ConfigMode
|
||||
}
|
||||
},
|
||||
git::{
|
||||
GitConfig,
|
||||
GitFileIdentifier
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
mod linux;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
mod macos;
|
||||
|
||||
pub mod common;
|
||||
mod installer;
|
||||
|
||||
fn main() {
|
||||
if let Err(err) = common::utils::configure::configure(&common::utils::configure::ConfigMode::INSTALL) {
|
||||
if let Err(err) = configure(&ConfigMode::INSTALL) {
|
||||
eprintln!("[ERROR] {}", err);
|
||||
return;
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
windows::start();
|
||||
let fvm_config : GitConfig = GitConfig {
|
||||
package: String::from("leoafarias/fvm"),
|
||||
version: String::from("latest"),
|
||||
git_identifiers: vec![
|
||||
GitFileIdentifier {
|
||||
os_name: String::from("windows"),
|
||||
arch: String::from("x86_64"),
|
||||
os_identifier: String::from("windows"),
|
||||
arch_identifier: String::from("x64")
|
||||
},
|
||||
GitFileIdentifier {
|
||||
os_name: String::from("windows"),
|
||||
arch: String::from("x86"),
|
||||
os_identifier: String::from("windows"),
|
||||
arch_identifier: String::from("ia32")
|
||||
},
|
||||
GitFileIdentifier {
|
||||
os_name: String::from("macos"),
|
||||
arch: String::from("x86_64"),
|
||||
os_identifier: String::from("macos"),
|
||||
arch_identifier: String::from("x64")
|
||||
},
|
||||
GitFileIdentifier {
|
||||
os_name: String::from("linux"),
|
||||
arch: String::from("x86_64"),
|
||||
os_identifier: String::from("linux"),
|
||||
arch_identifier: String::from("x64")
|
||||
},
|
||||
GitFileIdentifier {
|
||||
os_name: String::from("linux"),
|
||||
arch: String::from("x86"),
|
||||
os_identifier: String::from("linux"),
|
||||
arch_identifier: String::from("ia32")
|
||||
}
|
||||
],
|
||||
install_type: InstallType::Command,
|
||||
archive_subfolder: String::from("fvm")
|
||||
};
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
linux::start();
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
macos::start();
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
println!("[ERROR] Operating system not supported");
|
||||
if let Err(err) = installer::process(Installer::GIT(fvm_config), ConfigMode::INSTALL) {
|
||||
eprintln!("[ERROR] {}", err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
|
||||
use structopt::{
|
||||
StructOpt,
|
||||
clap::{
|
||||
arg_enum
|
||||
}
|
||||
};
|
||||
|
||||
arg_enum! {
|
||||
#[derive(Debug)]
|
||||
enum Tool {
|
||||
Fvm
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "autoconfig", about = "Auto install local config.")]
|
||||
struct Opt {
|
||||
#[structopt(short = "i", long = "install", help = "Available values : fvm (Flutter version manager)")]
|
||||
tools: Vec<Tool>,
|
||||
}
|
||||
|
||||
|
||||
pub fn start() {
|
||||
let opt = Opt::from_args();
|
||||
for tool in &opt.tools {
|
||||
let result = match tool {
|
||||
Tool::Fvm => super::common::packages::fvm::install()
|
||||
};
|
||||
|
||||
if let Err(err) = result {
|
||||
eprintln!("[ERROR] {}", err);
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue