Add 'data_dir' config option
This commit is contained in:
parent
84ff1f068b
commit
e40948567a
3 changed files with 31 additions and 8 deletions
|
|
@ -17,6 +17,7 @@ Procedure when bumping the version number:
|
||||||
### Added
|
### Added
|
||||||
- Config file
|
- Config file
|
||||||
- `ephemeral` config option
|
- `ephemeral` config option
|
||||||
|
- `data_dir` config option
|
||||||
|
|
||||||
## v0.3.0 - 2022-08-22
|
## v0.3.0 - 2022-08-22
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
|
@ -7,6 +7,7 @@ use crate::macros::ok_or_return;
|
||||||
|
|
||||||
#[derive(Debug, Default, Deserialize)]
|
#[derive(Debug, Default, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
pub data_dir: Option<PathBuf>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub ephemeral: bool,
|
pub ephemeral: bool,
|
||||||
}
|
}
|
||||||
|
|
@ -14,6 +15,12 @@ pub struct Config {
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn load(path: &Path) -> Self {
|
pub fn load(path: &Path) -> Self {
|
||||||
let content = ok_or_return!(fs::read_to_string(path), Self::default());
|
let content = ok_or_return!(fs::read_to_string(path), Self::default());
|
||||||
ok_or_return!(toml::from_str(&content), Self::default())
|
match toml::from_str(&content) {
|
||||||
|
Ok(config) => config,
|
||||||
|
Err(err) => {
|
||||||
|
println!("Error loading config file: {err}");
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
27
src/main.rs
27
src/main.rs
|
|
@ -30,8 +30,9 @@ use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use cookie::CookieJar;
|
use cookie::CookieJar;
|
||||||
use directories::ProjectDirs;
|
use directories::{BaseDirs, ProjectDirs};
|
||||||
use log::info;
|
use log::info;
|
||||||
|
use macros::some_or_return;
|
||||||
use toss::terminal::Terminal;
|
use toss::terminal::Terminal;
|
||||||
use ui::Ui;
|
use ui::Ui;
|
||||||
use vault::Vault;
|
use vault::Vault;
|
||||||
|
|
@ -81,6 +82,22 @@ struct Args {
|
||||||
command: Option<Command>,
|
command: Option<Command>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_data_dir(config: &mut Config, args_data_dir: Option<PathBuf>) {
|
||||||
|
let data_dir = some_or_return!(args_data_dir);
|
||||||
|
let data_dir = if let Some(base_dirs) = BaseDirs::new() {
|
||||||
|
base_dirs.home_dir().join(data_dir)
|
||||||
|
} else {
|
||||||
|
data_dir
|
||||||
|
};
|
||||||
|
config.data_dir = Some(data_dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_ephemeral(config: &mut Config, args_ephemeral: bool) {
|
||||||
|
if args_ephemeral {
|
||||||
|
config.ephemeral = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
@ -91,15 +108,13 @@ async fn main() -> anyhow::Result<()> {
|
||||||
.unwrap_or_else(|| dirs.config_dir().join("config.toml"));
|
.unwrap_or_else(|| dirs.config_dir().join("config.toml"));
|
||||||
println!("Config file: {}", config_path.to_string_lossy());
|
println!("Config file: {}", config_path.to_string_lossy());
|
||||||
let mut config = Config::load(&config_path);
|
let mut config = Config::load(&config_path);
|
||||||
|
set_data_dir(&mut config, args.data_dir);
|
||||||
if args.ephemeral {
|
set_ephemeral(&mut config, args.ephemeral);
|
||||||
config.ephemeral = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
let vault = if config.ephemeral {
|
let vault = if config.ephemeral {
|
||||||
vault::launch_in_memory()?
|
vault::launch_in_memory()?
|
||||||
} else {
|
} else {
|
||||||
let data_dir = args
|
let data_dir = config
|
||||||
.data_dir
|
.data_dir
|
||||||
.unwrap_or_else(|| dirs.data_dir().to_path_buf());
|
.unwrap_or_else(|| dirs.data_dir().to_path_buf());
|
||||||
println!("Data dir: {}", data_dir.to_string_lossy());
|
println!("Data dir: {}", data_dir.to_string_lossy());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue