12 files changed
+123
-116
smbcloud-cli/src/account/lib.rs
+4
-4
@@ -245,9 +245,9 @@ pub async fn save_token(env: Environment, response: &Response) -> Result<()> {
245
pub async fn protected_request(env: Environment) -> Result<()> {
246
// Check if token file exists
247
if smb_token_file_path(env).is_none() {
248
- return Err(anyhow!(
249
- fail_message("Please authorize your account first with `smb account login` command.")
250
- ));
248
+ return Err(anyhow!(fail_message(
249
+ "Please authorize your account first with `smb account login` command."
250
+ )));
251
}
252
Ok(())
253
-}
\ No newline at end of file
253
+}
smbcloud-cli/src/account/login/process.rs
+16
-12
@@ -22,7 +22,8 @@ use smbcloud_networking::{
22
PATH_LINK_GITHUB_ACCOUNT, PATH_RESEND_CONFIRMATION, PATH_RESET_PASSWORD_INSTRUCTIONS,
23
PATH_USERS_PASSWORD, PATH_USERS_SIGN_IN,
24
},
25
- environment::Environment, smb_base_url_builder, smb_token_file_path,
25
+ environment::Environment,
26
+ smb_base_url_builder, smb_token_file_path,
27
};
28
use smbcloud_utils::email_validation;
29
use spinners::Spinner;
@@ -307,11 +308,15 @@ async fn do_process_login(env: Environment, args: LoginArgs) -> Result<CommandRe
308
},
309
};
310
310
- let response = Client::new()
311
+ let response = match Client::new()
312
.post(build_smb_login_url(env))
313
.json(&login_params)
314
.send()
314
- .await?;
315
+ .await
316
+ {
317
+ Ok(response) => response,
318
+ Err(_) => return Err(anyhow!(fail_message("Check your internet connection."))),
319
+ };
320
321
match response.status() {
322
StatusCode::OK => {
@@ -333,8 +338,8 @@ async fn do_process_login(env: Environment, args: LoginArgs) -> Result<CommandRe
338
spinners::Spinners::SimpleDotsScrolling,
339
style("Account not found.").green().bold().to_string(),
340
),
336
- symbol: style("✔").green().to_string(),
337
- msg: "Please signup!".to_owned(),
341
+ symbol: fail_symbol(),
342
+ msg: fail_message("Please signup!"),
343
})
344
}
345
StatusCode::UNPROCESSABLE_ENTITY => {
@@ -343,7 +348,9 @@ async fn do_process_login(env: Environment, args: LoginArgs) -> Result<CommandRe
348
// println!("Result: {:#?}", &result);
349
verify_or_set_password(&env, result).await
350
}
346
- _ => Err(anyhow!("Login failed. Check your username and password.")),
351
+ _ => Err(anyhow!(fail_message(
352
+ "Login failed. Check your username and password."
353
+ ))),
354
}
355
}
356
@@ -477,13 +484,10 @@ async fn input_reset_password_token(env: Environment) -> Result<CommandResult> {
484
match response.status() {
485
StatusCode::OK => Ok(CommandResult {
486
spinner,
480
- symbol: "✅".to_owned(),
481
- msg: "Password reset!".to_owned(),
487
+ symbol: succeed_symbol(),
488
+ msg: succeed_message("Password reset!"),
489
}),
483
- _ => {
484
- let error = anyhow!("Failed to reset password.");
485
- Err(error)
486
- }
490
+ _ => Err(anyhow!(fail_message("Failed to reset password."))),
491
}
492
}
493
smbcloud-cli/src/account/logout/mod.rs
+73
-67
@@ -1,85 +1,91 @@
1
-use std::fs;
1
+use anyhow::{anyhow, Result};
2
use console::style;
3
use dialoguer::{theme::ColorfulTheme, Confirm};
4
use reqwest::{Client, StatusCode};
5
-use smbcloud_networking::{constants::PATH_USERS_SIGN_OUT, environment::Environment, get_smb_token, smb_base_url_builder, smb_token_file_path};
5
+use smbcloud_networking::{
6
+ constants::PATH_USERS_SIGN_OUT, environment::Environment, get_smb_token, smb_base_url_builder,
7
+ smb_token_file_path,
8
+};
9
use spinners::Spinner;
7
-use anyhow::{anyhow, Result};
10
+use std::fs;
11
9
-use crate::{cli::CommandResult, ui::{fail_message, fail_symbol, succeed_message, succeed_symbol}};
12
+use crate::{
13
+ cli::CommandResult,
14
+ ui::{fail_message, fail_symbol, succeed_message, succeed_symbol},
15
+};
16
17
pub async fn process_logout(env: Environment) -> Result<CommandResult> {
12
- // Logout if user confirms
13
- if let Some(token_path) = smb_token_file_path(env) {
14
- let confirm = match Confirm::with_theme(&ColorfulTheme::default())
15
- .with_prompt("Do you want to logout? y/n")
16
- .interact()
17
- {
18
- Ok(confirm) => confirm,
19
- Err(_) => {
20
- let error = anyhow!("Invalid input.");
21
- return Err(error);
22
- }
23
- };
24
- if !confirm {
25
- return Ok(CommandResult {
26
- spinner: Spinner::new(
27
- spinners::Spinners::SimpleDotsScrolling,
28
- style("Cancel operation.").green().bold().to_string(),
29
- ),
30
- symbol: succeed_symbol(),
31
- msg: "Doing nothing.".to_owned(),
32
- });
33
- }
18
+ // Logout if user confirms
19
+ if let Some(token_path) = smb_token_file_path(env) {
20
+ let confirm = match Confirm::with_theme(&ColorfulTheme::default())
21
+ .with_prompt("Do you want to logout? y/n")
22
+ .interact()
23
+ {
24
+ Ok(confirm) => confirm,
25
+ Err(_) => {
26
+ let error = anyhow!("Invalid input.");
27
+ return Err(error);
28
+ }
29
+ };
30
+ if !confirm {
31
+ return Ok(CommandResult {
32
+ spinner: Spinner::new(
33
+ spinners::Spinners::SimpleDotsScrolling,
34
+ style("Cancel operation.").green().bold().to_string(),
35
+ ),
36
+ symbol: succeed_symbol(),
37
+ msg: "Doing nothing.".to_owned(),
38
+ });
39
+ }
40
35
- let spinner = Spinner::new(
36
- spinners::Spinners::SimpleDotsScrolling,
37
- style("Logging you out...").green().bold().to_string(),
38
- );
41
+ let spinner = Spinner::new(
42
+ spinners::Spinners::SimpleDotsScrolling,
43
+ style("Logging you out...").green().bold().to_string(),
44
+ );
45
40
- // Call backend
41
- match do_process_logout(env).await {
42
- Ok(_) => {
43
- fs::remove_file(token_path)?;
44
- Ok(CommandResult {
45
- spinner,
46
- symbol: succeed_symbol(),
47
- msg: succeed_message("You are logged out!"),
48
- })
49
- }
50
- Err(e) => Err(anyhow!("{e}")),
51
- }
52
- } else {
53
- Ok(CommandResult {
54
- spinner: Spinner::new(
55
- spinners::Spinners::SimpleDotsScrolling,
56
- style("Loading...").green().bold().to_string(),
57
- ),
58
- symbol: fail_symbol(),
59
- msg: fail_message("You are not logged in. Please login first."),
60
- })
61
- }
46
+ // Call backend
47
+ match do_process_logout(env).await {
48
+ Ok(_) => {
49
+ fs::remove_file(token_path)?;
50
+ Ok(CommandResult {
51
+ spinner,
52
+ symbol: succeed_symbol(),
53
+ msg: succeed_message("You are logged out!"),
54
+ })
55
+ }
56
+ Err(e) => Err(anyhow!("{e}")),
57
+ }
58
+ } else {
59
+ Ok(CommandResult {
60
+ spinner: Spinner::new(
61
+ spinners::Spinners::SimpleDotsScrolling,
62
+ style("Loading...").green().bold().to_string(),
63
+ ),
64
+ symbol: fail_symbol(),
65
+ msg: fail_message("You are not logged in. Please login first."),
66
+ })
67
+ }
68
}
69
70
async fn do_process_logout(env: Environment) -> Result<()> {
65
- let token = get_smb_token(env).await?;
71
+ let token = get_smb_token(env).await?;
72
67
- let response = Client::new()
68
- .delete(build_smb_logout_url(env))
69
- .header("Authorization", token)
70
- .header("Accept", "application/json")
71
- .header("Content-Type", "application/x-www-form-urlencoded")
72
- .send()
73
- .await?;
73
+ let response = Client::new()
74
+ .delete(build_smb_logout_url(env))
75
+ .header("Authorization", token)
76
+ .header("Accept", "application/json")
77
+ .header("Content-Type", "application/x-www-form-urlencoded")
78
+ .send()
79
+ .await?;
80
75
- match response.status() {
76
- StatusCode::OK => Ok(()),
77
- _ => Err(anyhow!("Failed to logout.")),
78
- }
81
+ match response.status() {
82
+ StatusCode::OK => Ok(()),
83
+ _ => Err(anyhow!("Failed to logout.")),
84
+ }
85
}
86
87
fn build_smb_logout_url(env: Environment) -> String {
82
- let mut url_builder = smb_base_url_builder(env);
83
- url_builder.add_route(PATH_USERS_SIGN_OUT);
84
- url_builder.build()
88
+ let mut url_builder = smb_base_url_builder(env);
89
+ url_builder.add_route(PATH_USERS_SIGN_OUT);
90
+ url_builder.build()
91
}
smbcloud-cli/src/account/mod.rs
+2
-7
@@ -2,15 +2,10 @@ pub mod cli;
2
pub mod forgot;
3
pub mod lib;
4
pub mod login;
5
-pub mod signup;
5
pub mod logout;
6
+pub mod signup;
7
8
-use self::{
9
- cli::Commands,
10
- forgot::process_forgot,
11
- login::process_login,
12
- signup::process_signup,
13
-};
8
+use self::{cli::Commands, forgot::process_forgot, login::process_login, signup::process_signup};
9
use crate::cli::CommandResult;
10
use anyhow::Result;
11
use logout::process_logout;
smbcloud-cli/src/cli/mod.rs
+2
-2
@@ -9,7 +9,7 @@ pub struct CommandResult {
9
pub msg: String,
10
}
11
12
-impl CommandResult {
12
+impl CommandResult {
13
pub fn stop_and_persist(mut self) {
14
self.spinner.stop_and_persist(&self.symbol, self.msg);
15
}
@@ -51,7 +51,7 @@ pub enum Commands {
51
},
52
53
#[clap(about = "Initialize project. Requires an smbCloud account.")]
54
- Init { },
54
+ Init {},
55
56
#[clap(about = "Deploy project. It will use deploy.sh script in the .smb folder.")]
57
Deploy {},
smbcloud-cli/src/deploy/git.rs
+14
-8
@@ -1,4 +1,4 @@
1
-use anyhow::{Result, anyhow};
1
+use anyhow::{anyhow, Result};
2
use console::style;
3
use git2::{Remote, Repository};
4
use spinners::Spinner;
@@ -15,18 +15,24 @@ pub async fn remote_deployment_setup(repo: &Repository, repo_name: String) -> Re
15
let smbcloud = match repo.find_remote("smbcloud") {
16
Ok(remote) => remote,
17
Err(_) => {
18
- spinner.stop_and_persist("😩", "Remote deployment is not setup. Will setup remote deployment.".to_owned());
18
+ spinner.stop_and_persist(
19
+ "😩",
20
+ "Remote deployment is not setup. Will setup remote deployment.".to_owned(),
21
+ );
22
// Present the user with a message to setup remote deployment
20
- repo.remote("smbcloud", &format!("deploy@api.smbcloud.xyz:git/{}", repo_name))
21
- .map_err(|e: git2::Error| {
22
- spinner.stop_and_persist("😩", e.to_string());
23
- anyhow!("Failed to setup remote deployment: {e}")
24
- })?;
23
+ repo.remote(
24
+ "smbcloud",
25
+ &format!("deploy@api.smbcloud.xyz:git/{}", repo_name),
26
+ )
27
+ .map_err(|e: git2::Error| {
28
+ spinner.stop_and_persist("😩", e.to_string());
29
+ anyhow!("Failed to setup remote deployment: {e}")
30
+ })?;
31
return Err(anyhow!(
32
"Remote deployment is not setup. Please run `git remote add smbcloud <url>`."
33
));
34
}
29
- };
35
+ };
36
37
spinner.stop_and_persist("🚀", "Remote deployment setup complete.".to_owned());
38
smbcloud-cli/src/deploy/mod.rs
+3
-3
@@ -1,7 +1,6 @@
1
mod git;
2
mod smb_config;
3
4
-use std::{fs::File, io::BufReader};
4
use crate::{account::lib::protected_request, cli::CommandResult};
5
use anyhow::Result;
6
use console::style;
@@ -12,6 +11,7 @@ use smb_config::check_config;
11
use smbcloud_networking::environment::Environment;
12
use spinners::Spinner;
13
use ssh2_config::{ParseRule, SshConfig};
14
+use std::{fs::File, io::BufReader};
15
16
pub async fn process_deploy(env: Environment) -> Result<CommandResult> {
17
protected_request(env).await?;
@@ -20,7 +20,7 @@ pub async fn process_deploy(env: Environment) -> Result<CommandResult> {
20
let mut spinner = Spinner::new(
21
spinners::Spinners::SimpleDotsScrolling,
22
style("Deploying...").green().bold().to_string(),
23
- );
23
+ );
24
25
let repo = match Repository::open(".") {
26
Ok(repo) => repo,
@@ -188,7 +188,7 @@ pub async fn process_deploy(env: Environment) -> Result<CommandResult> {
188
}
189
};
190
*/
191
-
191
+
192
Ok(CommandResult {
193
spinner,
194
symbol: "🚀".to_owned(),
smbcloud-cli/src/lib.rs
+1
-1
@@ -2,4 +2,4 @@ pub mod account;
2
pub mod cli;
3
pub mod deploy;
4
pub mod project;
5
-mod ui;
\ No newline at end of file
5
+mod ui;
smbcloud-cli/src/main.rs
+1
-1
@@ -115,7 +115,7 @@ async fn run() -> Result<CommandResult> {
115
Commands::Login {} => process_login(cli.environment).await,
116
Commands::Logout {} => process_logout(cli.environment).await,
117
Commands::Project { command } => process_project(cli.environment, command).await,
118
- Commands::Init { } => { process_project_init(cli.environment).await }
118
+ Commands::Init {} => process_project_init(cli.environment).await,
119
Commands::Deploy {} => process_deploy(cli.environment).await,
120
}
121
}
smbcloud-cli/src/project/mod.rs
+2
-6
@@ -8,9 +8,7 @@ use console::style;
8
use dialoguer::{theme::ColorfulTheme, Input};
9
use init::process_project_init;
10
use log::debug;
11
-use smbcloud_model::
12
- project::{Config, Project}
13
-;
11
+use smbcloud_model::project::{Config, Project};
12
use smbcloud_networking::environment::Environment;
13
use smbcloud_networking_project::{delete_project, get_all, get_project};
14
use spinners::Spinner;
@@ -18,9 +16,7 @@ use std::{fs::OpenOptions, io::Write};
16
17
pub async fn process_project(env: Environment, commands: Commands) -> Result<CommandResult> {
18
match commands {
21
- Commands::New {} => {
22
- process_project_init(env).await
23
- }
19
+ Commands::New {} => process_project_init(env).await,
20
Commands::List {} => {
21
let mut spinner = Spinner::new(
22
spinners::Spinners::SimpleDotsScrolling,
smbcloud-cli/src/ui/mod.rs
+4
-4
@@ -1,17 +1,17 @@
1
use console::style;
2
3
pub fn succeed_symbol() -> String {
4
- style("✔").green().to_string()
4
+ style("✔").green().to_string()
5
}
6
7
pub fn fail_symbol() -> String {
8
- style("✘").for_stderr().red().to_string()
8
+ style("✘").for_stderr().red().to_string()
9
}
10
11
pub fn succeed_message(message: &str) -> String {
12
- style(message).bold().white().to_string()
12
+ style(message).bold().white().to_string()
13
}
14
15
pub fn fail_message(message: &str) -> String {
16
- style(message).italic().red().to_string()
16
+ style(message).italic().red().to_string()
17
}
smbcloud-networking/src/constants.rs
+1
-1
@@ -17,4 +17,4 @@ pub const PATH_USERS_SIGN_IN: &str = "v1/users/sign_in";
17
pub const PATH_LINK_GITHUB_ACCOUNT: &str = "v1/link_github_account";
18
pub const PATH_RESET_PASSWORD_INSTRUCTIONS: &str = "v1/resend_reset_password_instructions";
19
pub const PATH_RESEND_CONFIRMATION: &str = "v1/resend_confirmation";
20
-pub const PATH_AUTHORIZE: &str = "v1/authorize";
\ No newline at end of file
20
+pub const PATH_AUTHORIZE: &str = "v1/authorize";