Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 1 | /* |
| 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 Jeanneret | 0c9ae28 | 2019-07-18 18:14:28 -0400 | [diff] [blame] | 16 | |
| 17 | // Package version is used to inject build time information via -X variables |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 18 | package version |
| 19 | |
| 20 | import ( |
| 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` |
| 28 | var ( |
| 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 Jeanneret | 0c9ae28 | 2019-07-18 18:14:28 -0400 | [diff] [blame] | 38 | // InfoType is a collection of build time environment variables |
| 39 | type InfoType struct { |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 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 | |
Matt Jeanneret | 0c9ae28 | 2019-07-18 18:14:28 -0400 | [diff] [blame] | 49 | // VersionInfo is an instance of build time environment variables populated at build time via -X arguments |
| 50 | var VersionInfo InfoType |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 51 | |
| 52 | func init() { |
Matt Jeanneret | 0c9ae28 | 2019-07-18 18:14:28 -0400 | [diff] [blame] | 53 | VersionInfo = InfoType{ |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 54 | Version: version, |
| 55 | VcsRef: vcsRef, |
| 56 | VcsDirty: vcsDirty, |
| 57 | GoVersion: goVersion, |
| 58 | Os: os, |
| 59 | Arch: arch, |
| 60 | BuildTime: buildTime, |
| 61 | } |
| 62 | } |
| 63 | |
Matt Jeanneret | 0c9ae28 | 2019-07-18 18:14:28 -0400 | [diff] [blame] | 64 | func (v InfoType) String(indent string) string { |
Matt Jeanneret | f880eb6 | 2019-07-16 20:08:03 -0400 | [diff] [blame] | 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 | } |