version.go 2.3 KB

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