mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Refactor
This commit is contained in:
parent
90212c4ec0
commit
0a40f3c40f
30 changed files with 3651 additions and 2982 deletions
41
crunchy-cli-core/src/login/command.rs
Normal file
41
crunchy-cli-core/src/login/command.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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() {
|
||||
fs::create_dir_all(login_file_path.parent().unwrap())?;
|
||||
|
||||
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").join("session"))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue