Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame] | 2 | * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 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 | package version |
| 17 | |
| 18 | import ( |
| 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` |
| 26 | var ( |
| 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 | |
| 36 | type 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 | |
| 46 | var VersionInfo VersionInfoType |
| 47 | |
| 48 | func 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 | |
| 60 | func (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 | } |