|
| 1 | +#![allow(clippy::print_stdout)] |
| 2 | + |
| 3 | +use anyhow::{Result, bail}; |
| 4 | +use semver::Version; |
| 5 | + |
| 6 | +use crate::utils::{deprecation, git, paths}; |
| 7 | + |
| 8 | +/// Enact a deprecation: record it as removed and delete the deprecation.d fragment |
| 9 | +#[derive(clap::Args, Debug)] |
| 10 | +#[command()] |
| 11 | +pub struct Cli { |
| 12 | + /// Filename (slug) in deprecation.d/ to enact, e.g. "azure-monitor-logs-sink" |
| 13 | + slug: String, |
| 14 | + |
| 15 | + /// The Vector version in which this feature was removed, e.g. "0.58.0". |
| 16 | + /// Defaults to the next minor after the latest git tag. |
| 17 | + #[arg(long)] |
| 18 | + version: Option<Version>, |
| 19 | +} |
| 20 | + |
| 21 | +impl Cli { |
| 22 | + pub fn exec(self) -> Result<()> { |
| 23 | + let repo_root = paths::find_repo_root()?; |
| 24 | + let dir = repo_root.join(deprecation::DEPRECATION_DIR); |
| 25 | + |
| 26 | + // Accept "slug" or "slug.md"; reject path-like inputs to keep the |
| 27 | + // identifier unambiguous (and the file lookup safe). |
| 28 | + if self.slug.contains('/') || self.slug.contains('\\') { |
| 29 | + bail!( |
| 30 | + "expected a deprecation slug (e.g. \"azure-monitor-logs-sink\"), not a path: {}", |
| 31 | + self.slug |
| 32 | + ); |
| 33 | + } |
| 34 | + let stem = self.slug.strip_suffix(".md").unwrap_or(&self.slug); |
| 35 | + let filename = format!("{stem}.md"); |
| 36 | + |
| 37 | + let path = dir.join(&filename); |
| 38 | + if !path.exists() { |
| 39 | + bail!("No deprecation fragment found at {}", path.display()); |
| 40 | + } |
| 41 | + |
| 42 | + // Parse the fragment to get entry data. |
| 43 | + let entries = deprecation::read_deprecation_fragments(&dir)?; |
| 44 | + let entry = entries |
| 45 | + .into_iter() |
| 46 | + .find(|e| e.filename == filename) |
| 47 | + .ok_or_else(|| anyhow::anyhow!("Could not parse {filename}"))?; |
| 48 | + |
| 49 | + let version = if let Some(v) = self.version { |
| 50 | + // Reject `--version 0.58.0-alpha` and friends: only release-shaped |
| 51 | + // semver is valid here. |
| 52 | + if !v.pre.is_empty() || !v.build.is_empty() { |
| 53 | + bail!( |
| 54 | + "--version {v} has prerelease or build metadata; only plain X.Y.Z is allowed" |
| 55 | + ); |
| 56 | + } |
| 57 | + v |
| 58 | + } else { |
| 59 | + let latest = git::latest_release_version()?; |
| 60 | + Version::new(latest.major, latest.minor + 1, 0) |
| 61 | + }; |
| 62 | + |
| 63 | + if !deprecation::later_minor(&version, &entry.deprecated_since.0) { |
| 64 | + bail!( |
| 65 | + "removed_in ({version}) must be in a later minor release than deprecated_since ({}); \ |
| 66 | + the deprecation policy requires at least one minor release between \ |
| 67 | + the announcement and removal. \ |
| 68 | + Check --version or the fragment's `deprecated_since` field.", |
| 69 | + entry.deprecated_since |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + let enacted = deprecation::EnactedEntry { |
| 74 | + what: entry.what.clone(), |
| 75 | + deprecated_since: entry.deprecated_since.to_string(), |
| 76 | + removed_in: version.to_string(), |
| 77 | + description: entry.description.clone(), |
| 78 | + }; |
| 79 | + |
| 80 | + // Append to enacted JSON. |
| 81 | + deprecation::append_enacted(&repo_root, enacted)?; |
| 82 | + println!("Recorded enacted entry for: {}", entry.what); |
| 83 | + |
| 84 | + // Delete the deprecation.d fragment. |
| 85 | + std::fs::remove_file(&path)?; |
| 86 | + println!("Deleted {}", path.display()); |
| 87 | + |
| 88 | + // Regenerate deprecations.cue. |
| 89 | + deprecation::sync_deprecations_cue(&repo_root)?; |
| 90 | + println!( |
| 91 | + "Updated {}", |
| 92 | + repo_root.join(deprecation::DEPRECATIONS_JSON).display() |
| 93 | + ); |
| 94 | + |
| 95 | + Ok(()) |
| 96 | + } |
| 97 | +} |
0 commit comments