Skip to content

Commit

Permalink
Added -m #20, vault delete
Browse files Browse the repository at this point in the history
shubhexists committed Dec 17, 2023
1 parent ee19d05 commit 2ccaabf
Showing 5 changed files with 44 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/commands/commit.rs
Original file line number Diff line number Diff line change
@@ -137,7 +137,7 @@ fn handle_commit(dir_path: &Path) -> io::Result<Vec<TreeEntry>> {
}

//@TODO - Sync this function to config.yaml
pub fn commit(dir_path: &Path) -> io::Result<()> {
pub fn commit(dir_path: &Path, message: &str) -> io::Result<()> {
let commit: Result<Vec<TreeEntry>, io::Error> = handle_commit(dir_path);
let vault = Path::new(".vault");
let current_branch: Result<String, io::Error> = get_current_branch();
@@ -168,9 +168,8 @@ pub fn commit(dir_path: &Path) -> io::Result<()> {
}
let mut file: File = File::create(file_path)?;
let _ = file.write_all(&compressed_content);
// @TODO - the "-m " text should be passed here as a string
let current_commit: Result<Commit, io::Error> =
Commit::new_commit("New Commit !", hash_main_dir_in_sha256);
Commit::new_commit(message, hash_main_dir_in_sha256);
match current_commit {
Ok(current_commit) => {
let commit_content: String =
26 changes: 26 additions & 0 deletions src/commands/delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::fs;
use std::io;

use crate::utils::get_current_branch::get_current_branch;

pub fn delete(branch_to_delete: &str) -> io::Result<()> {
if branch_to_delete == "." {
let _ = fs::remove_dir_all(".vault");
let _ = fs::remove_file(".vaultignore");
} 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
}
}
Err(e) => {
panic!("Some Error Occurred: {e}")
}
}
}
panic!("Some unknown error occurred!")
}
2 changes: 1 addition & 1 deletion src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//This will have an init function which will currently create a .vault folder in the current directory
// VAULT INIT
use crate::commands::{create, switch};
use crate::commands::{create::create, switch::switch};
use crate::utils::yaml_layouts::InitLayout;
use std::fs::{self, create_dir};
use std::path::Path;
5 changes: 1 addition & 4 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -3,8 +3,5 @@ pub mod create;
pub mod init;
pub mod switch;
pub mod cat;
pub mod delete;

pub use commit::commit;
pub use create::create;
pub use switch::switch;
pub use cat::cat;
22 changes: 14 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -3,10 +3,12 @@
mod commands;
mod core;
mod utils;
use std::env;

use crate::commands::init::init;
use clap::{Parser, Subcommand};
use commands::{commit, create, switch, cat};
use std::env;
use commands::delete::delete;
use commands::{cat::cat, commit::commit, create::create, switch::switch};

#[derive(Parser)]
#[command(author, version, about, long_about=None)]
@@ -22,27 +24,31 @@ enum Arguments {
/// Initialize a new vault
Init,
/// Commit files to current branch
Commit,
Commit {
#[arg(short, long)]
message: String,
},
/// Create a new branch with given name
Create { branch_name: String },
/// Switch to given branch name
Switch { branch_name: String },
/// Get the actual data, stored in the ZLib hash
Cat { hash_string: String },
// Deletes the given branch
// Delete {branch_name: String},
Cat { hash_string: String },
/// Deletes the given branch,
/// Use `vault delete .` to entirely remove vault from your directory!
Delete { branch_name: String },
}

fn main() {
let cli: CLI = CLI::parse();
if let Ok(current_dir) = env::current_dir() {
let _ = match &cli.command {
Arguments::Init => init(),
Arguments::Commit => commit(&current_dir).unwrap(),
Arguments::Commit { message } => commit(&current_dir, message).unwrap(),
Arguments::Create { branch_name } => create(&branch_name),
Arguments::Switch { branch_name } => switch(&branch_name),
Arguments::Cat { hash_string } => cat(&hash_string).unwrap(),
// Arguments::Delete {branch_name} => delete(&branch_name),
Arguments::Delete { branch_name } => delete(&branch_name).unwrap(),
};
} else {
eprintln!("Failed to get the current working directory");

0 comments on commit 2ccaabf

Please sign in to comment.