chore: cleaning code

This commit is contained in:
grimhilt 2024-03-17 00:20:58 +01:00
parent a1b9cde71a
commit dc7df00ac9
13 changed files with 26 additions and 62 deletions

View File

@ -178,7 +178,7 @@ mod tests {
fn test_get_url_props() { fn test_get_url_props() {
let p = "/foo/bar"; let p = "/foo/bar";
let u = Some("user"); let u = Some("user");
let d = String::from("http://nextcloud.com"); // let d = String::from("http://nextcloud.com");
let sd = String::from("https://nextcloud.com"); let sd = String::from("https://nextcloud.com");
let sld = String::from("https://nextcloud.example.com"); let sld = String::from("https://nextcloud.example.com");
let ld = String::from("http://nextcloud.example.com"); let ld = String::from("http://nextcloud.example.com");

View File

@ -25,7 +25,7 @@ pub fn config_set(args: ConfigSetArgs) {
std::process::exit(1); std::process::exit(1);
} }
write_option_in_cat(category.unwrap(), name, value); let _ = write_option_in_cat(category.unwrap(), name, value);
} }
@ -34,7 +34,7 @@ pub fn find_option_in_cat(category: &str, option: &str) -> Option<String> {
config.push("config"); config.push("config");
let mut in_target_category = false; let mut in_target_category = false;
if let Ok(mut lines) = read::read_lines(config) { if let Ok(lines) = read::read_lines(config) {
for line in lines { for line in lines {
if let Ok(line) = line { if let Ok(line) = line {
@ -128,7 +128,7 @@ pub fn write_option_in_cat(category: &str, option: &str, value: &str) -> io::Res
} }
pub fn add_remote(name: &str, url: &str) -> io::Result<()> { pub fn add_remote(name: &str, url: &str) -> io::Result<()> {
let mut config = path::config(); let config = path::config();
// check if there is already a remote with this name // check if there is already a remote with this name
if get_remote(name).is_some() if get_remote(name).is_some()
@ -192,38 +192,3 @@ pub fn get_all_remote() -> Vec<(String, String)> {
pub fn get_core(name: &str) -> Option<String> { pub fn get_core(name: &str) -> Option<String> {
find_option_in_cat("core", name) find_option_in_cat("core", name)
} }
pub fn add_core(name: &str, value: &str) -> io::Result<()> {
let mut config = path::nextsync();
config.push("config");
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.append(true)
.open(config)?;
writeln!(file, "[core]")?;
writeln!(file, "\t{} = {}", name, value)?;
Ok(())
}
pub fn get(var: &str) -> Option<String> {
let mut config = path::config();
if let Ok(lines) = read::read_lines(config) {
for line in lines {
if let Ok(l) = line {
if l.starts_with(var) {
let (_, val) = l.split_once(" ").unwrap();
return Some(val.to_owned());
}
}
}
}
None
}

View File

@ -12,6 +12,7 @@ pub fn init() {
None => env::current_dir().unwrap(), None => env::current_dir().unwrap(),
}; };
// todo
// check if dir is empty // check if dir is empty
// if let Ok(entries) = read_folder(path.clone()) { // if let Ok(entries) = read_folder(path.clone()) {
// if entries.len() != 0 { // if entries.len() != 0 {
@ -58,6 +59,7 @@ pub fn init() {
Err(_) => println!("Error: cannot create index"), Err(_) => println!("Error: cannot create index"),
} }
// todo
path.pop(); path.pop();
path.pop(); path.pop();
path.push(".nextsyncignore"); path.push(".nextsyncignore");

View File

@ -1,4 +1,3 @@
use std::error::Error;
use std::path::PathBuf; use std::path::PathBuf;
use std::io; use std::io;
use crate::commands::status::{State, LocalObj}; use crate::commands::status::{State, LocalObj};

View File

@ -4,11 +4,10 @@ use std::io::Write;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::SystemTime; use std::time::SystemTime;
use crate::commands::status::{State}; use crate::commands::status::State;
use crate::utils::into::IntoPathBuf; use crate::utils::into::IntoPathBuf;
use crate::utils::{path, read}; use crate::utils::{path, read};
use crate::store::head; use crate::store::object::update_dates;
use crate::store::object::{update_dates, add_node, rm_node};
use crate::store::object::object::ObjMethods; use crate::store::object::object::ObjMethods;
use crate::store::object::object::Obj; use crate::store::object::object::Obj;

View File

@ -1,10 +1,9 @@
use crate::utils::into::IntoPathBuf; use crate::utils::into::IntoPathBuf;
use crate::store::object::object::Obj; use crate::store::object::object::Obj;
use std::path::PathBuf;
use crate::store::object::update_dates; use crate::store::object::update_dates;
use crate::store::object::object::ObjMethods; use crate::store::object::object::ObjMethods;
use std::fs::{self, File, OpenOptions}; use std::fs::{self, File, OpenOptions};
use std::io::{self, BufRead, BufReader, Write, Lines}; use std::io::{self, BufRead, BufReader, Write};
pub struct Tree { pub struct Tree {
pub obj: Obj, pub obj: Obj,

View File

@ -1,22 +1,18 @@
use clap::{App, Arg, SubCommand, ArgMatches}; use clap::{App, Arg, SubCommand, ArgMatches};
use textwrap::{fill, Options}; // use textwrap::{fill, Options};
use crate::commands::clone::{self, CloneArgs}; use crate::commands::clone::CloneArgs;
use crate::global; use crate::global;
use crate::commands; use crate::commands;
fn sized_str<'a>(content: &'a str) -> &'a str { // fn sized_str<'a>(content: &'a str) -> &'a str {
fill(content, Options::new(70).width).as_str(); // fill(content, Options::new(70).width).as_str();
"ok" // "ok"
} // }
fn test() -> String {
String::from("ok")
}
pub fn create() -> App<'static, 'static> { pub fn create() -> App<'static, 'static> {
let remote_desc = sized_str(&format!("The repository to clone from. See the NEXTSYNC URLS section below for more information on specifying repositories.")); // let remote_desc = sized_str(&format!("The repository to clone from. See the NEXTSYNC URLS section below for more information on specifying repositories."));
let depth_desc = sized_str(&format!("Depth of the recursive fetch of object properties. This value should be lower when there are a lot of files per directory and higher when there are a lot of subdirectories with fewer files. (Default: {})", clone::DEPTH)); // let depth_desc = sized_str(&format!("Depth of the recursive fetch of object properties. This value should be lower when there are a lot of files per directory and higher when there are a lot of subdirectories with fewer files. (Default: {})", clone::DEPTH));
SubCommand::with_name("clone") SubCommand::with_name("clone")
.arg( .arg(
Arg::with_name("remote") Arg::with_name("remote")

View File

@ -41,5 +41,4 @@ mod pull_tests {
client.clean(); client.clean();
server.clean(); server.clean();
} }
} }

View File

@ -1,12 +1,13 @@
use std::str; use std::str;
use std::process::{Command, Output}; use std::process::{Command, Output};
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::{Write, BufReader, BufRead}; use std::io::Write;
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use super::files_utils::has_files; use super::files_utils::has_files;
#[cfg(test)]
pub struct ClientTest { pub struct ClientTest {
user: String, // the nextcloud user user: String, // the nextcloud user
volume: String, // temp dir for the test volume: String, // temp dir for the test
@ -14,6 +15,7 @@ pub struct ClientTest {
exe_path: PathBuf, // absolute path of nextsync executable exe_path: PathBuf, // absolute path of nextsync executable
} }
#[cfg(test)]
impl ClientTest { impl ClientTest {
pub fn new(id: String) -> Self { pub fn new(id: String) -> Self {
// create a directory in /tmp with the given id // create a directory in /tmp with the given id

View File

@ -1,7 +1,8 @@
use std::io::{BufReader, BufRead, Write}; use std::io::{BufReader, BufRead};
use std::fs::{File}; use std::fs::File;
use std::path::PathBuf; use std::path::PathBuf;
#[cfg(test)]
pub fn has_files(full_path: PathBuf, file: &str, content: &str, test_id: String) -> bool pub fn has_files(full_path: PathBuf, file: &str, content: &str, test_id: String) -> bool
{ {
if !full_path.exists() { if !full_path.exists() {

View File

@ -1,7 +1,7 @@
use std::process::Command; use std::process::Command;
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use std::fs::{self, File, Permissions}; use std::fs::{self, File, Permissions};
use std::io::{BufReader, BufRead, Write}; use std::io::Write;
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;

View File

@ -1,4 +1,5 @@
#[cfg(test)]
pub fn lines_should_not_contains(lines: Vec<String>, str: &str) { pub fn lines_should_not_contains(lines: Vec<String>, str: &str) {
for line in lines { for line in lines {
if line.find(str).is_some() { if line.find(str).is_some() {

View File

@ -1,5 +1,6 @@
use rand::{distributions::Alphanumeric, Rng}; use rand::{distributions::Alphanumeric, Rng};
#[cfg(test)]
pub fn get_random_test_id() -> String { pub fn get_random_test_id() -> String {
let mut id: String = rand::thread_rng() let mut id: String = rand::thread_rng()
.sample_iter(&Alphanumeric) .sample_iter(&Alphanumeric)