posthog_cli/sourcemaps/
upload.rs

1use std::path::PathBuf;
2
3use anyhow::{Context, Ok, Result};
4use tracing::{info, warn};
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    /// DEPRECATED: Does nothing. Set project during `inject` instead
36    #[arg(long)]
37    pub project: Option<String>,
38
39    /// DEPRECATED: Does nothing. Set version during `inject` instead
40    #[arg(long)]
41    pub version: Option<String>,
42}
43
44pub fn upload_cmd(args: UploadArgs) -> Result<()> {
45    let UploadArgs {
46        directory,
47        ignore,
48        delete_after,
49        skip_ssl_verification: _,
50        batch_size,
51        project: p,
52        version: v,
53    } = args;
54
55    if p.is_some() || v.is_some() {
56        warn!("`--project` and `--version` are deprecated and do nothing. Set project and version during `inject` instead.");
57    }
58
59    context().capture_command_invoked("sourcemap_upload");
60
61    let pairs = read_pairs(&directory, &ignore)?;
62    let sourcemap_paths = pairs
63        .iter()
64        .map(|pair| pair.sourcemap.inner.path.clone())
65        .collect::<Vec<_>>();
66    info!("Found {} chunks to upload", pairs.len());
67
68    let uploads = pairs
69        .into_iter()
70        .map(TryInto::try_into)
71        .collect::<Result<Vec<SymbolSetUpload>>>()
72        .context("While preparing files for upload")?;
73
74    upload(&uploads, batch_size)?;
75
76    if delete_after {
77        delete_files(sourcemap_paths).context("While deleting sourcemaps")?;
78    }
79
80    Ok(())
81}