github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/version/semver.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package version 18 19 import ( 20 "strings" 21 "unicode" 22 23 "github.com/blang/semver" 24 "github.com/golang/glog" 25 ) 26 27 func Parse(gitversion string) (semver.Version, error) { 28 // optionally trim leading spaces then one v 29 var seen bool 30 gitversion = strings.TrimLeftFunc(gitversion, func(ch rune) bool { 31 if seen { 32 return false 33 } 34 if ch == 'v' { 35 seen = true 36 return true 37 } 38 return unicode.IsSpace(ch) 39 }) 40 41 return semver.Make(gitversion) 42 } 43 44 func MustParse(gitversion string) semver.Version { 45 v, err := Parse(gitversion) 46 if err != nil { 47 glog.Fatalf("failed to parse semver from gitversion %q: %v", gitversion, err) 48 } 49 return v 50 }