diff --git a/.gitignore b/.gitignore index c3dc704..10885cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target *.test .env +todo diff --git a/Cargo.lock b/Cargo.lock index 9ed46a7..c726443 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,26 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -44,6 +64,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -208,6 +243,15 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.2.6" @@ -444,6 +488,7 @@ dependencies = [ name = "next-sync" version = "0.1.0" dependencies = [ + "clap", "dotenv", "reqwest", "tokio", @@ -751,6 +796,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "syn" version = "2.0.18" @@ -775,6 +826,15 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -906,6 +966,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "url" version = "2.3.1" @@ -923,6 +989,12 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 9a782e1..852bd95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" reqwest = { version = "0.11", features = ["blocking", "json", "multipart"] } tokio = { version = "1", features = ["full"] } dotenv ="0.15.0" +clap = "2.33" diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..43763f1 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1 @@ +pub mod init; diff --git a/src/commands/init.rs b/src/commands/init.rs new file mode 100644 index 0000000..9e1d76b --- /dev/null +++ b/src/commands/init.rs @@ -0,0 +1,9 @@ +use std::fs::DirBuilder; + +pub fn init() { + let builder = DirBuilder::new(); + match builder.create("./test/dir") { + Ok(()) => println!("Directory successfuly created"), + Err(_) => println!("Error: cannot create directory"), + } +} diff --git a/src/main.rs b/src/main.rs index 90719d0..c533a02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,100 +1,28 @@ -use reqwest::Client; -use std::fs::File; -use std::io::Read; -use reqwest::header::{HeaderValue, CONTENT_TYPE, HeaderMap}; -use std::error::Error; -use std::env; -use dotenv::dotenv; - -async fn send_propfind_request() -> Result<(), Box> { - dotenv().ok(); - - let mut api_endpoint = env::var("HOST").unwrap().to_owned(); - api_endpoint.push_str("/remote.php/dav/files/"); - let username = env::var("USERNAME").unwrap(); - api_endpoint.push_str(&username); - api_endpoint.push_str("/test"); - let password = env::var("PASSWORD").unwrap(); - - // Create a reqwest client - let client = Client::new(); - - // Create the XML payload - let xml_payload = r#" - - - - - - - - - - "#; - - let mut headers = HeaderMap::new(); - headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/xml")); - - - // Send the request - let response = client - .request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), api_endpoint) - .basic_auth(username, Some(password)) - .headers(headers) - .body(xml_payload) - .send() - .await?; - - // Handle the response - if response.status().is_success() { - let body = response.text().await?; - println!("Response body: {}", body); - } else { - println!("Request failed with status code: {}", response.status()); - } - - Ok(()) -} - -async fn upload_file(path: &str) -> Result<(), Box> { - dotenv().ok(); - - let mut api_endpoint = env::var("HOST").unwrap().to_owned(); - api_endpoint.push_str("/remote.php/dav/files/"); - let username = env::var("USERNAME").unwrap(); - api_endpoint.push_str(&username); - api_endpoint.push_str("/test/ok"); - let password = env::var("PASSWORD").unwrap(); - - let mut file = File::open("./file.test")?; - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer)?; - - // Create a reqwest client - let client = Client::new(); - - // Send the request - let response = client - .request(reqwest::Method::PUT, api_endpoint) - .basic_auth(username, Some(password)) - .body(buffer) - .send() - .await?; - - // Handle the response - if response.status().is_success() { - let body = response.text().await?; - println!("Response body: {}", body); - } else { - println!("Request failed with status code: {}", response.status()); - } - Ok(()) -} +//use reqwest::Client; +//use std::fs::File; +//use std::io::Read; +//use reqwest::header::{HeaderValue, CONTENT_TYPE, HeaderMap}; +//use std::error::Error; +//use std::env; +//use dotenv::dotenv; +use clap::{App, SubCommand}; +mod commands; fn main() { - tokio::runtime::Runtime::new().unwrap().block_on(async { - if let Err(err) = upload_file("tkt").await { - eprintln!("Error: {}", err); - } - }); + let matches = App::new("NextSync") + .version("1.0") + .author("grimhilt") + .about("") + .subcommand(SubCommand::with_name("init")) + .get_matches(); + + if let Some(_) = matches.subcommand_matches("init") { + commands::init::init(); + } + + //tokio::runtime::Runtime::new().unwrap().block_on(async { + // if let Err(err) = upload_file("tkt").await { + // eprintln!("Error: {}", err); + // } + //}); } diff --git a/src/services/details.rs b/src/services/details.rs new file mode 100644 index 0000000..4a650c5 --- /dev/null +++ b/src/services/details.rs @@ -0,0 +1,50 @@ +async fn send_propfind_request() -> Result<(), Box> { + dotenv().ok(); + + let mut api_endpoint = env::var("HOST").unwrap().to_owned(); + api_endpoint.push_str("/remote.php/dav/files/"); + let username = env::var("USERNAME").unwrap(); + api_endpoint.push_str(&username); + api_endpoint.push_str("/test"); + let password = env::var("PASSWORD").unwrap(); + + // Create a reqwest client + let client = Client::new(); + + // Create the XML payload + let xml_payload = r#" + + + + + + + + + + "#; + + let mut headers = HeaderMap::new(); + headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/xml")); + + + // Send the request + let response = client + .request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), api_endpoint) + .basic_auth(username, Some(password)) + .headers(headers) + .body(xml_payload) + .send() + .await?; + + // Handle the response + if response.status().is_success() { + let body = response.text().await?; + println!("Response body: {}", body); + } else { + println!("Request failed with status code: {}", response.status()); + } + + Ok(()) +} + diff --git a/src/services/upload_file.rs b/src/services/upload_file.rs new file mode 100644 index 0000000..cd3eaab --- /dev/null +++ b/src/services/upload_file.rs @@ -0,0 +1,34 @@ +async fn upload_file(path: &str) -> Result<(), Box> { + dotenv().ok(); + + let mut api_endpoint = env::var("HOST").unwrap().to_owned(); + api_endpoint.push_str("/remote.php/dav/files/"); + let username = env::var("USERNAME").unwrap(); + api_endpoint.push_str(&username); + api_endpoint.push_str("/test/ok"); + let password = env::var("PASSWORD").unwrap(); + + let mut file = File::open("./file.test")?; + let mut buffer = Vec::new(); + file.read_to_end(&mut buffer)?; + + // Create a reqwest client + let client = Client::new(); + + // Send the request + let response = client + .request(reqwest::Method::PUT, api_endpoint) + .basic_auth(username, Some(password)) + .body(buffer) + .send() + .await?; + + // Handle the response + if response.status().is_success() { + let body = response.text().await?; + println!("Response body: {}", body); + } else { + println!("Request failed with status code: {}", response.status()); + } + Ok(()) +}