Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

89 linhas
2.1 KiB

  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. versions = append(versions, parseVersion(vStr))
  42. }
  43. sort.Slice(versions, func(i, j int) bool {
  44. return versionLess(versions[i], versions[j])
  45. })
  46. latest := versions[len(versions)-1]
  47. codeVersion := parseVersion("v" + opencensus.Version())
  48. if !versionLess(latest, codeVersion) {
  49. fmt.Printf("exporter.Version is out of date with Git tags. Got %s; want something greater than %s\n", opencensus.Version(), latest)
  50. os.Exit(1)
  51. }
  52. fmt.Printf("exporter.Version is up-to-date: %s\n", opencensus.Version())
  53. }
  54. type version [3]int
  55. func versionLess(v1, v2 version) bool {
  56. for c := 0; c < 3; c++ {
  57. if diff := v1[c] - v2[c]; diff != 0 {
  58. return diff < 0
  59. }
  60. }
  61. return false
  62. }
  63. func parseVersion(vStr string) version {
  64. split := strings.Split(vStr[1:], ".")
  65. var (
  66. v version
  67. err error
  68. )
  69. for i := 0; i < 3; i++ {
  70. v[i], err = strconv.Atoi(split[i])
  71. if err != nil {
  72. fmt.Printf("Unrecognized version tag %q: %s\n", vStr, err)
  73. os.Exit(2)
  74. }
  75. }
  76. return v
  77. }
  78. func (v version) String() string {
  79. return fmt.Sprintf("%d.%d.%d", v[0], v[1], v[2])
  80. }