optimize add
This commit is contained in:
parent
cbbf185b1a
commit
7a34b3c79b
@ -1,5 +1,6 @@
|
|||||||
use clap::Values;
|
use clap::Values;
|
||||||
use crate::utils::{self, nextsyncignore};
|
use crate::utils::{self};
|
||||||
|
use crate::utils::nextsyncignore::{self, ignore_file, ignore_files};
|
||||||
use crate::store;
|
use crate::store;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
@ -12,13 +13,21 @@ pub struct AddArgs<'a> {
|
|||||||
pub fn add(args: AddArgs) {
|
pub fn add(args: AddArgs) {
|
||||||
let mut index_file = store::index::open();
|
let mut index_file = store::index::open();
|
||||||
let mut added_files: Vec<String> = vec![];
|
let mut added_files: Vec<String> = vec![];
|
||||||
|
let rules = match nextsyncignore::read_lines() {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(_) => vec![],
|
||||||
|
};
|
||||||
|
let mut ignored_f = vec![];
|
||||||
let file_vec: Vec<&str> = args.files.collect();
|
let file_vec: Vec<&str> = args.files.collect();
|
||||||
for file in file_vec {
|
for file in file_vec {
|
||||||
|
if !args.force && ignore_file(&file.to_string(), rules.clone(), &mut ignored_f) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let path = Path::new(file);
|
let path = Path::new(file);
|
||||||
match path.exists() {
|
match path.exists() {
|
||||||
true => {
|
true => {
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
|
|
||||||
added_files.push(String::from(path.to_str().unwrap()));
|
added_files.push(String::from(path.to_str().unwrap()));
|
||||||
add_folder_content(path.to_path_buf(), &mut added_files);
|
add_folder_content(path.to_path_buf(), &mut added_files);
|
||||||
} else {
|
} else {
|
||||||
@ -32,19 +41,16 @@ pub fn add(args: AddArgs) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check ignored file if not forced
|
if ignored_f.len() > 0 {
|
||||||
if !args.force {
|
|
||||||
let (ignored, ignored_files) = nextsyncignore::ignore_files(&mut added_files);
|
|
||||||
if ignored {
|
|
||||||
// todo multiple nextsyncignore
|
// todo multiple nextsyncignore
|
||||||
println!("The following paths are ignored by your .nextsyncignore file:");
|
println!("The following paths are ignored by your .nextsyncignore file:");
|
||||||
for file in ignored_files {
|
for file in ignored_f {
|
||||||
println!("{}", file);
|
println!("{}", file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// save all added_files in index
|
// save all added_files in index
|
||||||
|
// todo avoid duplication
|
||||||
for file in added_files {
|
for file in added_files {
|
||||||
match writeln!(index_file, "{}", file) {
|
match writeln!(index_file, "{}", file) {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
|
@ -85,7 +85,7 @@ pub fn clone(remote: Values<'_>) {
|
|||||||
// find folders and files in response
|
// find folders and files in response
|
||||||
let objects = get_objects_xml(body);
|
let objects = get_objects_xml(body);
|
||||||
let mut iter = objects.iter();
|
let mut iter = objects.iter();
|
||||||
iter.next(); // jump first element which the folder fetched
|
iter.next(); // jump first element which is the folder cloned
|
||||||
for object in iter {
|
for object in iter {
|
||||||
if object.chars().last().unwrap() == '/' {
|
if object.chars().last().unwrap() == '/' {
|
||||||
folders.push(object.to_string());
|
folders.push(object.to_string());
|
||||||
|
@ -3,7 +3,7 @@ use regex::Regex;
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Cursor, BufReader, BufRead};
|
use std::io::{Cursor, BufReader, BufRead};
|
||||||
|
|
||||||
fn read_lines() -> Result<Vec<String>, ()> {
|
pub fn read_lines() -> Result<Vec<String>, ()> {
|
||||||
if let Some(path) = path::nextsyncignore() {
|
if let Some(path) = path::nextsyncignore() {
|
||||||
let file = match File::open(path) {
|
let file = match File::open(path) {
|
||||||
Ok(buffer) => buffer,
|
Ok(buffer) => buffer,
|
||||||
|
Loading…
Reference in New Issue
Block a user