Skip to content

Commit

Permalink
Add logout command
Browse files Browse the repository at this point in the history
  • Loading branch information
itowlson committed Dec 7, 2023
1 parent a7ab8c4 commit 45c39f5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ pub struct LoginCommand {
pub list: bool,
}

/// Log out of Fermyon Cloud.
#[derive(Parser, Debug)]
pub struct LogoutCommand {
/// The environment name to log out of.
#[clap(
name = "environment-name",
long = "environment-name",
env = DEPLOYMENT_ENV_NAME_ENV,
hidden = true
)]
pub deployment_env_id: Option<String>,
}

fn parse_url(url: &str) -> Result<url::Url> {
let mut url = Url::parse(url).map_err(|error| {
anyhow::format_err!(
Expand Down Expand Up @@ -313,6 +326,30 @@ impl LoginCommand {
}
}

impl LogoutCommand {
pub async fn run(&self) -> Result<()> {
let path = self.config_file_path()?;
if path.is_file() {
std::fs::remove_file(path)?;
}
Ok(())
}

fn config_file_path(&self) -> Result<PathBuf> {
let root = config_root_dir()?;

let file_stem = match &self.deployment_env_id {
None => "config",
Some(id) => id,
};
let file = format!("{}.json", file_stem);

let path = root.join(file);

Ok(path)
}
}

fn config_root_dir() -> Result<PathBuf, anyhow::Error> {
let root = dirs::config_dir()
.context("Cannot find configuration directory")?
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use commands::{
apps::AppsCommand,
deploy::DeployCommand,
link::{LinkCommand, UnlinkCommand},
login::LoginCommand,
login::{LoginCommand, LogoutCommand},
logs::LogsCommand,
sqlite::SqliteCommand,
variables::VariablesCommand,
Expand All @@ -34,8 +34,10 @@ enum CloudCli {
Apps(AppsCommand),
/// Package and upload an application to the Fermyon Cloud.
Deploy(DeployCommand),
/// Login to Fermyon Cloud
/// Log into Fermyon Cloud
Login(LoginCommand),
/// Log out of Fermyon Cloud
Logout(LogoutCommand),
/// Fetch logs for an app from Fermyon Cloud
Logs(LogsCommand),
/// Manage Spin application variables
Expand Down Expand Up @@ -65,6 +67,7 @@ async fn main() -> Result<(), Error> {
CloudCli::Apps(cmd) => cmd.run().await,
CloudCli::Deploy(cmd) => cmd.run().await,
CloudCli::Login(cmd) => cmd.run().await,
CloudCli::Logout(cmd) => cmd.run().await,
CloudCli::Logs(cmd) => cmd.run().await,
CloudCli::Variables(cmd) => cmd.run().await,
CloudCli::Sqlite(cmd) => cmd.run().await,
Expand Down

0 comments on commit 45c39f5

Please sign in to comment.