blob: 269d748d429ed3766066c0c608aecad8328e02a6 [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001/*
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 */
16
17// Package version is used to inject build time information via -X variables
18package version
19
20import (
21 "fmt"
22 "strings"
23)
24
25// Default build-time variable.
26// These values can (should) be overridden via ldflags when built with
27// `make`
28var (
29 version = "unknown-version"
30 goVersion = "unknown-goversion"
31 vcsRef = "unknown-vcsref"
32 vcsDirty = "unknown-vcsdirty"
33 buildTime = "unknown-buildtime"
34 os = "unknown-os"
35 arch = "unknown-arch"
36)
37
38// InfoType is a collection of build time environment variables
39type InfoType struct {
40 Version string `json:"version"`
41 GoVersion string `json:"goversion"`
42 VcsRef string `json:"vcsref"`
43 VcsDirty string `json:"vcsdirty"`
44 BuildTime string `json:"buildtime"`
45 Os string `json:"os"`
46 Arch string `json:"arch"`
47}
48
49// VersionInfo is an instance of build time environment variables populated at build time via -X arguments
50var VersionInfo InfoType
51
52func init() {
53 VersionInfo = InfoType{
54 Version: version,
55 VcsRef: vcsRef,
56 VcsDirty: vcsDirty,
57 GoVersion: goVersion,
58 Os: os,
59 Arch: arch,
60 BuildTime: buildTime,
61 }
62}
63
64func (v InfoType) String(indent string) string {
65 builder := strings.Builder{}
66
67 builder.WriteString(fmt.Sprintf("%sVersion: %s\n", indent, VersionInfo.Version))
68 builder.WriteString(fmt.Sprintf("%sGoVersion: %s\n", indent, VersionInfo.GoVersion))
69 builder.WriteString(fmt.Sprintf("%sVCS Ref: %s\n", indent, VersionInfo.VcsRef))
70 builder.WriteString(fmt.Sprintf("%sVCS Dirty: %s\n", indent, VersionInfo.VcsDirty))
71 builder.WriteString(fmt.Sprintf("%sBuilt: %s\n", indent, VersionInfo.BuildTime))
72 builder.WriteString(fmt.Sprintf("%sOS/Arch: %s/%s\n", indent, VersionInfo.Os, VersionInfo.Arch))
73 return builder.String()
74}