···11-use std::io::{self, Write};
11+use anyhow::Result;
22+use std::io::Write;
23use std::path::{Path, PathBuf};
3445use crate::config::SafirConfig;
···78use sysinfo::{Pid, System, SystemExt};
89use tokio::fs;
9101111+pub async fn init() -> Result<SafirConfig> {
1212+ let store_dir = create_safir_directory().await?;
1313+ let cfg = load_safir_config(&store_dir).await?;
1414+ Ok(cfg)
1515+}
1616+1017pub fn check_rubin_installed() -> bool {
1118 if which::which("rubin").is_ok() {
1219 return true;
···27342835pub fn is_safir_running(pid: Option<u32>) -> bool {
2936 match pid {
3030- Some(pid) => {
3131- return check_process_running(pid);
3232- }
3737+ Some(pid) => check_process_running(pid),
3338 None => false,
3439 }
3540}
···3843 path.as_ref().exists()
3944}
40454141-pub async fn create_safir_directory() -> io::Result<PathBuf> {
4646+pub async fn create_safir_directory() -> Result<PathBuf> {
4247 let home_dir = dirs::home_dir().unwrap();
4348 let store_path = home_dir.join(".safirstore");
4449 fs::create_dir_all(&store_path).await?;
···4752}
48534954#[cfg(target_family = "unix")]
5050-pub async fn kill_process(pid: u32) -> io::Result<()> {
5555+pub async fn kill_process(pid: u32) -> Result<()> {
5156 if let Ok(process) = psutil::process::Process::new(pid) {
5257 if let Err(err) = process.kill() {
5358 eprintln!("failed to kill process: {}", err);
···95100 .expect("unable to get input from user");
9610197102 let answer = answer.trim().to_lowercase();
9898- if answer == "y" {
103103+ if answer == "y" || answer == "yes" {
99104 return true;
100105 }
101106102107 false
103108}
104109105105-pub async fn load_safir_config(safir_cfg: impl AsRef<Path>) -> io::Result<SafirConfig> {
106106- let mut cfg = if path_exists(&safir_cfg).await {
107107- SafirConfig::load(&safir_cfg).await?
110110+pub async fn load_safir_config(store_dir: impl AsRef<Path>) -> Result<SafirConfig> {
111111+ let cfg_path = &store_dir.as_ref().join("safir.cfg");
112112+ let mut cfg = if path_exists(&cfg_path).await {
113113+ SafirConfig::load(&cfg_path).await?
108114 } else {
109115 SafirConfig::new()
110116 };
111117118118+ cfg.root_path = store_dir.as_ref().to_owned();
119119+ cfg.config_path = cfg_path.to_owned();
120120+112121 // Used in cases where the process has ended ungracefully and the config hasnt been updated
113122 if let Some(pid) = cfg.memcache_pid {
114123 if !check_process_running(pid) {
115124 cfg = SafirConfig::new();
116116- cfg.write(&safir_cfg).await?;
125125+ cfg.write().await?;
117126 }
118127 }
119128
+4
safir-mem/CHANGELOG.md
···2233Documenting changes between versions
4455+## v0.2.1
66+77+Changes to the internal code structure, no changes to operation
88+59## v0.2.0
610711Moved the core of the control to a new internal library called `safir-core`
+2-1
safir-mem/Cargo.toml
···11[package]
22name = "safir-mem"
33-version = "0.2.0"
33+version = "0.2.1"
44edition = "2021"
55authors = ["Graham Keenan graham.keenan@outlook.com"]
66license = "MIT OR Apache-2.0"
···1717clap = { version = "4.2.5" , features = ["derive"] }
1818tokio = { version = "1.28.2", features = ["full"] }
1919safir-core = { version = "0.1.0", path = "../safir-core" }
2020+anyhow = "1.0.75"
+20-17
safir-mem/src/main.rs
···11mod cli;
2233use cli::*;
44-use safir_core::{mem::SafirMemcache, utils};
44+use safir_core::{utils, Safir, SafirEngineType};
5566+use anyhow::Result;
67use std::process::{Command, Stdio};
7889#[tokio::main]
99-async fn main() -> std::io::Result<()> {
1010+async fn main() -> Result<()> {
1011 let cli = Cli::parse();
1111- let store_dir = utils::create_safir_directory().await?;
1212- let safir_cfg = &store_dir.join("safir.cfg");
1313- let mut cfg = utils::load_safir_config(&safir_cfg).await?;
1414-1515- let safir_state = utils::is_safir_running(cfg.memcache_pid);
1616- let safir_mem = SafirMemcache::new(safir_state);
1212+ let mut safir_mem = Safir::new(SafirEngineType::Memcache).await?;
17131814 match &cli.command {
1919- Commands::Add(args) => safir_mem.add_entry(&args.key, &args.value).await?,
1515+ Commands::Add(args) => {
1616+ safir_mem
1717+ .add_entry(args.key.to_owned(), args.value.to_owned())
1818+ .await?
1919+ }
2020 Commands::Get(args) => {
2121 if let Some(key) = &args.key {
2222- safir_mem.get_string(key).await?;
2222+ safir_mem.get_entry(key.to_string()).await?;
2323 } else {
2424 utils::print_header();
2525 utils::print_output("A key is required for memcache GET command!");
···4545 return Ok(());
4646 }
47474848- if let Some(pid) = cfg.memcache_pid {
4848+ let config = &mut safir_mem.config;
4949+ if let Some(pid) = config.memcache_pid {
4950 println!(
5051 "Safir memcache service is already running on 127.0.0.1:9876 - PID {}",
5152 pid
···6364 .expect("unable to spawn child process");
64656566 let pid = child.id();
6666- cfg.memcache_pid = Some(pid);
6767- cfg.write(&safir_cfg).await?;
6767+ config.memcache_pid = Some(pid);
6868+ config.write().await?;
6869 println!(
6970 "Safir memcache service started at 127.0.0.1:9876 - PID {}",
7071 pid
···7677 return Ok(());
7778 }
78797979- let pid = match cfg.memcache_pid {
8080+ let config = &mut safir_mem.config;
8181+ let pid = match config.memcache_pid {
8082 Some(pid) => pid,
8183 None => {
8284 println!("Safir memcache service does not seem to be running.");
···9092 err
9193 );
9294 } else {
9393- cfg.memcache_pid = None;
9494- cfg.write(&safir_cfg).await?;
9595+ config.memcache_pid = None;
9696+ config.write().await?;
9597 println!("Stopping Safir memcache service!");
9698 }
9799 }
98100 Commands::Dump(args) => {
9999- if let Err(e) = safir_mem.dump_store(&args.path).await {
101101+ let inner = safir_mem.as_safir_memcache();
102102+ if let Err(e) = inner.dump_store(&args.path).await {
100103 eprintln!("unable to dump Safir memcache service: {}", e);
101104 }
102105 }
+8
safir/CHANGELOG.md
···2233Documenting changes between versions beginning from v0.3.0
4455+## v0.7.1
66+77+Changes to the internal code structure, no changes to operation
88+99+## v0.7.0
1010+1111+Something happened here but not major...
1212+513## v0.6.0
614715Removed the Safir Memcache and moved it to its own project.
+2-1
safir/Cargo.toml
···11[package]
22name = "safir"
33-version = "0.7.0"
33+version = "0.7.1"
44edition = "2021"
55authors = ["Graham Keenan graham.keenan@outlook.com"]
66license = "MIT OR Apache-2.0"
···1717clap = { version = "4.2.5" , features = ["derive"] }
1818tokio = { version = "1.28.2", features = ["full"] }
1919safir-core = { version = "0.1.0", path = "../safir-core" }
2020+anyhow = "1.0.75"