Added a way to save files easily

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-26 11:02:31 +02:00
parent c37beb32c2
commit 16f52a330b
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
4 changed files with 52 additions and 7 deletions

31
Cargo.lock generated
View File

@ -140,6 +140,26 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "dotenv"
version = "0.15.0"
@ -399,6 +419,7 @@ name = "lychee_client"
version = "0.1.0"
dependencies = [
"clap",
"dirs",
"dotenv",
"hyper",
"hyper-tls",
@ -658,6 +679,16 @@ dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
dependencies = [
"getrandom",
"redox_syscall",
]
[[package]]
name = "regex"
version = "1.5.4"

View File

@ -13,6 +13,7 @@ hyper-tls = "0.5.0"
dotenv = "0.15.0"
json = "0.12.4"
clap = "2.33.3"
dirs = "4.0.0"
[dev-dependencies]
mockito = "0.30.0"

View File

@ -1,3 +1,5 @@
use std::{fs::{File, create_dir, create_dir_all}, io::{Read, Write}};
use hyper_tls::HttpsConnector;
use hyper::{Body, Client, body, client::HttpConnector};
use clap::{Arg, App, SubCommand};
@ -10,8 +12,7 @@ mod albums;
pub struct LycheeClient {
client: Client<HttpsConnector<HttpConnector>>,
endpoint: String,
api_key: String,
config_path: String
api_key: String
}
pub async fn body_to_str(res: Body) -> String {
@ -25,8 +26,21 @@ fn get_config_folder() -> String {
return xdg_val + "/Lychee_CLIent";
}
fn write_to_file(d: String, p: String) {
let mut o = File::create(p).expect("Cannot create file.");
o.write_all(d.as_bytes()).expect("Cannot write to file.");
}
fn read_from_file(p: String) -> String {
let mut d = String::new();
let mut ifile = File::open(p).expect("Cannot open file.");
ifile.read_to_string(&mut d).expect("Cannot read file.");
return d;
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
create_dir_all(get_config_folder()).expect("Cannot create folder.");
let matches = App::new("Lychee CLIent")
.version("0.1.0")
.author("Louis Vallat <contact@louis-vallat.xyz>")
@ -54,8 +68,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let client = LycheeClient {
client: Client::builder().build::<_, hyper::Body>(HttpsConnector::new()),
endpoint: std::env::var("LYCHEE_ENDPOINT").unwrap(),
api_key: std::env::var("LYCHEE_API_KEY").unwrap(),
config_path: get_config_folder()
api_key: std::env::var("LYCHEE_API_KEY").unwrap()
};
@ -63,7 +76,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let username = matches.value_of("username").unwrap();
let password = matches.value_of("password").unwrap();
let lychee_session = login(&client, username, password).await;
println!("{:?}", lychee_session);
write_to_file(lychee_session, get_config_folder() + "/session");
}
Ok(())
}

View File

@ -2,6 +2,7 @@ use hyper::{Body, Method, Request, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE,
use json::object;
use crate::{LycheeClient, body_to_str};
pub async fn login(client: &LycheeClient, login: &str, password: &str) -> String {
let login_data = object! { "username": login, "password": password };
@ -46,8 +47,7 @@ mod session_tests {
return LycheeClient {
client: Client::builder().build::<_, hyper::Body>(https),
endpoint: mockito::server_url(),
api_key: "value".to_string(),
config_path: "".to_string()
api_key: "value".to_string()
};
}