Skip to content

Commit

Permalink
Addressed review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Kirtana Ashok <kiashok@microsoft.com>
  • Loading branch information
kiashok committed Mar 14, 2024
1 parent 508d5d9 commit 5009917
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
37 changes: 13 additions & 24 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package platforms

import (
"regexp"
"runtime"
"strings"
)
Expand Down Expand Up @@ -60,35 +59,25 @@ func isKnownArch(arch string) bool {
return false
}

// normalizeOSVersion will only include the OSVersion if present.
// The formart of OS on the platform specifier is <OS>[(<OSVersion>)]
// normalizeOS will return OS and OSVersion.
// The formart of OSAndVersion is <OS>[(<OSVersion>)]
// OSVersion is optional only and is currently used only by windows OS.
// If OsVersion is not specified, empty string is returned.
func normalizeOSVersion(OSAndVersion string) string {
func normalizeOS(OSAndVersion string) (os, osversion string) {
if OSAndVersion == "" {
return ""
os = runtime.GOOS
return
}
parts := strings.SplitAfter(OSAndVersion, "(")

parts := regexp.MustCompile("[()]").Split(OSAndVersion, -1)
if len(parts) > 1 {
if parts[1] != "" {
return parts[1]
}
}
return ""
}

func normalizeOS(OSAndVersion string) string {
if OSAndVersion == "" {
return runtime.GOOS
}
os := strings.Split(strings.ToLower(OSAndVersion), "(")[0]

switch os {
case "macos":
os = "darwin"
switch len(parts) {
case 2:
osversion = strings.ToLower(strings.TrimRight(parts[1], ")"))
fallthrough
case 1:
os = strings.ToLower(parts[0])
}
return os
return
}

// normalizeArch normalizes the architecture.
Expand Down
11 changes: 4 additions & 7 deletions platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ func Parse(specifier string) (specs.Platform, error) {
// we have very little information about the platform here, we are
// going to be a little more strict if we don't know about the argument
// value.
p.OS = normalizeOS(parts[0])
p.OSVersion = normalizeOSVersion(parts[0])
p.OS, p.OSVersion = normalizeOS(parts[0])
if isKnownOS(p.OS) {
// picks a default architecture
p.Architecture = runtime.GOARCH
Expand All @@ -233,8 +232,7 @@ func Parse(specifier string) (specs.Platform, error) {
case 2:
// In this case, we treat as a regular os[(osversion)]/arch pair. We don't care
// about whether or not we know of the platform.
p.OS = normalizeOS(parts[0])
p.OSVersion = normalizeOSVersion(parts[0])
p.OS, p.OSVersion = normalizeOS(parts[0])
p.Architecture, p.Variant = normalizeArch(parts[1], "")
if p.Architecture == "arm" && p.Variant == "v7" {
p.Variant = ""
Expand All @@ -243,8 +241,7 @@ func Parse(specifier string) (specs.Platform, error) {
return p, nil
case 3:
// we have a fully specified variant, this is rare
p.OS = normalizeOS(parts[0])
p.OSVersion = normalizeOSVersion(parts[0])
p.OS, p.OSVersion = normalizeOS(parts[0])
p.Architecture, p.Variant = normalizeArch(parts[1], parts[2])
if p.Architecture == "arm64" && p.Variant == "" {
p.Variant = "v8"
Expand Down Expand Up @@ -285,7 +282,7 @@ func Format(platform specs.Platform) string {
// For example, if "Aarch64" is encountered, we change it to "arm64" or if
// "x86_64" is encountered, it becomes "amd64".
func Normalize(platform specs.Platform) specs.Platform {
platform.OS = normalizeOS(platform.OS)
platform.OS, platform.OSVersion = normalizeOS(platform.OS)
platform.Architecture, platform.Variant = normalizeArch(platform.Architecture, platform.Variant)

return platform
Expand Down

0 comments on commit 5009917

Please sign in to comment.