Skip to content

Commit

Permalink
Work on CLI arg logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Not-A-Normal-Robot committed Oct 31, 2024
1 parent fef6d10 commit 2f06cb9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/conf.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -168,6 +179,18 @@ fn check_dependencies() -> Result<(), Vec<String>> {
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(
Expand Down
2 changes: 1 addition & 1 deletion src/main_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MainWindow, slint::PlatformError> {
Expand Down

0 comments on commit 2f06cb9

Please sign in to comment.