diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 9ae0e4a..cbaea05 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -23,3 +23,14 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} args: --all-features + + spell_check: + runs-on: ubuntu-latest + steps: + - name: Checkout Actions Repository + uses: actions/checkout@v2 + + - name: Writes changes in the local checkout + uses: crate-ci/typos@master + with: + write_changes: true diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..aa04737 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,5 @@ +repos: + - repo: https://github.com/crate-ci/typos + rev: v1.14.5 + hooks: + - id: typos diff --git a/README.md b/README.md index ecf53a9..ef8c4cd 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Get the latest binary: It is a single binary. You can just run it, or add it to your path so you can call it from anywhere. Quick way to add it to your path: -or dowload the zip from the releases page +or download the zip from the releases page ``` cd Downloads sudo unzip protonup-rs-linux-amd64.zip -d /usr/bin diff --git a/libprotonup/src/constants.rs b/libprotonup/src/constants.rs index e57511b..0f19392 100644 --- a/libprotonup/src/constants.rs +++ b/libprotonup/src/constants.rs @@ -8,10 +8,12 @@ pub const DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK: &str = "~/.var/app/net.lutris.Lutris/data/lutris/runners/wine/"; pub const TEMP_DIR: &str = "/tmp/"; -pub const GITHUB: &str = "https://api.github.com/repos"; -pub const GITHUB_REPO: &str = "proton-ge-custom"; -pub const LUTRIS_GITHUB_REPO: &str = "wine-ge-custom"; -pub const GITHUB_ACCOUNT: &str = "GloriousEggroll"; +pub const GITHUB_URL: &str = "https://api.github.com/repos"; + +pub const GEPROTON_GITHUB_REPO: &str = "proton-ge-custom"; +pub const WINEGE_GITHUB_REPO: &str = "wine-ge-custom"; +pub const GE_GITHUB_ACCOUNT: &str = "GloriousEggroll"; + pub const USER_AGENT: &str = "protoup-rs"; // pub const CONFIG_FILE: &str = "~/.config/protonup/config.ini"; diff --git a/libprotonup/src/file.rs b/libprotonup/src/file.rs index 6789420..a85f279 100644 --- a/libprotonup/src/file.rs +++ b/libprotonup/src/file.rs @@ -84,7 +84,7 @@ pub fn create_progress_trackers() -> (Arc, Arc) { ) } -pub fn check_if_exists(path: String, tag: String) -> bool { +pub fn check_if_exists(path: &str, tag: &str) -> bool { let f_path = utils::expand_tilde(format!("{path}/{tag}")).unwrap(); let p = std::path::Path::new(&f_path); p.is_dir() diff --git a/libprotonup/src/github.rs b/libprotonup/src/github.rs index b1dcd19..22175bb 100644 --- a/libprotonup/src/github.rs +++ b/libprotonup/src/github.rs @@ -1,4 +1,5 @@ -use super::constants; +use crate::constants; +use crate::parameters::VariantParameters; use anyhow::Result; use serde::{Deserialize, Serialize}; @@ -24,18 +25,12 @@ pub struct Asset { browser_download_url: String, } -pub async fn list_releases(lutris: bool) -> Result { +pub async fn list_releases(source: &VariantParameters) -> Result { let agent = format!("{}/v{}", constants::USER_AGENT, constants::VERSION,); let url = format!( "{}/{}/{}/releases", - constants::GITHUB, - constants::GITHUB_ACCOUNT, - if lutris { - constants::LUTRIS_GITHUB_REPO - } else { - constants::GITHUB_REPO - }, + source.repository_url, source.repository_account, source.repository_name, ); let client = reqwest::Client::builder().user_agent(agent).build()?; @@ -54,7 +49,10 @@ pub struct Download { pub created_at: String, } -pub async fn fetch_data_from_tag(tag: &str, lutris: bool) -> Result { +pub async fn fetch_data_from_tag( + tag: &str, + source: &VariantParameters, +) -> Result { let agent = format!("{}/v{}", constants::USER_AGENT, constants::VERSION,); let client = reqwest::Client::builder().user_agent(agent).build()?; @@ -64,13 +62,7 @@ pub async fn fetch_data_from_tag(tag: &str, lutris: bool) -> Result { let url = format!( "{}/{}/{}/releases/latest", - constants::GITHUB, - constants::GITHUB_ACCOUNT, - if lutris { - constants::LUTRIS_GITHUB_REPO - } else { - constants::GITHUB_REPO - }, + source.repository_url, source.repository_account, source.repository_name, ); let rel: Release = client.get(url).send().await?.json().await?; rel @@ -78,14 +70,7 @@ pub async fn fetch_data_from_tag(tag: &str, lutris: bool) -> Result { let url = format!( "{}/{}/{}/releases/tags/{}", - constants::GITHUB, - constants::GITHUB_ACCOUNT, - if lutris { - constants::LUTRIS_GITHUB_REPO - } else { - constants::GITHUB_REPO - }, - &tag + source.repository_url, source.repository_account, source.repository_name, &tag ); let rel: Release = client.get(url).send().await?.json().await?; rel @@ -110,29 +95,88 @@ pub async fn fetch_data_from_tag(tag: &str, lutris: bool) -> Result data, - Err(e) => { - eprintln!("Error: {}", e); - std::process::exit(1) - } - }; + let result = result.unwrap(); + + assert!( + result.download.len() > 5, + "case : '{}' test: fetch_data_from_tag returned an wrong download link", + desc + ); + assert!( + result.sha512sum.len() > 5, + "case : '{}' test: fetch_data_from_tag returned an wrong sha512sum", + desc + ); + assert!( + result.size > 100, + "case : '{}' test: fetch_data_from_tag returned an wrong sha512sum", + desc + ); + assert!( + result.version.len() > 2, + "case : '{}' test: fetch_data_from_tag returned an wrong version", + desc + ); + } + } - println!("Got result: {:?}", result); + #[tokio::test] + async fn test_list_releases() { + let conditions = &[ + (parameters::Variant::WineGE.parameters(), "List WineGE"), + (parameters::Variant::GEProton.parameters(), "List GEProton"), + ]; + + for (source_parameters, desc) in conditions { + let result = list_releases(source_parameters).await; + + assert!( + result.is_ok(), + "case : '{}' test: fetch_data_from_tag returned error", + desc + ); + + let result = result.unwrap(); + + assert!( + result.len() > 1, + "case : '{}' test: test_list_releases returned an empty list", + desc + ); + } } #[tokio::test] - async fn test_releases() { + async fn test_get_release() { let agent = format!("{}/v{}", constants::USER_AGENT, constants::VERSION,); let client = match reqwest::Client::builder().user_agent(agent).build() { @@ -143,24 +187,32 @@ mod tests { } }; - let url = format!( - "{}/{}/{}/releases/latest", - constants::GITHUB, - constants::GITHUB_ACCOUNT, - constants::LUTRIS_GITHUB_REPO, - ); + let conditions = &[ + (parameters::Variant::WineGE.parameters(), "Get WineGE"), + (parameters::Variant::GEProton.parameters(), "Get GEProton"), + ]; + for (source_parameters, desc) in conditions { + let url = format!( + "{}/{}/{}/releases/latest", + source_parameters.repository_url, + source_parameters.repository_account, + source_parameters.repository_name + ); - let rel: Release = match client.get(url).send().await { - Ok(res) => res, - Err(e) => { - eprintln!("Error: {}", e); - std::process::exit(1) + let rel = match client.get(url).send().await { + Ok(res) => res, + Err(e) => { + panic!("Error: {}", e); + } } - } - .json() - .await - .unwrap(); + .json::() + .await; - println!("Result: {:?}", rel); + assert!( + rel.is_ok(), + "case : '{}' test: test_get_release wrong", + desc + ); + } } } diff --git a/libprotonup/src/lib.rs b/libprotonup/src/lib.rs index bcb2c45..0d59783 100644 --- a/libprotonup/src/lib.rs +++ b/libprotonup/src/lib.rs @@ -1,4 +1,5 @@ pub mod constants; pub mod file; pub mod github; +pub mod parameters; pub mod utils; diff --git a/libprotonup/src/parameters.rs b/libprotonup/src/parameters.rs new file mode 100644 index 0000000..ba98112 --- /dev/null +++ b/libprotonup/src/parameters.rs @@ -0,0 +1,90 @@ +use super::constants::*; +use std::{fmt, str::FromStr}; +// VariantParameters stores the parameters for a variant of Proton +pub struct VariantParameters { + /// this is a link back to the enum variant + variant_ref: Variant, + /// URL of the repository server (GitHub compatible URL only at the moment) + pub repository_url: String, + /// GitHub account for the variant + pub repository_account: String, + /// name of the repository + pub repository_name: String, +} + +impl VariantParameters { + /// new_custom is a generator for custom VariantParameters + pub fn new_custom( + variant: Variant, + repository_url: String, + repository_account: String, + repository_name: String, + ) -> VariantParameters { + VariantParameters { + variant_ref: variant, + repository_url, + repository_account, + repository_name, + } + } + + /// + pub fn variant_type(&self) -> &Variant { + return &self.variant_ref; + } +} + +/// Variant is an enum with all supported "Proton" versions +pub enum Variant { + GEProton, + WineGE, +} + +impl fmt::Display for Variant { + /// returns a string representation of this variant + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Variant::GEProton => write!(f, "GEProton"), + Variant::WineGE => write!(f, "WineGE"), + } + } +} + +impl FromStr for Variant { + type Err = (); + fn from_str(input: &str) -> Result { + match input { + "GEProton" => Ok(Variant::GEProton), + "WineGE" => Ok(Variant::WineGE), + _ => Err(()), + } + } +} + +impl Variant { + /// returns the application target for the Variant. Steam and Lutris are the current options + pub fn intended_application(&self) -> &str { + match self { + Variant::GEProton => "Steam", + Variant::WineGE => "Lutris", + } + } + + /// returns the default parameters for this Variant. + pub fn parameters(&self) -> VariantParameters { + match self { + Variant::GEProton => VariantParameters { + variant_ref: Variant::GEProton, + repository_url: GITHUB_URL.to_owned(), + repository_name: GEPROTON_GITHUB_REPO.to_owned(), + repository_account: GE_GITHUB_ACCOUNT.to_owned(), + }, + Variant::WineGE => VariantParameters { + variant_ref: Variant::WineGE, + repository_url: GITHUB_URL.to_owned(), + repository_name: WINEGE_GITHUB_REPO.to_owned(), + repository_account: GE_GITHUB_ACCOUNT.to_owned(), + }, + } + } +} diff --git a/protonup-rs/Cargo.toml b/protonup-rs/Cargo.toml index 23a13f4..c2694b4 100644 --- a/protonup-rs/Cargo.toml +++ b/protonup-rs/Cargo.toml @@ -12,9 +12,9 @@ description = "TUI Program for Custom Proton Download and installation written i [dependencies] # Use this to use the local changes in libprotonup. -# libprotonup = { path = "../libprotonup" } +libprotonup = { path = "../libprotonup" } # This is necessary to publish to crates.io -libprotonup = { version = "0.4.2" } +# libprotonup = { version = "0.4.2" } inquire = { version = "0.6", default-features = false, features = ["termion"] } indicatif = { version = "0.17", features = [ "improved_unicode", diff --git a/protonup-rs/src/main.rs b/protonup-rs/src/main.rs index a50b296..abd4389 100644 --- a/protonup-rs/src/main.rs +++ b/protonup-rs/src/main.rs @@ -9,26 +9,10 @@ use std::thread; use std::{sync::Arc, time::Duration}; mod file_path; -use libprotonup::{constants, file, github, utils}; +use libprotonup::{constants, file, github, parameters, utils}; #[derive(Debug, Parser)] struct Opt { - // /// install a specific version - // #[structopt(short, long)] - // tag: Option, - // #[structopt(short, long)] - // /// list installed versions - // list: Option, - // /// remove existing installations - // #[structopt(short, long)] - // remove: Option, - // /// set specific output - // #[structopt(short, long)] - // output: Option, - // /// set installation directory - // #[structopt(short, long)] - // dir: Option, - // /// disable prompts and logs /// Skip Menu and download latest directly #[arg(short, long)] quick_download: bool, @@ -39,12 +23,6 @@ struct Opt { lutris_quick_download: bool, #[arg(short = 'L', long)] lutris_quick_download_flatpak: bool, - // /// download only - // #[structopt(long)] - // download: bool, - // /// list available versions - // #[structopt(long)] - // releases: bool, } #[derive(Debug, Copy, Clone)] @@ -130,327 +108,144 @@ fn confirm_menu(text: String) -> bool { #[tokio::main] async fn main() { - let Opt { - // tag, - // list, - // remove, - // output, - // dir, - quick_download, - quick_download_flatpak, - lutris_quick_download, - lutris_quick_download_flatpak, - // download, - // releases, - } = Opt::parse(); - - if quick_download { - download_file("latest", constants::DEFAULT_INSTALL_DIR.to_string(), false) - .await - .unwrap(); + // run quick downloads and skip menu + if run_quick_downloads().await { return; } - if quick_download_flatpak { - download_file( - "latest", - constants::DEFAULT_INSTALL_DIR_FLATPAK.to_string(), - false, - ) - .await - .unwrap(); - return; - } + // Default Parameters + let source: parameters::VariantParameters; + let mut install_dir = constants::DEFAULT_INSTALL_DIR.to_string(); + let mut tags: Vec = vec![String::from("latest")]; - if lutris_quick_download { - download_file( - "latest", - constants::DEFAULT_LUTRIS_INSTALL_DIR.to_string(), - true, - ) - .await - .unwrap(); - return; - } - - if lutris_quick_download_flatpak { - download_file( - "latest", - constants::DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK.to_string(), - true, - ) - .await - .unwrap(); - return; - } + let mut should_open_tag_selector = false; + let mut should_open_dir_selector = false; let answer: Menu = Select::new("ProtonUp Menu: Chose your action:", Menu::VARIANTS.to_vec()) .with_page_size(9) .prompt() .unwrap_or_else(|_| std::process::exit(0)); + // Set parameters based on users choice match answer { Menu::QuickUpdate => { - let tag = match github::fetch_data_from_tag("latest", false).await { - Ok(data) => data, - Err(e) => { - eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); - std::process::exit(1) - } - }; - - if file::check_if_exists( - constants::DEFAULT_INSTALL_DIR.to_owned(), - tag.version.clone(), - ) && !confirm_menu(format!( - "Version {} exists in installation path. Overwrite?", - tag.version - )) { - return; - } - - download_file("latest", constants::DEFAULT_INSTALL_DIR.to_string(), false) - .await - .unwrap(); + source = parameters::Variant::GEProton.parameters(); + install_dir = constants::DEFAULT_INSTALL_DIR.to_owned(); } Menu::QuickUpdateFlatpak => { - let tag = match github::fetch_data_from_tag("latest", false).await { - Ok(data) => data, - Err(e) => { - eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); - std::process::exit(1) - } - }; - - if file::check_if_exists( - constants::DEFAULT_INSTALL_DIR_FLATPAK.to_owned(), - tag.version.clone(), - ) && !confirm_menu(format!( - "Version {} exists in installation path. Overwrite?", - tag.version - )) { - return; - } - - download_file("latest", constants::DEFAULT_INSTALL_DIR_FLATPAK.to_string(), false) - .await - .unwrap(); + source = parameters::Variant::GEProton.parameters(); + install_dir = constants::DEFAULT_INSTALL_DIR_FLATPAK.to_owned(); } - Menu::QuickUpdateLutris => { - let tag = match github::fetch_data_from_tag("latest", true).await { - Ok(data) => data, - Err(e) => { - eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); - std::process::exit(1) - } - }; - - if file::check_if_exists( - constants::DEFAULT_LUTRIS_INSTALL_DIR.to_owned(), - tag.version.clone(), - ) && !confirm_menu(format!( - "Version {} already exists; Overwrite?", - tag.version - )) { - return; - } - - match download_file( - "latest", - constants::DEFAULT_LUTRIS_INSTALL_DIR.to_string(), - true, - ) - .await - { - Ok(_) => {} - Err(e) => { - eprintln!("Error downloading {}, make sure you're connected to the internet\nError: {}", tag.version, e) - } - } + source = parameters::Variant::WineGE.parameters(); + install_dir = constants::DEFAULT_LUTRIS_INSTALL_DIR.to_owned(); } - Menu::QuickUpdateLutrisFlatpak => { - let tag = match github::fetch_data_from_tag("latest", true).await { - Ok(data) => data, - Err(e) => { - eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); - std::process::exit(1) - } - }; - if file::check_if_exists( - constants::DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK.to_owned(), - tag.version.clone(), - ) && !confirm_menu(format!( - "Version {} already exists; Overwrite?", - tag.version - )) { - return; - } - - match download_file( - "latest", - constants::DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK.to_string(), - true, - ) - .await - { - Ok(_) => {} - Err(e) => { - eprintln!("Error downloading {}, make sure you're connected to the internet\nError: {}", tag.version, e) - } - } + source = parameters::Variant::WineGE.parameters(); + install_dir = constants::DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK.to_owned(); } - Menu::ChoseReleases => { - let release_list = match github::list_releases(false).await { - Ok(data) => data, - Err(e) => { - eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); - std::process::exit(1) - } - }; - let tag_list: Vec = release_list.into_iter().map(|r| (r.tag_name)).collect(); - let list = tag_menu(tag_list); - for tag in list.iter() { - if file::check_if_exists(constants::DEFAULT_INSTALL_DIR.to_owned(), tag.to_owned()) - && !confirm_menu(format!( - "Version {tag} exists in installation path. Overwrite?" - )) - { - return; - } - download_file(tag, constants::DEFAULT_INSTALL_DIR.to_string(), false) - .await - .unwrap(); - } + source = parameters::Variant::GEProton.parameters(); + install_dir = constants::DEFAULT_INSTALL_DIR.to_owned(); + should_open_tag_selector = true; } Menu::ChoseReleasesFlatpak => { - let release_list = match github::list_releases(false).await { - Ok(data) => data, - Err(e) => { - eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); - std::process::exit(1) - } - }; - let tag_list: Vec = release_list.into_iter().map(|r| (r.tag_name)).collect(); - let list = tag_menu(tag_list); - for tag in list.iter() { - if file::check_if_exists( - constants::DEFAULT_INSTALL_DIR_FLATPAK.to_owned(), - tag.to_owned(), - ) && !confirm_menu(format!( - "Version {tag} exists in installation path. Overwrite?" - )) { - return; - } - download_file( - tag, - constants::DEFAULT_INSTALL_DIR_FLATPAK.to_string(), - false, - ) - .await - .unwrap(); - } + source = parameters::Variant::GEProton.parameters(); + install_dir = constants::DEFAULT_INSTALL_DIR_FLATPAK.to_owned(); + should_open_tag_selector = true; } Menu::ChoseReleasesCustomDir => { - let current_dir = std::env::current_dir().unwrap(); - let help_message = format!("Current directory: {}", current_dir.to_string_lossy()); - let answer = Text::new("Installation path:") - .with_autocomplete(file_path::FilePathCompleter::default()) - .with_help_message(&help_message) - .prompt(); - - let chosen_path = match answer { - Ok(path) => path, - Err(error) => { - println!("Error choosing custom path. Using the default. Error: {error:?}"); - constants::DEFAULT_INSTALL_DIR.to_string() - } - }; - let release_list = match github::list_releases(false).await { - Ok(data) => data, - Err(e) => { - eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); - std::process::exit(1) - } - }; - let tag_list: Vec = release_list.into_iter().map(|r| (r.tag_name)).collect(); - let list = tag_menu(tag_list); - for tag in list.iter() { - if file::check_if_exists(constants::DEFAULT_INSTALL_DIR.to_owned(), tag.to_owned()) - && !confirm_menu(format!( - "Version {tag} exists in installation path. Overwrite?" - )) - { - return; - } - - download_file(tag, chosen_path.clone(), false) - .await - .unwrap(); - } + source = parameters::Variant::GEProton.parameters(); + should_open_dir_selector = true; + should_open_tag_selector = true; } Menu::ChoseReleasesLutris => { - let release_list = match github::list_releases(true).await { - Ok(data) => data, - Err(e) => { - eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); - std::process::exit(1) - } - }; - let tag_list: Vec = release_list.into_iter().map(|r| (r.tag_name)).collect(); - let list = tag_menu(tag_list); - for tag in list.iter() { - if file::check_if_exists( - constants::DEFAULT_LUTRIS_INSTALL_DIR.to_owned(), - tag.to_owned(), - ) && !confirm_menu(format!( - "Version {tag} exists in installation path. Overwrite?" - )) { - return; - } - download_file(tag, constants::DEFAULT_LUTRIS_INSTALL_DIR.to_string(), true) - .await - .unwrap(); - } + source = parameters::Variant::WineGE.parameters(); + install_dir = constants::DEFAULT_LUTRIS_INSTALL_DIR.to_owned(); + should_open_tag_selector = true; } Menu::ChoseReleasesLutrisFlatpak => { - let release_list = match github::list_releases(true).await { - Ok(data) => data, - Err(e) => { - eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); - std::process::exit(1) - } - }; - let tag_list: Vec = release_list.into_iter().map(|r| (r.tag_name)).collect(); - let list = tag_menu(tag_list); - for tag in list.iter() { - if file::check_if_exists( - constants::DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK.to_owned(), - tag.to_owned(), - ) && !confirm_menu(format!( - "Version {tag} exists in installation path. Overwrite?" - )) { - return; - } - download_file( - tag, - constants::DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK.to_string(), - true, + source = parameters::Variant::WineGE.parameters(); + install_dir = constants::DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK.to_owned(); + should_open_tag_selector = true; + } + } + + // This is where the execution happens + + if should_open_dir_selector { + let current_dir = std::env::current_dir().unwrap(); + let help_message = format!("Current directory: {}", current_dir.to_string_lossy()); + let answer = Text::new("Installation path:") + .with_autocomplete(file_path::FilePathCompleter::default()) + .with_help_message(&help_message) + .prompt(); + + match answer { + Ok(path) => install_dir = path, + Err(error) => { + println!("Error choosing custom path. Using the default. Error: {error:?}"); + } + }; + } + + if should_open_tag_selector { + tags = vec![]; + let release_list = match github::list_releases(&source).await { + Ok(data) => data, + Err(e) => { + eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); + std::process::exit(1) + } + }; + let tag_list: Vec = release_list.into_iter().map(|r| (r.tag_name)).collect(); + let list = tag_menu(tag_list); + for tag_iter in list.iter() { + let tag = String::from(tag_iter); + tags.push(tag); + } + } + + tags.retain(|tag_name| { + // Check if versions exist in disk. + // If they do, ask the user if it should be overwritten + !(file::check_if_exists(&install_dir, &tag_name) + && !confirm_menu(format!( + "Version {tag_name} exists in installation path. Overwrite?" + ))) + }); + + // install the versions that are in the tags array + for tag_name in tags { + let tag = match github::fetch_data_from_tag(&tag_name, &source).await { + Ok(data) => data, + Err(e) => { + eprintln!("Failed to fetch Github data, make sure you're connected to the internet.\nError: {}", e); + std::process::exit(1) + } + }; + + match download_file(&tag_name, &install_dir, &source).await { + Ok(_) => {} + Err(e) => { + eprintln!( + "Error downloading {}, make sure you're connected to the internet\nError: {}", + tag.version, e ) - .await - .unwrap(); } } } } -pub async fn download_file(tag: &str, install_path: String, lutris: bool) -> Result<(), String> { +pub async fn download_file( + tag: &str, + install_path: &str, + source: ¶meters::VariantParameters, +) -> Result<(), String> { let install_dir = utils::expand_tilde(install_path).unwrap(); let mut temp_dir = utils::expand_tilde(constants::TEMP_DIR).unwrap(); - let download = match github::fetch_data_from_tag(tag, lutris).await { + let download = match github::fetch_data_from_tag(tag, &source).await { Ok(data) => data, Err(e) => { eprintln!("Failed to fetch GitHub data, make sure you're connected to the internet\nError: {}", e); @@ -504,7 +299,7 @@ pub async fn download_file(tag: &str, install_path: String, lutris: bool) -> Res thread::sleep(wait_time); } pb.set_message(format!("Downloaded {url} to {tmp_dir}")); - pb.abandon(); // closes progress bas without blanking terminal + pb.abandon(); // closes progress bar without blanking terminal println!("Checking file integrity"); // This is being printed here because the progress bar needs to be closed before printing. }); @@ -523,11 +318,82 @@ pub async fn download_file(tag: &str, install_path: String, lutris: bool) -> Res } println!("Unpacking files into install location. Please wait"); file::decompress(temp_dir.as_path(), install_dir.clone().as_path()).unwrap(); + let source_type = source.variant_type(); println!( - "Done! Restart {}. {} GE installed in {}", - if lutris { "Lutris" } else { "Steam" }, - if lutris { "Wine" } else { "Proton" }, + "Done! Restart {}. {} installed in {}", + source_type.intended_application(), + source_type.to_string(), install_dir.to_string_lossy(), ); Ok(()) } + +async fn run_quick_downloads() -> bool { + let Opt { + quick_download, + quick_download_flatpak, + lutris_quick_download, + lutris_quick_download_flatpak, + } = Opt::parse(); + + if quick_download { + let source = parameters::Variant::GEProton; + let destination = constants::DEFAULT_INSTALL_DIR.to_string(); + println!( + "\nQuick Download: {} / {} into -> {}\n", + source.to_string(), + source.intended_application(), + destination + ); + download_file("latest", &destination, &source.parameters()) + .await + .unwrap(); + } + + if quick_download_flatpak { + let source = parameters::Variant::GEProton; + let destination = constants::DEFAULT_INSTALL_DIR_FLATPAK.to_string(); + println!( + "\nQuick Download: {} / {} into -> {}\n", + source.to_string(), + source.intended_application(), + destination + ); + download_file("latest", &destination, &source.parameters()) + .await + .unwrap(); + } + + if lutris_quick_download { + let source = parameters::Variant::WineGE; + let destination = constants::DEFAULT_LUTRIS_INSTALL_DIR.to_string(); + println!( + "\nQuick Download: {} / {} into -> {}\n", + source.to_string(), + source.intended_application(), + destination + ); + download_file("latest", &destination, &source.parameters()) + .await + .unwrap(); + } + + if lutris_quick_download_flatpak { + let source = parameters::Variant::WineGE; + let destination = constants::DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK.to_string(); + println!( + "\nQuick Download: {} / {} into -> {}\n", + source.to_string(), + source.intended_application(), + destination + ); + download_file("latest", &destination, &source.parameters()) + .await + .unwrap(); + } + + return quick_download + || quick_download_flatpak + || lutris_quick_download + || lutris_quick_download_flatpak; +}