Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update resolve_version to support both inventory formats #940

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions common/nodejs-utils/src/bin/resolve_version.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// Required due to: https://github.com/rust-lang/rust/issues/95513
#![allow(unused_crate_dependencies)]

use heroku_nodejs_utils::{inv::Inventory, vrs::Requirement};
use std::fs;
use std::env::consts;
use sha2::Sha256;

use libherokubuildpack::inventory::artifact::{Arch, Os};
use libherokubuildpack::inventory::Inventory;
use heroku_nodejs_utils::vrs::{Requirement, Version};
use heroku_nodejs_utils::inv::Inventory as NodeJSInventory;

const SUCCESS_EXIT_CODE: i32 = 0;
const ARGS_EXIT_CODE: i32 = 1;
Expand All @@ -11,7 +18,7 @@ const INVENTORY_EXIT_CODE: i32 = 3;
fn main() {
let args: Vec<String> = std::env::args().collect();

if &args[1] == "-v" || &args[1] == "--version" {
if args.len() > 0 && (&args[1] == "-v" || &args[1] == "--version") {
const VERSION: &str = env!("CARGO_PKG_VERSION");
println!("v{VERSION}");
std::process::exit(SUCCESS_EXIT_CODE);
Expand All @@ -29,15 +36,36 @@ fn main() {
std::process::exit(VERSION_REQS_EXIT_CODE);
});

let inv = Inventory::read(filename).unwrap_or_else(|e| {
eprintln!("Error reading '{filename}': {e}");
// Try to parse the new inventory file format first
let inv = fs::read_to_string(&filename).unwrap_or_else(|e| {
eprintln!("Error reading '{filename}'': {e}");
std::process::exit(INVENTORY_EXIT_CODE);
});
}).parse::<Inventory<Version, Sha256, Option<()>>>().ok();

let version = inv.resolve(&version_requirements);
if let Some(version) = version {
println!("{} {}", version.version, version.url);
} else {
println!("No result");
// Fall back to the old format. Remove this after cutting over to the new format.
if inv.is_none() {
let inv = NodeJSInventory::read(filename).unwrap_or_else(|e| {
eprintln!("Error reading '{filename}': {e}");
std::process::exit(INVENTORY_EXIT_CODE);
});

let version = inv.resolve(&version_requirements);
if let Some(version) = version {
println!("{} {}", version.version, version.url);
} else {
eprintln!("No result");
}
std::process::exit(SUCCESS_EXIT_CODE);
}

let inv = inv.unwrap();
let artifact = match (consts::OS.parse::<Os>(), consts::ARCH.parse::<Arch>()) {
(Ok(os), Ok(arch)) => inv.resolve(os, arch, &version_requirements),
(_, _) => None,
}.unwrap_or_else(|| {
eprintln!("Could not find version to satisfy requirements \"{}\" for OS {} on arch {}", version_requirements.to_string(), consts::OS.to_string(), consts::ARCH.to_string());
std::process::exit(VERSION_REQS_EXIT_CODE);
});

println!("{} {}", artifact.version, artifact.url);
}