···1616anyhow = "1.0.75"
1717dirs = "5.0.1"
1818serde_json = "1.0.108"
1919+serde = { version = "1.0.203", features = ["derive"] }
2020+tokio = { version = "1.38.0", features = ["full"] }
2121+sqlx = { version = "0.7.4", features = ["runtime-tokio", "sqlite"] }
19222023# The profile that 'cargo dist' will build with
2124[profile.dist]
+5-1
src/cli.rs
···11//! CLI for using the Safir binary
22-pub use clap::{Args, Parser, Subcommand};
22+use crate::store::SafirMode;
33+pub use clap::{Parser, Subcommand};
3445/// CLI arguments for running the program
56#[derive(Parser, Debug)]
···4748 /// Keys to export the values
4849 keys: Vec<String>,
4950 },
5151+5252+ /// Sets the mode for Safir (KV-file store or SQLite store - active on next run)
5353+ Mode { mode: SafirMode },
50545155 /// List all values in the store
5256 List,
···11-use std::{io::Write, collections::HashMap, path::Path};
11+use std::{
22+ collections::HashMap,
33+ fs,
44+ io::Write,
55+ path::{Path, PathBuf},
66+};
2738/// Confirmation dialog for important calls
49pub fn confirm_entry(msg: &str) -> bool {
···25302631/// Loads the store from disk
2732pub fn load_store(path: impl AsRef<Path>) -> HashMap<String, String> {
2828- let contents = std::fs::read_to_string(path.as_ref())
2929- .expect("unable to store contents");
3333+ let contents = std::fs::read_to_string(path.as_ref()).expect("unable to store contents");
30343135 return serde_json::from_str::<HashMap<String, String>>(&contents)
3236 .expect("unable to deserialize store contents");
···34383539/// Writes the store to disk
3640pub fn write_store(store: &HashMap<String, String>, path: impl AsRef<Path>) {
3737- let str_store = serde_json::to_string_pretty(store)
3838- .expect("unable to serialize store contents");
4141+ let str_store =
4242+ serde_json::to_string_pretty(store).expect("unable to serialize store contents");
39434040- let mut file = std::fs::File::create(&path)
4141- .expect("unable to get file handle");
4444+ let mut file = std::fs::File::create(&path).expect("unable to get file handle");
42454346 file.write_all(str_store.as_bytes())
4447 .expect("unable to write store out to disk");
···46494750/// Remove the .safirstore directory
4851pub fn purge_directory(path: impl AsRef<Path>) {
4949- std::fs::remove_dir_all(path)
5050- .expect("unable to remove safirstore directory");
5252+ std::fs::remove_dir_all(path).expect("unable to remove safirstore directory");
5353+}
5454+5555+/// Create the .safirstore directory in the user HOME
5656+pub fn create_safir_workspace() -> PathBuf {
5757+ match dirs::home_dir() {
5858+ Some(home) => {
5959+ let working_dir = home.join(".safirstore");
6060+ fs::create_dir_all(&working_dir).expect("unable to create main directory");
6161+6262+ working_dir
6363+ }
6464+ None => {
6565+ eprintln!("unable to obtain home directory path!");
6666+ std::process::exit(-1);
6767+ }
6868+ }
5169}