cleaning some code

This commit is contained in:
grimhilt 2023-06-12 01:30:39 +02:00
parent 05d17c296f
commit e405ce7f8f
5 changed files with 16 additions and 17 deletions

View File

@ -10,7 +10,6 @@ use crate::services::api::ApiError;
use crate::services::list_folders::ListFolders;
use crate::services::download_files::DownloadFiles;
use crate::utils::object;
use crate::utils::path;
use crate::commands;
use crate::global::global::{DIR_PATH, set_dir_path};
@ -72,11 +71,15 @@ pub fn clone(remote: Values<'_>) {
} else {
// create folder
let local_folder = get_local_path(folder, local_path.clone(), username, dist_path_str);
dbg!(DirBuilder::new().recursive(true).create(local_folder.clone()));
if let Err(err) = DirBuilder::new().recursive(true).create(local_folder.clone()) {
eprintln!("error: cannot create directory {}: {}", local_folder.display(), err);
}
// add tree
let path_folder = local_folder.strip_prefix(local_path.clone()).unwrap();
object::add_tree(&path_folder);
if object::add_tree(&path_folder).is_err() {
eprintln!("error: cannot store object {}", path_folder.display());
}
}
// find folders and files in response
@ -114,7 +117,7 @@ fn write_file(path: PathBuf, content: &Vec<u8>, local_p: PathBuf) -> io::Result<
f.write_all(&content)?;
let relative_p = Path::new(&path).strip_prefix(local_p).unwrap();
object::add_blob(relative_p, "tmpdate");
object::add_blob(relative_p, "tmpdate")?;
Ok(())
}

View File

@ -1,9 +1,9 @@
use crate::utils::path;
use crate::utils::read;
use std::fs::OpenOptions;
use std::io::Write;
use std::io::{self, Write};
pub fn set(var: &str, val: &str) {
pub fn set(var: &str, val: &str) -> io::Result<()> {
let mut root = match path::nextsync() {
Some(path) => path,
None => {
@ -19,12 +19,13 @@ pub fn set(var: &str, val: &str) {
.write(true)
.create(true)
.append(true)
.open(root).unwrap();
.open(root)?;
let mut line = var.to_owned();
line.push_str(" ");
line.push_str(val);
writeln!(file, "{}", line);
writeln!(file, "{}", line)?;
Ok(())
}
pub fn get(var: &str) -> Option<String> {

View File

@ -4,7 +4,6 @@ use crypto::sha1::Sha1;
use std::collections::HashMap;
use colored::Colorize;
use std::path::PathBuf;
use std::path::Path;
use std::io::{self, Lines, BufReader};
use crate::utils::{self, object};
@ -45,9 +44,7 @@ pub fn get_diff() -> (Vec<String>, Vec<String>, Vec<Obj>) {
dbg!(utils::path::current());
let nextsync_path = utils::path::nextsync().unwrap();
let current_p = utils::path::current().unwrap();
let mut dist_path = current_p.strip_prefix(root.clone()).unwrap().to_path_buf();
dbg!(dist_path.clone());
let dist_path = current_p.strip_prefix(root.clone()).unwrap().to_path_buf();
if let Ok(lines) = read_head(nextsync_path.clone()) {
add_to_hashmap(lines, &mut hashes, dist_path.clone());

View File

@ -100,7 +100,9 @@ fn main() {
} else if let Some(matches) = matches.subcommand_matches("config") {
if let Some(mut var) = matches.values_of("variable") {
if let Some(mut val) = matches.values_of("value") {
commands::config::set(var.next().unwrap(), val.next().unwrap());
if commands::config::set(var.next().unwrap(), val.next().unwrap()).is_err() {
eprintln!("fatal: cannot save the value");
}
}
}
}

View File

@ -132,16 +132,12 @@ fn add_node(path: &Path, node: &str) -> io::Result<()> {
let (dir, rest) = hash_obj(path.clone().to_str().unwrap());
dbg!(root.clone());
root.push(dir);
if !root.exists() {
todo!();
}
root.push(rest);
dbg!("create node");
dbg!(root.clone());
dbg!(node.clone());
let mut file = OpenOptions::new()
.read(true)
.write(true)