blob: 269d748d429ed3766066c0c608aecad8328e02a6 [file] [log] [blame]
Matt Jeanneretf880eb62019-07-16 20:08:03 -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 */
Matt Jeanneret0c9ae282019-07-18 18:14:28 -040016
17// Package version is used to inject build time information via -X variables
Matt Jeanneretf880eb62019-07-16 20:08:03 -040018package 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
Matt Jeanneret0c9ae282019-07-18 18:14:28 -040038// InfoType is a collection of build time environment variables
39type InfoType struct {
Matt Jeanneretf880eb62019-07-16 20:08:03 -040040 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
Matt Jeanneret0c9ae282019-07-18 18:14:28 -040049// VersionInfo is an instance of build time environment variables populated at build time via -X arguments
50var VersionInfo InfoType
Matt Jeanneretf880eb62019-07-16 20:08:03 -040051
52func init() {
Matt Jeanneret0c9ae282019-07-18 18:14:28 -040053 VersionInfo = InfoType{
Matt Jeanneretf880eb62019-07-16 20:08:03 -040054 Version: version,
55 VcsRef: vcsRef,
56 VcsDirty: vcsDirty,
57 GoVersion: goVersion,
58 Os: os,
59 Arch: arch,
60 BuildTime: buildTime,
61 }
62}
63
Matt Jeanneret0c9ae282019-07-18 18:14:28 -040064func (v InfoType) String(indent string) string {
Matt Jeanneretf880eb62019-07-16 20:08:03 -040065 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}