use std::path::Path;
use anyhow::{Context, Result};
use crate::cli::AuthCommand;
use crate::connect::connect;
pub async fn run(
host: &str,
token_file: &Path,
timeout: u64,
sub: Option<AuthCommand>,
json: bool,
) -> Result<()> {
match sub {
None => {
let _tv = connect(host, token_file, timeout).await?;
if json {
println!("{}", serde_json::json!({"status": "paired"}));
} else {
println!(
"Successfully paired with TV at {host}. Token saved to {}.",
token_file.display()
);
}
}
Some(AuthCommand::Verify) => {
let tv = connect(host, token_file, timeout).await?;
tv.get_device_info()
.await
.context("token verification failed")?;
if json {
println!("{}", serde_json::json!({"status": "valid"}));
} else {
println!("Auth token is valid.");
}
}
Some(AuthCommand::Reset) => {
if token_file.exists() {
std::fs::remove_file(token_file)
.with_context(|| format!("failed to delete {}", token_file.display()))?;
if json {
println!("{}", serde_json::json!({"status": "deleted"}));
} else {
println!("Auth token deleted: {}", token_file.display());
}
} else if json {
println!("{}", serde_json::json!({"status": "not_found"}));
} else {
println!("No token file found at {}.", token_file.display());
}
}
}
Ok(())
}