From 4a5aed6b228ebbc5aa8853ea819fb0d612910de2 Mon Sep 17 00:00:00 2001 From: Natalie Arellano Date: Tue, 9 Jul 2024 11:17:10 -0400 Subject: [PATCH] Target data: populate os/arch as well as distro information Fixes https://github.com/buildpacks/lifecycle/issues/1371 Signed-off-by: Natalie Arellano --- platform/target_data.go | 12 ++++++++++-- platform/target_data_test.go | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/platform/target_data.go b/platform/target_data.go index b77ce4774..7690d1d0a 100644 --- a/platform/target_data.go +++ b/platform/target_data.go @@ -1,6 +1,8 @@ package platform import ( + "runtime" + "github.com/buildpacks/imgutil" "github.com/buildpacks/lifecycle/buildpack" @@ -87,10 +89,16 @@ func matches(target1, target2 string) bool { return target1 == target2 } -// GetTargetOSFromFileSystem populates the target metadata you pass in if the information is available -// returns a boolean indicating whether it populated any data. +// GetTargetOSFromFileSystem populates the provided target metadata with information from /etc/os-release +// if it is available. func GetTargetOSFromFileSystem(d fsutil.Detector, tm *files.TargetMetadata, logger log.Logger) { if d.HasSystemdFile() { + if tm.OS == "" { + tm.OS = "linux" + } + if tm.Arch == "" { + tm.Arch = runtime.GOARCH // in a future world where we support cross platform builds, this should be removed + } contents, err := d.ReadSystemdFile() if err != nil { logger.Warnf("Encountered error trying to read /etc/os-release file: %s", err.Error()) diff --git a/platform/target_data_test.go b/platform/target_data_test.go index cb78ade9d..ddc66448a 100644 --- a/platform/target_data_test.go +++ b/platform/target_data_test.go @@ -2,6 +2,7 @@ package platform_test import ( "fmt" + "runtime" "testing" "github.com/apex/log" @@ -129,6 +130,8 @@ func testTargetData(t *testing.T, when spec.G, it spec.S) { t: t, HasFile: true} platform.GetTargetOSFromFileSystem(&d, &tm, logr) + h.AssertEq(t, "linux", tm.OS) + h.AssertEq(t, runtime.GOARCH, tm.Arch) h.AssertEq(t, "opensesame", tm.Distro.Name) h.AssertEq(t, "3.14", tm.Distro.Version) })