From b8cfa558e671e1cd5620a8bfe759dee1c0cf76c5 Mon Sep 17 00:00:00 2001 From: Rune Soerensen Date: Wed, 4 Sep 2024 16:52:14 -0400 Subject: [PATCH] Prefer inventory::artifact::Arch --- jruby_executable/src/bin/jruby_build.rs | 10 ++-- jruby_executable/src/bin/jruby_check.rs | 5 +- ruby_executable/src/bin/ruby_build.rs | 9 ++-- ruby_executable/src/bin/ruby_check.rs | 5 +- shared/src/base_image.rs | 66 ------------------------- shared/src/lib.rs | 12 ++--- 6 files changed, 20 insertions(+), 87 deletions(-) diff --git a/jruby_executable/src/bin/jruby_build.rs b/jruby_executable/src/bin/jruby_build.rs index f395a5d..b6deb30 100644 --- a/jruby_executable/src/bin/jruby_build.rs +++ b/jruby_executable/src/bin/jruby_build.rs @@ -3,12 +3,12 @@ use clap::Parser; use fs_err::PathExt; use gem_version::GemVersion; use indoc::formatdoc; -use inventory::artifact::Artifact; +use inventory::artifact::{Arch, Artifact}; use jruby_executable::jruby_build_properties; use shared::{ append_filename_with, artifact_is_different, artifact_same_url_different_checksum, atomic_inventory_update, download_tar, sha256_from_path, source_dir, tar_dir_to_file, - untar_to_dir, ArtifactMetadata, BaseImage, CpuArch, TarDownloadPath, + untar_to_dir, ArtifactMetadata, BaseImage, TarDownloadPath, }; use std::convert::From; use std::error::Error; @@ -123,12 +123,12 @@ fn jruby_build(args: &Args) -> Result<(), Box> { fs_err::copy(tar_file.path(), &sha_seven_path)?; let timestamp = chrono::Utc::now(); - for cpu_arch in &[CpuArch::new("amd64")?, CpuArch::new("arm64")?] { + for cpu_arch in [Arch::Amd64, Arch::Arm64] { let distro_version = base_image.distro_version(); let artifact = Artifact { version: GemVersion::from_str(version)?, os: inventory::artifact::Os::Linux, - arch: cpu_arch.try_into()?, + arch: cpu_arch, url: format!( "{S3_BASE_URL}/{}", sha_seven_path.strip_prefix(&volume_output_dir)?.display() @@ -165,7 +165,7 @@ fn jruby_build(args: &Args) -> Result<(), Box> { } // Can be removed once manifest file support is fully rolled out - for cpu_arch in &[CpuArch::new("amd64")?, CpuArch::new("arm64")?] { + for cpu_arch in [Arch::Amd64, Arch::Arm64] { let dir = volume_output_dir .join(base_image.to_string()) .join(cpu_arch.to_string()); diff --git a/jruby_executable/src/bin/jruby_check.rs b/jruby_executable/src/bin/jruby_check.rs index e55e0ca..d276067 100644 --- a/jruby_executable/src/bin/jruby_check.rs +++ b/jruby_executable/src/bin/jruby_check.rs @@ -2,8 +2,9 @@ use bullet_stream::{style, Print}; use clap::Parser; use fun_run::CommandWithName; use indoc::formatdoc; +use inventory::artifact::Arch; use jruby_executable::jruby_build_properties; -use shared::{source_dir, BaseImage, CpuArch}; +use shared::{source_dir, BaseImage}; use std::error::Error; use std::io::Write; use std::{path::PathBuf, process::Command}; @@ -13,7 +14,7 @@ static INNER_OUTPUT: &str = "/tmp/output"; #[derive(Parser, Debug)] struct RubyArgs { #[arg(long)] - arch: CpuArch, + arch: Arch, #[arg(long)] version: String, diff --git a/ruby_executable/src/bin/ruby_build.rs b/ruby_executable/src/bin/ruby_build.rs index 693411f..043ce0c 100644 --- a/ruby_executable/src/bin/ruby_build.rs +++ b/ruby_executable/src/bin/ruby_build.rs @@ -4,12 +4,11 @@ use fs_err::PathExt; use fun_run::CommandWithName; use gem_version::GemVersion; use indoc::{formatdoc, indoc}; -use inventory::artifact::Artifact; +use inventory::artifact::{Arch, Artifact}; use shared::{ append_filename_with, artifact_is_different, artifact_same_url_different_checksum, atomic_inventory_update, download_tar, output_tar_path, sha256_from_path, source_dir, - validate_version_for_stack, ArtifactMetadata, BaseImage, CpuArch, RubyDownloadVersion, - TarDownloadPath, + validate_version_for_stack, ArtifactMetadata, BaseImage, RubyDownloadVersion, TarDownloadPath, }; use std::{ io::Write, @@ -25,7 +24,7 @@ static S3_BASE_URL: &str = "https://heroku-buildpack-ruby.s3.us-east-1.amazonaws #[derive(Parser, Debug)] struct RubyArgs { #[arg(long)] - arch: CpuArch, + arch: Arch, #[arg(long)] version: RubyDownloadVersion, @@ -176,7 +175,7 @@ fn ruby_build(args: &RubyArgs) -> Result<(), Box> { let artifact = Artifact { version: GemVersion::from_str(&version.bundler_format())?, os: inventory::artifact::Os::Linux, - arch: arch.try_into()?, + arch: *arch, url, checksum: format!("sha256:{sha}").parse()?, metadata: ArtifactMetadata { diff --git a/ruby_executable/src/bin/ruby_check.rs b/ruby_executable/src/bin/ruby_check.rs index 702043b..6ad12cf 100644 --- a/ruby_executable/src/bin/ruby_check.rs +++ b/ruby_executable/src/bin/ruby_check.rs @@ -2,7 +2,8 @@ use bullet_stream::{style, Print}; use clap::Parser; use fun_run::CommandWithName; use indoc::formatdoc; -use shared::{output_tar_path, source_dir, BaseImage, CpuArch, RubyDownloadVersion}; +use inventory::artifact::Arch; +use shared::{output_tar_path, source_dir, BaseImage, RubyDownloadVersion}; use std::{error::Error, path::PathBuf, process::Command}; static INNER_OUTPUT: &str = "/tmp/output"; @@ -10,7 +11,7 @@ static INNER_OUTPUT: &str = "/tmp/output"; #[derive(Parser, Debug)] struct RubyArgs { #[arg(long)] - arch: CpuArch, + arch: Arch, #[arg(long)] version: RubyDownloadVersion, diff --git a/shared/src/base_image.rs b/shared/src/base_image.rs index 6c5b6b0..e8477d6 100644 --- a/shared/src/base_image.rs +++ b/shared/src/base_image.rs @@ -3,7 +3,6 @@ use std::str::FromStr; use serde::{Deserialize, Serialize}; -static KNOWN_ARCHITECTURES: [&str; 2] = ["amd64", "arm64"]; static KNOWN_BASE_IMAGES: &[(&str, &str)] = &[ ("heroku-20", "20"), ("heroku-22", "22"), @@ -68,68 +67,3 @@ impl FromStr for BaseImage { BaseImage::new(s) } } - -#[derive(Debug, Clone)] -pub struct CpuArch { - name: String, -} - -impl Display for CpuArch { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.name) - } -} - -impl FromStr for CpuArch { - type Err = CpuArchError; - - fn from_str(s: &str) -> Result { - CpuArch::new(s) - } -} - -#[derive(Debug, thiserror::Error)] -pub enum CpuArchError { - #[error("Invalid CPU architecture {0} must be one of {}", KNOWN_ARCHITECTURES.join(", "))] - Unknown(String), - - #[error("CPU architecture {0} cannot be converted {1}")] - CannotConvert(String, inventory::artifact::UnsupportedArchError), -} - -impl TryFrom<&CpuArch> for inventory::artifact::Arch { - type Error = CpuArchError; - - fn try_from(value: &CpuArch) -> Result { - Self::from_str(&value.name).map_err(|e| CpuArchError::CannotConvert(value.name.clone(), e)) - } -} - -impl CpuArch { - pub fn new(s: &str) -> Result { - KNOWN_ARCHITECTURES - .iter() - .find(|&&name| name == s) - .map(|_| Self { name: s.to_owned() }) - .ok_or_else(|| CpuArchError::Unknown(s.to_owned())) - } - - pub fn from_system() -> Result { - let arch = if cfg!(target_arch = "aarch64") { - "arm64" - } else if cfg!(target_arch = "x86_64") { - "amd64" - } else { - "Unknown architecture" - }; - - Self::new(arch) - } - - #[cfg(test)] - pub(crate) fn from_test_str(name: &str) -> Self { - Self { - name: name.to_string(), - } - } -} diff --git a/shared/src/lib.rs b/shared/src/lib.rs index 424b13d..2a62978 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -2,6 +2,7 @@ use bullet_stream::state::SubBullet; use bullet_stream::Print; use fs_err::{File, PathExt}; use fun_run::CommandWithName; +use inventory::artifact::Arch; use std::io::{Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; use std::process::Command; @@ -10,7 +11,7 @@ mod base_image; mod download_ruby_version; mod inventory_help; -pub use base_image::{BaseImage, CpuArch, CpuArchError}; +pub use base_image::BaseImage; pub use download_ruby_version::RubyDownloadVersion; pub use inventory_help::{ artifact_is_different, artifact_same_url_different_checksum, atomic_inventory_update, @@ -81,9 +82,6 @@ pub fn untar_to_dir(tar_path: &TarDownloadPath, workspace: &Path) -> Result<(), #[derive(thiserror::Error, Debug)] pub enum Error { - #[error("Error {0}")] - UnknownArchitecture(CpuArchError), - #[error("Command failed {0}")] CmdError(fun_run::CmdError), @@ -155,7 +153,7 @@ pub fn output_tar_path( output: &Path, version: &RubyDownloadVersion, base_image: &BaseImage, - cpu_architecture: &CpuArch, + cpu_architecture: &Arch, ) -> PathBuf { let directory = if base_image.is_arch_aware() { PathBuf::from(base_image.to_string()).join(cpu_architecture.to_string()) @@ -307,7 +305,7 @@ mod test { let output = PathBuf::from("/tmp"); let version = RubyDownloadVersion::from_str("2.7.3").unwrap(); let base_image = BaseImage::new("heroku-20").unwrap(); - let cpu_architecture = CpuArch::from_test_str("amd64"); + let cpu_architecture = Arch::Amd64; let tar_path = output_tar_path(&output, &version, &base_image, &cpu_architecture); @@ -320,7 +318,7 @@ mod test { let output = PathBuf::from("/tmp"); let version = RubyDownloadVersion::from_str("2.7.3").unwrap(); let base_image = BaseImage::new("heroku-24").unwrap(); - let cpu_architecture = CpuArch::from_test_str("amd64"); + let cpu_architecture = Arch::Amd64; let tar_path = output_tar_path(&output, &version, &base_image, &cpu_architecture);