vdev/commands/release/
homebrew.rs1use crate::git;
2use anyhow::Result;
3use hex;
4use reqwest;
5use sha2::Digest;
6use std::path::Path;
7use std::{env, fs};
8use tempfile::TempDir;
9
10#[derive(clap::Args, Debug)]
12#[command()]
13pub struct Cli {
14 #[arg(long, default_value = "vectordotdev")]
16 username: String,
17}
18
19impl Cli {
20 pub fn exec(self) -> Result<()> {
21 let td = TempDir::new()?;
23 env::set_current_dir(td.path())?;
24
25 debug!("Cloning the homebrew repository for username: {}", self.username);
26 clone_and_setup_git(&self.username)?;
27
28 let vector_version = env::var("VECTOR_VERSION")?;
29 debug!("Updating the vector.rb formula for VECTOR_VERSION={vector_version}.");
30 update_formula("Formula/vector.rb", &vector_version)?;
31
32 debug!("Committing and pushing changes (if any).");
33 commit_and_push_changes(&vector_version)?;
34
35 td.close()?;
36 Ok(())
37 }
38}
39
40
41fn clone_and_setup_git(username: &str) -> Result<()> {
43 let github_token = env::var("HOMEBREW_TOKEN")?;
44 let homebrew_repo = format!(
45 "https://{username}:{github_token}@github.com/{username}/homebrew-brew.git"
46 );
47
48
49 git::clone(&homebrew_repo)?;
50 env::set_current_dir("homebrew-brew")?;
51 git::set_config_value("user.name", "vic")?;
52 git::set_config_value("user.email", "vector@datadoghq.com")?;
53 Ok(())
54}
55
56fn update_formula<P>(file_path: P, vector_version: &str) -> Result<()>
58where
59 P: AsRef<Path>,
60{
61 let x86_package_url = format!(
63 "https://packages.timber.io/vector/{vector_version}/vector-{vector_version}-x86_64-apple-darwin.tar.gz"
64 );
65 let x86_package_sha256 = hex::encode(sha2::Sha256::digest(
66 reqwest::blocking::get(&x86_package_url)?.bytes()?,
67 ));
68
69 let arm_package_url = format!(
70 "https://packages.timber.io/vector/{vector_version}/vector-{vector_version}-arm64-apple-darwin.tar.gz"
71 );
72 let arm_package_sha256 = hex::encode(sha2::Sha256::digest(
73 reqwest::blocking::get(&arm_package_url)?.bytes()?,
74 ));
75
76 let content = fs::read_to_string(&file_path)?;
77
78 let updated_content = content
80 .lines()
81 .map(|line| {
82 if line.trim_start().starts_with("version \"") {
83 format!(" version \"{vector_version}\"")
84 } else if line.contains("# x86_64 url") {
85 format!(" url \"{x86_package_url}\" # x86_64 url")
86 } else if line.contains("# x86_64 sha256") {
87 format!(" sha256 \"{x86_package_sha256}\" # x86_64 sha256")
88 } else if line.contains("# arm64 url") {
89 format!(" url \"{arm_package_url}\" # arm64 url")
90 } else if line.contains("# arm64 sha256") {
91 format!(" sha256 \"{arm_package_sha256}\" # arm64 sha256")
92 } else {
93 line.to_string()
94 }
95 })
96 .collect::<Vec<_>>()
97 .join("\n");
98
99 fs::write(file_path, updated_content)?;
100 Ok(())
101}
102
103fn commit_and_push_changes(vector_version: &str) -> Result<()> {
105 let has_changes = !git::check_git_repository_clean()?;
106 if has_changes {
107 debug!("Modified lines {:?}", git::get_modified_files());
108 let commit_message = format!("Release Vector {vector_version}");
109 git::commit(&commit_message)?;
110 git::push()?;
111 } else {
112 debug!("No changes to push.");
113 }
114 Ok(())
115}