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.
This commit is contained in:
Martin Atkins 2020-02-20 17:54:35 -08:00
parent 3078d21c5f
commit d82a36b6f5
1 changed files with 22 additions and 0 deletions

View File

@ -30,6 +30,28 @@ func (p Platform) String() string {
return p.OS + "_" + p.Arch 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. // CurrentPlatform is the platform where the current program is running.
// //
// If attempting to install providers for use on the same system where the // If attempting to install providers for use on the same system where the