Skip to content

Commit

Permalink
✨ Add 'introspect' subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
mleduque committed May 24, 2024
1 parent 8b9553e commit 935b556
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 79 deletions.
5 changes: 5 additions & 0 deletions modda-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ edition = "2021"
ansi_term = "0.12.1"
anyhow = "1.0.83"
clap = "~4.4.18" # https://github.com/rust-build/rust-build.action/issues/88
dialoguer = "0.11.0"
env_logger = "0.11.3"
handlebars = "5.1.2"
itertools = "0.13.0"
log = "0.4.21"
modda-lib = { path = "../modda-lib" }
serde_json = "1.0.117"
serde_yaml = "0.9.34-deprecated"
12 changes: 12 additions & 0 deletions modda-cli/src/log_settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


use log:: LevelFilter;

#[derive(Debug, Clone)]
pub struct LogSettings {
pub max_level: LevelFilter,
pub log_var_name: String,
pub log_var_value: String,
pub log_style_name: String,
pub log_style_value: String,
}
33 changes: 23 additions & 10 deletions modda-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@

mod log_settings;
mod subcommands;

use anyhow::{bail, Result};
use clap::Parser;
use env_logger::{Env, Target};
use log::debug;

use log_settings::LogSettings;
use modda_lib::args::{ Cli, Commands };
use modda_lib::cache::Cache;
use modda_lib::canon_path::CanonPath;
use modda_lib::chitin::ensure_chitin_key;
use modda_lib::modda_context::WeiduContext;
use modda_lib::run_weidu::check_weidu_exe;
use modda_lib::config::read_settings;
use modda_lib::config::{global_conf_dir, Settings};
use modda_lib::sub::append_mod::append_mod;
use modda_lib::sub::discover::discover;
use modda_lib::sub::extract_manifest::extract_manifest;
use modda_lib::sub::install::install;
use modda_lib::sub::invalidate::invalidate;
use modda_lib::sub::list_components::sub_list_components;
use modda_lib::sub::reset::reset;
use modda_lib::sub::search::search;


use subcommands::discover::discover;
use subcommands::introspect::introspect;
use subcommands::list_components::sub_list_components;
use subcommands::reset::reset;
use subcommands::search::search;

fn main() -> Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info"))
.target(Target::Stdout)
.init();

let log_settings = LogSettings {
max_level: log::max_level(),
log_var_name: "RUST_LOG".to_string(),
log_var_value: std::env::var("RUST_LOG").unwrap_or("<not present>".to_string()),
log_style_name: "RUST_LOG_STYLE".to_string(),
log_style_value: std::env::var("RUST_LOG_STYLE").unwrap_or("<not present>".to_string()),
};

let cli = Cli::parse();

let current_dir = std::env::current_dir()?;
Expand All @@ -38,10 +48,11 @@ fn main() -> Result<()> {
} else {
debug!("chitin.key found");
}
let config = read_settings()?;
let settings = Settings::read_settings(&current_dir)?;
let config = &settings.combined;
let weidu_context = WeiduContext{ config: &config, current_dir: &current_dir };
check_weidu_exe(&weidu_context)?;
let cache = Cache::ensure_from_config(&config).unwrap();
let cache = Cache::ensure_from_config(config).unwrap();

match cli.command {
Commands::Install(ref install_opts) => install(install_opts, &config, &current_dir, &cache),
Expand All @@ -52,6 +63,8 @@ fn main() -> Result<()> {
Commands::AppendMod(ref params) => append_mod(params, &weidu_context),
Commands::Reset(ref reset_args) => reset(reset_args, &weidu_context),
Commands::Discover(ref params) => discover(params, &weidu_context),
Commands::Introspect(ref params) => introspect(params, &settings, &current_dir,
&global_conf_dir(),
&log_settings),
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ use std::io::BufWriter;
use anyhow::{bail, Result};
use log::{debug, info};

use crate::args::Discover;
use crate::modda_context::WeiduContext;
use crate::module::components::Components;
use crate::module::module::Module;
use crate::module::weidu_mod::WeiduMod;
use crate::tp2::find_game_tp2;

use super::extract_manifest::generate_manifest;

use modda_lib::args::Discover;
use modda_lib::modda_context::WeiduContext;
use modda_lib::module::components::Components;
use modda_lib::module::module::Module;
use modda_lib::module::weidu_mod::WeiduMod;
use modda_lib::sub::extract_manifest::generate_manifest;
use modda_lib::tp2::find_game_tp2;

pub fn discover(params: &Discover, weidu_context: &WeiduContext) -> Result<()> {
let game_dir = weidu_context.current_dir;
Expand Down
94 changes: 94 additions & 0 deletions modda-cli/src/subcommands/introspect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::path::PathBuf;

use anyhow::Result;
use handlebars::Handlebars;
use serde_json::json;

use modda_lib::args::Introspect;
use modda_lib::canon_path::CanonPath;
use modda_lib::config::{ConfigSource, Settings, ARCHIVE_CACHE_ENV_VAR, EXTRACT_LOCATION_ENV_VAR, IGNORE_CURRENT_DIR_WEIDU_ENV_VAR, WEIDU_PATH_ENV_VAR};

use crate::log_settings::LogSettings;


static TEMPLATE : &'static str =
r#"
Global config directory: {{global_config_dir}}
Found global config file: {{global_config_file}}
Game dir: {{game_dir}}
Found local config file: {{local_config_file}}
Config options from environment variables:
{{environment}}
Concrete config:
{{concrete_config}}
Log settings:
max level: {{max_level}}
{{log_var_name}}="{{log_var_value}}"
{{log_style_name}}="{{log_style_value}}"
"#;

pub fn introspect(params:&Introspect, settings: &Settings, game_dir: &CanonPath,
global_conf_dir: &Option<PathBuf>, log_settings: &LogSettings) -> Result<()> {
let registry = Handlebars::new();

let context = &json!({
"game_dir": game_dir.to_path_buf().as_os_str().to_string_lossy(),
"global_config_dir": match global_conf_dir {
None => "undetermined".to_string(),
Some(value) => value.as_os_str().to_string_lossy().to_string(),
},
"global_config_file": match &settings.global {
None => "no".to_string(),
Some(file_name) => file_name.id.clone(),
},
"local_config_file": match &settings.local {
None => "no".to_string(),
Some(file_name) => file_name.id.clone(),
},
"environment": display_environment(&settings.env_config)
.iter()
.map(|(key, value)| format!(r#"{key} = "{value}""#))
.collect::<Vec<_>>()
.join("\n"),
"concrete_config": match serde_yaml::to_string(&settings.combined) {
Ok(value) => value,
Err(error) => format!("[error, could not serialize configuration:\n{error}]")
},
"max_level": log_settings.max_level.to_string(),
"log_var_name": log_settings.log_var_name,
"log_var_value": log_settings.log_var_value,
"log_style_name": log_settings.log_style_name,
"log_style_value": log_settings.log_style_value,
});
println!("{}", registry.render_template(TEMPLATE, context)?);
Ok(())
}

const TRUE: &'static str = "true";
const FALSE: &'static str = "false";

fn display_environment(env_config: &ConfigSource) -> Vec<(String, String)> {
vec![
(ARCHIVE_CACHE_ENV_VAR, &env_config.config.as_ref().and_then(|value| value.archive_cache.to_owned())),
(EXTRACT_LOCATION_ENV_VAR, &env_config.config.as_ref().and_then(|value| value.extract_location.to_owned())),
(WEIDU_PATH_ENV_VAR, &env_config.config.as_ref().and_then(|value| value.weidu_path.to_owned())),
(IGNORE_CURRENT_DIR_WEIDU_ENV_VAR, &env_config.config.as_ref()
.and_then(|value|
value.ignore_current_dir_weidu.map(|value|
(if value { TRUE } else { FALSE }).to_owned()
)
)
),
].into_iter()
.filter_map(|(key, value)|
match value {
None => None,
Some(s) if s.trim() == "" => None,
Some(value) => Some((key.to_owned(), value.to_owned()))
}
).collect()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
use anyhow::{bail, Result};
use itertools::Itertools;

use crate::args::ListComponents;
use crate::list_components::list_components;
use crate::modda_context::WeiduContext;
use modda_lib::args::ListComponents;
use modda_lib::list_components::list_components;
use modda_lib::modda_context::WeiduContext;

pub fn sub_list_components(param: &ListComponents, weidu_context: &WeiduContext) -> Result<()> {
match list_components(&param.module_name, param.lang, weidu_context) {
Expand Down
6 changes: 6 additions & 0 deletions modda-cli/src/subcommands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

pub mod discover;
pub mod introspect;
pub mod list_components;
pub mod reset;
pub mod search;
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use anyhow::{Result, bail};
use itertools::Itertools;
use log::info;

use crate::args::Reset;
use crate::modda_context::WeiduContext;
use crate::module::components::Components;
use crate::module::manifest::Manifest;
use crate::module::module::Module;
use crate::run_weidu::run_weidu_uninstall;
use crate::sub::extract_manifest::extract_bare_mods;
use crate::tp2::find_tp2_str;
use modda_lib::args::Reset;
use modda_lib::modda_context::WeiduContext;
use modda_lib::module::components::Components;
use modda_lib::module::manifest::Manifest;
use modda_lib::module::module::Module;
use modda_lib::run_weidu::run_weidu_uninstall;
use modda_lib::sub::extract_manifest::extract_bare_mods;
use modda_lib::tp2::find_tp2_str;


pub fn reset(args: &Reset, weidu_context: &WeiduContext) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@


use anyhow::{Result};
use anyhow::Result;

use crate::args::Search;
use crate::module::manifest::Manifest;
use modda_lib::args::Search;
use modda_lib::module::manifest::Manifest;


pub fn search(opts: &Search) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion modda-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ handlebars = "5.1.2"
humantime = "2.1.0"
indicatif = "0.17.8"
indoc ="2.0.5"
itertools = "0.12.1"
itertools = "0.13.0"
lazy_static = "1.4.0"
log = "0.4.21"
patch = { path = "../patch-rs" }
Expand Down
9 changes: 9 additions & 0 deletions modda-lib/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum Commands {
Reset(Reset),
/// Discovers mods in the game directory and builds a manifest skeleton.
Discover(Discover),
/// Show configuration/settings information.
Introspect(Introspect)
}

#[derive(Args, Debug, Default)]
Expand Down Expand Up @@ -193,3 +195,10 @@ pub struct Discover {
#[arg(long, short)]
pub output: String,
}

#[derive(Args, Debug)]
pub struct Introspect {
/// Name of the file that will be generated.
#[arg(long, short)]
pub show_config: bool,
}
Loading

0 comments on commit 935b556

Please sign in to comment.