forked from trinitrix/core
1
0
Fork 0

feat(app): Add support for a Lua based configuration file

This commit is contained in:
Benedikt Peetz 2023-09-09 21:29:46 +02:00
parent 7aea23f3b6
commit 357c42332f
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
7 changed files with 107 additions and 5 deletions

39
Cargo.lock generated
View File

@ -732,6 +732,27 @@ dependencies = [
"subtle", "subtle",
] ]
[[package]]
name = "directories"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys",
]
[[package]] [[package]]
name = "discard" name = "discard"
version = "1.0.4" version = "1.0.4"
@ -1744,6 +1765,12 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]] [[package]]
name = "ordered-float" name = "ordered-float"
version = "2.10.0" version = "2.10.0"
@ -2044,6 +2071,17 @@ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
] ]
[[package]]
name = "redox_users"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom 0.2.10",
"redox_syscall 0.2.16",
"thiserror",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.9.5" version = "1.9.5"
@ -2677,6 +2715,7 @@ dependencies = [
"clap", "clap",
"cli-log", "cli-log",
"crossterm", "crossterm",
"directories",
"indexmap 2.0.0", "indexmap 2.0.0",
"language_macros", "language_macros",
"matrix-sdk", "matrix-sdk",

View File

@ -29,6 +29,7 @@ crossterm = { version = "0.25", optional = true }
tokio-util = { version = "0.7", optional = true } tokio-util = { version = "0.7", optional = true }
serde = { version = "1.0", optional = true } serde = { version = "1.0", optional = true }
indexmap = { version = "2.0.0", optional = true } indexmap = { version = "2.0.0", optional = true }
directories = "5.0.1"
[profile.release] [profile.release]
lto = true lto = true

14
src/app/config/lua/mod.rs Normal file
View File

@ -0,0 +1,14 @@
use std::path::PathBuf;
use anyhow::Result;
use cli_log::info;
use tokio::{fs, sync::mpsc::Sender};
use crate::app::events::event_types::Event;
pub async fn load(tx: Sender<Event>, path: PathBuf) -> Result<()> {
let lua_config_code = fs::read_to_string(&path).await?;
tx.send(Event::LuaCommand(lua_config_code)).await?;
info!("Loaded config file at `{}`", path.display());
Ok(())
}

1
src/app/config/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod lua;

View File

@ -1,11 +1,13 @@
pub mod command_interface; pub mod command_interface;
pub mod config;
pub mod events; pub mod events;
pub mod status; pub mod status;
use std::path::Path; use std::path::{Path, PathBuf};
use anyhow::{Context, Error, Result}; use anyhow::{Context, Error, Result};
use cli_log::info; use cli_log::{info, warn};
use directories::ProjectDirs;
use matrix_sdk::Client; use matrix_sdk::Client;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
@ -33,6 +35,8 @@ pub struct App<'ui> {
matrix_listener_killer: CancellationToken, matrix_listener_killer: CancellationToken,
lua: LuaCommandManager, lua: LuaCommandManager,
project_dirs: ProjectDirs,
} }
impl App<'_> { impl App<'_> {
@ -57,16 +61,53 @@ impl App<'_> {
matrix_listener_killer: CancellationToken::new(), matrix_listener_killer: CancellationToken::new(),
lua: LuaCommandManager::new(tx), lua: LuaCommandManager::new(tx),
// TODO: We probably want to populate the strings below a bit more <2023-09-09>
project_dirs: ProjectDirs::from("", "", "trinitrix").context(
"Failed to allocate project direcectory paths, \
please ensure your $HOME is set correctly",
)?,
}) })
} }
pub async fn run(&mut self) -> Result<()> { pub async fn run(&mut self, cli_lua_config_file: Option<PathBuf>) -> Result<()> {
// Spawn input event listener // Spawn input event listener
tokio::task::spawn(events::poll_input_events( tokio::task::spawn(events::poll_input_events(
self.tx.clone(), self.tx.clone(),
self.input_listener_killer.clone(), self.input_listener_killer.clone(),
)); ));
let config_dir = self.project_dirs.config_dir();
let mut lua_config_file = None;
if config_dir
.try_exists()
.context("Failed to search for a config file")?
{
lua_config_file = Some(config_dir.join("init.lua"));
info!(
"Found config dir: `{}`",
config_dir.display()
);
} else {
warn!("No config dir found!");
}
if let Some(config_file) = cli_lua_config_file {
config::lua::load(self.tx.clone(), config_file).await?;
} else if let Some(config_file) = lua_config_file {
info!("Searching for config file at `{}`", config_file.display());
if config_file.try_exists().with_context(|| {
format!(
"Failed to check, if the lua config file at `{}` exists",
config_file.display()
)
})? {
config::lua::load(self.tx.clone(), config_file).await?;
} else {
warn!("No config file found!");
}
}
if self.account().is_err() { if self.account().is_err() {
info!("No saved sessions found -> jumping into setup"); info!("No saved sessions found -> jumping into setup");
self.setup().await?; self.setup().await?;

View File

@ -1,3 +1,5 @@
use std::path::PathBuf;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
// TODO: The description could be better // TODO: The description could be better
@ -8,10 +10,14 @@ pub struct Args {
#[command(subcommand)] #[command(subcommand)]
/// The subcommand to execute, default is start /// The subcommand to execute, default is start
pub subcommand: Option<Command>, pub subcommand: Option<Command>,
#[clap(value_parser, long, short)]
/// Path to the Lua config file, executed instead of the normal one
pub lua_config_file: Option<PathBuf>,
} }
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug)]
pub enum Command { pub enum Command {
/// Starts the main tui client
#[clap(value_parser)] #[clap(value_parser)]
/// Starts the main TUI client
Start {}, Start {},
} }

View File

@ -16,7 +16,7 @@ async fn main() -> anyhow::Result<()> {
match command { match command {
Command::Start {} => { Command::Start {} => {
let mut app = app::App::new()?; let mut app = app::App::new()?;
app.run().await?; app.run(args.lua_config_file).await?;
} }
}; };