|
| 1 | +use std::fs; |
| 2 | +use std::io::{self, Seek as _}; |
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +use anyhow::{bail, Result}; |
| 6 | +use clap::{Arg, ArgMatches, Command}; |
| 7 | + |
| 8 | +use crate::api::Api; |
| 9 | +use crate::config::Config; |
| 10 | +use crate::utils::args::ArgExt as _; |
| 11 | +use crate::utils::fs::path_as_url; |
| 12 | + |
| 13 | +const EXPERIMENTAL_WARNING: &str = |
| 14 | + "[EXPERIMENTAL] The \"snapshots download\" command is experimental. \ |
| 15 | + The command is subject to breaking changes, including removal, in any Sentry CLI release."; |
| 16 | + |
| 17 | +pub fn make_command(command: Command) -> Command { |
| 18 | + command |
| 19 | + .about("[EXPERIMENTAL] Download baseline snapshot images from Sentry.") |
| 20 | + .long_about(format!( |
| 21 | + "Download baseline snapshot images from Sentry's preprod system to a local directory.\n\n\ |
| 22 | + {EXPERIMENTAL_WARNING}" |
| 23 | + )) |
| 24 | + .org_arg() |
| 25 | + .arg( |
| 26 | + Arg::new("app_id") |
| 27 | + .long("app-id") |
| 28 | + .value_name("APP_ID") |
| 29 | + .help("App identifier (e.g. sentry-frontend). Mutually exclusive with --snapshot-id.") |
| 30 | + .conflicts_with("snapshot_id"), |
| 31 | + ) |
| 32 | + .arg( |
| 33 | + Arg::new("snapshot_id") |
| 34 | + .long("snapshot-id") |
| 35 | + .value_name("ID") |
| 36 | + .help("Direct snapshot artifact ID. Mutually exclusive with --app-id.") |
| 37 | + .conflicts_with("app_id"), |
| 38 | + ) |
| 39 | + .arg( |
| 40 | + Arg::new("branch") |
| 41 | + .long("branch") |
| 42 | + .value_name("NAME") |
| 43 | + .help("Git branch filter (only with --app-id).") |
| 44 | + .requires("app_id"), |
| 45 | + ) |
| 46 | + .arg( |
| 47 | + Arg::new("output") |
| 48 | + .long("output") |
| 49 | + .value_name("DIR") |
| 50 | + .help("Directory for extracted images.") |
| 51 | + .default_value("./snapshots-base/"), |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +pub fn execute(matches: &ArgMatches) -> Result<()> { |
| 56 | + eprintln!("{EXPERIMENTAL_WARNING}"); |
| 57 | + |
| 58 | + let config = Config::current(); |
| 59 | + let org = config.get_org(matches)?; |
| 60 | + let api_ref = Api::current(); |
| 61 | + let api = api_ref.authenticated()?; |
| 62 | + |
| 63 | + let app_id = matches.get_one::<String>("app_id"); |
| 64 | + let snapshot_id_arg = matches.get_one::<String>("snapshot_id"); |
| 65 | + let branch = matches.get_one::<String>("branch").map(|s| s.as_str()); |
| 66 | + let output_dir = PathBuf::from( |
| 67 | + matches |
| 68 | + .get_one::<String>("output") |
| 69 | + .expect("output has a default value"), |
| 70 | + ); |
| 71 | + |
| 72 | + let snapshot_id = match (app_id, snapshot_id_arg) { |
| 73 | + (Some(app_id), None) => { |
| 74 | + eprintln!("Resolving latest baseline snapshot for app '{app_id}'..."); |
| 75 | + match api.get_latest_base_snapshot(&org, app_id, branch)? { |
| 76 | + Some(resp) => { |
| 77 | + eprintln!( |
| 78 | + "Found snapshot {} ({} images)", |
| 79 | + resp.head_artifact_id, resp.image_count |
| 80 | + ); |
| 81 | + resp.head_artifact_id |
| 82 | + } |
| 83 | + None => { |
| 84 | + let branch_msg = branch |
| 85 | + .map(|b| format!(" on branch '{b}'")) |
| 86 | + .unwrap_or_default(); |
| 87 | + bail!("No baseline snapshot found for app '{app_id}'{branch_msg}"); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + (None, Some(id)) => id.clone(), |
| 92 | + _ => bail!("Exactly one of --app-id or --snapshot-id must be provided"), |
| 93 | + }; |
| 94 | + |
| 95 | + eprintln!("Downloading snapshot {snapshot_id}..."); |
| 96 | + let mut tmp = tempfile::tempfile()?; |
| 97 | + let response = api.download_snapshot_zip(&org, &snapshot_id, &mut tmp)?; |
| 98 | + |
| 99 | + if response.failed() { |
| 100 | + bail!( |
| 101 | + "Failed to download snapshot (server returned status {}).", |
| 102 | + response.status() |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + tmp.seek(io::SeekFrom::Start(0))?; |
| 107 | + let mut archive = zip::ZipArchive::new(&mut tmp)?; |
| 108 | + |
| 109 | + fs::create_dir_all(&output_dir)?; |
| 110 | + |
| 111 | + let mut extracted = 0usize; |
| 112 | + for i in 0..archive.len() { |
| 113 | + let mut entry = archive.by_index(i)?; |
| 114 | + if entry.is_dir() { |
| 115 | + continue; |
| 116 | + } |
| 117 | + let Some(enclosed_name) = entry.enclosed_name() else { |
| 118 | + continue; |
| 119 | + }; |
| 120 | + let out_path = output_dir.join(&enclosed_name); |
| 121 | + if let Some(parent) = out_path.parent() { |
| 122 | + fs::create_dir_all(parent)?; |
| 123 | + } |
| 124 | + let mut out_file = fs::File::create(&out_path)?; |
| 125 | + io::copy(&mut entry, &mut out_file)?; |
| 126 | + extracted += 1; |
| 127 | + } |
| 128 | + |
| 129 | + eprintln!( |
| 130 | + "\nDownloaded {extracted} images from snapshot {snapshot_id} to {}", |
| 131 | + path_as_url(&output_dir) |
| 132 | + ); |
| 133 | + |
| 134 | + Ok(()) |
| 135 | +} |
0 commit comments