9cfbdf60a9d9f93bfe5926b1b55792dd56536861
[govpp.git] / version / version.go
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 keeps track of versioning info for GoVPP.
16 package version
17
18 import (
19         "fmt"
20         "runtime"
21         "strconv"
22         "time"
23 )
24
25 const (
26         Major      = 0
27         Minor      = 5
28         Patch      = 0
29         PreRelease = "dev"
30 )
31
32 // String formats the version string using semver format.
33 func String() string {
34         v := fmt.Sprintf("v%d.%d.%d", Major, Minor, Patch)
35         if PreRelease != "" {
36                 v += "-" + PreRelease
37         }
38         return v
39 }
40
41 // Following variables should normally be updated via `-ldflags "-X ..."`.
42 // However, the version string is hard-coded to ensure it is always included
43 // even with bare go build/install.
44 var (
45         name       = "govpp"
46         version    = "v0.5.0-dev"
47         commit     = "unknown"
48         branch     = "HEAD"
49         buildStamp = ""
50         buildUser  = ""
51         buildHost  = ""
52
53         buildDate time.Time
54 )
55
56 func init() {
57         buildstampInt64, _ := strconv.ParseInt(buildStamp, 10, 64)
58         if buildstampInt64 == 0 {
59                 buildstampInt64 = time.Now().Unix()
60         }
61         buildDate = time.Unix(buildstampInt64, 0)
62 }
63
64 func Version() string {
65         return version
66 }
67
68 func Info() string {
69         return fmt.Sprintf(`%s %s`, name, version)
70 }
71
72 func Verbose() string {
73         return fmt.Sprintf(`%s
74   Version:      %s
75   Branch:       %s
76   Revision:     %s
77   Built by:     %s@%s 
78   Build date:   %s
79   Go runtime:   %s (%s/%s)`,
80                 name,
81                 version, branch, commit,
82                 buildUser, buildHost, buildDate.Format(time.UnixDate),
83                 runtime.Version(), runtime.GOOS, runtime.GOARCH,
84         )
85 }