Load config file on startup

This commit is contained in:
Joscha 2022-08-25 22:12:29 +02:00
parent 8419afd2e1
commit d61e0ceab7
5 changed files with 49 additions and 12 deletions

View file

@ -14,6 +14,9 @@ Procedure when bumping the version number:
## Unreleased ## Unreleased
### Added
- Config file
## v0.3.0 - 2022-08-22 ## v0.3.0 - 2022-08-22
### Added ### Added

11
Cargo.lock generated
View file

@ -187,11 +187,13 @@ dependencies = [
"log", "log",
"parking_lot", "parking_lot",
"rusqlite", "rusqlite",
"serde",
"serde_json", "serde_json",
"thiserror", "thiserror",
"time", "time",
"tokio", "tokio",
"tokio-tungstenite", "tokio-tungstenite",
"toml",
"toss", "toss",
"unicode-segmentation", "unicode-segmentation",
"unicode-width", "unicode-width",
@ -1173,6 +1175,15 @@ dependencies = [
"webpki", "webpki",
] ]
[[package]]
name = "toml"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "toss" name = "toss"
version = "0.1.0" version = "0.1.0"

View file

@ -14,9 +14,11 @@ edit = "0.1.4"
log = { version = "0.4.17", features = ["std"] } log = { version = "0.4.17", features = ["std"] }
parking_lot = "0.12.1" parking_lot = "0.12.1"
rusqlite = { version = "0.28.0", features = ["bundled", "time"] } rusqlite = { version = "0.28.0", features = ["bundled", "time"] }
serde = { version = "1.0.144", features = ["derive"] }
serde_json = "1.0.85" serde_json = "1.0.85"
thiserror = "1.0.32" thiserror = "1.0.32"
tokio = { version = "1.20.1", features = ["full"] } tokio = { version = "1.20.1", features = ["full"] }
toml = "0.5.9"
unicode-segmentation = "1.9.0" unicode-segmentation = "1.9.0"
unicode-width = "0.1.9" unicode-width = "0.1.9"

16
src/config.rs Normal file
View file

@ -0,0 +1,16 @@
use std::fs;
use std::path::Path;
use serde::Deserialize;
use crate::macros::ok_or_return;
#[derive(Debug, Default, Deserialize)]
pub struct Config {}
impl Config {
pub fn load(path: &Path) -> Self {
let content = ok_or_return!(fs::read_to_string(path), Self::default());
ok_or_return!(toml::from_str(&content), Self::default())
}
}

View file

@ -17,6 +17,7 @@
// TODO Enable warn(unreachable_pub)? // TODO Enable warn(unreachable_pub)?
// TODO Clean up use and manipulation of toss Pos and Size // TODO Clean up use and manipulation of toss Pos and Size
mod config;
mod euph; mod euph;
mod export; mod export;
mod logger; mod logger;
@ -35,6 +36,7 @@ use toss::terminal::Terminal;
use ui::Ui; use ui::Ui;
use vault::Vault; use vault::Vault;
use crate::config::Config;
use crate::logger::Logger; use crate::logger::Logger;
#[derive(Debug, clap::Subcommand)] #[derive(Debug, clap::Subcommand)]
@ -58,6 +60,10 @@ impl Default for Command {
#[derive(Debug, clap::Parser)] #[derive(Debug, clap::Parser)]
#[clap(version)] #[clap(version)]
struct Args { struct Args {
/// Path to the config file.
#[clap(long, short)]
config: Option<PathBuf>,
/// Path to a directory for cove to store its data in. /// Path to a directory for cove to store its data in.
#[clap(long, short)] #[clap(long, short)]
data_dir: Option<PathBuf>, data_dir: Option<PathBuf>,
@ -75,25 +81,24 @@ struct Args {
command: Option<Command>, command: Option<Command>,
} }
fn data_dir(args_data_dir: Option<PathBuf>) -> PathBuf {
if let Some(data_dir) = args_data_dir {
data_dir
} else {
let dirs =
ProjectDirs::from("de", "plugh", "cove").expect("unable to determine directories");
dirs.data_dir().to_path_buf()
}
}
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let args = Args::parse(); let args = Args::parse();
let dirs = ProjectDirs::from("de", "plugh", "cove").expect("unable to determine directories");
let config_path = args
.config
.unwrap_or_else(|| dirs.config_dir().join("config.toml"));
println!("Config file: {}", config_path.to_string_lossy());
let config = Config::load(&config_path);
let vault = if args.ephemeral { let vault = if args.ephemeral {
vault::launch_in_memory()? vault::launch_in_memory()?
} else { } else {
let data_dir = data_dir(args.data_dir); let data_dir = args
println!("Data dir: {}", data_dir.to_string_lossy()); .data_dir
.unwrap_or_else(|| dirs.data_dir().to_path_buf());
println!("Data dir: {}", data_dir.to_string_lossy());
vault::launch(&data_dir.join("vault.db"))? vault::launch(&data_dir.join("vault.db"))?
}; };