5 files changed
+68
-13
Cargo.lock
+11
@@ -309,6 +309,15 @@ version = "0.3.1"
309
source = "registry+https://github.com/rust-lang/crates.io-index"
310
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
311
312
+[[package]]
313
+name = "home"
314
+version = "0.5.4"
315
+source = "registry+https://github.com/rust-lang/crates.io-index"
316
+checksum = "747309b4b440c06d57b0b25f2aee03ee9b5e5397d288c60e21fc709bb98a7408"
317
+dependencies = [
318
+ "winapi",
319
+]
320
+
321
[[package]]
322
name = "http"
323
version = "0.2.9"
@@ -932,8 +941,10 @@ dependencies = [
941
"anyhow",
942
"clap",
943
"console",
944
+ "home",
945
"reqwest",
946
"serde",
947
+ "serde_json",
948
"tokio",
949
"tracing",
950
"tracing-bunyan-formatter",
Cargo.toml
+3
-1
@@ -17,9 +17,11 @@ path = "src/main.rs"
17
anyhow = "1.0.58"
18
clap = { version = "4.1.6", features = ["derive"] }
19
console = "0.15.0"
20
+home = "0.5.4"
21
reqwest = { version = "0.11", features = ["json"] }
22
serde = { version = "1", features = ["derive"] }
23
+serde_json = "1.0.82"
24
tokio = { version = "1.14.1", features = ["full"] }
25
tracing = { version = "0.1.35", features = ["log"] }
26
tracing-bunyan-formatter = "0.3.3"
25
-tracing-subscriber = { version = "0.3.14", features = ["registry", "env-filter"] }
\ No newline at end of file
27
+tracing-subscriber = { version = "0.3.14", features = ["registry", "env-filter"] }
src/constants.rs
+2
-1
@@ -6,4 +6,5 @@ pub const BASE_URL: &'static str = "http://localhost:3000";
6
#[cfg(not(debug_assertions))]
7
pub const BASE_URL: &'static str = "https://api.smbpndk.com/";
8
9
-pub const GUARD_EMOJI: Emoji<'_, '_> = Emoji("🛡 ", "");
9
+pub const OK_EMOJI: Emoji<'_, '_> = Emoji("✅ ", "");
10
+pub const ERROR_EMOJI: Emoji<'_, '_> = Emoji("❌ ", "");
src/login/process.rs
+48
-9
@@ -1,4 +1,9 @@
1
-use anyhow::Result;
1
+use std::{
2
+ fs::{create_dir_all, OpenOptions},
3
+ io::Write,
4
+};
5
+
6
+use anyhow::{anyhow, Result};
7
use reqwest::Client;
8
use serde::{Deserialize, Serialize};
9
@@ -22,8 +27,21 @@ struct User {
27
28
#[derive(Debug, Serialize, Deserialize)]
29
struct LoginResult {
25
- status: i32,
26
- error: String,
30
+ status: Status,
31
+ data: Data,
32
+}
33
+
34
+#[derive(Debug, Serialize, Deserialize)]
35
+struct Status {
36
+ code: i32,
37
+ message: String,
38
+}
39
+
40
+#[derive(Debug, Serialize, Deserialize)]
41
+struct Data {
42
+ id: i32,
43
+ email: String,
44
+ created_at: String,
45
}
46
47
pub async fn process_login(args: LoginArgs) -> Result<()> {
@@ -42,14 +60,35 @@ pub async fn process_login(args: LoginArgs) -> Result<()> {
60
61
match response.status() {
62
reqwest::StatusCode::OK => {
45
- let login_result: LoginResult = response.json().await?;
46
- println!("Login result: {:?}", login_result);
47
- }
48
- reqwest::StatusCode::UNAUTHORIZED => {
49
- println!("UNAUTHORIZED");
63
+ let headers = response.headers();
64
+ match headers.get("Authorization") {
65
+ Some(token) => {
66
+ println!("Token: {:?}", token.to_str()?);
67
+ match home::home_dir() {
68
+ Some(path) => {
69
+ print!("Home directory: {:?}", path);
70
+ create_dir_all(path.join(".smb"))?;
71
+ let mut file = OpenOptions::new()
72
+ .create(true)
73
+ .write(true)
74
+ .open([path.to_str().unwrap(), "/.smb/token"].join(""))?;
75
+ file.write_all(token.to_str()?.as_bytes())?;
76
+ }
77
+ None => {
78
+ let error = anyhow!("Failed to get home directory.");
79
+ return Err(error);
80
+ }
81
+ }
82
+ }
83
+ None => {
84
+ let error = anyhow!("Failed to get token. Probably a backend issue.");
85
+ return Err(error);
86
+ }
87
+ }
88
}
89
_ => {
52
- println!("Login failed: {:?}", response);
90
+ let error = anyhow!("Failed to log in. Check your username and password.");
91
+ return Err(error);
92
}
93
}
94
src/main.rs
+4
-2
@@ -2,8 +2,10 @@ use std::{fs::OpenOptions, path::PathBuf, str::FromStr};
2
3
use anyhow::{anyhow, Result};
4
use clap::Parser;
5
+use console::style;
6
use smbpndk_cli::{
7
cli::{Cli, Commands},
8
+ constants::{ERROR_EMOJI, OK_EMOJI},
9
login::{process_login, LoginArgs},
10
};
11
use tracing::subscriber::set_global_default;
@@ -42,10 +44,10 @@ fn setup_logging(level: Option<EnvFilter>) -> Result<()> {
44
async fn main() {
45
match run().await {
46
Ok(_) => {
45
- println!("Command successful.");
47
+ println!("\n{} {}", OK_EMOJI, style("Command successful.").green());
48
}
49
Err(e) => {
48
- eprintln!("{}", e);
50
+ println!("\n{} {}", ERROR_EMOJI, style(e).red());
51
std::process::exit(1);
52
}
53
}