mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 20:22:01 -06:00
Rewrite it in Rust
This commit is contained in:
parent
d4bef511cb
commit
039d7cfb81
51 changed files with 4018 additions and 3208 deletions
39
crunchy-cli-core/src/cli/login.rs
Normal file
39
crunchy-cli-core/src/cli/login.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
use crate::utils::context::Context;
|
||||
use crate::Execute;
|
||||
use anyhow::bail;
|
||||
use anyhow::Result;
|
||||
use crunchyroll_rs::crunchyroll::SessionToken;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, clap::Parser)]
|
||||
#[clap(about = "Save your login credentials persistent on disk")]
|
||||
pub struct Login {
|
||||
#[arg(help = "Remove your stored credentials (instead of save them)")]
|
||||
#[arg(long)]
|
||||
pub remove: bool,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
impl Execute for Login {
|
||||
async fn execute(self, ctx: Context) -> Result<()> {
|
||||
if let Some(login_file_path) = login_file_path() {
|
||||
match ctx.crunchy.session_token().await {
|
||||
SessionToken::RefreshToken(refresh_token) => Ok(fs::write(
|
||||
login_file_path,
|
||||
format!("refresh_token:{}", refresh_token),
|
||||
)?),
|
||||
SessionToken::EtpRt(etp_rt) => {
|
||||
Ok(fs::write(login_file_path, format!("etp_rt:{}", etp_rt))?)
|
||||
}
|
||||
SessionToken::Anonymous => bail!("Anonymous login cannot be saved"),
|
||||
}
|
||||
} else {
|
||||
bail!("Cannot find config path")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn login_file_path() -> Option<PathBuf> {
|
||||
dirs::config_dir().map(|config_dir| config_dir.join(".crunchy-cli-core"))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue