7 files changed
+25
-20
docs/debugging.md
+8
@@ -4,4 +4,12 @@ To debug the app on VSCode, use [CodeLLDB](https://marketplace.visualstudio.com/
4
5
```bash
6
$ cargo debugger --manifest-path smbcloud-cli/Cargo.toml -- account signup
7
+```
8
+
9
+
10
+## Environment
11
+
12
+Default environment is production. To run against dev environemt:
13
+```bash
14
+$ cargo run -- -e dev account login
15
```
\ No newline at end of file
smbcloud-cli/src/account/lib.rs
+4
-4
@@ -161,7 +161,7 @@ pub async fn process_connect_github(env: Environment, code: String) -> Result<Sm
161
StatusCode::OK => {
162
// Account authorized and token received
163
spinner.stop_and_persist("✅", "You are logged in with your GitHub account!".into());
164
- save_token(&response).await?;
164
+ save_token(env, &response).await?;
165
let result = response.json().await?;
166
// println!("Result: {:#?}", &result);
167
Ok(result)
@@ -215,7 +215,7 @@ fn github_base_url_builder() -> URLBuilder {
215
url_builder
216
}
217
218
-pub async fn save_token(response: &Response) -> Result<()> {
218
+pub async fn save_token(env: Environment, response: &Response) -> Result<()> {
219
let headers = response.headers();
220
// println!("Headers: {:#?}", &headers);
221
match headers.get("Authorization") {
@@ -224,11 +224,11 @@ pub async fn save_token(response: &Response) -> Result<()> {
224
match home::home_dir() {
225
Some(path) => {
226
debug!("{}", path.to_str().unwrap());
227
- create_dir_all(path.join(".smb"))?;
227
+ create_dir_all(path.join(env.smb_dir()))?;
228
let mut file = OpenOptions::new()
229
.create(true)
230
.write(true)
231
- .open([path.to_str().unwrap(), "/.smb/token"].join(""))?;
231
+ .open([path.to_str().unwrap(), "/", &env.smb_dir(), "/token"].join(""))?;
232
file.write_all(token.to_str()?.as_bytes())?;
233
Ok(())
234
}
smbcloud-cli/src/account/login/process.rs
+4
-4
@@ -30,7 +30,7 @@ use std::fs::{self};
30
31
pub async fn process_login(env: Environment) -> Result<CommandResult> {
32
// Check if token file exists
33
- if smb_token_file_path().is_some() {
33
+ if smb_token_file_path(env).is_some() {
34
return Ok(CommandResult {
35
spinner: Spinner::new(
36
spinners::Spinners::SimpleDotsScrolling,
@@ -63,7 +63,7 @@ pub async fn process_login(env: Environment) -> Result<CommandResult> {
63
64
pub async fn process_logout(env: Environment) -> Result<CommandResult> {
65
// Logout if user confirms
66
- if let Some(token_path) = smb_token_file_path() {
66
+ if let Some(token_path) = smb_token_file_path(env) {
67
let confirm = match Confirm::with_theme(&ColorfulTheme::default())
68
.with_prompt("Do you want to logout? y/n")
69
.interact()
@@ -374,7 +374,7 @@ async fn do_process_login(env: Environment, args: LoginArgs) -> Result<CommandRe
374
match response.status() {
375
StatusCode::OK => {
376
// Login successful
377
- save_token(&response).await?;
377
+ save_token(env, &response).await?;
378
Ok(CommandResult {
379
spinner: Spinner::new(
380
spinners::Spinners::SimpleDotsScrolling,
@@ -546,7 +546,7 @@ async fn input_reset_password_token(env: Environment) -> Result<CommandResult> {
546
}
547
548
async fn do_process_logout(env: Environment) -> Result<()> {
549
- let token = get_smb_token().await?;
549
+ let token = get_smb_token(env).await?;
550
551
let response = Client::new()
552
.delete(build_smb_logout_url(env))
smbcloud-cli/src/account/signup/process.rs
+1
-1
@@ -15,7 +15,7 @@ use spinners::Spinner;
15
16
pub async fn process_signup(env: Environment) -> Result<CommandResult> {
17
// Check if token file exists
18
- if smb_token_file_path().is_some() {
18
+ if smb_token_file_path(env).is_some() {
19
return Ok(CommandResult {
20
spinner: Spinner::new(
21
spinners::Spinners::SimpleDotsScrolling,
smbcloud-networking-project/src/lib.rs
+4
-4
@@ -9,7 +9,7 @@ use smbcloud_networking::{environment::Environment, get_smb_token, smb_base_url_
9
10
pub async fn get_all(env: Environment) -> Result<Vec<Project>> {
11
// Get current token
12
- let token = get_smb_token().await?;
12
+ let token = get_smb_token(env).await?;
13
14
debug!("Current token: {}", token);
15
@@ -31,7 +31,7 @@ pub async fn get_all(env: Environment) -> Result<Vec<Project>> {
31
32
pub async fn create_project(env: Environment, project: ProjectCreate) -> Result<Project> {
33
// Get current token
34
- let token = get_smb_token().await?;
34
+ let token = get_smb_token(env).await?;
35
36
let response = Client::new()
37
.post(build_project_url(env))
@@ -52,7 +52,7 @@ pub async fn create_project(env: Environment, project: ProjectCreate) -> Result<
52
53
pub async fn get_project(env: Environment, id: String) -> Result<Project> {
54
// Get current token
55
- let token = get_smb_token().await?;
55
+ let token = get_smb_token(env).await?;
56
57
let response = Client::new()
58
.get(build_project_url_with_id(env, id))
@@ -72,7 +72,7 @@ pub async fn get_project(env: Environment, id: String) -> Result<Project> {
72
73
pub async fn delete_project(env: Environment, id: String) -> Result<()> {
74
// Get current token
75
- let token = get_smb_token().await?;
75
+ let token = get_smb_token(env).await?;
76
77
let response = Client::new()
78
.delete(build_project_url_with_id(env, id))
smbcloud-networking/src/constants.rs
-2
@@ -1,5 +1,3 @@
1
-pub const TOKEN_PATH_STR: &str = ".smb/token";
2
-
1
#[cfg(debug_assertions)]
2
pub const GH_OAUTH_CLIENT_ID: &str = "053e152f1b78ecee552b";
3
#[cfg(not(debug_assertions))]
smbcloud-networking/src/lib.rs
+4
-5
@@ -1,7 +1,6 @@
1
pub mod constants;
2
pub mod environment;
3
4
-use crate::constants::TOKEN_PATH_STR;
4
use anyhow::{anyhow, Result};
5
use constants::{SMB_CLIENT_ID, SMB_CLIENT_SECRET};
6
use environment::Environment;
@@ -9,8 +8,8 @@ use log::debug;
8
use std::path::PathBuf;
9
use url_builder::URLBuilder;
10
12
-pub async fn get_smb_token() -> Result<String> {
13
- if let Some(path) = smb_token_file_path() {
11
+pub async fn get_smb_token(env: Environment) -> Result<String> {
12
+ if let Some(path) = smb_token_file_path(env) {
13
std::fs::read_to_string(path).map_err(|e| {
14
debug!("Error while reading token: {}", &e);
15
anyhow!("Error while reading token. Are you logged in?")
@@ -20,11 +19,11 @@ pub async fn get_smb_token() -> Result<String> {
19
}
20
}
21
23
-pub fn smb_token_file_path() -> Option<PathBuf> {
22
+pub fn smb_token_file_path(env: Environment) -> Option<PathBuf> {
23
match home::home_dir() {
24
Some(path) => {
25
debug!("Home directory: {}.", path.to_str().unwrap());
27
- let token_path = path.join(TOKEN_PATH_STR);
26
+ let token_path = path.join(env.smb_dir()).join("/token");
27
if token_path.exists() && token_path.is_file() {
28
return Some(token_path);
29
}