start with init command

This commit is contained in:
grimhilt 2023-05-31 23:08:33 +02:00
parent 66ee1e9191
commit 9341a6db7b
8 changed files with 193 additions and 97 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
target target
*.test *.test
.env .env
todo

72
Cargo.lock generated
View File

@ -2,6 +2,26 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 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]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -44,6 +64,21 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 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]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.3" version = "0.9.3"
@ -208,6 +243,15 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" 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]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.2.6" version = "0.2.6"
@ -444,6 +488,7 @@ dependencies = [
name = "next-sync" name = "next-sync"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap",
"dotenv", "dotenv",
"reqwest", "reqwest",
"tokio", "tokio",
@ -751,6 +796,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.18" version = "2.0.18"
@ -775,6 +826,15 @@ dependencies = [
"windows-sys 0.45.0", "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]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"
@ -906,6 +966,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]] [[package]]
name = "url" name = "url"
version = "2.3.1" version = "2.3.1"
@ -923,6 +989,12 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View File

@ -9,3 +9,4 @@ edition = "2021"
reqwest = { version = "0.11", features = ["blocking", "json", "multipart"] } reqwest = { version = "0.11", features = ["blocking", "json", "multipart"] }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
dotenv ="0.15.0" dotenv ="0.15.0"
clap = "2.33"

1
src/commands.rs Normal file
View File

@ -0,0 +1 @@
pub mod init;

9
src/commands/init.rs Normal file
View File

@ -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"),
}
}

View File

@ -1,100 +1,28 @@
use reqwest::Client; //use reqwest::Client;
use std::fs::File; //use std::fs::File;
use std::io::Read; //use std::io::Read;
use reqwest::header::{HeaderValue, CONTENT_TYPE, HeaderMap}; //use reqwest::header::{HeaderValue, CONTENT_TYPE, HeaderMap};
use std::error::Error; //use std::error::Error;
use std::env; //use std::env;
use dotenv::dotenv; //use dotenv::dotenv;
async fn send_propfind_request() -> Result<(), Box<dyn Error>> {
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#"<?xml version="1.0" encoding="UTF-8"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<d:getlastmodified/>
<d:getcontentlength/>
<d:getcontenttype/>
<oc:permissions/>
<d:resourcetype/>
<d:getetag/>
</d:prop>
</d:propfind>"#;
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<dyn Error>> {
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 clap::{App, SubCommand};
mod commands;
fn main() { fn main() {
tokio::runtime::Runtime::new().unwrap().block_on(async { let matches = App::new("NextSync")
if let Err(err) = upload_file("tkt").await { .version("1.0")
eprintln!("Error: {}", err); .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);
// }
//});
} }

50
src/services/details.rs Normal file
View File

@ -0,0 +1,50 @@
async fn send_propfind_request() -> Result<(), Box<dyn Error>> {
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#"<?xml version="1.0" encoding="UTF-8"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<d:getlastmodified/>
<d:getcontentlength/>
<d:getcontenttype/>
<oc:permissions/>
<d:resourcetype/>
<d:getetag/>
</d:prop>
</d:propfind>"#;
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(())
}

View File

@ -0,0 +1,34 @@
async fn upload_file(path: &str) -> Result<(), Box<dyn Error>> {
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(())
}