diff --git a/Cargo.lock b/Cargo.lock index fddddba..f46cbac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index a4de9fd..02e41d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index 9ed1316..522f489 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>, 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> { + create_dir_all(get_config_folder()).expect("Cannot create folder."); let matches = App::new("Lychee CLIent") .version("0.1.0") .author("Louis Vallat ") @@ -54,8 +68,7 @@ async fn main() -> Result<(), Box> { 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> { 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(()) } diff --git a/src/session.rs b/src/session.rs index a7e3f45..a11e22f 100644 --- a/src/session.rs +++ b/src/session.rs @@ -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() }; }