...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package main
18
19 import (
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
32 func 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
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
63 type version [3]int
64
65 func 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
74 func isPreRelease(vStr string) bool {
75 split := strings.Split(vStr[1:], ".")
76 return strings.Contains(split[2], "-")
77 }
78
79 func 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
95 func (v version) String() string {
96 return fmt.Sprintf("%d.%d.%d", v[0], v[1], v[2])
97 }
98
View as plain text