Skip to content

Commit

Permalink
feat(log_mode): Add weidu log mode, closes #84 (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
dark0dave authored Jul 3, 2024
2 parents 7df361f + 45c5912 commit 29734c6
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,30 @@ Usage: mod_installer [OPTIONS] --log-file <LOG_FILE> \
--mod-directories <MOD_DIRECTORIES>

Options:
--log-file <LOG_FILE>
Full path to target log [env: LOG_FILE=]
-f, --log-file <LOG_FILE>
Full path to target log [env: LOG_FILE=]
-g, --game-directory <GAME_DIRECTORY>
Full path to game directory [env: GAME_DIRECTORY=]
Full path to game directory [env: GAME_DIRECTORY=]
-w, --weidu-binary <WEIDU_BINARY>
Full Path to weidu binary [env: WEIDU_BINARY=]
Full Path to weidu binary [env: WEIDU_BINARY=]
-m, --mod-directories <MOD_DIRECTORIES>
Full Path to mod directories [env: MOD_DIRECTORIES=]
Full Path to mod directories [env: MOD_DIRECTORIES=]
-l, --language <LANGUAGE>
Game Language [default: en_US]
Game Language [default: en_US]
-d, --depth <DEPTH>
Depth to walk folder structure [env: DEPTH=] [default: 3]
Depth to walk folder structure [env: DEPTH=] [default: 3]
-s, --skip-installed
Compare against installed weidu log, note this is best effort [env: SKIP_INSTALLED=]
Compare against installed weidu log, note this is best effort [env: SKIP_INSTALLED=]
-a, --abort-on-warnings
If a warning occurs in the weidu child process exit [env: ABORT_ON_WARNINGS=]
If a warning occurs in the weidu child process exit [env: ABORT_ON_WARNINGS=]
-t, --timeout <TIMEOUT>
Timeout time per mod in seconds, default is 1 hour [env: TIMEOUT=] [default: 3600]
Timeout time per mod in seconds, default is 1 hour [env: TIMEOUT=] [default: 3600]
-u, --weidu-log-mode <WEIDU_LOG_MODE>
Full path to debug log for weidu [env: WEIDU_LOG_MODE=] [default: --autolog]
-h, --help
Print help
Print help
-V, --version
Print version
Print version
```

## Log levels
Expand Down
73 changes: 72 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ use std::path::{Path, PathBuf};

use clap::{ArgAction, Parser};

const WEIDU_LOG_MODE_ERROR: &str = r"
Please provide a valid weidu logging setting, options are:
--weidu-log-mode log X log output and details to X
--weidu-log-mode autolog log output and details to WSETUP.DEBUG
--weidu-log-mode logapp append to log instead of overwriting
--weidu-log-mode log-extern also log output from commands invoked by WeiDU
";

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
/// Full path to target log
#[clap(env, long, required = true)]
#[clap(env, long, short = 'f', required = true)]
pub log_file: PathBuf,

/// Full path to game directory
Expand Down Expand Up @@ -48,6 +56,32 @@ pub struct Args {
/// Timeout time per mod in seconds, default is 1 hour
#[clap(env, long, short, default_value = "3600")]
pub timeout: usize,

/// Full path to debug log for weidu
#[clap(env, long, short='u', default_value = "--autolog", value_parser = parse_weidu_log_mode, required = false)]
pub weidu_log_mode: String,
}

fn parse_weidu_log_mode(arg: &str) -> Result<String, String> {
let mut args = arg.split(' ');
let mut output = vec![];
while let Some(arg) = args.next() {
match arg {
"log"
if Path::new(args.clone().next().unwrap_or("").trim())
.parent()
.is_some() =>
{
let path = args.next().unwrap();
output.push(format!("--{arg} {path}"));
}
"autolog" => output.push(format!("--{arg}")),
"logapp" => output.push(format!("--{arg}")),
"log-extern" => output.push(format!("--{arg}")),
_ => return Err(format!("{}Provided {}", WEIDU_LOG_MODE_ERROR, arg)),
};
}
Ok(output.join(" "))
}

fn parse_absolute_path(arg: &str) -> Result<PathBuf, String> {
Expand All @@ -58,3 +92,40 @@ fn parse_absolute_path(arg: &str) -> Result<PathBuf, String> {
Err("Please provide the absolute path".to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn test_parse_weidu_log_mode() {
let tests = vec![
("autolog", Ok("--autolog".to_string())),
("log /home", Ok("--log /home".to_string())),
("autolog logapp", Ok("--autolog --logapp".to_string())),
(
"autolog logapp log-extern",
Ok("--autolog --logapp --log-extern".to_string()),
),
(
"log /home logapp log-extern",
Ok("--log /home --logapp --log-extern".to_string()),
),
(
"fish",
Err(format!("{}Provided {}", WEIDU_LOG_MODE_ERROR, "fish")),
),
(
"log /home fish",
Err(format!("{}Provided {}", WEIDU_LOG_MODE_ERROR, "fish")),
),
];
for (test, expected) in tests {
let result = parse_weidu_log_mode(test);
assert_eq!(
result, expected,
"Result {result:?} didn't match Expected {expected:?}",
);
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn main() {
&args.game_directory,
&weidu_mod,
&args.language,
&args.weidu_log_mode,
args.timeout,
) {
InstallationResult::Fail(message) => {
Expand Down
8 changes: 5 additions & 3 deletions src/weidu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ pub fn get_user_input() -> String {
input.to_string()
}

fn generate_args(weidu_mod: &ModComponent, language: &str) -> Vec<String> {
format!("{mod_name}/{mod_tp_file} --autolog --force-install {component} --use-lang {game_lang} --language {mod_lang}",
fn generate_args(weidu_mod: &ModComponent, weidu_log_mode: &str, language: &str) -> Vec<String> {
format!("{mod_name}/{mod_tp_file} {weidu_log_mode} --force-install {component} --use-lang {game_lang} --language {mod_lang}",
mod_name = weidu_mod.name,
mod_tp_file = weidu_mod.tp_file,
weidu_log_mode = weidu_log_mode,
component = weidu_mod.component,
mod_lang = weidu_mod.lang,
game_lang = language
Expand All @@ -46,9 +47,10 @@ pub fn install(
game_directory: &PathBuf,
weidu_mod: &ModComponent,
language: &str,
weidu_log_mode: &str,
timeout: usize,
) -> InstallationResult {
let weidu_args = generate_args(weidu_mod, language);
let weidu_args = generate_args(weidu_mod, weidu_log_mode, language);
let mut command = Command::new(weidu_binary);
let weidu_process = command.current_dir(game_directory).args(weidu_args);

Expand Down

0 comments on commit 29734c6

Please sign in to comment.