version.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2020 Google LLC. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gensupport
  5. import (
  6. "runtime"
  7. "strings"
  8. "unicode"
  9. )
  10. // GoVersion returns the Go runtime version. The returned string
  11. // has no whitespace.
  12. func GoVersion() string {
  13. return goVersion
  14. }
  15. var goVersion = goVer(runtime.Version())
  16. const develPrefix = "devel +"
  17. func goVer(s string) string {
  18. if strings.HasPrefix(s, develPrefix) {
  19. s = s[len(develPrefix):]
  20. if p := strings.IndexFunc(s, unicode.IsSpace); p >= 0 {
  21. s = s[:p]
  22. }
  23. return s
  24. }
  25. if strings.HasPrefix(s, "go1") {
  26. s = s[2:]
  27. var prerelease string
  28. if p := strings.IndexFunc(s, notSemverRune); p >= 0 {
  29. s, prerelease = s[:p], s[p:]
  30. }
  31. if strings.HasSuffix(s, ".") {
  32. s += "0"
  33. } else if strings.Count(s, ".") < 2 {
  34. s += ".0"
  35. }
  36. if prerelease != "" {
  37. s += "-" + prerelease
  38. }
  39. return s
  40. }
  41. return ""
  42. }
  43. func notSemverRune(r rune) bool {
  44. return !strings.ContainsRune("0123456789.", r)
  45. }