blob: 49c0b10413d4be295e89df6b6e1ecc755cfa6b8f [file] [log] [blame]
David K. Bainbridgef430cd52019-05-28 15:00:35 -07001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package version
17
18import (
19 "fmt"
20 "strings"
21)
22
23// Default build-time variable.
24// These values can (should) be overridden via ldflags when built with
25// `make`
26var (
27 version = "unknown-version"
28 goVersion = "unknown-goversion"
29 vcsRef = "unknown-vcsref"
30 vcsDirty = "unknown-vcsdirty"
31 buildTime = "unknown-buildtime"
32 os = "unknown-os"
33 arch = "unknown-arch"
34)
35
36type VersionInfoType struct {
37 Version string `json:"version"`
38 GoVersion string `json:"goversion"`
39 VcsRef string `json:"vcsref"`
40 VcsDirty string `json:"vcsdirty"`
41 BuildTime string `json:"buildtime"`
42 Os string `json:"os"`
43 Arch string `json:"arch"`
44}
45
46var VersionInfo VersionInfoType
47
48func init() {
49 VersionInfo = VersionInfoType{
50 Version: version,
51 VcsRef: vcsRef,
52 VcsDirty: vcsDirty,
53 GoVersion: goVersion,
54 Os: os,
55 Arch: arch,
56 BuildTime: buildTime,
57 }
58}
59
60func (v VersionInfoType) String(indent string) string {
61 builder := strings.Builder{}
62
63 builder.WriteString(fmt.Sprintf("%sVersion: %s\n", indent, VersionInfo.Version))
64 builder.WriteString(fmt.Sprintf("%sGoVersion: %s\n", indent, VersionInfo.GoVersion))
65 builder.WriteString(fmt.Sprintf("%sVCS Ref: %s\n", indent, VersionInfo.VcsRef))
66 builder.WriteString(fmt.Sprintf("%sVCS Dirty: %s\n", indent, VersionInfo.VcsDirty))
67 builder.WriteString(fmt.Sprintf("%sBuilt: %s\n", indent, VersionInfo.BuildTime))
68 builder.WriteString(fmt.Sprintf("%sOS/Arch: %s/%s\n", indent, VersionInfo.Os, VersionInfo.Arch))
69 return builder.String()
70}