Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/cli/src/account/logout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ async fn do_process_logout(env: Environment) -> Result<()> {
let token = get_smb_token(env)?;
match logout(env, client(), token).await {
Ok(_) => Ok(()),
Err(e) => Err(anyhow!("{e}")),
Err(e) => {
// A 401 means the session is already expired on the server.
// Treat this as success — the session is gone either way.
if e.to_string().contains("Unauthorized") {
Ok(())
} else {
Err(anyhow!("{e}"))
}
}
}
}
2 changes: 2 additions & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub mod project;
mod token;
mod ui;

pub use token::clear_smb_token::clear_smb_token;

pub(crate) fn client() -> (&'static SmbClient, &'static str) {
let secret = env!("CLI_CLIENT_SECRET");
(&SmbClient::Cli, secret)
Expand Down
28 changes: 19 additions & 9 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use {
console::style,
smbcloud_cli::{
account::{login::process_login, logout::process_logout, me::process_me, process_account},
clear_smb_token,
cli::{Cli, CommandResult, Commands},
deploy::process_deploy::process_deploy,
project::{crud_create::process_project_init, process::process_project},
Expand Down Expand Up @@ -72,25 +73,34 @@ fn setup_logging(env: Environment, level: Option<EnvFilter>) -> Result<()> {

#[tokio::main]
async fn main() {
match run().await {
let cli = Cli::parse();
let environment = cli.environment;
match run(cli).await {
Ok(result) => {
result.stop_and_persist();
std::process::exit(0);
}
Err(e) => {
println!(
"\n{} {}",
style("✘".to_string()).for_stderr().red(),
style(e).red()
);
if e.to_string().contains("Unauthorized access.") {
let _ = clear_smb_token(environment);
println!(
"\n{} {}",
style("✘".to_string()).for_stderr().red(),
style("Your session has expired. Please login again with `smb login`.").red()
);
} else {
println!(
"\n{} {}",
style("✘".to_string()).for_stderr().red(),
style(e).red()
);
}
std::process::exit(1);
}
}
}

async fn run() -> Result<CommandResult> {
let cli = Cli::parse();

async fn run(cli: Cli) -> Result<CommandResult> {
// println!("Environment: {}", cli.environment);

let log_level_error: Result<CommandResult> = Err(anyhow!(
Expand Down
11 changes: 11 additions & 0 deletions crates/cli/src/token/clear_smb_token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use {
crate::token::smb_token_file_path::smb_token_file_path, anyhow::Result,
smbcloud_network::environment::Environment, std::fs,
};

pub fn clear_smb_token(env: Environment) -> Result<()> {
if let Some(path) = smb_token_file_path(env) {
fs::remove_file(path)?;
}
Ok(())
}
1 change: 1 addition & 0 deletions crates/cli/src/token/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod clear_smb_token;
pub(crate) mod get_smb_token;
pub(crate) mod is_logged_in;
pub(crate) mod smb_token_file_path;