mod commands;
mod output;
mod read_password;
mod tui;
use std::path::PathBuf;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use tracing_subscriber::EnvFilter;
use ipatool_core::credential;
use ipatool_core::guid;
use ipatool_core::model::Platform;
use output::OutputFormat;
#[derive(Parser)]
#[command(
name = "ipatool",
version,
about = "Download iOS IPA files from the App Store"
)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
#[arg(long, global = true, default_value = "text")]
format: OutputFormat,
#[arg(long, global = true)]
verbose: bool,
#[arg(long, global = true)]
non_interactive: bool,
#[arg(long, global = true)]
keychain_passphrase: Option<String>,
}
#[derive(Subcommand)]
enum Commands {
Auth {
#[command(subcommand)]
action: AuthAction,
},
Search {
term: String,
#[arg(short, long, default_value = "5")]
limit: u32,
#[arg(long, default_value = "iphone")]
platform: Platform,
#[arg(long, default_value = "US")]
country: String,
},
Purchase {
#[arg(short = 'b', long)]
bundle_identifier: String,
},
Download {
#[arg(short = 'b', long)]
bundle_identifier: Option<String>,
#[arg(short = 'i', long)]
app_id: Option<i64>,
#[arg(long)]
version_id: Option<String>,
#[arg(short, long)]
output: Option<PathBuf>,
#[arg(long)]
purchase: bool,
#[arg(long, default_value = "iphone")]
platform: Platform,
},
Version {
#[command(subcommand)]
action: VersionAction,
},
}
#[derive(Subcommand)]
enum AuthAction {
Login {
#[arg(long)]
email: String,
#[arg(long)]
password: Option<String>,
#[arg(long)]
auth_code: Option<String>,
},
Info,
Revoke,
}
#[derive(Subcommand)]
enum VersionAction {
List {
#[arg(short = 'i', long)]
app_id: Option<i64>,
#[arg(short = 'b', long)]
bundle_identifier: Option<String>,
},
Meta {
#[arg(short = 'i', long)]
app_id: Option<i64>,
#[arg(short = 'b', long)]
bundle_identifier: Option<String>,
#[arg(long)]
version_id: String,
},
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
let filter = if cli.verbose {
EnvFilter::new("debug,cookie_store=warn,reqwest_cookie_store=warn")
} else {
EnvFilter::from_default_env()
};
tracing_subscriber::fmt()
.with_env_filter(filter)
.with_target(false)
.init();
let Some(command) = cli.command else {
return tui::run().await;
};
let machine = guid::generate_machine_identity().context("failed to generate GUID")?;
let data_dir = data_dir();
std::fs::create_dir_all(&data_dir).ok();
let cookie_path = data_dir.join("cookies.json");
let mut client =
ipatool_core::client::AppleClient::new(machine, Some(&cookie_path), cache_dir())
.context("failed to create client")?;
match command {
Commands::Auth { action } => match action {
AuthAction::Login {
email,
password,
auth_code,
} => {
commands::auth::login(
&mut client,
&email,
password.as_deref(),
auth_code.as_deref(),
cli.non_interactive,
cli.format,
)
.await?
}
AuthAction::Info => commands::auth::info(cli.format).await?,
AuthAction::Revoke => commands::auth::revoke().await?,
},
Commands::Search {
term,
limit,
platform,
country,
} => {
commands::search::search(&client, &term, limit, platform, &country, cli.format).await?
}
Commands::Purchase { bundle_identifier } => {
let account = load_account()?;
client.set_account(account.clone());
commands::purchase::purchase(&client, &bundle_identifier, &account, cli.format).await?
}
Commands::Download {
bundle_identifier,
app_id,
version_id,
output,
purchase,
platform,
} => {
let account = load_account()?;
client.set_account(account.clone());
commands::download::download(
&client,
bundle_identifier.as_deref(),
app_id,
version_id.as_deref(),
output,
purchase,
platform,
&account,
)
.await?
}
Commands::Version { action } => {
let account = load_account()?;
client.set_account(account.clone());
match action {
VersionAction::List {
app_id,
bundle_identifier,
} => {
commands::version::list(
&client,
app_id,
bundle_identifier.as_deref(),
&account,
cli.format,
)
.await?
}
VersionAction::Meta {
app_id,
bundle_identifier,
version_id,
} => {
commands::version::meta(
&client,
app_id,
bundle_identifier.as_deref(),
&version_id,
&account,
cli.format,
)
.await?
}
}
}
}
client.save_cookies(&cookie_path).ok();
Ok(())
}
fn non_empty_path(val: Option<std::ffi::OsString>) -> Option<PathBuf> {
val.filter(|s| !s.is_empty()).map(PathBuf::from)
}
fn home_dir_from_env(
home: Option<std::ffi::OsString>,
userprofile: Option<std::ffi::OsString>,
homedrive: Option<std::ffi::OsString>,
homepath: Option<std::ffi::OsString>,
) -> Option<PathBuf> {
if let Some(p) = non_empty_path(home) {
return Some(p);
}
if let Some(p) = non_empty_path(userprofile) {
return Some(p);
}
if let (Some(drive), Some(path)) = (
homedrive.filter(|s| !s.is_empty()),
homepath.filter(|s| !s.is_empty()),
) {
let mut combined = drive;
combined.push(path);
return Some(PathBuf::from(combined));
}
None
}
pub(crate) fn data_dir() -> PathBuf {
home_dir_from_env(
std::env::var_os("HOME"),
std::env::var_os("USERPROFILE"),
std::env::var_os("HOMEDRIVE"),
std::env::var_os("HOMEPATH"),
)
.or_else(std::env::home_dir)
.unwrap_or_else(|| PathBuf::from("."))
.join(".ipatool")
}
pub(crate) fn cache_dir() -> PathBuf {
data_dir().join("cache")
}
fn load_account() -> Result<ipatool_core::model::Account> {
credential::load_account()
.context("failed to load credentials")?
.context("not logged in, run `ipatool auth login` first")
}
#[cfg(test)]
mod tests {
use std::ffi::OsString;
use std::path::PathBuf;
use super::home_dir_from_env;
#[test]
fn prefers_home_when_present() {
let dir = home_dir_from_env(
Some(OsString::from("/Users/alice")),
Some(OsString::from(r"C:\Users\alice")),
None,
None,
);
assert_eq!(dir, Some(PathBuf::from("/Users/alice")));
}
#[test]
fn falls_back_to_userprofile_when_home_unset_or_empty() {
assert_eq!(
home_dir_from_env(None, Some(OsString::from(r"C:\Users\bob")), None, None),
Some(PathBuf::from(r"C:\Users\bob"))
);
assert_eq!(
home_dir_from_env(
Some(OsString::new()),
Some(OsString::from(r"C:\Users\bob")),
None,
None,
),
Some(PathBuf::from(r"C:\Users\bob"))
);
}
#[test]
fn falls_back_to_homedrive_and_homepath() {
let dir = home_dir_from_env(
None,
None,
Some(OsString::from("D:")),
Some(OsString::from(r"\Users\carol")),
);
assert_eq!(dir, Some(PathBuf::from(r"D:\Users\carol")));
}
#[test]
fn returns_none_when_all_env_vars_missing() {
assert_eq!(home_dir_from_env(None, None, None, None), None);
assert_eq!(
home_dir_from_env(None, None, Some(OsString::from("C:")), None),
None
);
}
}