|
| 1 | +use std::fs; |
| 2 | + |
| 3 | +use anyhow::{bail, Context as _, Result}; |
| 4 | +use clap::{Arg, ArgMatches, Command}; |
| 5 | +use serde::Deserialize; |
| 6 | + |
| 7 | +#[derive(Debug, Deserialize)] |
| 8 | +#[serde(rename_all = "camelCase")] |
| 9 | +struct CodeMapping { |
| 10 | + stack_root: String, |
| 11 | + source_root: String, |
| 12 | +} |
| 13 | + |
| 14 | +pub fn make_command(command: Command) -> Command { |
| 15 | + command |
| 16 | + .about("Upload code mappings for a project from a JSON file. Each mapping pairs a stack trace root (e.g. com/example/module) with the corresponding source path in your repository (e.g. modules/module/src/main/java/com/example/module).") |
| 17 | + .arg( |
| 18 | + Arg::new("path") |
| 19 | + .value_name("PATH") |
| 20 | + .required(true) |
| 21 | + .help("Path to a JSON file containing code mappings."), |
| 22 | + ) |
| 23 | + .arg( |
| 24 | + Arg::new("repo") |
| 25 | + .long("repo") |
| 26 | + .value_name("REPO") |
| 27 | + .help("The repository name (e.g. owner/repo). Defaults to the git remote."), |
| 28 | + ) |
| 29 | + .arg( |
| 30 | + Arg::new("default_branch") |
| 31 | + .long("default-branch") |
| 32 | + .value_name("BRANCH") |
| 33 | + .default_value("main") |
| 34 | + .help("The default branch name."), |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +pub fn execute(matches: &ArgMatches) -> Result<()> { |
| 39 | + let path = matches |
| 40 | + .get_one::<String>("path") |
| 41 | + .expect("path is a required argument"); |
| 42 | + let data = fs::read(path).with_context(|| format!("Failed to read mappings file '{path}'"))?; |
| 43 | + |
| 44 | + let mappings: Vec<CodeMapping> = |
| 45 | + serde_json::from_slice(&data).context("Failed to parse mappings JSON")?; |
| 46 | + |
| 47 | + if mappings.is_empty() { |
| 48 | + bail!("Mappings file contains an empty array. Nothing to upload."); |
| 49 | + } |
| 50 | + |
| 51 | + for (i, mapping) in mappings.iter().enumerate() { |
| 52 | + if mapping.stack_root.is_empty() { |
| 53 | + bail!("Mapping at index {i} has an empty stackRoot."); |
| 54 | + } |
| 55 | + if mapping.source_root.is_empty() { |
| 56 | + bail!("Mapping at index {i} has an empty sourceRoot."); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + println!("Found {} code mapping(s) in {path}", mappings.len()); |
| 61 | + |
| 62 | + Ok(()) |
| 63 | +} |
0 commit comments