From c83f9773bc2cc93b4b335acb82a889cbbcdb6894 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 15 Aug 2018 11:30:46 +0100 Subject: [PATCH] platforms: Add `MustParse` This function is analogous to `regexp.MustCompile` and can simplify production of a `Platform` from a hard-coded strings, e.g. for global variable initialisation. Signed-off-by: Ian Campbell --- platforms/platforms.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/platforms/platforms.go b/platforms/platforms.go index 29b7ad6b5..2c2cc1102 100644 --- a/platforms/platforms.go +++ b/platforms/platforms.go @@ -109,6 +109,7 @@ package platforms import ( "regexp" "runtime" + "strconv" "strings" "github.com/containerd/containerd/errdefs" @@ -230,6 +231,16 @@ func Parse(specifier string) (specs.Platform, error) { return specs.Platform{}, errors.Wrapf(errdefs.ErrInvalidArgument, "%q: cannot parse platform specifier", specifier) } +// MustParse is like Parses but panics if the specifier cannot be parsed. +// Simplifies initialization of global variables. +func MustParse(specifier string) specs.Platform { + p, err := Parse(specifier) + if err != nil { + panic("platform: Parse(" + strconv.Quote(specifier) + "): " + err.Error()) + } + return p +} + // Format returns a string specifier from the provided platform specification. func Format(platform specs.Platform) string { if platform.OS == "" {