Skip to content

Commit

Permalink
feat: Vault Delete is now fully functional
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhexists committed Dec 23, 2023
1 parent 363e786 commit f111b6c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vault"
version = "1.0.1"
version = "1.2.1"
edition = "2021"
authors = [
"Shubham singh <github: github.com/shubhexists>",
Expand Down
58 changes: 45 additions & 13 deletions src/commands/delete.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
use std::fs;
use std::io;

use crate::utils::get_current_branch::get_current_branch;
use crate::utils::yaml_layouts::InitLayout;
use std::fs;
use std::fs::File;
use std::io::{self, BufReader};
use std::path::{Path,PathBuf};

pub fn delete(branch_to_delete: &str) -> io::Result<()> {
if branch_to_delete == "." {
let _ = fs::remove_dir_all(".vault");
let _ = fs::remove_file(".vaultignore");
Ok(())
} else {
let current_branch: Result<String, io::Error> = get_current_branch();
match current_branch {
Ok(current_branch) => {
if current_branch == branch_to_delete {
panic!("You can not delete an Active Branch!")
} else {
//@TODO - Get add branches and check if the branch actually exists
let init_file: &Path = Path::new(".vault/init.yaml");
let file: Result<File, io::Error> = File::open(&init_file);
match file {
Ok(file) => {
let reader: BufReader<File> = BufReader::new(file);
let mut parsed: InitLayout =
serde_yaml::from_reader(reader).expect("Failed to read YAML");
let current_branch: Result<String, io::Error> = get_current_branch();
match current_branch {
Ok(current_branch) => {
if current_branch == branch_to_delete {
panic!("You can not delete an Active Branch!Kindly switch to another branch first.")
} else {
let exists: bool = does_branch_exists(&branch_to_delete, &parsed);
if exists {
let vault_path: &Path = Path::new(".vault");
let branch_to_delete_path: PathBuf = vault_path.join(branch_to_delete);
let _ = fs::remove_dir_all(branch_to_delete_path);
parsed.branches.retain(|x| x != branch_to_delete);
let yaml_string: String = serde_yaml::to_string(&parsed).unwrap();
fs::write(init_file,yaml_string).unwrap();
Ok(())
} else {
panic!("Branch not found")
}
}
}
Err(e) => {
panic!("Some Error Occurred: {e}")
}
}
}
Err(e) => {
panic!("Some Error Occurred: {e}")
Err(_e) => {
panic!(
"Couldn't detect directory as a vault. Kindly Run `vault init` to make a vault"
)
}
}
}
panic!("Some unknown error occurred!")
}

fn does_branch_exists(branch_to_check: &str, parsed: &InitLayout) -> bool {
let branch_array: &Vec<String> = &parsed.branches;
branch_array.contains(&branch_to_check.to_string())
}

0 comments on commit f111b6c

Please sign in to comment.