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
5package gensupport
6
7import (
8	"runtime"
9	"strings"
10	"unicode"
11)
12
13// GoVersion returns the Go runtime version. The returned string
14// has no whitespace.
15func GoVersion() string {
16	return goVersion
17}
18
19var goVersion = goVer(runtime.Version())
20
21const develPrefix = "devel +"
22
23func goVer(s string) string {
24	if strings.HasPrefix(s, develPrefix) {
25		s = s[len(develPrefix):]
26		if p := strings.IndexFunc(s, unicode.IsSpace); p >= 0 {
27			s = s[:p]
28		}
29		return s
30	}
31
32	if strings.HasPrefix(s, "go1") {
33		s = s[2:]
34		var prerelease string
35		if p := strings.IndexFunc(s, notSemverRune); p >= 0 {
36			s, prerelease = s[:p], s[p:]
37		}
38		if strings.HasSuffix(s, ".") {
39			s += "0"
40		} else if strings.Count(s, ".") < 2 {
41			s += ".0"
42		}
43		if prerelease != "" {
44			s += "-" + prerelease
45		}
46		return s
47	}
48	return ""
49}
50
51func notSemverRune(r rune) bool {
52	return !strings.ContainsRune("0123456789.", r)
53}
54