github.com/XiaoMi/Gaea@v1.2.5/core/version.go (about)

     1  // Copyright 2019 The Gaea Authors. All Rights Reserved.
     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 core
    16  
    17  import (
    18  	"fmt"
    19  	"runtime"
    20  )
    21  
    22  // The following fields are populated at build time using -ldflags -X.
    23  // Note that DATE is omitted for reproducible builds
    24  var (
    25  	buildVersion     = "unknown"
    26  	buildGitRevision = "unknown"
    27  	buildUser        = "unknown"
    28  	buildHost        = "unknown"
    29  	buildStatus      = "unknown"
    30  	buildTime        = "unknown"
    31  	buildBranch      = "unknown"
    32  	buildGitDirty    = "0"
    33  )
    34  
    35  // BuildInfo describes version information about the binary build.
    36  type BuildInfo struct {
    37  	Version       string `json:"version"`
    38  	GitRevision   string `json:"revision"`
    39  	User          string `json:"user"`
    40  	Host          string `json:"host"`
    41  	GolangVersion string `json:"golang_version"`
    42  	BuildStatus   string `json:"status"`
    43  	BuildTime     string `json:"time"`
    44  	BuildBranch   string `json:"build_branch"`
    45  	BuildGitDirty string `json:"build_git_dirty"`
    46  }
    47  
    48  var (
    49  	// Info exports the build version information.
    50  	Info BuildInfo
    51  )
    52  
    53  // String produces a single-line version info
    54  //
    55  // This looks like:
    56  //
    57  // ```
    58  // user@host-<version>-<git revision>-<build status>
    59  // ```
    60  func (b BuildInfo) String() string {
    61  	return fmt.Sprintf("%v@%v-%v-%v-%v-%v-%v-%v",
    62  		b.User,
    63  		b.Host,
    64  		b.Version,
    65  		b.GitRevision,
    66  		b.BuildStatus,
    67  		b.BuildTime,
    68  		b.BuildBranch,
    69  		b.BuildGitDirty)
    70  }
    71  
    72  // LongForm returns a multi-line version information
    73  //
    74  // This looks like:
    75  //
    76  // ```
    77  // Version: <version>
    78  // GitRevision: <git revision>
    79  // User: user@host
    80  // GolangVersion: go1.10.2
    81  // BuildStatus: <build status>
    82  // ```
    83  func (b BuildInfo) LongForm() string {
    84  	return fmt.Sprintf(`Version: %v
    85  GitRevision: %v
    86  User: %v@%v
    87  GolangVersion: %v
    88  BuildStatus: %v
    89  BuildTime: %v
    90  BuildBranch: %v
    91  BuildGitDirty: %v
    92  `,
    93  		b.Version,
    94  		b.GitRevision,
    95  		b.User,
    96  		b.Host,
    97  		b.GolangVersion,
    98  		b.BuildStatus,
    99  		b.BuildTime,
   100  		b.BuildBranch,
   101  		b.BuildGitDirty)
   102  }
   103  
   104  func init() {
   105  	Info = BuildInfo{
   106  		Version:       buildVersion,
   107  		GitRevision:   buildGitRevision,
   108  		User:          buildUser,
   109  		Host:          buildHost,
   110  		GolangVersion: runtime.Version(),
   111  		BuildStatus:   buildStatus,
   112  		BuildTime:     buildTime,
   113  		BuildBranch:   buildBranch,
   114  		BuildGitDirty: buildGitDirty,
   115  	}
   116  }