1// Copyright 2016 Google LLC
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
15//go:generate ./update_version.sh
16
17// Package version contains version information for Google Cloud Client
18// Libraries for Go, as reported in request headers.
19package version
20
21import (
22	"runtime"
23	"strings"
24	"unicode"
25)
26
27// Repo is the current version of the client libraries in this
28// repo. It should be a date in YYYYMMDD format.
29const Repo = "20191119"
30
31// Go returns the Go runtime version. The returned string
32// has no whitespace.
33func Go() string {
34	return goVersion
35}
36
37var goVersion = goVer(runtime.Version())
38
39const develPrefix = "devel +"
40
41func goVer(s string) string {
42	if strings.HasPrefix(s, develPrefix) {
43		s = s[len(develPrefix):]
44		if p := strings.IndexFunc(s, unicode.IsSpace); p >= 0 {
45			s = s[:p]
46		}
47		return s
48	}
49
50	if strings.HasPrefix(s, "go1") {
51		s = s[2:]
52		var prerelease string
53		if p := strings.IndexFunc(s, notSemverRune); p >= 0 {
54			s, prerelease = s[:p], s[p:]
55		}
56		if strings.HasSuffix(s, ".") {
57			s += "0"
58		} else if strings.Count(s, ".") < 2 {
59			s += ".0"
60		}
61		if prerelease != "" {
62			s += "-" + prerelease
63		}
64		return s
65	}
66	return ""
67}
68
69func notSemverRune(r rune) bool {
70	return !strings.ContainsRune("0123456789.", r)
71}
72