1// Copyright 2018, OpenCensus Authors 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// Command version checks that the version string matches the latest Git tag. 16// This is expected to pass only on the master branch. 17package main 18 19import ( 20 "bytes" 21 "fmt" 22 "log" 23 "os" 24 "os/exec" 25 "sort" 26 "strconv" 27 "strings" 28 29 opencensus "go.opencensus.io" 30) 31 32func main() { 33 cmd := exec.Command("git", "tag") 34 var buf bytes.Buffer 35 cmd.Stdout = &buf 36 err := cmd.Run() 37 if err != nil { 38 log.Fatal(err) 39 } 40 var versions []version 41 for _, vStr := range strings.Split(buf.String(), "\n") { 42 if len(vStr) == 0 { 43 continue 44 } 45 // ignore pre-release versions 46 if isPreRelease(vStr) { 47 continue 48 } 49 versions = append(versions, parseVersion(vStr)) 50 } 51 sort.Slice(versions, func(i, j int) bool { 52 return versionLess(versions[i], versions[j]) 53 }) 54 latest := versions[len(versions)-1] 55 codeVersion := parseVersion("v" + opencensus.Version()) 56 if !versionLess(latest, codeVersion) { 57 fmt.Printf("exporter.Version is out of date with Git tags. Got %s; want something greater than %s\n", opencensus.Version(), latest) 58 os.Exit(1) 59 } 60 fmt.Printf("exporter.Version is up-to-date: %s\n", opencensus.Version()) 61} 62 63type version [3]int 64 65func versionLess(v1, v2 version) bool { 66 for c := 0; c < 3; c++ { 67 if diff := v1[c] - v2[c]; diff != 0 { 68 return diff < 0 69 } 70 } 71 return false 72} 73 74func isPreRelease(vStr string) bool { 75 split := strings.Split(vStr[1:], ".") 76 return strings.Contains(split[2], "-") 77} 78 79func parseVersion(vStr string) version { 80 split := strings.Split(vStr[1:], ".") 81 var ( 82 v version 83 err error 84 ) 85 for i := 0; i < 3; i++ { 86 v[i], err = strconv.Atoi(split[i]) 87 if err != nil { 88 fmt.Printf("Unrecognized version tag %q: %s\n", vStr, err) 89 os.Exit(2) 90 } 91 } 92 return v 93} 94 95func (v version) String() string { 96 return fmt.Sprintf("%d.%d.%d", v[0], v[1], v[2]) 97} 98