github.com/jaylevin/jenkins-library@v1.230.4/pkg/multiarch/multiarch.go (about) 1 package multiarch 2 3 import ( 4 "fmt" 5 "regexp" 6 "strings" 7 8 "github.com/SAP/jenkins-library/pkg/log" 9 "github.com/SAP/jenkins-library/pkg/piperutils" 10 ) 11 12 var knownGoos = []string{"aix", "android", "darwin", "dragonfly", "freebsd", "hurd", "illumos", "ios", "js", "linux", "nacl", "netbsd", "openbsd", "plan9", "solaris", "windows", "zos"} 13 var knownGoarch = []string{"386", "amd64", "amd64p32", "arm", "arm64", "arm64be", "armbe", "loong64", "mips", "mips64", "mips64le", "mips64p32", "mips64p32le", "mipsle", "ppc", "ppc64", "ppc64le", "riscv", "riscv64", "s390", "s390x", "sparc", "sparc64", "wasm"} 14 15 // Platform . 16 type Platform struct { 17 OS string 18 Arch string 19 Variant string 20 } 21 22 // ToString returns a string representation of the platform 23 func (p Platform) ToString() string { 24 if len(p.Variant) > 0 { 25 return fmt.Sprintf("%s/%s/%s", p.OS, p.Arch, p.Variant) 26 } 27 28 return fmt.Sprintf("%s/%s", p.OS, p.Arch) 29 } 30 31 // ParsePlatformString parses the given string and returns a platform obj 32 func ParsePlatformString(s string) (Platform, error) { 33 r := regexp.MustCompile(`(?P<os>[^,/]+)[,/](?P<arch>[^,/]+)(?:[,/](?P<variant>[^,/]+))?`) 34 35 matches := r.FindStringSubmatch(strings.ToLower(s)) 36 37 if len(matches) < 2 { 38 return Platform{}, fmt.Errorf("unable to parse platform '%s'", s) 39 } 40 41 p := Platform{} 42 43 p.OS = strings.Trim(matches[1], " ") 44 45 if !piperutils.ContainsString(knownGoos, p.OS) { 46 log.Entry().Warningf("OS '%s' is unknown to us", p.OS) 47 } 48 49 p.Arch = strings.Trim(matches[2], " ") 50 51 if !piperutils.ContainsString(knownGoarch, p.Arch) { 52 log.Entry().Warningf("Architecture '%s' is unknown to us", p.Arch) 53 } 54 55 p.Variant = strings.Trim(matches[3], " ") 56 57 return p, nil 58 } 59 60 // ParsePlatformStrings parses the given slice of strings and returns a slice with platform objects 61 func ParsePlatformStrings(ss []string) ([]Platform, error) { 62 pp := []Platform{} 63 64 for _, s := range ss { 65 if p, err := ParsePlatformString(s); err == nil { 66 pp = append(pp, p) 67 } else { 68 return nil, err 69 } 70 } 71 72 return pp, nil 73 }