diff --git a/src/conf.rs b/src/conf.rs index 888fedb..05eea9a 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1,6 +1,6 @@ use std::fs; use crate::{dirs::paths, CliInstruction}; -use crate::INSTRUCTION; +use crate::{git, INSTRUCTION}; use crate::slint_types::Settings; use serde::{Serialize, Deserialize}; @@ -32,6 +32,19 @@ fn get_cli_config_flags() -> Option<&'static str> { }; } +fn get_cli_repo_path() -> Option<&'static str> { + let instruction = INSTRUCTION.get()?; + + let instruction = instruction.as_ref()?; + + return match instruction { + CliInstruction::Run { repo_path, .. } => + Some(repo_path.as_ref()?.as_str()), + CliInstruction::ListVersions { repo_path } => + Some(repo_path.as_ref()?.as_str()), + }; +} + impl Config { pub fn new() -> Self { Self { @@ -65,6 +78,7 @@ impl Config { let mut cfg = Self::load_from_file(); if let Some(flags) = get_cli_config_flags() { + cfg.use_gui = false; for byte in flags.trim().chars() { match byte { 's' => cfg.sandboxed = false, @@ -83,6 +97,19 @@ impl Config { } } + if let Some(path) = get_cli_repo_path() { + if git::is_repo_valid(path) { + cfg.repo_initialized = true; + } else { + eprintln!("Invalid repository path: {path:?}\n{}", + "Make sure the directory exists and contains a main.lua file and a .git folder." + ); + std::process::exit(1); + } + + cfg.game_repo_path = path.to_string(); + } + return cfg; } pub fn save(&self) { diff --git a/src/main.rs b/src/main.rs index 5e65527..b5eb67b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,6 +115,17 @@ fn main() -> Result<(), slint::PlatformError> { if config.use_gui { main_window::open(&config)?; } else { + let version = get_version_from_cli(); + + if let Some(v) = version { + git::checkout( + &config.game_repo_path, + v + ).expect( + format!("Failed to run `git checkout {v}`").as_str() + ); + } + game::run(&config); } @@ -168,6 +179,18 @@ fn check_dependencies() -> Result<(), Vec> { return Ok(()); } +fn get_version_from_cli() -> Option<&'static str> { + let instruction = INSTRUCTION + .get() + .expect("`INSTRUCTION` static var not set!") + .as_ref()?; + + if let CliInstruction::Run { version, .. } = instruction { + return version.as_deref(); + } + + return None; +} #[deprecated( diff --git a/src/main_window.rs b/src/main_window.rs index fa87034..36aad5f 100644 --- a/src/main_window.rs +++ b/src/main_window.rs @@ -5,7 +5,7 @@ use crate::conf::Config; use crate::game; use crate::git; use crate::error_window; -use crate::slint_types::{MainWindow}; +use crate::slint_types::MainWindow; use slint::{ModelRc, VecModel, SharedString, ModelExt, ComponentHandle}; pub fn open(cfg: &Config) -> Result {