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