| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- use std::{
- fs,
- path::{Path, PathBuf},
- };
- use crate::{Error, Result};
- pub fn expand_path(path: &str) -> Result<PathBuf> {
- let ret: PathBuf;
- if path.starts_with("~/") {
- let homedir = dirs::home_dir().unwrap();
- let remains = PathBuf::from(path.strip_prefix("~/").unwrap());
- ret = [homedir, remains].iter().collect();
- } else if path.starts_with('~') {
- ret = dirs::home_dir().unwrap();
- } else {
- ret = PathBuf::from(path);
- }
- Ok(ret)
- }
- pub fn join_config_path(file: &Path) -> Result<PathBuf> {
- let mut path = PathBuf::new();
- let dfi_path = Path::new("darkfi");
- if let Some(v) = dirs::config_dir() {
- path.push(v);
- }
- path.push(dfi_path);
- path.push(file);
- Ok(path)
- }
- pub fn load_keypair_to_str(path: PathBuf) -> Result<String> {
- if Path::new(&path).exists() {
- let key = fs::read(&path)?;
- let str_buff = std::str::from_utf8(&key)?;
- Ok(str_buff.to_string())
- } else {
- println!("Could not parse keypair path");
- Err(Error::KeypairPathNotFound)
- }
- }
|