Skip to content

Commit

Permalink
Implemented config! and config_set! macros and using a global
Browse files Browse the repository at this point in the history
configuration store.
  • Loading branch information
jaytaph committed Nov 22, 2023
1 parent c7e9625 commit fd2f4c5
Show file tree
Hide file tree
Showing 11 changed files with 475 additions and 137 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ log = "0.4.20"
domain-lookup-tree = "0.1"
hickory-resolver = "0.24.0"
simple_logger = "4.2.0"
shared_singleton = "0.1.0"
testing_logger = "0.1.1"

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
Expand Down
27 changes: 14 additions & 13 deletions src/bin/config-store.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::anyhow;
use clap::{Parser, Subcommand};
use derive_more::Display;
use gosub_engine::config;
use gosub_engine::config::settings::Setting;
use gosub_engine::config::storage::json_storage::JsonStorageAdapter;
use gosub_engine::config::storage::sqlite_storage::SqliteStorageAdapter;
use gosub_engine::config::{ConfigStore, Store};
use gosub_engine::config::storage::json::JsonStorageAdapter;
use gosub_engine::config::storage::sqlite::SqliteStorageAdapter;
use gosub_engine::config::StorageAdapter;
use std::str::FromStr;

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -74,40 +75,40 @@ struct GlobalOpts {
fn main() -> anyhow::Result<()> {
let args = Cli::parse();

let storage_box: Box<dyn Store> = match args.global_opts.engine {
let storage: Box<dyn StorageAdapter> = match args.global_opts.engine {
Engine::Sqlite => Box::new(SqliteStorageAdapter::try_from(&args.global_opts.path)?),
Engine::Json => Box::new(JsonStorageAdapter::try_from(&args.global_opts.path)?),
};

let mut store = ConfigStore::from_storage(storage_box, true)?;
config::config_store_write().set_storage(storage);

match args.command {
Commands::View { key } => {
if !store.has(&key) {
if !config::config_store().has(&key) {
println!("Key not found");
return Ok(());
}

let info = store.get_info(&key).unwrap();
let value = store.get(&key);
let info = config::config_store().get_info(&key).unwrap();
let value = config::config_store().get(&key).unwrap();

println!("Key : {}", key);
println!("Current Value : {}", value);
println!("Default Value : {}", info.default);
println!("Description : {}", info.description);
}
Commands::List => {
for key in store.find("*") {
let value = store.get(&key);
for key in config::config_store().find("*") {
let value = config::config_store().get(&key).unwrap();
println!("{:40}: {}", key, value);
}
}
Commands::Set { key, value } => {
store.set(&key, Setting::from_str(&value).expect("incorrect value"));
config::config_store().set(&key, Setting::from_str(&value).expect("incorrect value"));
}
Commands::Search { key } => {
for key in store.find(&key) {
let value = store.get(&key);
for key in config::config_store().find(&key) {
let value = config::config_store().get(&key).unwrap();
println!("{:40}: {}", key, value);
}
}
Expand Down
Loading

0 comments on commit fd2f4c5

Please sign in to comment.