Skip to content

Commit

Permalink
Now creating plugins and config directory if they do not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1N committed Mar 28, 2024
1 parent eeaee90 commit c771dfd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
26 changes: 0 additions & 26 deletions src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,39 +199,13 @@ pub async fn handle(matches: &ArgMatches) -> Result<()> {
}
}

async fn ensure_config_file_path() -> Result<()> {
if cfg!(unix) {
// create ~/.config folder
let path = dirs::home_dir().unwrap().join(".config");
if !path.exists() {
tokio::fs::create_dir(&path).await?;
}

// create ~/.config/memflowup folder
let path = path.join("memflowup");
if !path.exists() {
tokio::fs::create_dir(&path).await?;
}
}

// create file
let path = util::config_file_path();
if !path.exists() {
tokio::fs::write(path, b"{}").await?;
}

Ok(())
}

pub async fn read_config() -> Result<Config> {
ensure_config_file_path().await?;
let content = tokio::fs::read_to_string(util::config_file_path()).await?;
let config: Config = serde_json::from_str(&content)?;
Ok(config)
}

pub async fn write_config(config: Config) -> Result<()> {
ensure_config_file_path().await?;
let content = serde_json::to_string(&config)?;
Ok(tokio::fs::write(util::config_file_path(), content.as_bytes()).await?)
}
28 changes: 24 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,51 @@ use crate::error::{Error, Result};
/// On unix this is returns ~/.local/lib/memflow
/// On windows this returns C:\Users\[Username]\Documents\memflow
pub(crate) fn plugins_path() -> PathBuf {
if cfg!(unix) {
let path = if cfg!(unix) {
dirs::home_dir()
.unwrap()
.join(".local")
.join("lib")
.join("memflow")
} else {
dirs::document_dir().unwrap().join("memflow")
};

// ensure plugins path exists
if !path.exists() {
std::fs::create_dir_all(&path).expect("unable to create plugins directory");
}

path
}

// TODO: move this to utils
/// Returns the path in which memflowup config is stored.
pub(crate) fn config_path() -> PathBuf {
if cfg!(unix) {
let path = if cfg!(unix) {
dirs::home_dir().unwrap().join(".config").join("memflowup")
} else {
dirs::document_dir().unwrap()
};

// ensure config folder exists
if !path.exists() {
std::fs::create_dir_all(&path).expect("unable to create config directory");
}

path
}

/// Returns the path that points to the memflowup config.
#[inline]
pub(crate) fn config_file_path() -> PathBuf {
config_path().join("config.json")
let file_path = config_path().join("config.json");

// ensure config file exists and contains valid json
if !file_path.exists() {
std::fs::write(&file_path, b"{}").expect("unable to write config file");
}

file_path
}

/// Constructs the filename of this plugin for the current os.
Expand Down

0 comments on commit c771dfd

Please sign in to comment.