| 1 | //! A simple tool to query PPA information from Launchpad. |
| 2 | |
| 3 | use apt_sources::launchpad::{self, Error, PpaInfo, PpaSigningKey, PpaValidationResult}; |
| 4 | use clap::{Parser, Subcommand}; |
| 5 | use std::process::ExitCode; |
| 6 | |
| 7 | #[derive(Parser)] |
| 8 | #[command(name = "ppa-info")] |
| 9 | #[command(about = "Query PPA information from Launchpad")] |
| 10 | #[command(version)] |
| 11 | struct Cli { |
| 12 | /// Enable verbose output |
| 13 | #[arg(short, long)] |
| 14 | verbose: bool, |
| 15 | |
| 16 | /// Require authentication (for private PPAs) |
| 17 | #[arg(short, long)] |
| 18 | login: bool, |
| 19 | |
| 20 | #[command(subcommand)] |
| 21 | command: Commands, |
| 22 | } |
| 23 | |
| 24 | #[derive(Subcommand)] |
| 25 | enum Commands { |
| 26 | /// Show information about a PPA |
| 27 | Info { |
| 28 | /// PPA specification (e.g., "ppa:user/name" or "user/name") |
| 29 | ppa: String, |
| 30 | }, |
| 31 | /// Get the signing key for a PPA |
| 32 | Key { |
| 33 | /// PPA specification (e.g., "ppa:user/name" or "user/name") |
| 34 | ppa: String, |
| 35 | |
| 36 | /// Output only the key fingerprint |
| 37 | #[arg(long)] |
| 38 | fingerprint_only: bool, |
| 39 | }, |
| 40 | /// Get the subscription URL for a private PPA |
| 41 | SubscriptionUrl { |
| 42 | /// PPA specification (e.g., "ppa:user/name" or "user/name") |
| 43 | ppa: String, |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | fn parse_ppa(ppa: &str) -> Result<PpaInfo, String> { |
| 48 | PpaInfo::parse(ppa) |
| 49 | } |
| 50 | |
| 51 | fn show_info(ppa_info: &PpaInfo, auth_required: bool) -> Result<(), Error> { |
| 52 | let result: PpaValidationResult = launchpad::validate_ppa(ppa_info, auth_required)?; |
| 53 | |
| 54 | if !result.exists { |
| 55 | println!("PPA {}/{} does not exist", ppa_info.user, ppa_info.name); |
| 56 | return Ok(()); |
| 57 | } |
| 58 | |
| 59 | println!("PPA: {}/{}", ppa_info.user, ppa_info.name); |
| 60 | println!("Display Name: {}", result.display_name); |
| 61 | println!("Private: {}", if result.is_private { "yes" } else { "no" }); |
| 62 | println!( |
| 63 | "Debug Symbols: {}", |
| 64 | if result.publishes_debug_symbols { |
| 65 | "yes" |
| 66 | } else { |
| 67 | "no" |
| 68 | } |
| 69 | ); |
| 70 | |
| 71 | Ok(()) |
| 72 | } |
| 73 | |
| 74 | fn show_key(ppa_info: &PpaInfo, auth_required: bool, fingerprint_only: &bool) -> Result<(), Error> { |
| 75 | let signing_key: PpaSigningKey = launchpad::download_ppa_signing_key(ppa_info, auth_required)?; |
| 76 | |
| 77 | if *fingerprint_only { |
| 78 | println!("{}", signing_key.fingerprint); |
| 79 | } else { |
| 80 | println!("Fingerprint: {}", signing_key.fingerprint); |
| 81 | println!(); |
| 82 | println!("{}", signing_key.key_data); |
| 83 | } |
| 84 | |
| 85 | Ok(()) |
| 86 | } |
| 87 | |
| 88 | fn show_subscription_url(ppa_info: &PpaInfo) -> Result<(), Error> { |
| 89 | let url = launchpad::get_private_ppa_url(ppa_info)?; |
| 90 | println!("{}", url); |
| 91 | Ok(()) |
| 92 | } |
| 93 | |
| 94 | fn main() -> ExitCode { |
| 95 | let cli = Cli::parse(); |
| 96 | |
| 97 | if cli.verbose { |
| 98 | env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init(); |
| 99 | } else { |
| 100 | env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init(); |
| 101 | } |
| 102 | |
| 103 | let result = match &cli.command { |
| 104 | Commands::Info { ppa } => { |
| 105 | let ppa_info = match parse_ppa(&ppa) { |
| 106 | Ok(info) => info, |
| 107 | Err(e) => { |
| 108 | eprintln!("Error: {}", e); |
| 109 | return ExitCode::FAILURE; |
| 110 | } |
| 111 | }; |
| 112 | show_info(&ppa_info, cli.login) |
| 113 | } |
| 114 | Commands::Key { |
| 115 | ppa, |
| 116 | fingerprint_only, |
| 117 | } => { |
| 118 | let ppa_info = match parse_ppa(&ppa) { |
| 119 | Ok(info) => info, |
| 120 | Err(e) => { |
| 121 | eprintln!("Error: {}", e); |
| 122 | return ExitCode::FAILURE; |
| 123 | } |
| 124 | }; |
| 125 | show_key(&ppa_info, cli.login, fingerprint_only) |
| 126 | } |
| 127 | Commands::SubscriptionUrl { ppa } => { |
| 128 | let ppa_info = match parse_ppa(&ppa) { |
| 129 | Ok(info) => info, |
| 130 | Err(e) => { |
| 131 | eprintln!("Error: {}", e); |
| 132 | return ExitCode::FAILURE; |
| 133 | } |
| 134 | }; |
| 135 | show_subscription_url(&ppa_info) |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | match result { |
| 140 | Ok(()) => ExitCode::SUCCESS, |
| 141 | Err(e) => { |
| 142 | eprintln!("Error: {}", e); |
| 143 | ExitCode::FAILURE |
| 144 | } |
| 145 | } |
| 146 | } |