@smbcloud / cli / commits / c70ee9d

refactor

Seto Elkahfi committed May 4, 2025 at 00:15 UTC c70ee9dbec4d737ed42d608b5aceaf5de529bdfb
5 files changed +97 -82
smbcloud-cli/src/account/login/process.rs
+4 -81
@@ -3,7 +3,8 @@ use crate::{
3 lib::{authorize_github, save_token},
4 signup::{do_signup, SignupMethod},
5 },
6 - cli::CommandResult, ui::{fail_message, fail_symbol, succeed_message, succeed_symbol},
6 + cli::CommandResult,
7 + ui::{fail_message, fail_symbol, succeed_message, succeed_symbol},
8 };
9 use anyhow::{anyhow, Result};
10 use console::{style, Term};
@@ -19,14 +20,12 @@ use smbcloud_model::{
20 use smbcloud_networking::{
21 constants::{
22 PATH_LINK_GITHUB_ACCOUNT, PATH_RESEND_CONFIRMATION, PATH_RESET_PASSWORD_INSTRUCTIONS,
22 - PATH_USERS_PASSWORD, PATH_USERS_SIGN_IN, PATH_USERS_SIGN_OUT,
23 + PATH_USERS_PASSWORD, PATH_USERS_SIGN_IN,
24 },
24 - environment::Environment,
25 - get_smb_token, smb_base_url_builder, smb_token_file_path,
25 + environment::Environment, smb_base_url_builder, smb_token_file_path,
26 };
27 use smbcloud_utils::email_validation;
28 use spinners::Spinner;
29 -use std::fs::{self};
29
30 pub async fn process_login(env: Environment) -> Result<CommandResult> {
31 // Check if token file exists
@@ -61,59 +60,6 @@ pub async fn process_login(env: Environment) -> Result<CommandResult> {
60 }
61 }
62
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(env) {
67 - let confirm = match Confirm::with_theme(&ColorfulTheme::default())
68 - .with_prompt("Do you want to logout? y/n")
69 - .interact()
70 - {
71 - Ok(confirm) => confirm,
72 - Err(_) => {
73 - let error = anyhow!("Invalid input.");
74 - return Err(error);
75 - }
76 - };
77 - if !confirm {
78 - return Ok(CommandResult {
79 - spinner: Spinner::new(
80 - spinners::Spinners::SimpleDotsScrolling,
81 - style("Cancel operation.").green().bold().to_string(),
82 - ),
83 - symbol: succeed_symbol(),
84 - msg: "Doing nothing.".to_owned(),
85 - });
86 - }
87 -
88 - let spinner = Spinner::new(
89 - spinners::Spinners::SimpleDotsScrolling,
90 - style("Logging you out...").green().bold().to_string(),
91 - );
92 -
93 - // Call backend
94 - match do_process_logout(env).await {
95 - Ok(_) => {
96 - fs::remove_file(token_path)?;
97 - Ok(CommandResult {
98 - spinner,
99 - symbol: succeed_symbol(),
100 - msg: succeed_message("You are logged out!"),
101 - })
102 - }
103 - Err(e) => Err(anyhow!("{e}")),
104 - }
105 - } else {
106 - Ok(CommandResult {
107 - spinner: Spinner::new(
108 - spinners::Spinners::SimpleDotsScrolling,
109 - style("Loading...").green().bold().to_string(),
110 - ),
111 - symbol: fail_symbol(),
112 - msg: "You are not logged in.".to_owned(),
113 - })
114 - }
115 -}
116 -
63 // Private functions
64
65 async fn login_with_github(env: Environment) -> Result<CommandResult> {
@@ -541,35 +487,12 @@ async fn input_reset_password_token(env: Environment) -> Result<CommandResult> {
487 }
488 }
489
544 -async fn do_process_logout(env: Environment) -> Result<()> {
545 - let token = get_smb_token(env).await?;
546 -
547 - let response = Client::new()
548 - .delete(build_smb_logout_url(env))
549 - .header("Authorization", token)
550 - .header("Accept", "application/json")
551 - .header("Content-Type", "application/x-www-form-urlencoded")
552 - .send()
553 - .await?;
554 -
555 - match response.status() {
556 - StatusCode::OK => Ok(()),
557 - _ => Err(anyhow!("Failed to logout.")),
558 - }
559 -}
560 -
490 fn build_smb_login_url(env: Environment) -> String {
491 let mut url_builder = smb_base_url_builder(env);
492 url_builder.add_route(PATH_USERS_SIGN_IN);
493 url_builder.build()
494 }
495
567 -fn build_smb_logout_url(env: Environment) -> String {
568 - let mut url_builder = smb_base_url_builder(env);
569 - url_builder.add_route(PATH_USERS_SIGN_OUT);
570 - url_builder.build()
571 -}
572 -
496 fn build_smb_resend_email_verification_url(env: Environment) -> String {
497 let mut url_builder = smb_base_url_builder(env);
498 url_builder.add_route(PATH_RESEND_CONFIRMATION);
smbcloud-cli/src/account/logout/mod.rs new
+85
@@ -0,0 +1,85 @@
1 +use std::fs;
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};
6 +use spinners::Spinner;
7 +use anyhow::{anyhow, Result};
8 +
9 +use crate::{cli::CommandResult, ui::{fail_message, fail_symbol, succeed_message, succeed_symbol}};
10 +
11 +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 + }
34 +
35 + let spinner = Spinner::new(
36 + spinners::Spinners::SimpleDotsScrolling,
37 + style("Logging you out...").green().bold().to_string(),
38 + );
39 +
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 + }
62 +}
63 +
64 +async fn do_process_logout(env: Environment) -> Result<()> {
65 + let token = get_smb_token(env).await?;
66 +
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?;
74 +
75 + match response.status() {
76 + StatusCode::OK => Ok(()),
77 + _ => Err(anyhow!("Failed to logout.")),
78 + }
79 +}
80 +
81 +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()
85 +}
smbcloud-cli/src/account/mod.rs
+3 -1
@@ -3,15 +3,17 @@ pub mod forgot;
3 pub mod lib;
4 pub mod login;
5 pub mod signup;
6 +pub mod logout;
7
8 use self::{
9 cli::Commands,
10 forgot::process_forgot,
10 - login::{process_login, process_logout},
11 + login::process_login,
12 signup::process_signup,
13 };
14 use crate::cli::CommandResult;
15 use anyhow::Result;
16 +use logout::process_logout;
17 use smbcloud_networking::environment::Environment;
18
19 pub async fn process_account(env: Environment, commands: Commands) -> Result<CommandResult> {
smbcloud-cli/src/cli/mod.rs
+3
@@ -41,6 +41,9 @@ pub enum Commands {
41 #[clap(about = "Login to your account.")]
42 Login {},
43
44 + #[clap(about = "Logout from your account.")]
45 + Logout {},
46 +
47 #[clap(about = "Manage your projects. Add, delete, edit. Need authentication.")]
48 Project {
49 #[clap(subcommand)]
smbcloud-cli/src/main.rs
+2
@@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
2 use clap::Parser;
3 use console::style;
4 use smbcloud_cli::account::login::process_login;
5 +use smbcloud_cli::account::logout::process_logout;
6 use smbcloud_cli::cli::CommandResult;
7 use smbcloud_cli::project::init::process_project_init;
8 use smbcloud_cli::{
@@ -112,6 +113,7 @@ async fn run() -> Result<CommandResult> {
113 match cli.command {
114 Commands::Account { command } => process_account(cli.environment, command).await,
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 }
119 Commands::Deploy {} => process_deploy(cli.environment).await,