From d82a36b6f59757bb2dd3f5fff37b33078246bb25 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 20 Feb 2020 17:54:35 -0800 Subject: [PATCH] internal/getproviders: ParsePlatform method This is just to have a centralized set of logic for converting from a platform string (like "linux_amd64") to a Platform object, so we can do normalization and validation consistently. --- internal/getproviders/types.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/internal/getproviders/types.go b/internal/getproviders/types.go index 83515173b1..a351af8366 100644 --- a/internal/getproviders/types.go +++ b/internal/getproviders/types.go @@ -30,6 +30,28 @@ func (p Platform) String() string { return p.OS + "_" + p.Arch } +// ParsePlatform parses a string representation of a platform, like +// "linux_amd64", or returns an error if the string is not valid. +func ParsePlatform(str string) (Platform, error) { + underPos := strings.Index(str, "_") + if underPos < 1 || underPos >= len(str)-2 { + return Platform{}, fmt.Errorf("must be two words separated by an underscore") + } + + os, arch := str[:underPos], str[underPos+1:] + if strings.ContainsAny(os, " \t\n\r") { + return Platform{}, fmt.Errorf("OS portion must not contain whitespace") + } + if strings.ContainsAny(arch, " \t\n\r") { + return Platform{}, fmt.Errorf("architecture portion must not contain whitespace") + } + + return Platform{ + OS: os, + Arch: arch, + }, nil +} + // CurrentPlatform is the platform where the current program is running. // // If attempting to install providers for use on the same system where the