Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global config store #267

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 13 additions & 13 deletions src/bin/config-store.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a - in the filename, I've seen this also in the other files in the bin directory now. Everywhere we have underscores and not dashes in the file names. I guess going for consistency is probably good. I'd generally avoid dashes/spaces and so on in file names, I just use a underscore for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally like dashes in binaries and options :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here — I prefer dashes to underscores in binary executable names. E.g., in Linux, the executable name is google-chrome. (For binary executables, even better is a short name with no dashes or underscores.)

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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::*;
use gosub_engine::config::StorageAdapter;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name "store" was a bit ambiguous. They are actually storage adapters since we already have a config store.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would make sense to reexport the StorageAdapters in gosub_engine::config::storage, so you can import them directly with

use gosub_engine::config::storage::*

Then you could also make your json memory and sqlite module private

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sharktheone Can you guve an example on what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, doing this in storage.rs

mod json;
mod memory;
mod sqlite;

pub use json::*;
pub use memory::*;
pub use sqlite::*;

Then you can do this

use gosub_engine::config::storage::*

and still use JsonStorageAdapter and SqliteStorageAdapter. This makes things cleaner. Most of the time when you import one of the storage atapters, you most likely need all.

use std::str::FromStr;

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -74,40 +74,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
Loading