Skip to content

Commit

Permalink
Reuse v1.Platform parser and Stringer
Browse files Browse the repository at this point in the history
There is some special handling around "all" that I left in place, but at
least we can drop some of this code.
  • Loading branch information
jonjohnsonjr committed Feb 8, 2023
1 parent 9b77df4 commit f8186de
Showing 1 changed file with 2 additions and 31 deletions.
33 changes: 2 additions & 31 deletions cmd/crane/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cmd

import (
"fmt"
"strings"

v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -75,41 +74,13 @@ func platformToString(p *v1.Platform) string {
if p == nil {
return "all"
}
platform := ""
if p.OS != "" && p.Architecture != "" {
platform = p.OS + "/" + p.Architecture
}
if p.Variant != "" {
platform += "/" + p.Variant
}
return platform
return p.String()
}

func parsePlatform(platform string) (*v1.Platform, error) {
if platform == "all" {
return nil, nil
}

p := &v1.Platform{}

parts := strings.SplitN(platform, ":", 2)
if len(parts) == 2 {
p.OSVersion = parts[1]
}

parts = strings.Split(parts[0], "/")

if len(parts) > 3 {
return nil, fmt.Errorf("failed to parse platform '%s': too many slashes", platform)
}

p.OS = parts[0]
if len(parts) > 1 {
p.Architecture = parts[1]
}
if len(parts) > 2 {
p.Variant = parts[2]
}

return p, nil
return v1.ParsePlatform(platform)
}

0 comments on commit f8186de

Please sign in to comment.