diff --git a/vdev/src/commands/release/homebrew.rs b/vdev/src/commands/release/homebrew.rs index 25f4c1b02ec4f..ab04ecc2a923e 100644 --- a/vdev/src/commands/release/homebrew.rs +++ b/vdev/src/commands/release/homebrew.rs @@ -1,17 +1,20 @@ use crate::git; use anyhow::Result; use hex; -use regex; use reqwest; use sha2::Digest; -use std::env; use std::path::Path; +use std::{env, fs}; use tempfile::TempDir; /// Releases latest version to the vectordotdev homebrew tap #[derive(clap::Args, Debug)] #[command()] -pub struct Cli {} +pub struct Cli { + /// GitHub username for the repository. + #[arg(long, default_value = "vectordotdev")] + username: String, +} impl Cli { pub fn exec(self) -> Result<()> { @@ -19,73 +22,93 @@ impl Cli { let td = TempDir::new()?; env::set_current_dir(td.path())?; - let github_token = env::var("GITHUB_TOKEN")?; - - // Clone the homebrew-brew repository - let homebrew_repo = - format!("https://{github_token}:x-oauth-basic@github.com/vectordotdev/homebrew-brew"); - git::clone(&homebrew_repo)?; - env::set_current_dir("homebrew-brew")?; - - // Set git configurations - git::set_config_value("user.name", "vic")?; - git::set_config_value("user.email", "vector@datadoghq.com")?; + debug!("Cloning the homebrew repository for username: {}", self.username); + clone_and_setup_git(&self.username)?; - // Get package details for updating Formula/vector.rb let vector_version = env::var("VECTOR_VERSION")?; - let package_url = format!("https://packages.timber.io/vector/{vector_version}/vector-{vector_version}-x86_64-apple-darwin.tar.gz"); - let package_sha256 = hex::encode(sha2::Sha256::digest( - reqwest::blocking::get(&package_url)?.bytes()?, - )); + debug!("Updating the vector.rb formula for VECTOR_VERSION={vector_version}."); + update_formula("Formula/vector.rb", &vector_version)?; - // Update content of Formula/vector.rb - update_content( - "Formula/vector.rb", - &package_url, - &package_sha256, - &vector_version, - )?; + debug!("Committing and pushing changes (if any)."); + commit_and_push_changes(&vector_version)?; - // Check if there is any change in git index - let has_changes = !git::check_git_repository_clean()?; - if has_changes { - let commit_message = format!("Release Vector {vector_version}"); - git::commit(&commit_message)?; - } - - git::push()?; - - // Remove temporary directory td.close()?; Ok(()) } } -/// Open the vector.rb file and update the new content -fn update_content
( - file_path: P, - package_url: &str, - package_sha256: &str, - vector_version: &str, -) -> Result<()> + +/// Clones the repository and sets up Git configuration +fn clone_and_setup_git(username: &str) -> Result<()> { + let github_token = env::var("GITHUB_TOKEN")?; + let homebrew_repo = format!( + "https://{github_token}:x-oauth-basic@github.com/{username}/homebrew-brew" + ); + + git::clone(&homebrew_repo)?; + env::set_current_dir("homebrew-brew")?; + git::set_config_value("user.name", "vic")?; + git::set_config_value("user.email", "vector@datadoghq.com")?; + Ok(()) +} + +/// Updates the vector.rb formula with new URLs, SHA256s, and version +fn update_formula
(file_path: P, vector_version: &str) -> Result<()>
where
P: AsRef