github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/pkg/osarch/osarch.go (about) 1 // Copyright 2016 Palantir Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package osarch 16 17 import ( 18 "fmt" 19 "runtime" 20 "strings" 21 ) 22 23 type OSArch struct { 24 OS string 25 Arch string 26 } 27 28 // String returns a string representation of the form "GOOS-GOARCH". 29 func (o OSArch) String() string { 30 return fmt.Sprintf("%v-%v", o.OS, o.Arch) 31 } 32 33 // Current returns and OSArch that reflects the GOOS/GOARCH value for the current runtime. 34 func Current() OSArch { 35 return OSArch{ 36 OS: runtime.GOOS, 37 Arch: runtime.GOARCH, 38 } 39 } 40 41 // New returns an OSArch value for the provided input. Returns an error if the provided input is not of the correct 42 // format, which is defined as two non-empty alphanumeric strings separated by a single hyphen ("-"). 43 func New(input string) (OSArch, error) { 44 if parts := strings.Split(input, "-"); len(parts) == 2 && isAlphaNumericOnly(parts[0]) && isAlphaNumericOnly(parts[1]) { 45 return OSArch{OS: parts[0], Arch: parts[1]}, nil 46 } 47 return OSArch{}, fmt.Errorf("not a valid OSArch value: %s", input) 48 } 49 50 func isAlphaNumericOnly(input string) bool { 51 if input == "" { 52 return false 53 } 54 for _, r := range input { 55 if !((r >= '0' && r <= '9') || (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z')) { 56 return false 57 } 58 } 59 return true 60 }