Skip to content

Commit

Permalink
dvs status
Browse files Browse the repository at this point in the history
  • Loading branch information
jenna-a2ai committed Mar 28, 2024
1 parent 20dc006 commit a621f17
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 88 deletions.
51 changes: 51 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ blake3 = { version = "1.5.1", features = ["rayon"] }
memmap2 = "0.7.1"
rayon = "1.7.0"
file-owner = "0.1.2"
chrono = "0.4.37"
chrono = "0.4.37"
walkdir = "2.4.0"
92 changes: 86 additions & 6 deletions src/cmd/status_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,105 @@
use std::time::SystemTime;
use std::path::PathBuf;

use crate::internal::config::config;
use crate::internal::file::hash;
use crate::internal::git::repo;
use crate::internal::git::{self, repo};
use crate::internal::log::json;
use crate::internal::meta::{file, parse};

#[derive(Debug)]
pub struct JsonFileResult {
pub path: String,
pub path: PathBuf,
pub status: String,
pub file_size: u64,
pub file_hash: String,
pub time_stamp: SystemTime,
pub time_stamp: String,
pub saved_by: String,
pub message: String
}

pub fn run_status_cmd(files: &Vec<PathBuf>) {

// struct JsonResult {
// files: Vec<JsonFileResult>,
// errors:Vec<json::JsonIssue>
// }

// impl JsonResult {
// fn add_file(&mut self, file_result: JsonFileResult) {
// self.files.push(file_result);
// }

// fn add_error(&mut self, json_issue: json::JsonIssue) {
// self.errors.push(json_issue);
// }
// } // impl

pub fn run_status_cmd(files: &Vec<String>) -> Result<Vec<JsonFileResult>, std::io::Error> {

let mut json_logger: Vec<JsonFileResult> = Vec::new();

let mut meta_paths: Vec<PathBuf> = Vec::new();

//iIf no arguments are provided, get the status of all files in the current git repository
if files.is_empty() {
// Get git root
let git_repo = match repo::get_nearest_repo_dir(&PathBuf::from(".")) {
Ok(git_repo) => {
// json
git_repo
}
Err(e) => {
// json
return Err(e)
}
};

// get config
match config::read(&git_repo) {
Ok(config) => config,
Err(_) => return Err(std::io::Error::other("devious is not initialized")),
};

// get meta files
meta_paths = parse::get_all_meta_files(git_repo);
} // if doing all files
else {
meta_paths = parse::parse_globs(files);
} // else specific files

if meta_paths.is_empty() {return Ok(json_logger)}

json_logger = meta_paths.into_iter().map(|path| {
// get relative path
let rel_path = repo::get_relative_path(&PathBuf::from("."), &path).expect("couldn't get relative path");

// get file info
let metadata = file::load(&path).expect("couldn't get metadata");

// get whether file was hashable and file hash
let file_hash_result = hash::get_file_hash(&path);

let mut file_hash = String::from("placeholder");
if file_hash_result.is_ok() {file_hash = file_hash_result.unwrap()}
// else, file_hash_result was an error, so stick with the nonsense default value so they don't match

// asign status: not-present by default
let mut status = String::from("out-of-date");
if !path.exists() {status = String::from("not-present")}
else if file_hash == metadata.file_hash {
status = String::from("up-to-date")
}
// else, the file exists, but the hash isn't up to date, so still with default: out-of-date

// assemble info into JsonFileResult
JsonFileResult{
path: rel_path,
status: status,
file_size: metadata.file_size,
file_hash: metadata.file_hash,
time_stamp: metadata.time_stamp,
saved_by: metadata.saved_by,
message: metadata.message
}
}).collect::<Vec<JsonFileResult>>();

Ok(json_logger)
} // run_status_cmd
10 changes: 5 additions & 5 deletions src/internal/git/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use std::path::{PathBuf, Path};
use std::fs;

pub fn get_relative_path(root_dir: &PathBuf, file_path: &PathBuf) -> Result<PathBuf, std::io::Error> {
let abs_file_path = match file_path.canonicalize() {
Ok(path) => path,
Err(e) => return Err(e),
};
// let abs_file_path = match file_path.canonicalize() {
// Ok(path) => path,
// Err(e) => return Err(e),
// };

let abs_root_dir = match root_dir.canonicalize() {
Ok(path) => path,
Err(e) => return Err(e),
};

match abs_file_path.strip_prefix(abs_root_dir) {
match file_path.strip_prefix(abs_root_dir) {
Ok(path) => return Ok(path.to_path_buf()),
Err(_) => return Err(std::io::Error::other("paths not relative")),
}
Expand Down
5 changes: 3 additions & 2 deletions src/internal/log/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct JsonLog {
pub issues: Vec<JsonIssue>
}

#[derive(Serialize, Deserialize)]
pub struct JsonLogger(Vec<JsonLog>);
pub static JSON_LOGGING: bool = false;



3 changes: 2 additions & 1 deletion src/internal/log/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod json;
pub mod json;
pub mod pretty;
7 changes: 7 additions & 0 deletions src/internal/log/pretty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::internal::log::json;

pub fn print(string: String) {
if json::JSON_LOGGING {return}

println!("{}", string)
}
14 changes: 14 additions & 0 deletions src/internal/meta/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::path::PathBuf;
use walkdir::WalkDir;

pub fn parse_globs(globs: &Vec<String>) -> Vec<PathBuf> {
let mut meta_files: Vec<PathBuf> = Vec::new();
Expand All @@ -22,4 +23,17 @@ pub fn parse_globs(globs: &Vec<String>) -> Vec<PathBuf> {
}

return meta_files;
}

pub fn get_all_meta_files(dir: PathBuf) -> Vec<PathBuf> {
//let mut meta_files: Vec<String> = Vec::new();
WalkDir::new(&dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().map_or(false, |ext| ext == "dvsmeta"))
.map(|e| {
let string = e.into_path().display().to_string().replace(".dvsmeta", "");
PathBuf::from(string)
})
.collect()
}
48 changes: 0 additions & 48 deletions src/lib.rs

This file was deleted.

49 changes: 26 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,35 @@ mod cmd;
use crate::cmd::init_cmd;
use crate::cmd::add_cmd;
use crate::cmd::get_cmd;

use crate::cmd::status_cmd;

fn main() -> io::Result<()> {
// dvs init src/test_directory_storage --mode=0o764 --group=datascience
let storage_dir = PathBuf::from(r"src/test_directory_storage");
// if mode specified, set mode
// else, mode is set to default
let mode: u32 = 0o764;
// if group specified, set group
// else, group is set to default
let group = String::from("datascience");
fs::set_permissions(&storage_dir.canonicalize().unwrap(), fs::Permissions::from_mode(0o777)).unwrap();
init_cmd::run_init_cmd(&storage_dir, &mode, &group)?;
println!("initialized devious");

// dvs add src/test_directory/test1.txt src/test_directory/test2.txt "derived DA files"
let files: Vec<String> = vec![String::from("src/test_directory/test1.txt"), String::from("src/test_directory/test2.txt")];
let message = String::from("derived DA files");
add_cmd::run_add_cmd(&files, &message)?;

fs::remove_file(PathBuf::from("src/test_directory/test1.txt"))?;
fs::remove_file(PathBuf::from("src/test_directory/test2.txt"))?;

// dvs get src/test_directory/test1.txt src/test_directory/test2.txt
get_cmd::run_get_cmd(&files)?;

let storage_dir = PathBuf::from(r"src/test_directory_storage");
// if mode specified, set mode
// else, mode is set to default
let mode: u32 = 0o764;
// if group specified, set group
// else, group is set to default
let group = String::from("datascience");
fs::set_permissions(&storage_dir.canonicalize().unwrap(), fs::Permissions::from_mode(0o777)).unwrap();
init_cmd::run_init_cmd(&storage_dir, &mode, &group)?;
println!("initialized devious");

// dvs add src/test_directory/test1.txt src/test_directory/test2.txt "derived DA files"
let files: Vec<String> = vec![String::from("src/test_directory/test1.txt"), String::from("src/test_directory/test2.txt")];
let message = String::from("derived DA files");
//add_cmd::run_add_cmd(&files, &message)?;

//fs::remove_file(PathBuf::from("src/test_directory/test1.txt"))?;
// fs::remove_file(PathBuf::from("src/test_directory/test2.txt"))?;

// dvs get src/test_directory/test1.txt src/test_directory/test2.txt
//get_cmd::run_get_cmd(&files)?;

let empty_vec: Vec<String> = Vec::new();
let vec = status_cmd::run_status_cmd(&empty_vec)?;
println!("{:?}", vec);
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/test_directory/test1.txt.dvsmeta
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"file_hash": "71fe44583a6268b56139599c293aeb854e5c5a9908eca00105d81ad5e22b7bb6",
"file_size": 445,
"time_stamp": "2024-03-28 10:10:22.930945979 -04:00",
"time_stamp": "2024-03-28 15:02:29.963093889 -04:00",
"message": "derived DA files",
"saved_by": "jenna"
}
Loading

0 comments on commit a621f17

Please sign in to comment.