code.vegaprotocol.io/vega@v0.79.0/version/version.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package version
    17  
    18  import (
    19  	"runtime/debug"
    20  	"strings"
    21  )
    22  
    23  var (
    24  	cliVersionHash = ""
    25  	cliVersion     = "v0.79.0"
    26  )
    27  
    28  func init() {
    29  	info, _ := debug.ReadBuildInfo()
    30  	// for some reason in jenkins integration tests this return nil
    31  	if info == nil {
    32  		cliVersionHash = "unknown"
    33  		return
    34  	}
    35  
    36  	modified := false
    37  
    38  	for _, v := range info.Settings {
    39  		if v.Key == "vcs.revision" {
    40  			cliVersionHash = v.Value
    41  		}
    42  		if v.Key == "vcs.modified" && v.Value == "true" {
    43  			modified = true
    44  		}
    45  	}
    46  	if modified {
    47  		cliVersionHash += "-modified"
    48  	}
    49  }
    50  
    51  // Get returns the version of the software. When the version is a development
    52  // version, the first 8 characters of the git hash, is appended as a build tag.
    53  // Any dash separated addition to the hash is appended as a build tag as well.
    54  func Get() string {
    55  	finalVersion := cliVersion
    56  
    57  	if strings.HasSuffix(cliVersion, "+dev") {
    58  		splatHash := strings.Split(cliVersionHash, "-")
    59  
    60  		// Verifying if splitting the version hash gave results.
    61  		if len(splatHash) == 0 {
    62  			return finalVersion
    63  		}
    64  
    65  		// Verifying if there is a commit hash.
    66  		if splatHash[0] != "" {
    67  			finalVersion = finalVersion + "." + splatHash[0][:8]
    68  		}
    69  
    70  		// Anything left from the splitting is appended as build tags behind.
    71  		for i := 1; i < len(splatHash); i++ {
    72  			finalVersion = finalVersion + "." + splatHash[i]
    73  		}
    74  	}
    75  	return finalVersion
    76  }
    77  
    78  func GetCommitHash() string {
    79  	return cliVersionHash
    80  }