Skip to content

Commit

Permalink
add command line option to specify configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
gusevfe authored and tony-sol committed Sep 26, 2024
1 parent 51c84cd commit 4bb20a4
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 8 deletions.
116 changes: 115 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tokio = { version = "1.29.1", features = ["full"] }
async-std = "1.7.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clap = { version = "4.0.0", features = ["derive"] }

[target.'cfg(target_os = "linux")'.dependencies]
pulsectl-rs = "0.3.2"
Expand Down
16 changes: 11 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

#[derive(serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Config {
Expand All @@ -15,7 +17,7 @@ pub struct Device {
pub usage_page: u16,
}

pub fn get_config() -> Config {
pub fn get_config(maybe_path: Option<PathBuf>) -> Config {
let default_config = Config {
device: Device {
vendor_id: 0,
Expand All @@ -27,17 +29,21 @@ pub fn get_config() -> Config {
reconnect_delay: 5000,
};

if let Ok(file) = std::fs::read_to_string("./qmk-hid-host.json") {
let path = maybe_path.unwrap_or("./qmk-hid-host.json".into());

if let Ok(file) = std::fs::read_to_string(&path) {
if let Ok(file_config) = serde_json::from_str::<Config>(&file) {
tracing::info!("Read config from file");
tracing::info!("Read config from file {}", path.to_string_lossy());
return file_config;
} else {
tracing::error!("Error while parsing JSON from file config file");
}

} else {
tracing::error!("Error while reading config from file");
}

let file_content = serde_json::to_string_pretty(&default_config).unwrap();
std::fs::write("./qmk-hid-host.json", &file_content).unwrap();
std::fs::write(&path, &file_content).unwrap();
tracing::info!("New config file created");

return default_config;
Expand Down
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ mod data_type;
mod keyboard;
mod providers;

use std::path::PathBuf;

use std::thread;
use tokio::sync::{broadcast, mpsc};
use config::get_config;
use keyboard::Keyboard;
use clap::Parser;

#[derive(Parser, Debug)]
struct Args {
/// Path to the configuration file
#[arg(short, long)]
config: Option<PathBuf>,
}

#[cfg(not(target_os = "macos"))]
use providers::{_base::Provider, layout::LayoutProvider, time::TimeProvider, media::MediaProvider, volume::VolumeProvider};


#[cfg(target_os = "macos")]
use {
providers::{_base::Provider, layout::LayoutProvider, time::TimeProvider, volume::VolumeProvider},
Expand Down Expand Up @@ -76,7 +86,8 @@ fn main() {
.from_env_lossy();
let tracing_subscriber = tracing_subscriber::fmt().with_env_filter(env_filter).finish();
let _ = tracing::subscriber::set_global_default(tracing_subscriber);
let config = get_config();
let args = Args::parse();
let config = get_config(args.config);

let keyboard = Keyboard::new(config.device, config.reconnect_delay);
let (connected_sender, data_sender) = keyboard.connect();
Expand Down

0 comments on commit 4bb20a4

Please sign in to comment.