posthog_cli/sourcemaps/
upload.rs

1use std::path::PathBuf;
2
3use anyhow::{Context, Ok, Result};
4use tracing::info;
5
6use crate::api::symbol_sets::{upload, SymbolSetUpload};
7use crate::invocation_context::context;
8
9use crate::sourcemaps::source_pair::read_pairs;
10use crate::utils::files::delete_files;
11
12#[derive(clap::Args, Clone)]
13pub struct UploadArgs {
14    /// The directory containing the bundled chunks
15    #[arg(short, long)]
16    pub directory: PathBuf,
17
18    /// One or more directory glob patterns to ignore
19    #[arg(short, long)]
20    pub ignore: Vec<String>,
21
22    /// Whether to delete the source map files after uploading them
23    #[arg(long, default_value = "false")]
24    pub delete_after: bool,
25
26    /// Whether to skip SSL verification when uploading chunks - only use when using self-signed certificates for
27    /// self-deployed instances
28    #[arg(long, default_value = "false")]
29    pub skip_ssl_verification: bool,
30
31    /// The maximum number of chunks to upload in a single batch
32    #[arg(long, default_value = "50")]
33    pub batch_size: usize,
34}
35
36pub fn upload_cmd(args: UploadArgs) -> Result<()> {
37    let UploadArgs {
38        directory,
39        ignore,
40        delete_after,
41        skip_ssl_verification: _,
42        batch_size,
43    } = args;
44
45    context().capture_command_invoked("sourcemap_upload");
46
47    let pairs = read_pairs(&directory, &ignore)?;
48    let sourcemap_paths = pairs
49        .iter()
50        .map(|pair| pair.sourcemap.inner.path.clone())
51        .collect::<Vec<_>>();
52    info!("Found {} chunks to upload", pairs.len());
53
54    let uploads = pairs
55        .into_iter()
56        .map(TryInto::try_into)
57        .collect::<Result<Vec<SymbolSetUpload>>>()
58        .context("While preparing files for upload")?;
59
60    upload(&uploads, batch_size)?;
61
62    if delete_after {
63        delete_files(sourcemap_paths).context("While deleting sourcemaps")?;
64    }
65
66    Ok(())
67}