implemented bookmark add/remove functions
This commit is contained in:
+1
-1
@@ -102,7 +102,7 @@ pub struct BookmarkSubArgs {
|
|||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
||||||
/// path of bookmark to add
|
/// path of bookmark to add
|
||||||
pub path: Option<String>,
|
pub path: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// empty struct for subcommands with no arguments
|
/// empty struct for subcommands with no arguments
|
||||||
|
|||||||
+45
-6
@@ -1,21 +1,23 @@
|
|||||||
//! handle the config file and bookmarks stored
|
//! handle the config file and bookmarks stored
|
||||||
//! in said config file
|
//! in said config file
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::env::var;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Error, Result};
|
use std::io::{Error, Result};
|
||||||
use std::path::{PathBuf};
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::env::var;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub conf_dir: PathBuf,
|
conf_dir: PathBuf,
|
||||||
pub bookmarks: HashMap<String, PathBuf>,
|
bookmarks: HashMap<String, PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
const BOOKMARK_FILE_NAME : &str = "bookmarks.conf";
|
||||||
|
|
||||||
/// generates and populates a new instance of Config
|
/// generates and populates a new instance of Config
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> Result<Self> {
|
||||||
let mut bookmarks = Config {
|
let mut bookmarks = Config {
|
||||||
@@ -36,9 +38,33 @@ impl Config {
|
|||||||
Ok(bookmarks)
|
Ok(bookmarks)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_bookmarks(&mut self) -> &mut HashMap<String, PathBuf> {
|
||||||
|
&mut self.bookmarks
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_bookmark(&mut self, name: &String, path: &PathBuf) -> Result<()> {
|
||||||
|
if !path.is_dir() {
|
||||||
|
return Err(Error::other("-- provided path argument does not point to a valid directory"))
|
||||||
|
} else {
|
||||||
|
self.bookmarks.insert(name.to_string(), path.to_path_buf());
|
||||||
|
self.write_bookmark_file()?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_bookmark(&mut self, name: &String) -> Result<()> {
|
||||||
|
if self.bookmarks.contains_key(name) {
|
||||||
|
_ = self.bookmarks.remove(name);
|
||||||
|
self.write_bookmark_file()?;
|
||||||
|
} else {
|
||||||
|
return Err(Error::other("-- bookmark requested to delete does not exist"));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn build_config(&mut self) -> Result<()> {
|
fn build_config(&mut self) -> Result<()> {
|
||||||
let mut bookmark_file = self.conf_dir.clone();
|
let mut bookmark_file = self.conf_dir.clone();
|
||||||
bookmark_file.push("bookmarks.conf");
|
bookmark_file.push(Self::BOOKMARK_FILE_NAME);
|
||||||
|
|
||||||
if !bookmark_file.is_file() {
|
if !bookmark_file.is_file() {
|
||||||
_ = File::create(bookmark_file.clone())?;
|
_ = File::create(bookmark_file.clone())?;
|
||||||
@@ -64,4 +90,17 @@ impl Config {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_bookmark_file(&self) -> Result<()> {
|
||||||
|
let mut file_content = String::new();
|
||||||
|
for (mark, path) in self.bookmarks.iter() {
|
||||||
|
file_content.push_str(&format!("{}={}\n", mark, path.to_str().unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut path = self.conf_dir.clone();
|
||||||
|
path.push(Self::BOOKMARK_FILE_NAME);
|
||||||
|
|
||||||
|
fs::write(path, file_content)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-6
@@ -96,13 +96,13 @@ fn handle_bookmark(args: &BookmarkArgs, stack: &mut Stack) -> Result<()> {
|
|||||||
// if args.bookmark_action.is_some() {
|
// if args.bookmark_action.is_some() {
|
||||||
if args.bookmark_action.is_some() {
|
if args.bookmark_action.is_some() {
|
||||||
match args.bookmark_action.clone().unwrap() {
|
match args.bookmark_action.clone().unwrap() {
|
||||||
BookmarkAction::list(_) => list_bookmarks(&config)?,
|
BookmarkAction::list(_) => list_bookmarks(&mut config)?,
|
||||||
BookmarkAction::add(args) => add_bookmarks(&args, &mut config)?,
|
BookmarkAction::add(args) => add_bookmarks(&args, &mut config)?,
|
||||||
BookmarkAction::remove(args) => remove_bookmarks(&args, &mut config)?,
|
BookmarkAction::remove(args) => remove_bookmarks(&args, &mut config)?,
|
||||||
};
|
};
|
||||||
} else if args.name.is_some() {
|
} else if args.name.is_some() {
|
||||||
let path = match config
|
let path = match config
|
||||||
.bookmarks
|
.get_bookmarks()
|
||||||
.get(args.name.as_ref().unwrap())
|
.get(args.name.as_ref().unwrap())
|
||||||
{
|
{
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
@@ -115,20 +115,26 @@ fn handle_bookmark(args: &BookmarkArgs, stack: &mut Stack) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_bookmarks(config: &Config) -> Result<()> {
|
fn list_bookmarks(config: &mut Config) -> Result<()> {
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
for (mark, path) in &config.bookmarks {
|
for (mark, path) in config.get_bookmarks() {
|
||||||
buffer.push_str(&format!("{} : {}\n", mark, path.to_str().unwrap()));
|
buffer.push_str(&format!("{} : {}\n", mark, path.to_str().unwrap()));
|
||||||
}
|
}
|
||||||
println!("echo '{}'", buffer);
|
println!("echo '{}'", buffer);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_bookmarks(_args: &BookmarkSubArgs, _bookmarks: &mut Config) -> Result<()> {
|
fn add_bookmarks(args: &BookmarkSubArgs, config: &mut Config) -> Result<()> {
|
||||||
|
if args.path.is_none() {
|
||||||
|
return Err(Error::other("-- missing path argument"));
|
||||||
|
} else {
|
||||||
|
config.add_bookmark(&args.name, &args.path.clone().unwrap())?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_bookmarks(_args: &BookmarkSubArgs, _bookmarks: &mut Config) -> Result<()> {
|
fn remove_bookmarks(args: &BookmarkSubArgs, config: &mut Config) -> Result<()> {
|
||||||
|
config.remove_bookmark(&args.name)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user