Optimize socketclient adapter and add various code improvements
[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
16
17 import (
18         "fmt"
19         "runtime"
20         "strconv"
21         "time"
22 )
23
24 // Following variables should normally be updated via `-ldflags "-X ..."`.
25 // However, the version string is hard-coded to ensure it is always included
26 // even with bare go build/install.
27 var (
28         name       = "govpp"
29         version    = "v0.4.0-dev"
30         commit     = "unknown"
31         branch     = "HEAD"
32         buildStamp = ""
33         buildUser  = ""
34         buildHost  = ""
35
36         buildDate time.Time
37 )
38
39 func init() {
40         buildstampInt64, _ := strconv.ParseInt(buildStamp, 10, 64)
41         if buildstampInt64 == 0 {
42                 buildstampInt64 = time.Now().Unix()
43         }
44         buildDate = time.Unix(buildstampInt64, 0)
45 }
46
47 func Version() string {
48         return version
49 }
50
51 func Info() string {
52         return fmt.Sprintf(`%s %s`, name, version)
53 }
54
55 func Verbose() string {
56         return fmt.Sprintf(`%s
57   Version:      %s
58   Branch:       %s
59   Revision:     %s
60   Built by:     %s@%s 
61   Build date:   %s
62   Go runtime:   %s (%s/%s)`,
63                 name,
64                 version, branch, commit,
65                 buildUser, buildHost, buildDate.Format(time.UnixDate),
66                 runtime.Version(), runtime.GOOS, runtime.GOARCH,
67         )
68 }