go.ligato.io/vpp-agent/v3@v3.5.0/pkg/version/version.go (about) 1 // Copyright (c) 2019 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package version provides information about app version. 16 package version 17 18 import ( 19 "fmt" 20 "os" 21 "runtime" 22 "strconv" 23 "time" 24 ) 25 26 var ( 27 app = "vpp-agent" 28 version = "v3.5.0" 29 gitCommit = "unknown" 30 gitBranch = "HEAD" 31 buildUser = "unknown" 32 buildHost = "unknown" 33 buildDate = "" 34 ) 35 36 var buildTime time.Time 37 var revision string 38 39 func init() { 40 if buildDate == "" { 41 buildDate = getBuildDate() 42 } 43 if buildDate != "" { 44 buildstampInt64, _ := strconv.ParseInt(buildDate, 10, 64) 45 buildTime = time.Unix(buildstampInt64, 0) 46 } 47 revision = gitCommit 48 if len(revision) > 7 { 49 revision = revision[:7] 50 } 51 if gitBranch != "HEAD" { 52 revision += fmt.Sprintf("@%s", gitBranch) 53 } 54 } 55 56 func getBuildDate() string { 57 bin, err := os.Executable() 58 if err != nil { 59 return "" 60 } 61 info, err := os.Stat(bin) 62 if err != nil { 63 return "" 64 } 65 return fmt.Sprint(info.ModTime().Unix()) 66 } 67 68 // App returns app name. 69 func App() string { 70 return app 71 } 72 73 // Version returns version string. 74 func Version() string { 75 return version 76 } 77 78 // GitCommit returns git commit hash. 79 func GitCommit() string { 80 return gitCommit 81 } 82 83 // GitBranch returns git branch name. 84 func GitBranch() string { 85 return gitBranch 86 } 87 88 // BuildUser returns username. 89 func BuildUser() string { 90 return buildUser 91 } 92 93 // BuildHost returns host name. 94 func BuildHost() string { 95 return buildHost 96 } 97 98 // BuildTime returns Unix time when Agent was compiled. 99 func BuildTime() int64 { 100 return buildTime.Unix() 101 } 102 103 // Data returns version data. 104 func Data() (ver, rev, date string) { 105 return version, revision, buildTime.Format(time.UnixDate) 106 } 107 108 func Short() string { 109 return fmt.Sprintf(`%s %s`, app, version) 110 } 111 112 func BuiltOn() string { 113 stamp := buildTime.Format(time.UnixDate) 114 if !buildTime.IsZero() { 115 stamp += fmt.Sprintf(" (%s)", timeAgo(buildTime)) 116 } 117 return stamp 118 } 119 120 func BuiltBy() string { 121 return fmt.Sprintf("%s@%s (%s %s/%s)", 122 buildUser, buildHost, runtime.Version(), runtime.GOOS, runtime.GOARCH, 123 ) 124 } 125 126 // Info returns string with complete version info on single line. 127 func Info() string { 128 return fmt.Sprintf(`%s %s (%s) built by %s@%s on %v`, 129 app, version, revision, buildUser, buildHost, BuiltOn(), 130 ) 131 } 132 133 // Detail returns string with detailed version info on separate lines. 134 func Detail() string { 135 return fmt.Sprintf(`%s 136 Version: %s 137 Branch: %s 138 Revision: %s 139 Built By: %s@%s 140 Build Date: %s 141 Go Runtime: %s (%s/%s)`, 142 app, version, gitBranch, revision, 143 buildUser, buildHost, buildTime.Format(time.UnixDate), 144 runtime.Version(), runtime.GOOS, runtime.GOARCH, 145 ) 146 } 147 148 func timeAgo(t time.Time) string { 149 const timeDay = time.Hour * 24 150 if ago := time.Since(t); ago > timeDay { 151 return fmt.Sprintf("%v days ago", float64(ago.Round(timeDay)/timeDay)) 152 } else if ago > time.Hour { 153 return fmt.Sprintf("%v hours ago", ago.Round(time.Hour).Hours()) 154 } else if ago > time.Minute { 155 return fmt.Sprintf("%v minutes ago", ago.Round(time.Minute).Minutes()) 156 } 157 return "just now" 158 }