Skip to content

Commit

Permalink
Minor cleanup. Some docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Mar 6, 2024
1 parent 7353544 commit 47438ee
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions rust/agama-cli/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub async fn run(subcommand: AuthCommands) -> anyhow::Result<()> {
}
}

/// Reads stored JWT token and returns it
pub fn jwt() -> anyhow::Result<String> {
if let Some(file) = jwt_file() {
if let Ok(token) = read_line_from_file(&file.as_path()) {
Expand Down Expand Up @@ -214,27 +215,32 @@ async fn login(password: String) -> anyhow::Result<()> {
set_file_permissions(path.as_path())?;
}


Ok(())
}

/// Releases JWT
fn logout() -> anyhow::Result<()> {
let path = jwt_file();

if !&path.clone().is_some_and(|p| p.as_path().exists()) {
if !&path.clone().is_some_and(|p| p.exists()) {
// mask if the file with the JWT doesn't exist (most probably no login before logout)
return Ok(());
}

// panicking is right thing to do if expect fails, becase it was already checked twice that
// the path exists
let file = path.expect("Cannot locate stored JWT");

return Ok(std::fs::remove_file(file)?)
Ok(std::fs::remove_file(file)?)
}

/// Shows stored JWT on stdout
fn show() -> anyhow::Result<()> {
println!("{}", jwt()?);
// we do not care if jwt() fails or not. If there is something to print, show it otherwise
// stay silent
if let Ok(token) = jwt() {
println!("{}", token);
}

Ok(())
}

0 comments on commit 47438ee

Please sign in to comment.